Last active
December 11, 2020 15:47
-
-
Save goran-mahovlic/33bd2a9e4cde4ee48b26f52d7a5b9d24 to your computer and use it in GitHub Desktop.
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
/* shifts left the '1' over pos times to create a single HIGH bit at location pos. */ | |
#define BIT(pos) ( 1<<(pos) ) | |
/* Set single bit at pos to '1' by generating a mask | |
in the proper bit location and ORing x with the mask. */ | |
#define SET_BIT(x, pos) ( (x) |= (BIT(pos)) ) | |
/* Set single bit at pos to '0' by generating a mask | |
in the proper bit location and ORing x with the mask. */ | |
#define UNSET_BIT(x, pos) ( (x) &= ~(BIT(pos)) ) | |
/* Set single bit at pos to opposite of what is currently is by generating a mask | |
in the proper bit location and ORing x with the mask. */ | |
#define FLIP_BIT(x, pos) ( (x) ^= (BIT(pos)) ) | |
/* Return '1' if the bit value at position pos within y is '1' and '0' if it's 0 by | |
ANDing x with a bit mask where the bit in pos's position is '1' and '0' elsewhere and | |
comparing it to all 0's. Returns '1' in least significant bit position if the value | |
of the bit is '1', '0' if it was '0'. */ | |
#define CHECK_BIT(var,pos)((var) & (1<<(pos))) | |
#include <PololuLedStrip.h> | |
// Create an ledStrip object and specify the pin it will use. | |
PololuLedStrip<21> ledStrip; | |
// Create a buffer for holding the colors (3 bytes per color). | |
#define LED_COUNT 100 | |
#define MAX_RX_BYTES 20 | |
#define HEADER 0xFF | |
#define FOOTER 0xFE | |
#define SET_PIXELS_WHITE_VALUE 0x01 | |
#define SET_PIXELS_RBG_VALUE 0x02 | |
#define SET_GROUPS 0x03 | |
#define SET_PIXEL_WHITE_BY_GROUPS 0x04 | |
#define SET_PIXEL_RBG_BY_GROUPS 0x05 | |
rgb_color colors[LED_COUNT]; | |
int incomingByte = 0; // for incoming serial data | |
bool statusLED1 = false; | |
bool statusLED2 = false; | |
bool statusLED3 = false; | |
bool dataReady = false; | |
byte RX[MAX_RX_BYTES]; | |
int rxIndex; | |
int rxCount; | |
uint16_t ledGroups[LED_COUNT]; | |
// First two bits could be used for something else, as we do not need to store them SET_ALL/SET_INDIVIDUAL | VALUE_LOAD_TYPE | |
// SET_ALL/SET_INDIVIDUAL | VALUE_LOAD_TYPE | PERSISTENT/BLINK_INTERVAL | RANDOM_INTERVAL/WAVE_INTERVAL | INTERVAL 4 bits | |
uint8_t ledValueType[LED_COUNT]; | |
// Default module address | |
uint8_t MODULE_ADDRESS = 0; | |
#define SW1 22 | |
#define SW2 23 | |
#define SW3 5 | |
#define SW4 6 | |
#define SW5 7 | |
#define SW6 8 | |
#define SW7 9 | |
#define SW8 10 | |
void setup() { | |
// Set serial speed | |
Serial.begin(57600); | |
pinMode(SW1, INPUT_PULLUP); | |
pinMode(SW2, INPUT_PULLUP); | |
pinMode(SW3, INPUT_PULLUP); | |
pinMode(SW4, INPUT_PULLUP); | |
pinMode(SW5, INPUT_PULLUP); | |
pinMode(SW6, INPUT_PULLUP); | |
pinMode(SW7, INPUT_PULLUP); | |
pinMode(SW8, INPUT_PULLUP); | |
// PinMode za SWITCH | |
getModuleAddress(); | |
initBlink(); | |
// As default set all leds to group 0 binary BIN = 0000000000000001 >> DEC = 1 | |
setAllLedsToGroups(0); | |
rxIndex = 0; | |
rxCount = 0; | |
} | |
void loop() { | |
// on each loop get device address - that way we can simplycheck device change | |
getModuleAddress(); | |
if(!dataReady){ | |
getSerialData(); | |
} | |
if(dataReady){ | |
checkData(); | |
} | |
} | |
void checkData(void){ | |
// Switch commands | |
switch (RX[1]){ | |
case SET_PIXELS_WHITE_VALUE: | |
// Check if module is in range | |
if (RX[2] <= MODULE_ADDRESS && MODULE_ADDRESS <= RX[3]){ | |
// Set all LEDs in range | |
for(int i=RX[4];i<=RX[5];i++){ | |
// save all value types | |
ledValueType[i] = RX[6]; | |
colors[i] = rgb_color(RX[7], RX[7], RX[7]); | |
} | |
ledStrip.write(colors, LED_COUNT); | |
dataReady = false; | |
// Clear RX buffer | |
memset(RX, 0, sizeof(RX)); | |
} | |
else{ | |
// If we message is not for this pannel -- ignore it and wait for next | |
dataReady = false; | |
memset(RX, 0, sizeof(RX)); | |
} | |
break; | |
case SET_PIXELS_RBG_VALUE: | |
// statements | |
// Check if module is in range | |
if (RX[2] <= MODULE_ADDRESS && MODULE_ADDRESS <= RX[3]){ | |
// Set all LEDs in range | |
for(int i=RX[4];i<=RX[5];i++){ | |
ledValueType[i] = RX[6]; | |
colors[i] = rgb_color(RX[7], RX[8], RX[9]); | |
} | |
ledStrip.write(colors, LED_COUNT); | |
dataReady = false; | |
// Clear RX buffer | |
memset(RX, 0, sizeof(RX)); | |
} | |
else{ | |
// If we message is not for this pannel -- ignore it and wait for next | |
dataReady = false; | |
memset(RX, 0, sizeof(RX)); | |
} | |
break; | |
case SET_GROUPS: | |
// statements | |
// Check if module is in range | |
if (RX[2] <= MODULE_ADDRESS && MODULE_ADDRESS <= RX[3]){ | |
// Set all LEDs in range | |
for(int i=RX[4];i<=RX[5];i++){ | |
setLedToGroup(i,(RX[6]<<8 | RX[7])); | |
} | |
dataReady = false; | |
// Clear RX buffer | |
memset(RX, 0, sizeof(RX)); | |
} | |
else{ | |
// If we message is not for this pannel -- ignore it and wait for next | |
dataReady = false; | |
memset(RX, 0, sizeof(RX)); | |
} | |
break; | |
case SET_PIXEL_WHITE_BY_GROUPS: | |
// statements | |
for(int i=0;i<LED_COUNT;i++){ | |
// If LED is in the group | |
if(checkGroupBits(i,(RX[2]<<8 | RX[3]))){ | |
ledValueType[i]= RX[4]; | |
colors[i] = rgb_color(RX[5], RX[5], RX[5]); | |
} | |
} | |
ledStrip.write(colors, LED_COUNT); | |
dataReady = false; | |
// Clear RX buffer | |
memset(RX, 0, sizeof(RX)); | |
break; | |
case SET_PIXEL_RBG_BY_GROUPS: | |
// statements | |
for(int i=0;i<LED_COUNT;i++){ | |
// If LED is in the group | |
if(checkGroupBits(i,(RX[2]<<8 | RX[3]))){ | |
ledValueType[i]= RX[4]; | |
colors[i] = rgb_color(RX[5], RX[6], RX[7]); | |
} | |
} | |
ledStrip.write(colors, LED_COUNT); | |
dataReady = false; | |
// Clear RX buffer | |
memset(RX, 0, sizeof(RX)); | |
break; | |
// statements | |
dataReady = false; | |
// Clear RX buffer | |
memset(RX, 0, sizeof(RX)); | |
break; | |
} | |
} | |
void getSerialData(void){ | |
// get data: | |
if (Serial.available() > 0) { | |
RX[rxIndex] = Serial.read(); | |
if(RX[0] == HEADER){ | |
rxIndex++; | |
} | |
if(RX[rxIndex-1] == FOOTER){ | |
dataReady = true; | |
rxCount = rxIndex; | |
rxIndex=0; | |
} | |
} | |
} | |
boolean checkGroupBits(uint8_t ledID, uint16_t groups){ | |
for(int i=0;i<16;i++){ | |
if((CHECK_BIT(ledGroups[ledID],i)) && CHECK_BIT(groups,i)){ | |
return 1; | |
} | |
} | |
return 0; | |
} | |
void setAllLedsToGroups(uint16_t groups){ | |
for(int i=0;i<LED_COUNT;i++){ | |
setLedToGroup(i, groups); | |
} | |
} | |
void setLedToGroup(uint8_t ledID, uint16_t groups){ | |
ledGroups[ledID] = groups; | |
} | |
/* | |
uint16_t getLedGroup(uint8_t ledID){ | |
uint16_t group; | |
group = ledGroups[ledID]; | |
return group; | |
} | |
*/ | |
void getModuleAddress(void){ | |
MODULE_ADDRESS = digitalRead(SW8)<<7 | digitalRead(SW7)<<6 | digitalRead(SW6)<<5 | digitalRead(SW5)<<4 | digitalRead(SW4)<<3 | digitalRead(SW3)<<2 | digitalRead(SW2)<<1 | digitalRead(SW1); | |
MODULE_ADDRESS = ~MODULE_ADDRESS; | |
} | |
void initBlink(){ | |
for (uint16_t i = 0; i < LED_COUNT; i++){ | |
colors[i] = rgb_color(50, 50, 50); | |
} | |
ledStrip.write(colors, LED_COUNT); | |
delay(200); | |
for (uint16_t i = 0; i < LED_COUNT; i++){ | |
colors[i] = rgb_color(0, 0, 0); | |
} | |
ledStrip.write(colors, LED_COUNT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment