convert hex str to color
This commit is contained in:
@@ -5,22 +5,13 @@
|
||||
#include "utils.h"
|
||||
#include "bitmanip.h"
|
||||
|
||||
// prototypes
|
||||
void init_rgb();
|
||||
|
||||
void set_rgb(uint8_t r, uint8_t g, uint8_t b);
|
||||
|
||||
void wheel(uint8_t pos) {
|
||||
pos = 255 - pos;
|
||||
if (pos < 85) {
|
||||
set_rgb(255 - pos * 3, 0, pos * 3);
|
||||
} else if (pos < 170) {
|
||||
pos = pos - 85;
|
||||
set_rgb(0, pos * 3, 255 - pos * 3);
|
||||
} else {
|
||||
pos = pos - 170;
|
||||
set_rgb(pos * 3, 255 - pos * 3, 0);
|
||||
}
|
||||
}
|
||||
uint8_t get_red(char *input);
|
||||
uint8_t get_green(char *input);
|
||||
uint8_t get_blue(char *input);
|
||||
uint8_t hex_to_int(char c);
|
||||
|
||||
// Table 14-9 : Port D Pins Alternate Functions
|
||||
// OC2B (PD3) : Red
|
||||
@@ -56,14 +47,48 @@ void set_rgb(uint8_t r, uint8_t g, uint8_t b) {
|
||||
OCR0A = b; // Blue (PD6, Timer0 OCR0A)
|
||||
}
|
||||
|
||||
// convert hex to int : https://stackoverflow.com/a/39052987
|
||||
uint8_t hex_to_int(char c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if (c >= 'A' && c <= 'F') {
|
||||
return c - 'A' + 10;
|
||||
}
|
||||
if (c >= 'a' && c <= 'f') {
|
||||
return c - 'a' + 10;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// "<< 4" is equivalent to "* 16"
|
||||
uint8_t get_red(char *input) {
|
||||
if (input[0] == '#') {
|
||||
return (hex_to_int(input[1]) << 4) + hex_to_int(input[2]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
uint8_t get_green(char *input) {
|
||||
if (input[0] == '#') {
|
||||
return (hex_to_int(input[3]) << 4) + hex_to_int(input[4]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
uint8_t get_blue(char *input) {
|
||||
if (input[0] == '#') {
|
||||
return (hex_to_int(input[5]) << 4) + hex_to_int(input[6]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// led RGB D5 must turns on in a loop of colors using PWM
|
||||
int main() {
|
||||
init_rgb();
|
||||
|
||||
uint8_t pos = 0;
|
||||
while (1) {
|
||||
wheel(pos); // Set color based on wheel position
|
||||
_delay_ms(10); // Small delay for smooth transition
|
||||
pos++; // Increment color position
|
||||
}
|
||||
char *input = "#FFFF00";
|
||||
uint8_t r = get_red(input);
|
||||
uint8_t g = get_green(input);
|
||||
uint8_t b = get_blue(input);
|
||||
set_rgb(r, g, b);
|
||||
while (1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user