fixed all exercices
This commit is contained in:
parent
5763bbb065
commit
2f91952d78
49 changed files with 3151 additions and 24 deletions
157
ex04/src/main.c
157
ex04/src/main.c
|
|
@ -13,6 +13,7 @@
|
|||
#include "uart.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define BUFFER_SIZE (128)
|
||||
#define tabsize(tab) (sizeof(tab) / sizeof(tab[0]))
|
||||
|
||||
static inline void set_col(rgb* rgb, uint8_t i, uint8_t val) {
|
||||
|
|
@ -24,11 +25,105 @@ static inline void set_col(rgb* rgb, uint8_t i, uint8_t val) {
|
|||
rgb->b = val;
|
||||
}
|
||||
|
||||
typedef struct command {
|
||||
enum { UNKNOWN, HELP, LED, FULLRAINBOW } ty;
|
||||
struct {
|
||||
uint8_t output;
|
||||
rgb col;
|
||||
} args;
|
||||
} command;
|
||||
// true on error
|
||||
bool parse_string(char* str, command* cmd) {
|
||||
if (ft_memcmp(str, "HELP", 5)) {
|
||||
cmd->ty = HELP;
|
||||
return false;
|
||||
}
|
||||
if (ft_memcmp(str, "#FULLRAINBOW", 13)) {
|
||||
cmd->ty = FULLRAINBOW;
|
||||
return false;
|
||||
}
|
||||
if (str[0] == '#') {
|
||||
rgb col = {};
|
||||
uint8_t hex[6] = {};
|
||||
for (uint8_t i = 0; i < 6; i++) {
|
||||
if ((hex[i] = is_hex_digit(str[1 + i])) == 255) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
col.r = hex[0] * 16 + hex[1];
|
||||
col.g = hex[2] * 16 + hex[3];
|
||||
col.b = hex[4] * 16 + hex[5];
|
||||
|
||||
if (str[7] != 'D' || !(str[8] == '6' || str[8] == '7' || str[8] == '8') || str[9]) {
|
||||
return true;
|
||||
}
|
||||
cmd->ty = LED;
|
||||
cmd->args.col = col;
|
||||
cmd->args.output = str[8] - '6';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void read_command(command* cmd) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
uint8_t cursor;
|
||||
start:
|
||||
ft_bzero(buffer, BUFFER_SIZE);
|
||||
cursor = 0;
|
||||
|
||||
uart_sendstring("> ");
|
||||
while (true) {
|
||||
uint8_t chr = uart_rx();
|
||||
if (chr == '\x7f' || chr == '\b') {
|
||||
if (cursor) {
|
||||
buffer[--cursor] = 0;
|
||||
uart_sendstring("\b \b");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (chr == '\r') {
|
||||
uart_sendstring("\r\n");
|
||||
if (parse_string(buffer, cmd)) {
|
||||
uart_sendstring("Invalid input: `");
|
||||
uart_sendstring(buffer);
|
||||
uart_sendstring("`\r\n");
|
||||
goto start;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (chr < ' ' || chr > '~')
|
||||
continue;
|
||||
|
||||
if (cursor + 1 >= BUFFER_SIZE)
|
||||
continue;
|
||||
if (chr >= 'a' && chr <= 'z')
|
||||
chr -= 'a' - 'A';
|
||||
buffer[cursor++] = chr;
|
||||
uart_tx(chr);
|
||||
}
|
||||
}
|
||||
const char OUTPUTS[3][3] = {"D6", "D7", "D8"};
|
||||
|
||||
rgb wheel(uint8_t pos) {
|
||||
pos = 255 - pos;
|
||||
if (pos < 85) {
|
||||
return (rgb){255 - pos * 3, 0, pos * 3};
|
||||
} else if (pos < 170) {
|
||||
pos = pos - 85;
|
||||
return (rgb){0, pos * 3, 255 - pos * 3};
|
||||
} else {
|
||||
pos = pos - 170;
|
||||
return (rgb){pos * 3, 255 - pos * 3, 0};
|
||||
}
|
||||
}
|
||||
|
||||
#define FULLRAINBOW_STEP (255 * 4)
|
||||
|
||||
int main(void) {
|
||||
init_rgb();
|
||||
uart_init();
|
||||
spi_init();
|
||||
adc_init(ADC_AVCC, ADC_PRESCALER_128);
|
||||
|
||||
rgb colors[3] = {
|
||||
{0x0, 0x00, 0x00}, //
|
||||
|
|
@ -36,34 +131,48 @@ int main(void) {
|
|||
{0x0, 0x00, 0x00}, //
|
||||
};
|
||||
|
||||
uint8_t selected_led = 0;
|
||||
uint8_t selected_col = 0;
|
||||
|
||||
uint8_t brightness[tabsize(colors)] = {};
|
||||
for (uint8_t i = 0; i < tabsize(colors); i++)
|
||||
for (uint8_t i = 0; i < tabsize(brightness); i++)
|
||||
brightness[i] = 31;
|
||||
|
||||
rgb display_col = {0, 0, 0};
|
||||
command cmd = {};
|
||||
|
||||
apa102_set_color((rgb){0, 0, 0}, 31);
|
||||
uint8_t prev = 0;
|
||||
apa102_set_colors(colors, brightness, tabsize(colors));
|
||||
while (true) {
|
||||
uint8_t data = adc_read_pin(ADC_ADC0);
|
||||
display_col = (rgb){0, 0, 0};
|
||||
set_col(&display_col, selected_col, data);
|
||||
set_rgb(display_col.r, display_col.g, display_col.b);
|
||||
read_command(&cmd);
|
||||
switch (cmd.ty) {
|
||||
case (UNKNOWN): {
|
||||
uart_sendstring("Unknown command. Use HELP to get list of commands\r\n");
|
||||
break;
|
||||
}
|
||||
case (HELP): {
|
||||
uart_sendstring("Write #RRGGBB<output> to set the <output> to the color\r\n");
|
||||
uart_sendstring("Write #FULLRAINBOW to send fullrainbow !\r\n");
|
||||
uart_sendstring("HELP will show you this message\r\n");
|
||||
break;
|
||||
}
|
||||
case (LED): {
|
||||
uart_sendstring("Updated led ");
|
||||
uart_sendstring(OUTPUTS[cmd.args.output]);
|
||||
uart_sendstring(" with new color !\r\n");
|
||||
colors[cmd.args.output] = cmd.args.col;
|
||||
break;
|
||||
}
|
||||
case (FULLRAINBOW): {
|
||||
uart_sendstring("FULLRAINBOW !!!!!!\r\n");
|
||||
for (uint16_t i = 0; i < FULLRAINBOW_STEP; i++) {
|
||||
rgb t = wheel(i);
|
||||
colors[0] = t;
|
||||
colors[1] = t;
|
||||
colors[2] = t;
|
||||
apa102_set_colors(colors, brightness, tabsize(colors));
|
||||
_delay_ms(((uint16_t)2000 / (uint16_t)FULLRAINBOW_STEP));
|
||||
}
|
||||
ft_bzero(colors, sizeof(colors));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t cur = PIND & (BV(PD2) | BV(PD4));
|
||||
if ((prev & BV(PD2)) && !(cur & BV(PD2))) {
|
||||
set_col(&colors[selected_led], selected_col, data);
|
||||
selected_col += 1;
|
||||
selected_col %= 3;
|
||||
}
|
||||
if ((prev & BV(PD4)) && !(cur & BV(PD4))) {
|
||||
selected_led += 1;
|
||||
selected_led %= 3;
|
||||
}
|
||||
prev = cur;
|
||||
apa102_set_colors(colors, brightness, tabsize(colors));
|
||||
_delay_ms(20);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue