From c7c2fc4f48727a9f54f3b0f3e5f55d2a58fac7cb Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 9 Feb 2023 01:04:43 +0100 Subject: [PATCH] Update to new WS2812 interface and fix a few indentations --- cape_rccar/main.c | 62 +++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/cape_rccar/main.c b/cape_rccar/main.c index 2cdb275..0da5094 100644 --- a/cape_rccar/main.c +++ b/cape_rccar/main.c @@ -137,6 +137,16 @@ uint8_t max_forward_speed = SPEED_CTRL_SERVO_MAX; /* Lights - WS2812B Leds strip */ const struct pio leds = LPC_GPIO_0_3; +#define NB_LEDS 6 + +uint8_t ws2812_leds_data[NB_LEDS * 3]; +struct ws2812_conf led_strip = { + .nb_leds = NB_LEDS, + .led_data = ws2812_leds_data, + .inverted = 0, +}; + + /* External movement detector */ const struct pio ext_move_pin = LPC_GPIO_0_4; @@ -225,11 +235,11 @@ int servo_config(uint8_t uart_num) timer_conf.match_values[2] = servo_med_pos_cmd; timer_conf.period = servo_command_period; - timer_on(LPC_TIMER_32B0, 0, NULL); + timer_on(LPC_TIMER_32B0, 0, NULL); timer_pwm_config(LPC_TIMER_32B0, &timer_conf); timer_start(LPC_TIMER_32B0); - timer_on(LPC_TIMER_32B1, 0, NULL); + timer_on(LPC_TIMER_32B1, 0, NULL); timer_pwm_config(LPC_TIMER_32B1, &timer_conf); timer_start(LPC_TIMER_32B1); @@ -385,7 +395,7 @@ int ultrasound_sensors_config(int uart_num) add_systick_callback(ultrasound_sensors_update, 40); timer_conf.match[0] = match; - timer_on(LPC_TIMER_16B0, 0, ultrasound_sensors_trig_timer_int); + timer_on(LPC_TIMER_16B0, 0, ultrasound_sensors_trig_timer_int); timer_counter_config(LPC_TIMER_16B0, &timer_conf); #if (DEBUG == 1) @@ -474,22 +484,22 @@ void handle_rf_rx_data(void) void send_on_rf(void) { - uint8_t cc_tx_data[RF_BUFF_LEN + 2]; - uint8_t tx_len = cc_ptr; - int ret = 0; - - /* Create a local copy */ - memcpy((char*)&(cc_tx_data[2]), (char*)cc_tx_buff, tx_len); - /* "Free" the rx buffer as soon as possible */ - cc_ptr = 0; - /* Prepare buffer for sending */ - cc_tx_data[0] = tx_len + 1; - cc_tx_data[1] = 0; /* Broadcast */ - /* Send */ - if (cc1101_tx_fifo_state() != 0) { - cc1101_flush_tx_fifo(); - } - ret = cc1101_send_packet(cc_tx_data, (tx_len + 2)); + uint8_t cc_tx_data[RF_BUFF_LEN + 2]; + uint8_t tx_len = cc_ptr; + int ret = 0; + + /* Create a local copy */ + memcpy((char*)&(cc_tx_data[2]), (char*)cc_tx_buff, tx_len); + /* "Free" the rx buffer as soon as possible */ + cc_ptr = 0; + /* Prepare buffer for sending */ + cc_tx_data[0] = tx_len + 1; + cc_tx_data[1] = 0; /* Broadcast */ + /* Send */ + if (cc1101_tx_fifo_state() != 0) { + cc1101_flush_tx_fifo(); + } + ret = cc1101_send_packet(cc_tx_data, (tx_len + 2)); #if (DEBUG == 1) /* Give some feedback on UART 0 */ @@ -560,14 +570,14 @@ void handle_commands(struct packet* command) break; case PKT_TYPE_SET_RGB_LED: for (i = 0; i < head->data.size; i += 4) { - ws2812_set_pixel(data[i], data[i + 1], data[i + 2], data[i + 3]); + ws2812_set_pixel(&led_strip, data[i], data[i + 1], data[i + 2], data[i + 3]); uprintf(UART_DEBUG, "Set led %d to {%d, %d, %d}.\n", data[i], data[i + 1], data[i + 2], data[i + 3]); } uprintf(UART_DEBUG, "Set %d leds.\n", (i / 4)); break; case PKT_TYPE_CLEAR_LEDS: uprintf(UART_DEBUG, "All leds cleared.\n"); - ws2812_clear(); + ws2812_clear(&led_strip); break; case PKT_TYPE_SET_DIRECTION: if (data[0] > DIRECTION_CTRL_SERVO_MAX) { @@ -648,7 +658,7 @@ int main(void) servo_config(UART_DEBUG); /* Config Leds for lights */ - ws2812_config(&leds); + ws2812_config(&led_strip, &leds); while (1) { struct packet* pkt = NULL; @@ -698,7 +708,7 @@ int main(void) } /* Send leds frame if leds changed */ - ws2812_send_frame(0); + ws2812_send_frame(&led_strip, 0); /* RF */ if (cc_tx == 1) { @@ -706,9 +716,9 @@ int main(void) cc_tx = 0; } /* Do not leave radio in an unknown or unwated state */ - do { - status = (cc1101_read_status() & CC1101_STATE_MASK); - } while (status == CC1101_STATE_TX); + do { + status = (cc1101_read_status() & CC1101_STATE_MASK); + } while (status == CC1101_STATE_TX); if (status != CC1101_STATE_RX) { if (cc1101_rx_fifo_state() != 0) { -- 2.43.0