Hi guys...
After accepting a challenge in this thread to create a Library to control the NeoPixel type of thingy, I ordered myself a 8x5 WS2812B based shield.
So yesterday I got my shield...
https://cdn-shop.adafruit.com/1200x900/1430-03.jpg
... and starting doing my research.
Almost 80% of results took me to the NeoPixel Library... BUT, I found a Video on YouTube from a Guy called Kevin Darrah, in which he explained his Library-less approach... and found his code...
So after about an hour of head banging and trouble shooting, I had success in controlling my shield using his C code as an inline C Block.
So I decided to Trim it to its smallest footprint, and created a Module for it.
The code is as well commented as possible, Although I took out all of the unneeded C Code Comments.
Usage is very Straight-forward.
1st - Initialize the code module passing how many LEDs there are in your array.
2nd - In the code Module's C Code Block, Where you have "byte RGB[120]", you MUST replace it with the value of your Number Of Leds in the Array, multiplied by 3! THIS IS VERY IMPORTANT
3rd - Call SetLed, passing the Led to act upon, and the RGB values...
This code has Two features built in that will be very handy, I think...
1st - If -1 is passed as the Led number in SetLed, all the array will be set with the RGB values
2nd - You don't need to change the whole array to act upon a single LED!
I would like to post a small video of this in action, but my math skills are not that great...
So if anyone can create me a "simple" code where 2 or 3 coloured lines bounce around my 8x5 bounds, I'll be thankful!
[EDIT] Added a simple test project that includes the Code Module
After accepting a challenge in this thread to create a Library to control the NeoPixel type of thingy, I ordered myself a 8x5 WS2812B based shield.
So yesterday I got my shield...
https://cdn-shop.adafruit.com/1200x900/1430-03.jpg
... and starting doing my research.
Almost 80% of results took me to the NeoPixel Library... BUT, I found a Video on YouTube from a Guy called Kevin Darrah, in which he explained his Library-less approach... and found his code...
So after about an hour of head banging and trouble shooting, I had success in controlling my shield using his C code as an inline C Block.
So I decided to Trim it to its smallest footprint, and created a Module for it.
B4X:
'Original Function Creator : Kevin Darrah
'B4R Code Module Adaptation : Cableguy
'Usage:
'I Think it could not be simpler...
'Initialize it passing the amount of LEDs in your array
'In this Module, down in the C code block, change "byte RGB[120];", changing the size of this array to be 3 times the amount of leds passed in the initializer
'Call "SetLed" passing the Led number and the RGB values to set it to
'That's it!!!
Sub Process_Globals
Private PIN8 As Pin
Private LED As Int
Private RED As Byte
Private GREEN As Byte
Private BLUE As Byte
Private NLEDs As Int
End Sub
Public Sub Initialize(NumberOfLEDS As Int)
PIN8.Initialize(8, PIN8.MODE_OUTPUT)
NLEDs = NumberOfLEDS
End Sub
Public Sub SetLed(LEDNumber As Int, R As Byte, G As Byte, B As Byte)
RED = R
GREEN = G
BLUE = B
LED = LEDNumber
If LED = -1 Then
Log("all lit")
Log(NLEDs)
For n = 0 To NLEDs-1
LED=n
RunNative("RGB_update", Null)
Next
Else
RunNative("RGB_update", Null)
End If
End Sub
#if C
#define numberOfLEDs b4r_ws2812b::_nleds
byte RGB[120];//take your number of LEDs and multiply by 3 ! THIS IS VERY IMPORTANT!!!
void RGB_update(B4R::Object* o) {
byte ExistingPort, WS2812pinHIGH;
if(b4r_ws2812b::_led >=0){
RGB[ b4r_ws2812b::_led * 3] = b4r_ws2812b::_green;
RGB[ b4r_ws2812b::_led * 3 + 1] = b4r_ws2812b::_red;
RGB[ b4r_ws2812b::_led * 3 + 2] = b4r_ws2812b::_blue;
}
noInterrupts();
ExistingPort = PORTB;
WS2812pinHIGH = PORTB | 1;
int bitStream = numberOfLEDs * 3;
for (int i = 0; i < bitStream; i++) {
PORTB = WS2812pinHIGH;//bit 7 first, set the pin HIGH - it always goes high regardless of a 0/1
PORTB = ((RGB[i] & B10000000) && B10000000) | ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = WS2812pinHIGH;//bit 6
PORTB = ((RGB[i] & B01000000) && B01000000) | ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = WS2812pinHIGH;//bit 5
PORTB = ((RGB[i] & B00100000) && B00100000) | ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = WS2812pinHIGH;//bit 4
PORTB = ((RGB[i] & B00010000) && B00010000) | ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = WS2812pinHIGH;//bit 3
PORTB = ((RGB[i] & B00001000) && B00001000) | ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = WS2812pinHIGH;//bit 2
PORTB = ((RGB[i] & B00000100) && B00000100) | ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = WS2812pinHIGH;//bit 1
PORTB = ((RGB[i] & B00000010) && B00000010) | ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = WS2812pinHIGH;//bit 0
__asm__("nop\n\t");
PORTB = ((RGB[i] & B00000001) && B00000001) | ExistingPort;
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
PORTB = ExistingPort;
}
interrupts();
}
#end if
The code is as well commented as possible, Although I took out all of the unneeded C Code Comments.
Usage is very Straight-forward.
1st - Initialize the code module passing how many LEDs there are in your array.
2nd - In the code Module's C Code Block, Where you have "byte RGB[120]", you MUST replace it with the value of your Number Of Leds in the Array, multiplied by 3! THIS IS VERY IMPORTANT
3rd - Call SetLed, passing the Led to act upon, and the RGB values...
This code has Two features built in that will be very handy, I think...
1st - If -1 is passed as the Led number in SetLed, all the array will be set with the RGB values
2nd - You don't need to change the whole array to act upon a single LED!
I would like to post a small video of this in action, but my math skills are not that great...
So if anyone can create me a "simple" code where 2 or 3 coloured lines bounce around my 8x5 bounds, I'll be thankful!
[EDIT] Added a simple test project that includes the Code Module
Attachments
Last edited: