From 9b3a2ac71c9d2e64c4e3a686170a71263b3c935d Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Sat, 14 May 2022 20:48:54 +0100 Subject: [PATCH] Initial commit. --- project-lamp.ino | 188 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 project-lamp.ino diff --git a/project-lamp.ino b/project-lamp.ino new file mode 100644 index 0000000..9747018 --- /dev/null +++ b/project-lamp.ino @@ -0,0 +1,188 @@ +#include "FastLED.h" +#include "FastLED_RGBW.h" +#include + +#define NUM_LEDS 1 +#define DATA_PIN 4 + +#define IR_RECEIVE_PIN 2 + +CRGBW COL_RED (255, 0, 0, 0); +CRGBW COL_GREEN ( 0,255, 0, 0); +CRGBW COL_BLUE ( 0, 0,255, 0); +CRGBW COL_WHITE ( 0, 0, 0,255); +CRGBW COL_SUM (255,255,255,255); +CRGBW COL_AMBER (255,191, 0, 0); +CRGBW COL_LIME (127,255, 0, 0); +CRGBW COL_AZURE ( 0,127,255, 0); +CRGBW COL_ORANGE (255,127, 0, 0); +CRGBW COL_CYAN ( 0,255,255, 0); +CRGBW COL_UV ( 63, 0,255, 0); +CRGBW COL_PEACHY (255, 63, 0, 0); +CRGBW COL_SPRING ( 0,255,127, 0); +CRGBW COL_MAGENTA(255, 0,255, 0); +CRGBW COL_YELLOW (255,255, 0, 0); +CRGBW COL_VIOLET (127, 0,255, 0); +CRGBW COL_ROSE (255, 0,127, 0); + +CRGBW desaturate(CRGBW col) +{ + return CRGBW(col.red, col.green, col.blue, col.white + 64); +} + +struct ColourCode +{ + uint32_t code; + CRGBW colour; + CRGBW altColour; + + ColourCode(uint32_t c, CRGBW col) + { + code = c; + colour = col; + altColour = desaturate(col); + } + + ColourCode(uint32_t c, CRGBW colA, CRGBW colB) + { + code = c; + colour = colA; + altColour = colB; + } +}; + +const ColourCode colourCodes[] = +{ + {0x97483BFB, COL_RED}, + {0x86B0E697, COL_GREEN}, + {0x9EF4941F, COL_BLUE}, + {0xA3C8EDDB, COL_SUM, COL_WHITE}, + {0x5BE75E7F, COL_AMBER}, + {0xF377C5B7, COL_LIME}, + {0xC101E57B, COL_AZURE}, + {0xD7E84B1B, COL_ORANGE}, + {0xEE4ECCFB, COL_CYAN}, + {0x51E43D1B, COL_UV}, + {0x2A89195F, COL_PEACHY}, + {0xF63C8657, COL_SPRING}, + {0x44C407DB, COL_MAGENTA}, + {0x488F3CBB, COL_YELLOW}, + {0x13549BDF, COL_VIOLET}, + {0x35A9425F, COL_ROSE} +}; + +const uint32_t CODE_BRIGHTER = 0xE5CFBD7F; +const uint32_t CODE_DIMMER = 0xA23C94BF; +const uint32_t CODE_OFF = 0xE721C0DB; +const uint32_t CODE_ON = 0xF0C41643; + +const int NUM_COLOURCODES = sizeof(colourCodes) / sizeof(ColourCode); + +CRGBW leds[NUM_LEDS]; +CRGB *ledsRGB = (CRGB *) &leds[0]; + +IRrecv irrecv(IR_RECEIVE_PIN); + +uint8_t brightness = 128; +uint32_t lastCode = 0; + +decode_results results; + + +void setup() { + FastLED.addLeds(ledsRGB, getRGBWsize(NUM_LEDS)); + FastLED.setBrightness(128); + FastLED.show(); + pinMode(LED_BUILTIN, OUTPUT); + + Serial.begin(115200); +#if defined(__AVR_ATmega32U4__) + while (!Serial) + ; //delay for Leonardo, but this loops forever for Maple Serial +#endif +#if defined(SERIAL_USB) || defined(SERIAL_PORT_USBVIRTUAL) + delay(2000); // To be able to connect Serial monitor after reset and before first printout +#endif + // Just to know which program is running on my Arduino + Serial.println(F("START " __FILE__ " from " __DATE__)); + + // In case the interrupt driver crashes on setup, give a clue + // to the user what's going on. + Serial.println("Enabling IRin"); + irrecv.enableIRIn(); // Start the receiver + + Serial.print(F("Ready to receive IR signals at pin ")); + Serial.println(IR_RECEIVE_PIN); +} + +void colorFill(CRGBW c){ + for(int i = 0; i < NUM_LEDS; i++){ + leds[i] = c; + FastLED.show(); + delay(50); + } +} + +void colorFill(CRGB c){ + for(int i = 0; i < NUM_LEDS; i++){ + leds[i] = c; + FastLED.show(); + delay(50); + } +} + +void interpretColourCode(uint32_t code) +{ + for (int i = 0; i < NUM_COLOURCODES; i++) + { + ColourCode col = colourCodes[i]; + if (col.code == code) + { + if (code == lastCode) + { + colorFill(col.altColour); + lastCode = 0; + } + else + { + colorFill(col.colour); + lastCode = code; + } + break; + } + } +} + +void loop() +{ + if (irrecv.decode(&results)) + { + Serial.println(results.value, HEX); + irrecv.resume(); // Receive the next value + switch(results.value) + { + case CODE_BRIGHTER: + brightness += 32; + FastLED.setBrightness(brightness); + FastLED.show(); + break; + case CODE_DIMMER: + brightness -= 32; + FastLED.setBrightness(brightness); + FastLED.show(); + break; + case CODE_OFF: + FastLED.setBrightness(0); + FastLED.show(); + break; + case CODE_ON: + FastLED.setBrightness(brightness); + FastLED.show(); + break; + default: + interpretColourCode(results.value); + + } + } + delay(100); +}