Created
June 24, 2023 19:13
-
-
Save tihmstar/bb39bc85357254dc8a7e5a9b92df92d0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <FastLED.h> | |
/* | |
Using "Gelid Solutions Codi6 ARGB-Controller-Kit", controll Fans and LEDS | |
On socket PWM9, bridge LED "D" pin with the rightmost FAN pin (the one without description) | |
Using serial (baud 115200) | |
echo "200" > /dev/ttyUSB1 #write value 0-255 for fan speed | |
echo "r" > /dev/ttyUSB1 #make LEDs do colorful rainbow | |
echo "d" > /dev/ttyUSB1 #turn off LEDs | |
echo "c 0 255 0" > /dev/ttyUSB1 #manuall write <R> <G> <B> color | |
*/ | |
#define BRIGHTNESS 255 /* Control the brightness of your leds */ | |
#define SATURATION 255 /* Control the saturation of your leds */ | |
#define LEDS_FAN1 18 | |
#define NUM_LEDS LEDS_FAN1 | |
CRGB leds[NUM_LEDS]; | |
//configure Timer 1 (pins 9,10) to output 25kHz PWM | |
void setupTimer1(){ | |
//Set PWM frequency to about 25khz on pins 9,10 (timer 1 mode 10, no prescale, count to 320) | |
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11); | |
TCCR1B = (1 << CS10) | (1 << WGM13); | |
ICR1 = 320; | |
OCR1A = 0; | |
OCR1B = 0; | |
} | |
//equivalent of analogWrite on pin 9 | |
void setPWM1A(float f){ | |
f=f<0?0:f>1?1:f; | |
OCR1A = (uint16_t)(320*f); | |
} | |
void setLedsRainbow(){ | |
for (int j = 0; j < 255; j++) { | |
for (int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = CHSV(i - (j * 4), BRIGHTNESS, SATURATION); /* The higher the value 4 the less fade there is */ | |
} | |
FastLED.show(); | |
if (Serial.available() > 0) break; | |
delay(30); /* Change this to your hearts desire, the lower the value the faster your colors move (and vice versa) */ | |
} | |
} | |
void disableAllLeds(){ | |
for (int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = CHSV(0,0,0); | |
} | |
FastLED.show(); | |
} | |
void setLedToRGB(uint8_t r, uint8_t g, uint8_t b){ | |
for (int i = 0; i < NUM_LEDS; i++) { | |
CRGB color = {r,g,b}; | |
leds[i] = color; | |
} | |
FastLED.show(); | |
} | |
int speed = 0; | |
int haveSpeed = 0; | |
int isReadingColor = 0; | |
int isReadingSpeed = 0; | |
uint32_t color_RGB = 0; | |
int haveColor = 0; | |
int haveRainbow = 0; | |
void readSerial(){ | |
if (Serial.available() > 0) { | |
int didFail = 0; | |
int clearVals = 0; | |
char c = Serial.read(); | |
if ( c == 'c' ){ | |
if (!isReadingColor) isReadingColor = 1; | |
else didFail = 1; | |
color_RGB = 0; | |
speed = 0; | |
isReadingSpeed = 0; | |
} else if (c == 'r'){ | |
haveRainbow = 1; | |
clearVals = 1; | |
isReadingSpeed = 0; | |
Serial.println("Enabling Rainbow mode!"); | |
} else if (c == 'd'){ | |
disableAllLeds(); | |
haveRainbow = 0; | |
clearVals = 1; | |
isReadingSpeed = 0; | |
Serial.println("Disabling LEDs"); | |
} else if (c >= '0' && c <= '9'){ | |
speed *= 10; | |
speed += c - '0'; | |
isReadingSpeed = 1; | |
} else if (c == '\n' || c == '\r') { | |
if (isReadingColor) { | |
color_RGB <<= 8; | |
if (speed > 255) speed = 255; | |
color_RGB |= (speed & 0xff); | |
speed = 0; | |
haveColor = 1; | |
isReadingColor = 0; | |
}else if (isReadingSpeed){ | |
if (speed > 255) speed = 255; | |
haveSpeed = 1; | |
}else{ | |
Serial.println(""); | |
clearVals = 1; | |
} | |
} else if (c == ' '){ | |
color_RGB <<= 8; | |
if (speed > 255) speed = 255; | |
color_RGB |= (speed & 0xff); | |
speed = 0; | |
isReadingSpeed = 0; | |
} else { | |
didFail = 1; | |
} | |
if (didFail){ | |
Serial.println("Failed to read input!"); | |
clearVals = 1; | |
} | |
if (!isReadingSpeed && !isReadingColor){ | |
clearVals = 1; | |
} | |
if (clearVals){ | |
speed = 0; | |
haveSpeed = 0; | |
isReadingColor = 0; | |
isReadingSpeed = 0; | |
color_RGB = 0; | |
haveColor = 0; | |
} | |
} | |
} | |
/* --- MAIN --- */ | |
void setup(){ | |
//enable outputs for Timer 1 | |
pinMode(9,OUTPUT); //1A | |
pinMode(10,OUTPUT); //1B | |
setupTimer1(); | |
setPWM1A(0.3f); | |
FastLED.addLeds<NEOPIXEL, 6>(leds, 0, LEDS_FAN1); | |
disableAllLeds(); | |
Serial.begin(115200); | |
} | |
void loop(){ | |
readSerial(); | |
if (haveSpeed){ | |
haveSpeed = 0; | |
Serial.print("Setting speed to: "); | |
Serial.println(speed, DEC); | |
setPWM1A((float)speed/255); | |
speed = 0; | |
} | |
if (haveColor){ | |
haveRainbow = 0; | |
haveColor = 0; | |
uint8_t r = (uint8_t)(color_RGB >> 16); | |
uint8_t g = (uint8_t)(color_RGB >> 8); | |
uint8_t b = (uint8_t)(color_RGB >> 0); | |
Serial.print("Setting RGB to to: "); | |
Serial.print(r, DEC); | |
Serial.print(" "); | |
Serial.print(g, DEC); | |
Serial.print(" "); | |
Serial.println(b, DEC); | |
setLedToRGB(r,g,b); | |
} | |
if (haveRainbow){ | |
setLedsRainbow(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment