forked from teachop/xcore_neopixel_buffered
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
driver task split out into own files
- Loading branch information
Showing
4 changed files
with
139 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//--------------------------------------------------------- | ||
// Buffered NeoPixel driver header | ||
// by teachop | ||
// | ||
|
||
#ifndef __NEOPIXEL_H__ | ||
#define __NEOPIXEL_H__ | ||
|
||
#define LEDS 60 | ||
|
||
// neopixel driver interface, Adafruit library-like | ||
interface neopixel_if { | ||
void show(void); | ||
void setPixelColor(uint32_t pixel, uint32_t color); | ||
void setPixelColorRGB(uint32_t pixel, uint8_t r, uint8_t g, uint8_t b); | ||
void setBrightness(uint8_t bright); | ||
uint32_t getPixelColor(uint32_t pixel); | ||
uint32_t Color(uint8_t r, uint8_t g, uint8_t b); | ||
uint32_t numPixels(void); | ||
}; | ||
|
||
[[combinable]] void neopixel_task(port neo, interface neopixel_if server dvr); | ||
|
||
|
||
#endif // __NEOPIXEL_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//--------------------------------------------------------- | ||
// Buffered NeoPixel driver | ||
// by teachop | ||
// | ||
|
||
#include <xs1.h> | ||
#include <stdint.h> | ||
#include "neopixel.h" | ||
|
||
|
||
// --------------------------------------------------------- | ||
// neopixel_task - output driver for one neopixel strip | ||
// | ||
[[combinable]] | ||
void neopixel_task(port neo, interface neopixel_if server dvr) { | ||
uint32_t length = LEDS; | ||
uint8_t colors[LEDS*3]; | ||
const uint32_t delay_third = 42; | ||
uint8_t brightness=0; | ||
|
||
while( 1 ) { | ||
select { | ||
case dvr.Color(uint8_t r, uint8_t g, uint8_t b) -> uint32_t return_val: | ||
return_val = ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; | ||
break; | ||
case dvr.numPixels() -> uint32_t return_val: | ||
return_val = length; | ||
break; | ||
case dvr.setBrightness(uint8_t bright): | ||
brightness = bright+1; | ||
break; | ||
case dvr.getPixelColor(uint32_t pixel) -> uint32_t return_val: | ||
if ( length > pixel ) { | ||
uint32_t index = 3*pixel; | ||
return_val = ((uint32_t)colors[index+1] << 16) | ((uint32_t)colors[index] << 8) | colors[index+2]; | ||
} else { | ||
return_val = 0; | ||
} | ||
break; | ||
case dvr.setPixelColor(uint32_t pixel, uint32_t color): | ||
if ( length > pixel ) { | ||
uint32_t index = 3*pixel; | ||
colors[index++] = color>>8;//g | ||
colors[index++] = color>>16;//r | ||
colors[index] = color;//b | ||
} | ||
break; | ||
case dvr.setPixelColorRGB(uint32_t pixel, uint8_t r, uint8_t g, uint8_t b): | ||
if ( length > pixel ) { | ||
uint32_t index = 3*pixel; | ||
colors[index++] = g; | ||
colors[index++] = r; | ||
colors[index] = b; | ||
} | ||
break; | ||
case dvr.show(): | ||
// beginning of strip, sync counter | ||
uint32_t delay_count, bit; | ||
neo <: 0 @ delay_count; | ||
#pragma unsafe arrays | ||
for (uint32_t index=0; index<sizeof(colors); ++index) { | ||
uint32_t color_shift = colors[index]; | ||
uint32_t bit_count = 8; | ||
while (bit_count--) { | ||
// output low->high transition | ||
delay_count += delay_third; | ||
neo @ delay_count <: 1; | ||
// output high->data transition | ||
if ( brightness && (7==bit_count) ) { | ||
color_shift = (brightness*color_shift)>>8; | ||
} | ||
bit = (color_shift & 0x80)? 1 : 0; | ||
color_shift <<=1; | ||
delay_count += delay_third; | ||
neo @ delay_count <: bit; | ||
// output data->low transition | ||
delay_count += delay_third; | ||
neo @ delay_count <: 0; | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters