Driver: Add basic remote control

This commit is contained in:
jaseg 2023-12-23 16:44:12 +01:00
parent 6033f3c6b9
commit 621d39437a
2 changed files with 88 additions and 43 deletions

View file

@ -9,7 +9,7 @@ MUSL_DIR ?= upstream/musl
# Algorithm parameters
########################################################################################################################
# - none -
DRIVER_ADDR ?= 0
########################################################################################################################
# High-level build parameters
@ -91,7 +91,7 @@ CFLAGS += -fno-common -ffunction-sections -fdata-sections
COMMON_CFLAGS += -O$(OPT) -std=gnu2x -g
COMMON_CFLAGS += $(DEVICE_DEFINES)
COMMON_CFLAGS += -DDEBUG=$(DEBUG)
COMMON_CFLAGS += -DDEBUG=$(DEBUG) -DDRIVER_ADDR=$(DRIVER_ADDR)
GEN_HEADERS := $(BUILDDIR)/generated/waveform_tables.h

View file

@ -36,9 +36,37 @@ static union tx_buf_union *tx_buf_write = &tx_buf[2];
static bool idle_buf_ready = false;
void update_tx_buf(void);
int hex_to_int(char *hex, size_t len);
char usartbuf[256];
int usartp = 0;
int hex_to_int(char *hex, size_t len) {
int rv = 0;
while (len--) {
rv = rv<<4;
char c = hex[len];
if ('0' <= c && c <= '9')
c -= '0';
else if ('a' <= c && c <= 'f')
c -= 'a' + 0xa;
else if ('A' <= c && c <= 'F')
c -= 'A' + 0xa;
else
c = 0;
rv |= c;
}
return rv;
}
enum leds {
LED_ON = 1,
LED_PING = 2,
LED_OVERHEAT = 4,
LED_CONTROL_ERR = 8,
LED_INPUT_ERR = 16,
LED_OUTPUT_ERR = 32
};
char rxbuf[256];
size_t rxp = 0;
int main(void) {
/* Configure clocks for 64 MHz system clock.
@ -160,7 +188,12 @@ int main(void) {
GPIOD->MODER = IN(0) | IN(1) | IN(2) | IN(3) | IN(4) | IN(5) | IN(6) |
IN(8) | IN(9);
delay_us(5*1000*1000);
for (int i=0; i<20; i++) {
set_status_leds(LED_ON);
delay_us(500*1000);
set_status_leds(0);
delay_us(500*1000);
}
TIM1->CCMR1 = (6<<TIM_CCMR1_OC2M_Pos) | TIM_CCMR1_OC2PE;
TIM1->CCMR2 = (6<<TIM_CCMR2_OC3M_Pos) | TIM_CCMR2_OC3PE;
@ -194,48 +227,60 @@ int main(void) {
USART1->BRR = 6667; /* Set baudrate to 9600 Bd */
USART1->CR1 |= USART_CR1_UE; /* And... go! */
int i = 0;
int j = 0;
int k = 0;
int n = 0;
int rj45_rx_ctr = 0;
int ping_ctr = 0;
int control_err_ctr = 0;
while (23) {
if (USART1->ISR & USART_ISR_RXNE_RXFNE) {
usartbuf[usartp] = USART1->RDR;
usartp ++;
if (usartp >= sizeof(usartbuf)) {
usartp = 0;
}
}
}
if (rj45_rx_ctr) {
set_rj45_leds(0x5);
rj45_rx_ctr--;
{
i++;
j++;
i %= 6;
j %= 4;
delay_us(100000);
set_rj45_leds(1 << j);
set_status_leds(0x01);
if (i == 0) {
k++;
if (k == 10) {
k = 0;
n++;
if (n == 16) {
n = 0;
} else {
set_rj45_leds(0x0);
}
int leds = LED_ON;
if (ping_ctr) {
leds |= LED_PING;
ping_ctr--;
}
if (control_err_ctr) {
leds |= LED_CONTROL_ERR;
control_err_ctr--;
}
set_status_leds(leds);
if (USART1->ISR & USART_ISR_RXNE_RXFNE) {
rj45_rx_ctr = 100000;
if (USART1->RDR == '\n') {
if (rxp != 4*16*3) {
rxp = 0;
control_err_ctr = 10000000;
continue;
}
for (size_t i=0; i<16; i++) {
int brightness = hex_to_int(&rxbuf[DRIVER_ADDR*16*3 + i*3], 1);
int channels = hex_to_int(&rxbuf[DRIVER_ADDR*16*3 + i*3 + 1], 2);
tx_buf_write->packet.channels[i] = channels;
if ((i&1) == 0) {
tx_buf_write->packet.brightness[i>>1] = brightness;
} else {
tx_buf_write->packet.brightness[i>>1] |= brightness<<4;
}
}
rxp = 0;
ping_ctr = 1000000;
update_tx_buf();
} else {
if (rxp <= sizeof(rxbuf)) {
rxbuf[rxp] = USART1->RDR;
rxp ++;
}
}
uint8_t b = (k < 8) ? (1<<k) : ~(1<<(k-8));
memset(tx_buf_write, 0, sizeof(*tx_buf_write));
for (size_t i=0; i<COUNT_OF(tx_buf_write->packet.channels); i++) {
tx_buf_write->packet.channels[i] = (k < 9) ? (1<<k) : 0xff;
}
for (size_t i=0; i<COUNT_OF(tx_buf_write->packet.brightness); i++) {
tx_buf_write->packet.brightness[i] = 0xff; //(n<<4) | n;
}
update_tx_buf();
}
}
}