From 3b1528e9511b791c53c26ce62a47f6d8f83fd0dd Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 22 Jun 2022 20:36:12 +0200 Subject: [PATCH] Fixed mask and made buttons functional again --- software/main/src/twai.cpp | 46 ++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/software/main/src/twai.cpp b/software/main/src/twai.cpp index e9bce90..06dfa65 100644 --- a/software/main/src/twai.cpp +++ b/software/main/src/twai.cpp @@ -1,9 +1,15 @@ +#include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/twai.h" - #include "esp_log.h" +#include "hal/twai_types.h" + #include "twai.h" +#include "avrcp.h" +#include "can_data.h" +#include "helper.h" #define TWAI_TAG "APP_TWAI" @@ -19,16 +25,48 @@ static void listen(void*) { ESP_LOGI(TWAI_TAG, "Message is in Extended Format"); } - ESP_LOGI(TWAI_TAG, "ID=%d, Length=%d", message.identifier, message.data_length_code); + static bool enabled = true; + switch (message.identifier) { + case BUTTONS_ID: + if (enabled) { + + can::Buttons buttons = can::convert(message.data, message.data_length_code); + + static MultiPurposeButton button_forward(avrcp::play_pause, avrcp::forward); + button_forward.update(buttons.forward); + + static MultiPurposeButton button_backward(nullptr, avrcp::backward); + button_backward.update(buttons.backward); + } + break; + + case VOLUME_ID: + if (enabled) { + can::Volume volume = can::convert(message.data, message.data_length_code); + avrcp::set_volume(ceil(volume.volume * 4.2f)); + } + break; + + case RADIO_ID: + { + can::Radio radio = can::convert(message.data, message.data_length_code); + enabled = (radio.source == can::Source::AUX2) && (radio.enabled); + } + break; + + + default: + break; + } } } void twai::init() { - twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_5, GPIO_NUM_19, TWAI_MODE_NORMAL); + twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_5, GPIO_NUM_19, TWAI_MODE_LISTEN_ONLY); twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS(); twai_filter_config_t f_config = { .acceptance_code = (0b100100101 << 5) + (0b1000011111 << 21), - .acceptance_mask = (0b011000000 << 5), + .acceptance_mask = (0b011000000 << 5) + 0b11111 + (0b11111 << 16), .single_filter = false };