Make module count dynamic

This commit is contained in:
Thomas Kolb 2017-03-05 01:55:40 +01:00
parent a862d0e9ff
commit 0fc6f787da
5 changed files with 31 additions and 23 deletions

View file

@ -10,12 +10,12 @@ struct Colour {
int16_t red, green, blue; // value range is 0 to 255 int16_t red, green, blue; // value range is 0 to 255
}; };
struct Colour curColour[NUM_MODULES]; struct Colour curColour[MAX_NUM_MODULES];
struct Colour targetColour[NUM_MODULES]; struct Colour targetColour[MAX_NUM_MODULES];
void fader_init(void) void fader_init(void)
{ {
for(uint32_t i = 0; i < NUM_MODULES; i++) { for(uint32_t i = 0; i < ws2801_num_modules; i++) {
curColour[i].red = targetColour[i].red = 0; curColour[i].red = targetColour[i].red = 0;
curColour[i].green = targetColour[i].green = 0; curColour[i].green = targetColour[i].green = 0;
curColour[i].blue = targetColour[i].blue = 0; curColour[i].blue = targetColour[i].blue = 0;
@ -97,7 +97,7 @@ static void fade_colour(int16_t *cur, const int16_t *target, int *changed)
void fader_update(void) void fader_update(void)
{ {
for(uint32_t i = 0; i < NUM_MODULES; i++) { for(uint32_t i = 0; i < ws2801_num_modules; i++) {
fade_colour(&(curColour[i].red), &(targetColour[i].red), &somethingChanged); fade_colour(&(curColour[i].red), &(targetColour[i].red), &somethingChanged);
fade_colour(&(curColour[i].green), &(targetColour[i].green), &somethingChanged); fade_colour(&(curColour[i].green), &(targetColour[i].green), &somethingChanged);
fade_colour(&(curColour[i].blue), &(targetColour[i].blue), &somethingChanged); fade_colour(&(curColour[i].blue), &(targetColour[i].blue), &somethingChanged);

View file

@ -64,7 +64,7 @@ void setup()
ws2801_udp_setup(); ws2801_udp_setup();
for(int i = 0; i < NUM_MODULES; i++) { for(int i = 0; i < MAX_NUM_MODULES; i++) {
fader_set_colour(i, 0, 0, 0); fader_set_colour(i, 0, 0, 0);
fader_fade_colour(i, 0, 255, 0); fader_fade_colour(i, 0, 255, 0);
} }
@ -93,15 +93,15 @@ void loop()
if((fader_loop % 200) == 0) { if((fader_loop % 200) == 0) {
int round = (fader_loop / 200) % 3; int round = (fader_loop / 200) % 3;
if(round == 0) { if(round == 0) {
for(int i = 0; i < NUM_MODULES; i++) { for(int i = 0; i < MAX_NUM_MODULES; i++) {
fader_fade_colour(i, 255, 0, 0); fader_fade_colour(i, 255, 0, 0);
} }
} else if(round == 1) { } else if(round == 1) {
for(int i = 0; i < NUM_MODULES; i++) { for(int i = 0; i < MAX_NUM_MODULES; i++) {
fader_fade_colour(i, 0, 255, 0); fader_fade_colour(i, 0, 255, 0);
} }
} else { } else {
for(int i = 0; i < NUM_MODULES; i++) { for(int i = 0; i < MAX_NUM_MODULES; i++) {
fader_fade_colour(i, 0, 0, 255); fader_fade_colour(i, 0, 0, 255);
} }
} }

View file

@ -2,10 +2,14 @@
#include "ws2801.h" #include "ws2801.h"
uint8_t message[3 * NUM_MODULES]; uint8_t message[3 * MAX_NUM_MODULES];
uint32_t ws2801_num_modules;
void ws2801_init(void) void ws2801_init(void)
{ {
ws2801_num_modules = MAX_NUM_MODULES;
SPI.begin(); SPI.begin();
} }
@ -20,10 +24,16 @@ void ws2801_send_update(void)
{ {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0)); SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
for(uint32_t i = 0; i < 3*NUM_MODULES; i++) { for(uint32_t i = 0; i < 3*ws2801_num_modules; i++) {
SPI.transfer(message[i]); SPI.transfer(message[i]);
} }
SPI.endTransaction(); SPI.endTransaction();
} }
void ws2801_set_num_modules(uint32_t n)
{
if(n <= MAX_NUM_MODULES) {
ws2801_num_modules = n;
}
}

View file

@ -3,10 +3,13 @@
#include <stdint.h> #include <stdint.h>
#define NUM_MODULES 32 #define MAX_NUM_MODULES 255
extern uint32_t ws2801_num_modules;
void ws2801_init(void); void ws2801_init(void);
void ws2801_set_colour(uint32_t module, uint8_t red, uint8_t green, uint8_t blue); void ws2801_set_colour(uint32_t module, uint8_t red, uint8_t green, uint8_t blue);
void ws2801_send_update(void); void ws2801_send_update(void);
void ws2801_set_num_modules(uint32_t n);
#endif // WS2801_H #endif // WS2801_H

View file

@ -16,7 +16,8 @@ enum {
SET_COLOUR = 0, SET_COLOUR = 0,
FADE_COLOUR = 1, FADE_COLOUR = 1,
ADD_COLOUR = 2, ADD_COLOUR = 2,
SET_FADESTEP = 3 SET_FADESTEP = 3,
SET_NUM_MODULES = 4
}; };
void ws2801_udp_setup(void) void ws2801_udp_setup(void)
@ -40,16 +41,6 @@ void ws2801_udp_loop(void)
int packetSize = ws2801UDP.parsePacket(); int packetSize = ws2801UDP.parsePacket();
if(packetSize) if(packetSize)
{ {
/*
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = ws2801UDP.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(ws2801UDP.remotePort());
*/
// read the packet into packetBufffer // read the packet into packetBufffer
while((len = ws2801UDP.read(cmd, WS2801_CMD_LEN)) == WS2801_CMD_LEN) { while((len = ws2801UDP.read(cmd, WS2801_CMD_LEN)) == WS2801_CMD_LEN) {
action = cmd[0]; action = cmd[0];
@ -58,7 +49,7 @@ void ws2801_udp_loop(void)
g = cmd[3]; g = cmd[3];
b = cmd[4]; b = cmd[4];
if(module >= NUM_MODULES) { if(module >= ws2801_num_modules) {
// module index out of range // module index out of range
continue; continue;
} }
@ -80,6 +71,10 @@ void ws2801_udp_loop(void)
fader_set_fadestep(r); // red channel contains the fadestep in this case fader_set_fadestep(r); // red channel contains the fadestep in this case
break; break;
case SET_NUM_MODULES:
// red and green channels contain the number of modules, big endian
ws2801_set_num_modules(((uint32_t)r << 16) + ((uint32_t)g << 8) + b);
default: default:
//Serial.println("WS2801UDP: ERROR: invalid action received!"); //Serial.println("WS2801UDP: ERROR: invalid action received!");
break; break;