Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Playing with neopixels

netduino neopixels spi

  • Please log in to reply
1 reply to this topic

#1 twinnaz

twinnaz

    Advanced Member

  • Members
  • PipPipPip
  • 38 posts
  • LocationBrampton, Ontario

Posted 24 November 2014 - 11:40 PM

Playing around with a Neopixel library I wrote 

 

Fading 

https://onedrive.liv...E84D56B35!15115

 

Pulse fade

https://onedrive.liv...E84D56B35!15118

 

Will post code when I add a couple more functions 


Netduino Plus 2


#2 twinnaz

twinnaz

    Advanced Member

  • Members
  • PipPipPip
  • 38 posts
  • LocationBrampton, Ontario

Posted 28 November 2014 - 09:25 PM

Here's the class I made. After reading a lot about these Neopixels I decided to get a Neopixel stick. Thanks to Jcoders post and a lot of others I was able to write one myself. Im by far not a programmer just a hobby so code might not be the most efficient or professional but I tried.

Imports System
Imports Microsoft.SPOT
Imports System.Text

Public Class Pixel


    ' Gets the string reprsentation on an 24bit rgb color. For 1 Neopixel led Red = 8 bit Green = 8 Bit Blue = 8 bit 

    Function color(ByVal green As Integer, ByVal red As Integer, ByVal blue As Integer) As String
        Dim R As String = Convert2Bin(red)
        Dim G As String = Convert2Bin(green)
        Dim B As String = Convert2Bin(blue)
        red = CInt(R)
        green = CInt(G)
        blue = CInt(B)
        Dim binaryred = (red.ToString("D8"))
        Dim binarygreen = (green.ToString("D8"))
        Dim binaryblue = (blue.ToString("D8"))
        ' Return a 24 bit string in the GBR order needed for the neopixel
        Dim Led24bitcolor = binarygreen & binaryred & binaryblue
        Return Led24bitcolor
    End Function

    ' Same as above but used for more that 1 pixel ''' Obsolete uses alot of memory can crash when led count > 60 '''
    ' Use SendAllLedData(color,x)  instead of Color(string,x). In theroy can handle 400+ led YMMV (Ps** Will "light up 3000 leds" :):D no effects and stuff like that though :( try to keep it lower)

    Function color(ByVal green As Integer, ByVal red As Integer, ByVal blue As Integer, ByVal leds As Integer) As String
        Dim R As String = Convert2Bin(red)
        Dim G As String = Convert2Bin(green)
        Dim B As String = Convert2Bin(blue)
        red = CInt(R)
        green = CInt(G)
        blue = CInt(B)
        Dim binaryred = (red.ToString("D8"))
        Dim binarygreen = (green.ToString("D8"))
        Dim binaryblue = (blue.ToString("D8"))
        Dim Led24bitcolor = binarygreen & binaryred & binaryblue

        'Repeats the string by any given amount 

        Dim allLed24bitcolor As String = New StringBuilder().Insert(0, Led24bitcolor, leds).ToString
        Return allLed24bitcolor
    End Function


    ' Converts integer number representing a byte (0-255) to string in a binary format (00000000 to 11111111)
 
    Function Convert2Bin(ByVal LedValue As Integer) As String
        Dim sb As New System.Text.StringBuilder
        While LedValue <> 0
            sb.Insert(0, (LedValue Mod 2).ToString, 1)
            LedValue \= 2
        End While
        Return sb.ToString()
    End Function



    'Phases the string to a byte array. Neopixels require a centain timing logic for data. Using SPI the netduino is able to meet the demand. 

    Shared Function SendAllLedData(ByVal color As String, ByVal LedCount As Integer) As Byte()

        'Setting up the byte array and splitting the sting in a string array so that each element can be phases into required logic 1 or 0 

        Dim x As Integer = 0
        Dim bitcolor As String() = New String(color.Length - 1) {}
        Dim b2sa As Byte() = New Byte(bitcolor.Length - 1) {}
        Dim LedsData() As Byte = Nothing
        Dim y, z As Integer

        For i As Integer = 0 To color.Length - 1
            bitcolor(i) = color(i).ToString()
        Next

        ' Phases each element in the string array and replaces 0 with 128 and 1 with 252 then dumps each elemnet into a byte array in the same order it came in 

        For i As Integer = 0 To bitcolor.Length - 1
            Dim data As String = bitcolor(i)
            Select Case data
                Case "0"
                    bitcolor(i) = "128"
                Case "1"
                    bitcolor(i) = "252"
            End Select
        Next

        For i As Integer = 0 To bitcolor.Length - 1
            b2sa(i) = Byte.Parse(bitcolor(i))
        Next

        ' Used if controlling more than 1 pixel 

        y = (24 * LedCount) - 1
        z = 0
        ReDim LedsData(y)

        While z <= y
            Array.Copy(b2sa, 0, LedsData, z, 24)
            z += 24
        End While

        Return LedsData


    End Function
End Class

Here is how to use it 

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino
Imports NetduinoApplication10.Pixel




Module Module1
    Sub Main()

        Dim NeopixelConfig As SPI.Configuration
        Dim NeopixelSpi As SPI

        NeopixelConfig = New SPI.Configuration(Pins.GPIO_PIN_D10, False, 0, 0, False, True, 6666, SPI.SPI_module.SPI1)
        NeopixelSpi = New SPI(NeopixelConfig)

        ' Neopixels require a reset time of 9us to 40 us inbetween new data packets this just send 0 bytes lasting about 16 us. Still have to integrate into a funtion

        Dim Reset As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim Pixel As New Pixel
        ' Seting us the colot data 
        Dim White = Pixel.color(255, 255, 255)

        ' Preping the color data to be sent as one big array. Note - Using loops during spi.wirte causes a delay breaking the timing needed for the neopixels
        ' 8 is for the amount of leds I will be using (Neopixel stick) 

        Dim data = SendAllLedData(White, 8)

        'Send all data 

        NeopixelSpi.Write(data)
        NeopixelSpi.Write(Reset)

    End Sub

End Module

Still writing a bit of functions such as (shifting led colors, animation, and fading) for it will be added soon

If you have any algorithms or functions you'd like to share psot or pm 


Netduino Plus 2






Also tagged with one or more of these keywords: netduino, neopixels, spi

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.