From 84c3cf1fda38abf0abee120b8316b3a340706a6a Mon Sep 17 00:00:00 2001 From: Serge Bazanski <q3k@q3k.org> Date: Thu, 20 Jul 2023 21:51:09 +0200 Subject: [PATCH] *: reformat with clang-format --- components/badge23/captouch.c | 563 +++--- components/badge23/include/badge23/captouch.h | 67 +- components/bl00mbox/bl00mbox.c | 106 +- .../bl00mbox/buds/tinysynth/tinysynth.c | 143 +- .../bl00mbox/buds/tinysynth/tinysynth.h | 77 +- components/bl00mbox/include/bl00mbox.h | 6 +- components/bl00mbox/include/bud_registry.h | 4 +- components/flow3r_bsp/flow3r_bsp.h | 71 +- components/flow3r_bsp/flow3r_bsp_audio.c | 18 +- components/flow3r_bsp/flow3r_bsp_display.c | 72 +- components/flow3r_bsp/flow3r_bsp_gc9a01.c | 925 +++++----- components/flow3r_bsp/flow3r_bsp_gc9a01.h | 61 +- components/flow3r_bsp/flow3r_bsp_i2c.c | 116 +- components/flow3r_bsp/flow3r_bsp_i2c.h | 27 +- components/flow3r_bsp/flow3r_bsp_leds.c | 9 +- components/flow3r_bsp/flow3r_bsp_max7321.c | 55 +- components/flow3r_bsp/flow3r_bsp_max7321.h | 17 +- components/flow3r_bsp/flow3r_bsp_max98091.c | 215 ++- components/flow3r_bsp/flow3r_bsp_max98091.h | 3 +- .../flow3r_bsp/flow3r_bsp_max98091_hwregs.h | 4 +- components/flow3r_bsp/flow3r_bsp_rmtled.c | 38 +- components/flow3r_bsp/flow3r_bsp_rmtled.h | 3 +- components/flow3r_bsp/flow3r_bsp_spio.c | 411 +++-- .../micropython/include/mpconfigboard.h | 1 - components/st3m/st3m_audio.c | 295 +-- components/st3m/st3m_audio.h | 83 +- components/st3m/st3m_board_startup.c | 25 +- components/st3m/st3m_colors.c | 21 +- components/st3m/st3m_colors.h | 10 +- components/st3m/st3m_console.c | 610 +++---- components/st3m/st3m_counter.c | 131 +- components/st3m/st3m_counter.h | 16 +- components/st3m/st3m_fs.c | 14 +- components/st3m/st3m_gfx.c | 105 +- components/st3m/st3m_gfx.h | 22 +- components/st3m/st3m_io.c | 90 +- components/st3m/st3m_io.h | 66 +- components/st3m/st3m_leds.c | 88 +- components/st3m/st3m_leds.h | 15 +- components/st3m/st3m_mode.c | 43 +- components/st3m/st3m_mode.h | 18 +- components/st3m/st3m_scope.c | 40 +- components/st3m/st3m_scope.h | 25 +- components/st3m/st3m_usb.c | 173 +- components/st3m/st3m_usb.h | 90 +- components/st3m/st3m_usb_cdc.c | 12 +- components/st3m/st3m_usb_descriptors.c | 225 +-- components/st3m/st3m_usb_msc.c | 129 +- components/st3m/tusb_config.h | 10 +- usermodule/mp_audio.c | 338 ++-- usermodule/mp_badge_link.c | 164 +- usermodule/mp_bl00mbox.c | 58 +- usermodule/mp_captouch.c | 232 +-- usermodule/mp_hardware.c | 170 +- usermodule/mp_kernel.c | 214 ++- usermodule/mp_leds.c | 100 +- usermodule/mp_uctx.c | 1579 ++++++++--------- 57 files changed, 4283 insertions(+), 3940 deletions(-) diff --git a/components/badge23/captouch.c b/components/badge23/captouch.c index d90536ac13..b82b0ab5cc 100644 --- a/components/badge23/captouch.c +++ b/components/badge23/captouch.c @@ -1,8 +1,8 @@ #include "include/badge23/captouch.h" -#include "esp_log.h" -#include <stdint.h> #include <freertos/FreeRTOS.h> #include <freertos/atomic.h> +#include <stdint.h> +#include "esp_log.h" #include "flow3r_bsp_i2c.h" #define PETAL_PAD_TIP 0 @@ -10,10 +10,10 @@ #define PETAL_PAD_CW 2 #define PETAL_PAD_BASE 3 -#define CIN CDC_NONE 0 -#define CIN_CDC_NEG 1 -#define CIN_CDC_POS 2 -#define CIN_BIAS 3 +#define CIN CDC_NONE 0 +#define CIN_CDC_NEG 1 +#define CIN_CDC_POS 2 +#define CIN_BIAS 3 #define AFE_INCR_CAP 1000 @@ -21,30 +21,36 @@ static const uint8_t top_map[] = {0, 0, 0, 2, 2, 2, 6, 6, 6, 4, 4, 4}; static const uint8_t top_stages = 12; static const uint8_t bot_map[] = {1, 1, 3, 3, 5, 7, 7, 9, 9, 8, 8, 8}; static const uint8_t bot_stages = 12; -static const uint8_t bot_stage_config[] = {0,1,2,3,5,6,7,8,9,10,11,12}; +static const uint8_t bot_stage_config[] = {0, 1, 2, 3, 5, 6, + 7, 8, 9, 10, 11, 12}; #define DEFAULT_THRES_TOP 8000 #define DEFAULT_THRES_BOT 12000 #if defined(CONFIG_FLOW3R_HW_GEN_P4) -static const uint8_t top_segment_map[] = {1,3,2,2,3,1,1,3,2,1,3,2}; //PETAL_PAD_* -static const uint8_t bot_segment_map[] = {3,0,3,0,0,0,3,0,3,1,2,3}; //PETAL_PAD_* +static const uint8_t top_segment_map[] = {1, 3, 2, 2, 3, 1, + 1, 3, 2, 1, 3, 2}; // PETAL_PAD_* +static const uint8_t bot_segment_map[] = {3, 0, 3, 0, 0, 0, + 3, 0, 3, 1, 2, 3}; // PETAL_PAD_* #elif defined(CONFIG_FLOW3R_HW_GEN_P6) -static const uint8_t top_segment_map[] = {1,3,2,2,3,1,1,3,2,1,3,2}; //PETAL_PAD_* -static const uint8_t bot_segment_map[] = {3,0,3,0,0,0,3,0,3,1,2,3}; //PETAL_PAD_* +static const uint8_t top_segment_map[] = {1, 3, 2, 2, 3, 1, + 1, 3, 2, 1, 3, 2}; // PETAL_PAD_* +static const uint8_t bot_segment_map[] = {3, 0, 3, 0, 0, 0, + 3, 0, 3, 1, 2, 3}; // PETAL_PAD_* #elif defined(CONFIG_FLOW3R_HW_GEN_P3) -static const uint8_t top_segment_map[] = {0,1,2, 2,1,0, 0,1,2, 2,1,0}; //PETAL_PAD_* -static const uint8_t bot_segment_map[] = {3,0,3,0,0,0,3,0,3, 0,2,1}; //PETAL_PAD_* +static const uint8_t top_segment_map[] = {0, 1, 2, 2, 1, 0, + 0, 1, 2, 2, 1, 0}; // PETAL_PAD_* +static const uint8_t bot_segment_map[] = {3, 0, 3, 0, 0, 0, + 3, 0, 3, 0, 2, 1}; // PETAL_PAD_* #endif static const char *TAG = "captouch"; +#define AD7147_REG_PWR_CONTROL 0x00 +#define AD7147_REG_STAGE_CAL_EN 0x01 +#define AD7147_REG_STAGE_HIGH_INT_ENABLE 0x06 +#define AD7147_REG_DEVICE_ID 0x17 -#define AD7147_REG_PWR_CONTROL 0x00 -#define AD7147_REG_STAGE_CAL_EN 0x01 -#define AD7147_REG_STAGE_HIGH_INT_ENABLE 0x06 -#define AD7147_REG_DEVICE_ID 0x17 - -#define TIMEOUT_MS 1000 +#define TIMEOUT_MS 1000 #define PETAL_PRESSED_DEBOUNCE 2 @@ -58,9 +64,9 @@ typedef struct { uint8_t pressed; } petal_pad_t; -typedef struct{ +typedef struct { uint8_t config_mask; - petal_pad_t pads[4]; // ordered according to PETAL_PAD_* + petal_pad_t pads[4]; // ordered according to PETAL_PAD_* uint8_t pressed; } petal_t; @@ -85,23 +91,27 @@ static struct ad714x_chip chip_top_rev5 = { static struct ad714x_chip chip_bot_rev5 = { .addr = &flow3r_i2c_addresses.touch_bottom, .gpio = 15, - .pos_afe_offsets = {3, 2, 1, 1 ,1, 1, 1, 1, 2, 3, 3, 3}, + .pos_afe_offsets = {3, 2, 1, 1, 1, 1, 1, 1, 2, 3, 3, 3}, .stages = bot_stages, }; -static esp_err_t ad714x_i2c_write(const struct ad714x_chip *chip, const uint16_t reg, const uint16_t data) -{ +static esp_err_t ad714x_i2c_write(const struct ad714x_chip *chip, + const uint16_t reg, const uint16_t data) { const uint8_t tx[] = {reg >> 8, reg & 0xFF, data >> 8, data & 0xFF}; ESP_LOGD(TAG, "AD7147 write reg %X-> %X", reg, data); - return flow3r_bsp_i2c_write_to_device(*chip->addr, tx, sizeof(tx), TIMEOUT_MS / portTICK_PERIOD_MS); + return flow3r_bsp_i2c_write_to_device(*chip->addr, tx, sizeof(tx), + TIMEOUT_MS / portTICK_PERIOD_MS); } -static esp_err_t ad714x_i2c_read(const struct ad714x_chip *chip, const uint16_t reg, uint16_t *data, const size_t len) -{ +static esp_err_t ad714x_i2c_read(const struct ad714x_chip *chip, + const uint16_t reg, uint16_t *data, + const size_t len) { const uint8_t tx[] = {reg >> 8, reg & 0xFF}; uint8_t rx[len * 2]; - esp_err_t ret = flow3r_bsp_i2c_write_read_device(*chip->addr, tx, sizeof(tx), rx, sizeof(rx), TIMEOUT_MS / portTICK_PERIOD_MS); - for(int i = 0; i < len; i++) { + esp_err_t ret = flow3r_bsp_i2c_write_read_device( + *chip->addr, tx, sizeof(tx), rx, sizeof(rx), + TIMEOUT_MS / portTICK_PERIOD_MS); + for (int i = 0; i < len; i++) { data[i] = (rx[i * 2] << 8) | rx[i * 2 + 1]; } return ret; @@ -109,31 +119,55 @@ static esp_err_t ad714x_i2c_read(const struct ad714x_chip *chip, const uint16_t struct ad7147_stage_config { unsigned int cinX_connection_setup[13]; - unsigned int se_connection_setup:2; - unsigned int neg_afe_offset_disable:1; - unsigned int pos_afe_offset_disable:1; - unsigned int neg_afe_offset:6; - unsigned int neg_afe_offset_swap:1; - unsigned int pos_afe_offset:6; - unsigned int pos_afe_offset_swap:1; - unsigned int neg_threshold_sensitivity:4; - unsigned int neg_peak_detect:3; - unsigned int pos_threshold_sensitivity:4; - unsigned int pos_peak_detect:3; + unsigned int se_connection_setup : 2; + unsigned int neg_afe_offset_disable : 1; + unsigned int pos_afe_offset_disable : 1; + unsigned int neg_afe_offset : 6; + unsigned int neg_afe_offset_swap : 1; + unsigned int pos_afe_offset : 6; + unsigned int pos_afe_offset_swap : 1; + unsigned int neg_threshold_sensitivity : 4; + unsigned int neg_peak_detect : 3; + unsigned int pos_threshold_sensitivity : 4; + unsigned int pos_peak_detect : 3; }; - static const uint16_t bank2 = 0x80; -static void ad714x_set_stage_config(const struct ad714x_chip *chip, const uint8_t stage, const struct ad7147_stage_config * config) -{ - const uint16_t connection_6_0 = (config->cinX_connection_setup[6] << 12) | (config->cinX_connection_setup[5] << 10) | (config->cinX_connection_setup[4] << 8) | (config->cinX_connection_setup[3] << 6) | (config->cinX_connection_setup[2] << 4) | (config->cinX_connection_setup[1] << 2) | (config->cinX_connection_setup[0] << 0); - const uint16_t connection_12_7 = (config->pos_afe_offset_disable << 15) | (config->neg_afe_offset_disable << 14) | (config->se_connection_setup << 12) | (config->cinX_connection_setup[12] << 10) | (config->cinX_connection_setup[11] << 8) | (config->cinX_connection_setup[10] << 6) | (config->cinX_connection_setup[9] << 4) | (config->cinX_connection_setup[8] << 2) | (config->cinX_connection_setup[7] << 0); - const uint16_t afe_offset = (config->pos_afe_offset_swap << 15) | (config->pos_afe_offset << 8) | (config->neg_afe_offset_swap << 7) | (config->neg_afe_offset << 0); - const uint16_t sensitivity = (config->pos_peak_detect << 12) | (config->pos_threshold_sensitivity << 8) | (config->neg_peak_detect << 4) | (config->neg_threshold_sensitivity << 0); - - //ESP_LOGI(TAG, "Stage %d config-> %X %X %X %X", stage, connection_6_0, connection_12_7, afe_offset, sensitivity); - //ESP_LOGI(TAG, "Config: %X %X %X %X %X %X %X %X %X", config->pos_afe_offset_disable, config->pos_afe_offset_disable, config->se_connection_setup, config->cinX_connection_setup[12], config->cinX_connection_setup[11], config->cinX_connection_setup[10], config->cinX_connection_setup[9], config->cinX_connection_setup[8], config->cinX_connection_setup[7]); +static void ad714x_set_stage_config(const struct ad714x_chip *chip, + const uint8_t stage, + const struct ad7147_stage_config *config) { + const uint16_t connection_6_0 = (config->cinX_connection_setup[6] << 12) | + (config->cinX_connection_setup[5] << 10) | + (config->cinX_connection_setup[4] << 8) | + (config->cinX_connection_setup[3] << 6) | + (config->cinX_connection_setup[2] << 4) | + (config->cinX_connection_setup[1] << 2) | + (config->cinX_connection_setup[0] << 0); + const uint16_t connection_12_7 = (config->pos_afe_offset_disable << 15) | + (config->neg_afe_offset_disable << 14) | + (config->se_connection_setup << 12) | + (config->cinX_connection_setup[12] << 10) | + (config->cinX_connection_setup[11] << 8) | + (config->cinX_connection_setup[10] << 6) | + (config->cinX_connection_setup[9] << 4) | + (config->cinX_connection_setup[8] << 2) | + (config->cinX_connection_setup[7] << 0); + const uint16_t afe_offset = + (config->pos_afe_offset_swap << 15) | (config->pos_afe_offset << 8) | + (config->neg_afe_offset_swap << 7) | (config->neg_afe_offset << 0); + const uint16_t sensitivity = (config->pos_peak_detect << 12) | + (config->pos_threshold_sensitivity << 8) | + (config->neg_peak_detect << 4) | + (config->neg_threshold_sensitivity << 0); + + // ESP_LOGI(TAG, "Stage %d config-> %X %X %X %X", stage, connection_6_0, + // connection_12_7, afe_offset, sensitivity); ESP_LOGI(TAG, "Config: %X %X + // %X %X %X %X %X %X %X", config->pos_afe_offset_disable, + // config->pos_afe_offset_disable, config->se_connection_setup, + // config->cinX_connection_setup[12], config->cinX_connection_setup[11], + // config->cinX_connection_setup[10], config->cinX_connection_setup[9], + // config->cinX_connection_setup[8], config->cinX_connection_setup[7]); ad714x_i2c_write(chip, bank2 + stage * 8, connection_6_0); ad714x_i2c_write(chip, bank2 + stage * 8 + 1, connection_12_7); @@ -142,89 +176,115 @@ static void ad714x_set_stage_config(const struct ad714x_chip *chip, const uint8_ } struct ad7147_device_config { - unsigned int power_mode:2; - unsigned int lp_conv_delay:2; - unsigned int sequence_stage_num:4; - unsigned int decimation:2; - unsigned int sw_reset:1; - unsigned int int_pol:1; - unsigned int ext_source:1; - unsigned int cdc_bias:2; - - unsigned int stage0_cal_en:1; - unsigned int stage1_cal_en:1; - unsigned int stage2_cal_en:1; - unsigned int stage3_cal_en:1; - unsigned int stage4_cal_en:1; - unsigned int stage5_cal_en:1; - unsigned int stage6_cal_en:1; - unsigned int stage7_cal_en:1; - unsigned int stage8_cal_en:1; - unsigned int stage9_cal_en:1; - unsigned int stage10_cal_en:1; - unsigned int stage11_cal_en:1; - unsigned int avg_fp_skip:2; - unsigned int avg_lp_skip:2; - - unsigned int stage0_high_int_enable:1; - unsigned int stage1_high_int_enable:1; - unsigned int stage2_high_int_enable:1; - unsigned int stage3_high_int_enable:1; - unsigned int stage4_high_int_enable:1; - unsigned int stage5_high_int_enable:1; - unsigned int stage6_high_int_enable:1; - unsigned int stage7_high_int_enable:1; - unsigned int stage8_high_int_enable:1; - unsigned int stage9_high_int_enable:1; - unsigned int stage10_high_int_enable:1; - unsigned int stage11_high_int_enable:1; + unsigned int power_mode : 2; + unsigned int lp_conv_delay : 2; + unsigned int sequence_stage_num : 4; + unsigned int decimation : 2; + unsigned int sw_reset : 1; + unsigned int int_pol : 1; + unsigned int ext_source : 1; + unsigned int cdc_bias : 2; + + unsigned int stage0_cal_en : 1; + unsigned int stage1_cal_en : 1; + unsigned int stage2_cal_en : 1; + unsigned int stage3_cal_en : 1; + unsigned int stage4_cal_en : 1; + unsigned int stage5_cal_en : 1; + unsigned int stage6_cal_en : 1; + unsigned int stage7_cal_en : 1; + unsigned int stage8_cal_en : 1; + unsigned int stage9_cal_en : 1; + unsigned int stage10_cal_en : 1; + unsigned int stage11_cal_en : 1; + unsigned int avg_fp_skip : 2; + unsigned int avg_lp_skip : 2; + + unsigned int stage0_high_int_enable : 1; + unsigned int stage1_high_int_enable : 1; + unsigned int stage2_high_int_enable : 1; + unsigned int stage3_high_int_enable : 1; + unsigned int stage4_high_int_enable : 1; + unsigned int stage5_high_int_enable : 1; + unsigned int stage6_high_int_enable : 1; + unsigned int stage7_high_int_enable : 1; + unsigned int stage8_high_int_enable : 1; + unsigned int stage9_high_int_enable : 1; + unsigned int stage10_high_int_enable : 1; + unsigned int stage11_high_int_enable : 1; }; - -static void ad714x_set_device_config(const struct ad714x_chip *chip, const struct ad7147_device_config * config) -{ - const uint16_t pwr_control = (config->cdc_bias << 14) | (config->ext_source << 12) | (config->int_pol << 11) | (config->sw_reset << 10) | (config->decimation << 8) | (config->sequence_stage_num << 4) | (config->lp_conv_delay << 2) | (config->power_mode << 0); - const uint16_t stage_cal_en = (config->avg_lp_skip << 14) | (config->avg_fp_skip << 12) | (config->stage11_cal_en << 11) | (config->stage10_cal_en << 10) | (config->stage9_cal_en << 9) | (config->stage8_cal_en << 8) | (config->stage7_cal_en << 7) | (config->stage6_cal_en << 6) | (config->stage5_cal_en << 5) | (config->stage4_cal_en << 4) | (config->stage3_cal_en << 3) | (config->stage2_cal_en << 2) | (config->stage1_cal_en << 1) | (config->stage0_cal_en << 0); - const uint16_t stage_high_int_enable = (config->stage11_high_int_enable << 11) | (config->stage10_high_int_enable << 10) | (config->stage9_high_int_enable << 9) | (config->stage8_high_int_enable << 8) | (config->stage7_high_int_enable << 7) | (config->stage6_high_int_enable << 6) | (config->stage5_high_int_enable << 5) | (config->stage4_high_int_enable << 4) | (config->stage3_high_int_enable << 3) | (config->stage2_high_int_enable << 2) | (config->stage1_high_int_enable << 1) | (config->stage0_high_int_enable << 0); +static void ad714x_set_device_config( + const struct ad714x_chip *chip, const struct ad7147_device_config *config) { + const uint16_t pwr_control = + (config->cdc_bias << 14) | (config->ext_source << 12) | + (config->int_pol << 11) | (config->sw_reset << 10) | + (config->decimation << 8) | (config->sequence_stage_num << 4) | + (config->lp_conv_delay << 2) | (config->power_mode << 0); + const uint16_t stage_cal_en = + (config->avg_lp_skip << 14) | (config->avg_fp_skip << 12) | + (config->stage11_cal_en << 11) | (config->stage10_cal_en << 10) | + (config->stage9_cal_en << 9) | (config->stage8_cal_en << 8) | + (config->stage7_cal_en << 7) | (config->stage6_cal_en << 6) | + (config->stage5_cal_en << 5) | (config->stage4_cal_en << 4) | + (config->stage3_cal_en << 3) | (config->stage2_cal_en << 2) | + (config->stage1_cal_en << 1) | (config->stage0_cal_en << 0); + const uint16_t stage_high_int_enable = + (config->stage11_high_int_enable << 11) | + (config->stage10_high_int_enable << 10) | + (config->stage9_high_int_enable << 9) | + (config->stage8_high_int_enable << 8) | + (config->stage7_high_int_enable << 7) | + (config->stage6_high_int_enable << 6) | + (config->stage5_high_int_enable << 5) | + (config->stage4_high_int_enable << 4) | + (config->stage3_high_int_enable << 3) | + (config->stage2_high_int_enable << 2) | + (config->stage1_high_int_enable << 1) | + (config->stage0_high_int_enable << 0); ad714x_i2c_write(chip, AD7147_REG_PWR_CONTROL, pwr_control); ad714x_i2c_write(chip, AD7147_REG_STAGE_CAL_EN, stage_cal_en); - ad714x_i2c_write(chip, AD7147_REG_STAGE_HIGH_INT_ENABLE, stage_high_int_enable); + ad714x_i2c_write(chip, AD7147_REG_STAGE_HIGH_INT_ENABLE, + stage_high_int_enable); } -static struct ad7147_stage_config ad714x_default_config(void) -{ - return (struct ad7147_stage_config) { - .cinX_connection_setup={CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS}, - .se_connection_setup=0b01, - .pos_afe_offset=0, - }; +static struct ad7147_stage_config ad714x_default_config(void) { + return (struct ad7147_stage_config){ + .cinX_connection_setup = {CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, + CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS, + CIN_BIAS, CIN_BIAS, CIN_BIAS, CIN_BIAS}, + .se_connection_setup = 0b01, + .pos_afe_offset = 0, + }; } -static void captouch_configure_stage(struct ad714x_chip * chip, uint8_t stage){ +static void captouch_configure_stage(struct ad714x_chip *chip, uint8_t stage) { struct ad7147_stage_config stage_config; stage_config = ad714x_default_config(); - if(chip == chip_bot){ - stage_config.cinX_connection_setup[bot_stage_config[stage]] = CIN_CDC_POS; + if (chip == chip_bot) { + stage_config.cinX_connection_setup[bot_stage_config[stage]] = + CIN_CDC_POS; } else { stage_config.cinX_connection_setup[stage] = CIN_CDC_POS; } - stage_config.pos_afe_offset=chip->pos_afe_offsets[stage]; + stage_config.pos_afe_offset = chip->pos_afe_offsets[stage]; ad714x_set_stage_config(chip, stage, &stage_config); } -static int8_t captouch_configure_stage_afe_offset(uint8_t top, uint8_t stage, int8_t delta_afe){ +static int8_t captouch_configure_stage_afe_offset(uint8_t top, uint8_t stage, + int8_t delta_afe) { int8_t sat = 0; - struct ad714x_chip * chip = chip_bot; - if(top) chip = chip_top; + struct ad714x_chip *chip = chip_bot; + if (top) chip = chip_top; int8_t afe = chip->pos_afe_offsets[stage] - chip->neg_afe_offsets[stage]; - if((afe >= 63) && (delta_afe > 0)) sat = 1; - if((afe <= 63) && (delta_afe < 0)) sat = -1; + if ((afe >= 63) && (delta_afe > 0)) sat = 1; + if ((afe <= 63) && (delta_afe < 0)) sat = -1; afe += delta_afe; - if(afe >= 63) afe = 63; - if(afe <= -63)afe = -63; + if (afe >= 63) afe = 63; + if (afe <= -63) afe = -63; - if(afe>0){ + if (afe > 0) { chip->pos_afe_offsets[stage] = afe; chip->neg_afe_offsets[stage] = 0; } else { @@ -235,25 +295,25 @@ static int8_t captouch_configure_stage_afe_offset(uint8_t top, uint8_t stage, in return sat; } -static void captouch_init_chip(struct ad714x_chip* chip, const struct ad7147_device_config device_config) -{ +static void captouch_init_chip( + struct ad714x_chip *chip, const struct ad7147_device_config device_config) { uint16_t data; ad714x_i2c_read(chip, AD7147_REG_DEVICE_ID, &data, 1); ESP_LOGI(TAG, "DEVICE ID = %X", data); ad714x_set_device_config(chip, &device_config); - for(int i=0; i<chip->stages; i++) { + for (int i = 0; i < chip->stages; i++) { captouch_configure_stage(chip, i); } } -static void captouch_init_petals(){ - for(int i = 0; i < 10; i++){ - for(int j = 0; j < 4; j++){ +static void captouch_init_petals() { + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 4; j++) { petals[i].pads[j].amb = 0; petals[i].pads[j].cdc = 0; - if(i%2){ + if (i % 2) { petals[i].pads[j].thres = DEFAULT_THRES_BOT; } else { petals[i].pads[j].thres = DEFAULT_THRES_TOP; @@ -261,43 +321,43 @@ static void captouch_init_petals(){ } petals[i].config_mask = 0; } - for(int i = 0; i < bot_stages; i++){ - petals[bot_map[i]].config_mask |= 1 << bot_segment_map[i]; + for (int i = 0; i < bot_stages; i++) { + petals[bot_map[i]].config_mask |= 1 << bot_segment_map[i]; } - for(int i = 0; i < top_stages; i++){ - petals[top_map[i]].config_mask |= 1 << top_segment_map[i]; + for (int i = 0; i < top_stages; i++) { + petals[top_map[i]].config_mask |= 1 << top_segment_map[i]; } } -int32_t captouch_get_petal_rad(uint8_t petal){ - if(petal > 9) petal = 9; +int32_t captouch_get_petal_rad(uint8_t petal) { + if (petal > 9) petal = 9; uint8_t cf = petals[petal].config_mask; - if(cf == 0b1110){ //CCW, CW, BASE + if (cf == 0b1110) { // CCW, CW, BASE int32_t left = petals[petal].pads[PETAL_PAD_CCW].cdc; left -= petals[petal].pads[PETAL_PAD_CCW].amb; int32_t right = petals[petal].pads[PETAL_PAD_CW].cdc; right -= petals[petal].pads[PETAL_PAD_CW].amb; int32_t base = petals[petal].pads[PETAL_PAD_BASE].cdc; base -= petals[petal].pads[PETAL_PAD_BASE].amb; - return (left + right)/2 - base; + return (left + right) / 2 - base; } - if(cf == 0b111){ //CCW, CW, TIP + if (cf == 0b111) { // CCW, CW, TIP int32_t left = petals[petal].pads[PETAL_PAD_CCW].cdc; left -= petals[petal].pads[PETAL_PAD_CCW].amb; int32_t right = petals[petal].pads[PETAL_PAD_CW].cdc; right -= petals[petal].pads[PETAL_PAD_CW].amb; int32_t tip = petals[petal].pads[PETAL_PAD_TIP].cdc; tip -= petals[petal].pads[PETAL_PAD_TIP].amb; - return (-left - right)/2 + tip; + return (-left - right) / 2 + tip; } - if(cf == 0b1001){ //TIP, BASE + if (cf == 0b1001) { // TIP, BASE int32_t tip = petals[petal].pads[PETAL_PAD_TIP].cdc; tip -= petals[petal].pads[PETAL_PAD_TIP].amb; int32_t base = petals[petal].pads[PETAL_PAD_BASE].cdc; base -= petals[petal].pads[PETAL_PAD_BASE].amb; return tip - base; } - if(cf == 0b1){ //TIP + if (cf == 0b1) { // TIP int32_t tip = petals[petal].pads[PETAL_PAD_TIP].cdc; tip -= petals[petal].pads[PETAL_PAD_TIP].amb; return tip; @@ -305,10 +365,10 @@ int32_t captouch_get_petal_rad(uint8_t petal){ return 0; } -int32_t captouch_get_petal_phi(uint8_t petal){ - if(petal > 9) petal = 9; +int32_t captouch_get_petal_phi(uint8_t petal) { + if (petal > 9) petal = 9; uint8_t cf = petals[petal].config_mask; - if((cf == 0b1110) || (cf == 0b110) || (cf == 0b111)){ //CCW, CW, (BASE) + if ((cf == 0b1110) || (cf == 0b110) || (cf == 0b111)) { // CCW, CW, (BASE) int32_t left = petals[petal].pads[PETAL_PAD_CCW].cdc; left -= petals[petal].pads[PETAL_PAD_CCW].amb; int32_t right = petals[petal].pads[PETAL_PAD_CW].cdc; @@ -318,26 +378,27 @@ int32_t captouch_get_petal_phi(uint8_t petal){ return 0; } -void captouch_init(void) -{ +void captouch_init(void) { captouch_init_petals(); chip_top = &chip_top_rev5; chip_bot = &chip_bot_rev5; - captouch_init_chip(chip_top, (struct ad7147_device_config){.sequence_stage_num = 11, - .decimation = 1, - }); + captouch_init_chip(chip_top, (struct ad7147_device_config){ + .sequence_stage_num = 11, + .decimation = 1, + }); - captouch_init_chip(chip_bot, (struct ad7147_device_config){.sequence_stage_num = 11, - .decimation = 1, - }); + captouch_init_chip(chip_bot, (struct ad7147_device_config){ + .sequence_stage_num = 11, + .decimation = 1, + }); } -uint16_t read_captouch(){ +uint16_t read_captouch() { uint16_t bin_petals = 0; - for(int i = 0; i < 10; i++) { - if(petals[i].pressed){ - bin_petals |= (1<<i); + for (int i = 0; i < 10; i++) { + if (petals[i].pressed) { + bin_petals |= (1 << i); } } return bin_petals; @@ -346,38 +407,45 @@ uint16_t read_captouch(){ void read_captouch_ex(captouch_state_t *state) { for (int i = 0; i < 10; i++) { state->petals[i].pressed = petals[i].pressed > 0; - state->petals[i].pads.base_pressed = petals[i].pads[PETAL_PAD_BASE].pressed > 0; - state->petals[i].pads.tip_pressed = petals[i].pads[PETAL_PAD_TIP].pressed > 0; - state->petals[i].pads.cw_pressed = petals[i].pads[PETAL_PAD_CW].pressed > 0; - state->petals[i].pads.ccw_pressed = petals[i].pads[PETAL_PAD_CCW].pressed > 0; + state->petals[i].pads.base_pressed = + petals[i].pads[PETAL_PAD_BASE].pressed > 0; + state->petals[i].pads.tip_pressed = + petals[i].pads[PETAL_PAD_TIP].pressed > 0; + state->petals[i].pads.cw_pressed = + petals[i].pads[PETAL_PAD_CW].pressed > 0; + state->petals[i].pads.ccw_pressed = + petals[i].pads[PETAL_PAD_CCW].pressed > 0; } } -uint16_t cdc_data[2][12] = {0,}; -uint16_t cdc_ambient[2][12] = {0,}; +uint16_t cdc_data[2][12] = { + 0, +}; +uint16_t cdc_ambient[2][12] = { + 0, +}; static volatile uint32_t calib_active = 0; static uint8_t calib_cycles = 0; -void captouch_force_calibration(){ - if(!calib_cycles){ //last calib has finished - calib_cycles = 16; //goal cycles, can be argument someday +void captouch_force_calibration() { + if (!calib_cycles) { // last calib has finished + calib_cycles = 16; // goal cycles, can be argument someday Atomic_Increment_u32(&calib_active); } } -uint8_t captouch_calibration_active(){ - return Atomic_CompareAndSwap_u32(&calib_active, 0, 0) == ATOMIC_COMPARE_AND_SWAP_FAILURE; +uint8_t captouch_calibration_active() { + return Atomic_CompareAndSwap_u32(&calib_active, 0, 0) == + ATOMIC_COMPARE_AND_SWAP_FAILURE; } - -void check_petals_pressed(){ - for(int i = 0; i < 10; i++){ +void check_petals_pressed() { + for (int i = 0; i < 10; i++) { bool petal_pressed = false; - for(int j = 0; j < 4; j++){ + for (int j = 0; j < 4; j++) { bool pad_pressed = false; - if((petals[i].pads[j].amb + - petals[i].pads[j].thres) < + if ((petals[i].pads[j].amb + petals[i].pads[j].thres) < petals[i].pads[j].cdc) { petal_pressed = true; pad_pressed = true; @@ -389,16 +457,17 @@ void check_petals_pressed(){ petals[i].pads[j].pressed--; } } - if(petal_pressed){ + if (petal_pressed) { petals[i].pressed = PETAL_PRESSED_DEBOUNCE; - } else if(petals[i].pressed){ + } else if (petals[i].pressed) { petals[i].pressed--; } } } -void cdc_to_petal(bool bot, bool amb, uint16_t cdc_data[], uint8_t cdc_data_length){ - for(int i = 0; i < cdc_data_length; i++) { +void cdc_to_petal(bool bot, bool amb, uint16_t cdc_data[], + uint8_t cdc_data_length) { + for (int i = 0; i < cdc_data_length; i++) { size_t petal_index = bot ? bot_map[i] : top_map[i]; size_t pad_index = bot ? bot_segment_map[i] : top_segment_map[i]; petal_pad_t *pad = &petals[petal_index].pads[pad_index]; @@ -407,105 +476,111 @@ void cdc_to_petal(bool bot, bool amb, uint16_t cdc_data[], uint8_t cdc_data_leng } } -uint16_t captouch_get_petal_pad_raw(uint8_t petal, uint8_t pad){ - if(petal > 9) petal = 9; - if(pad > 3) pad = 3; +uint16_t captouch_get_petal_pad_raw(uint8_t petal, uint8_t pad) { + if (petal > 9) petal = 9; + if (pad > 3) pad = 3; return petals[petal].pads[pad].cdc; } -uint16_t captouch_get_petal_pad_calib_ref(uint8_t petal, uint8_t pad){ - if(petal > 9) petal = 9; - if(pad > 3) pad = 3; +uint16_t captouch_get_petal_pad_calib_ref(uint8_t petal, uint8_t pad) { + if (petal > 9) petal = 9; + if (pad > 3) pad = 3; return petals[petal].pads[pad].amb; } -uint16_t captouch_get_petal_pad(uint8_t petal, uint8_t pad){ - if(petal > 9) petal = 9; - if(pad > 3) pad = 3; - if(petals[petal].pads[pad].amb < petals[petal].pads[pad].cdc){ +uint16_t captouch_get_petal_pad(uint8_t petal, uint8_t pad) { + if (petal > 9) petal = 9; + if (pad > 3) pad = 3; + if (petals[petal].pads[pad].amb < petals[petal].pads[pad].cdc) { return petals[petal].pads[pad].cdc - petals[petal].pads[pad].amb; } return 0; } -void captouch_set_petal_pad_threshold(uint8_t petal, uint8_t pad, uint16_t thres){ - if(petal > 9) petal = 9; - if(pad > 3) pad = 3; - petals[petal].pads[pad].thres = thres; +void captouch_set_petal_pad_threshold(uint8_t petal, uint8_t pad, + uint16_t thres) { + if (petal > 9) petal = 9; + if (pad > 3) pad = 3; + petals[petal].pads[pad].thres = thres; } static int32_t calib_target = 6000; -void captouch_set_calibration_afe_target(uint16_t target){ +void captouch_set_calibration_afe_target(uint16_t target) { calib_target = target; } -void captouch_read_cycle(){ - static uint8_t calib_cycle = 0; - static uint8_t calib_div = 1; - static uint32_t ambient_acc[2][12] = {{0,}, {0,}}; - if(calib_cycles){ - if(calib_cycle == 0){ // last cycle has finished, setup new - calib_cycle = calib_cycles; - calib_div = calib_cycles; - for(int j=0;j<12;j++){ - ambient_acc[0][j] = 0; - ambient_acc[1][j] = 0; - } +void captouch_read_cycle() { + static uint8_t calib_cycle = 0; + static uint8_t calib_div = 1; + static uint32_t ambient_acc[2][12] = {{ + 0, + }, + { + 0, + }}; + if (calib_cycles) { + if (calib_cycle == 0) { // last cycle has finished, setup new + calib_cycle = calib_cycles; + calib_div = calib_cycles; + for (int j = 0; j < 12; j++) { + ambient_acc[0][j] = 0; + ambient_acc[1][j] = 0; } + } - ad714x_i2c_read(chip_top, 0xB, cdc_ambient[0], chip_top->stages); - ad714x_i2c_read(chip_bot, 0xB, cdc_ambient[1], chip_bot->stages); - for(int j=0;j<12;j++){ - ambient_acc[0][j] += cdc_ambient[0][j]; - ambient_acc[1][j] += cdc_ambient[1][j]; - } + ad714x_i2c_read(chip_top, 0xB, cdc_ambient[0], chip_top->stages); + ad714x_i2c_read(chip_bot, 0xB, cdc_ambient[1], chip_bot->stages); + for (int j = 0; j < 12; j++) { + ambient_acc[0][j] += cdc_ambient[0][j]; + ambient_acc[1][j] += cdc_ambient[1][j]; + } - // TODO: use median instead of average - calib_cycle--; - if(!calib_cycle){ //calib cycle is complete - for(int i = 0; i < 12; i++){ - cdc_ambient[0][i] = ambient_acc[0][i] / calib_div; - cdc_ambient[1][i] = ambient_acc[1][i] / calib_div; - } - cdc_to_petal(0, 1, cdc_ambient[0], 12); - cdc_to_petal(1, 1, cdc_ambient[1], 12); - calib_cycles = 0; - - uint8_t recalib = 0; - for(int i = 0; i < 12; i++){ - for(int j = 0; j < 2; j++){ - int32_t diff = ((int32_t) cdc_ambient[j][i]) - calib_target; - int8_t steps = diff/(AFE_INCR_CAP); - if((steps > 1) || (steps < -1)){ - if(!captouch_configure_stage_afe_offset(1-j, i, steps)){ - recalib = 1; - } + // TODO: use median instead of average + calib_cycle--; + if (!calib_cycle) { // calib cycle is complete + for (int i = 0; i < 12; i++) { + cdc_ambient[0][i] = ambient_acc[0][i] / calib_div; + cdc_ambient[1][i] = ambient_acc[1][i] / calib_div; + } + cdc_to_petal(0, 1, cdc_ambient[0], 12); + cdc_to_petal(1, 1, cdc_ambient[1], 12); + calib_cycles = 0; + + uint8_t recalib = 0; + for (int i = 0; i < 12; i++) { + for (int j = 0; j < 2; j++) { + int32_t diff = ((int32_t)cdc_ambient[j][i]) - calib_target; + int8_t steps = diff / (AFE_INCR_CAP); + if ((steps > 1) || (steps < -1)) { + if (!captouch_configure_stage_afe_offset(1 - j, i, + steps)) { + recalib = 1; } } } - if(recalib){ - calib_cycles = 16; // do another round - } else { - Atomic_Decrement_u32(&calib_active); - } } - } else { - ad714x_i2c_read(chip_top, 0xB, cdc_data[0], chip_top->stages); - cdc_to_petal(0, 0, cdc_data[0], 12); + if (recalib) { + calib_cycles = 16; // do another round + } else { + Atomic_Decrement_u32(&calib_active); + } + } + } else { + ad714x_i2c_read(chip_top, 0xB, cdc_data[0], chip_top->stages); + cdc_to_petal(0, 0, cdc_data[0], 12); - ad714x_i2c_read(chip_bot, 0xB, cdc_data[1], chip_bot->stages); - cdc_to_petal(1, 0, cdc_data[1], 12); + ad714x_i2c_read(chip_bot, 0xB, cdc_data[1], chip_bot->stages); + cdc_to_petal(1, 0, cdc_data[1], 12); - check_petals_pressed(); - } + check_petals_pressed(); + } } -static void captouch_print_debug_info_chip(const struct ad714x_chip* chip) -{ +static void captouch_print_debug_info_chip(const struct ad714x_chip *chip) { uint16_t *data; uint16_t *ambient; const int stages = chip->stages; - if(chip == chip_top) { + if (chip == chip_top) { data = cdc_data[0]; ambient = cdc_ambient[0]; } else { @@ -514,17 +589,19 @@ static void captouch_print_debug_info_chip(const struct ad714x_chip* chip) } // Appease clang-tidy. - (void)data; (void)ambient; - ESP_LOGI(TAG, "CDC results: %X %X %X %X %X %X %X %X %X %X %X %X", data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11]); - - for(int stage=0; stage<stages; stage++) { - ESP_LOGI(TAG, "stage %d ambient: %X diff: %d", stage, ambient[stage], data[stage] - ambient[stage]); + (void)data; + (void)ambient; + ESP_LOGI(TAG, "CDC results: %X %X %X %X %X %X %X %X %X %X %X %X", data[0], + data[1], data[2], data[3], data[4], data[5], data[6], data[7], + data[8], data[9], data[10], data[11]); + + for (int stage = 0; stage < stages; stage++) { + ESP_LOGI(TAG, "stage %d ambient: %X diff: %d", stage, ambient[stage], + data[stage] - ambient[stage]); } } -void captouch_print_debug_info(void) -{ +void captouch_print_debug_info(void) { captouch_print_debug_info_chip(chip_top); captouch_print_debug_info_chip(chip_bot); } - diff --git a/components/badge23/include/badge23/captouch.h b/components/badge23/include/badge23/captouch.h index 1afe8cd681..d25e5ae85d 100644 --- a/components/badge23/include/badge23/captouch.h +++ b/components/badge23/include/badge23/captouch.h @@ -1,13 +1,13 @@ #pragma once -#include <stdint.h> #include <stdbool.h> +#include <stdint.h> /* GENERAL INFORMATION * - * petal index: 0 is the top petal above the USB-C jack, increases ccw so that - * bottom petals are uneven and top petals even. - * TODO: LEDs are indexed differently, this should be harmonized - * in the impending API refactor. + * petal index: 0 is the top petal above the USB-C jack, increases ccw so + * that bottom petals are uneven and top petals even. + * TODO: LEDs are indexed differently, this should be + * harmonized in the impending API refactor. * * captouch data: full uint16_t range per stage, higher values indicate touch. * pad index: defined in captouch.c @@ -35,9 +35,10 @@ void captouch_read_cycle(void); * the ADC to optimize the readout range. according to the datasheet this should * be in the middle of the 16bit delta sigma ADC range (without much reasoning * supplied), however we found that having a higher range is beneficial. - * - * the calibration cycle is optimizing the afe coefficients so that the output of - * the "untouched" pads is close to the target set with this with this function/ + * + * the calibration cycle is optimizing the afe coefficients so that the output + * of the "untouched" pads is close to the target set with this with this + * function/ * * default target: 6000, manufacturer recommendation: 32676 */ @@ -45,13 +46,13 @@ void captouch_set_calibration_afe_target(uint16_t target); /* starts a a calibration cycle which is intended to run when the captouch * pads are not being touched. the cycle consists of two parts: - * + * * 1) optimize the afe coefficients (see captouch_set_calibration_afe_target) * - * 2) with the new afe coefficients applied, average the readings in the untouched - * state into a software calibration list which is normally subtracted from the - * captouch outputs. this makes up for the limited resolution of the of the afe - * coefficient calibration. + * 2) with the new afe coefficients applied, average the readings in the + * untouched state into a software calibration list which is normally + * subtracted from the captouch outputs. this makes up for the limited + * resolution of the of the afe coefficient calibration. */ void captouch_force_calibration(); @@ -64,28 +65,28 @@ void captouch_force_calibration(); uint8_t captouch_calibration_active(); typedef struct { - // Not all pads are present on all petals. - // Top petals have a base, cw and ccw pad. - // Bottom petlas have a base and a tip. - - // Is the tip pressed down? - bool tip_pressed; - // Is the base pressed down? - bool base_pressed; - // Is the clockwise pad pressed down? - bool cw_pressed; - // Is the counter-clockwise pad pressed down? - bool ccw_pressed; + // Not all pads are present on all petals. + // Top petals have a base, cw and ccw pad. + // Bottom petlas have a base and a tip. + + // Is the tip pressed down? + bool tip_pressed; + // Is the base pressed down? + bool base_pressed; + // Is the clockwise pad pressed down? + bool cw_pressed; + // Is the counter-clockwise pad pressed down? + bool ccw_pressed; } captouch_pad_state_t; typedef struct { - captouch_pad_state_t pads; - // Are any of the pads pressed down? - bool pressed; + captouch_pad_state_t pads; + // Are any of the pads pressed down? + bool pressed; } captouch_petal_state_t; typedef struct { - captouch_petal_state_t petals[10]; + captouch_petal_state_t petals[10]; } captouch_state_t; /* Extened/new API for reading captouch state. Allows for access to individual @@ -109,7 +110,8 @@ uint16_t read_captouch(void); * pad: pad index * thres: threshold value */ -void captouch_set_petal_pad_threshold(uint8_t petal, uint8_t pad, uint16_t thres); +void captouch_set_petal_pad_threshold(uint8_t petal, uint8_t pad, + uint16_t thres); /* returns last read captouch value from a petal pad without subtracting its * calibration reference. typically only needed for debugging. @@ -135,7 +137,7 @@ uint16_t captouch_get_petal_pad(uint8_t petal, uint8_t pad); /* estimates the azimuthal finger position on a petal in arbitrary units. * returns 0 if hardware doesn't support this. - * + * * petal: petal index * pad: pad index */ @@ -143,13 +145,12 @@ int32_t captouch_get_petal_phi(uint8_t petal); /* estimates the radial finger position on a petal in arbitrary units. * returns 0 if hardware doesn't support this. - * + * * petal: petal index * pad: pad index */ int32_t captouch_get_petal_rad(uint8_t petal); - /* configures the captouch chips, prefills internal structs etc. */ void captouch_init(void); diff --git a/components/bl00mbox/bl00mbox.c b/components/bl00mbox/bl00mbox.c index 8559cef739..e7d9e2ec60 100644 --- a/components/bl00mbox/bl00mbox.c +++ b/components/bl00mbox/bl00mbox.c @@ -1,77 +1,79 @@ -#include "st3m_audio.h" -#include "st3m_scope.h" #include <freertos/FreeRTOS.h> -#include <freertos/task.h> #include <freertos/queue.h> +#include <freertos/task.h> +#include "st3m_audio.h" +#include "st3m_scope.h" -#include <stdio.h> #include <math.h> +#include <stdio.h> #include <string.h> -typedef struct _audio_source_t{ - void * render_data; - int16_t (* render_function)(void *); +typedef struct _audio_source_t { + void *render_data; + int16_t (*render_function)(void *); uint16_t index; - struct _audio_source_t * next; + struct _audio_source_t *next; } audio_source_t; -static audio_source_t * _audio_sources = NULL; +static audio_source_t *_audio_sources = NULL; -uint16_t bl00mbox_source_add(void * render_data, void * render_function){ - //construct audio source struct - audio_source_t * src = malloc(sizeof(audio_source_t)); - if(src == NULL) return 0; +uint16_t bl00mbox_source_add(void *render_data, void *render_function) { + // construct audio source struct + audio_source_t *src = malloc(sizeof(audio_source_t)); + if (src == NULL) return 0; src->render_data = render_data; src->render_function = render_function; src->next = NULL; src->index = 0; - //handle empty list special case - if(_audio_sources == NULL){ + // handle empty list special case + if (_audio_sources == NULL) { _audio_sources = src; - return 0; //only nonempty lists from here on out! + return 0; // only nonempty lists from here on out! } - //searching for lowest unused index - audio_source_t * index_source = _audio_sources; - while(1){ - if(src->index == (index_source->index)){ - src->index++; //someone else has index already, try next - index_source = _audio_sources; //start whole list for new index + // searching for lowest unused index + audio_source_t *index_source = _audio_sources; + while (1) { + if (src->index == (index_source->index)) { + src->index++; // someone else has index already, try next + index_source = _audio_sources; // start whole list for new index } else { index_source = index_source->next; } - if(index_source == NULL){ //traversed the entire list + if (index_source == NULL) { // traversed the entire list break; } } - audio_source_t * audio_source = _audio_sources; - //append new source to linked list - while(audio_source != NULL){ - if(audio_source->next == NULL){ + audio_source_t *audio_source = _audio_sources; + // append new source to linked list + while (audio_source != NULL) { + if (audio_source->next == NULL) { audio_source->next = src; break; } else { - audio_source = audio_source->next; + audio_source = audio_source->next; } } return src->index; } -void bl00mbox_source_remove(uint16_t index){ - audio_source_t * audio_source = _audio_sources; - audio_source_t * start_gap = NULL; +void bl00mbox_source_remove(uint16_t index) { + audio_source_t *audio_source = _audio_sources; + audio_source_t *start_gap = NULL; - while(audio_source != NULL){ - if(index == audio_source->index){ - if(start_gap == NULL){ + while (audio_source != NULL) { + if (index == audio_source->index) { + if (start_gap == NULL) { _audio_sources = audio_source->next; } else { start_gap->next = audio_source->next; } - vTaskDelay(20 / portTICK_PERIOD_MS); //give other tasks time to stop using - free(audio_source); //terrible hack tbh + vTaskDelay( + 20 / + portTICK_PERIOD_MS); // give other tasks time to stop using + free(audio_source); // terrible hack tbh break; } start_gap = audio_source; @@ -79,40 +81,40 @@ void bl00mbox_source_remove(uint16_t index){ } } -uint16_t bl00mbox_sources_count(){ +uint16_t bl00mbox_sources_count() { uint16_t ret = 0; - audio_source_t * audio_source = _audio_sources; - while(audio_source != NULL){ + audio_source_t *audio_source = _audio_sources; + while (audio_source != NULL) { audio_source = audio_source->next; ret++; } return ret; } -void bl00mbox_player_function(int16_t * rx, int16_t * tx, uint16_t len){ +void bl00mbox_player_function(int16_t *rx, int16_t *tx, uint16_t len) { int32_t acc[len]; memset(acc, 0, len * sizeof(float)); - audio_source_t * audio_source = _audio_sources; - while(audio_source != NULL){ - for(uint16_t i = 0; i < len; i += 2){ - acc[i] += (*(audio_source->render_function))(audio_source->render_data); + audio_source_t *audio_source = _audio_sources; + while (audio_source != NULL) { + for (uint16_t i = 0; i < len; i += 2) { + acc[i] += + (*(audio_source->render_function))(audio_source->render_data); } audio_source = audio_source->next; } - for(uint16_t i = 0; i < len; i += 2){ - - st3m_scope_write((acc[i])>>4); + for (uint16_t i = 0; i < len; i += 2) { + st3m_scope_write((acc[i]) >> 4); - acc[i] = acc[i]>>3; + acc[i] = acc[i] >> 3; - if(acc[i] > 32767) acc[i] = 32767; - if(acc[i] < -32767) acc[i] = -32767; + if (acc[i] > 32767) acc[i] = 32767; + if (acc[i] < -32767) acc[i] = -32767; tx[i] = acc[i]; - tx[i+1] = acc[i]; + tx[i + 1] = acc[i]; } } -void bl00mbox_init(){ +void bl00mbox_init() { st3m_audio_set_player_function(bl00mbox_player_function); } diff --git a/components/bl00mbox/buds/tinysynth/tinysynth.c b/components/bl00mbox/buds/tinysynth/tinysynth.c index c7ac1415f0..ee0fc1c2d0 100644 --- a/components/bl00mbox/buds/tinysynth/tinysynth.c +++ b/components/bl00mbox/buds/tinysynth/tinysynth.c @@ -2,45 +2,46 @@ #include <math.h> #define SYNTH_UNDERSAMPLING 1 -#define SYNTH_SAMPLE_RATE ((SAMPLE_RATE)/(SYNTH_UNDERSAMPLING)) +#define SYNTH_SAMPLE_RATE ((SAMPLE_RATE) / (SYNTH_UNDERSAMPLING)) int16_t waveshaper(uint8_t shape, int16_t in); -int16_t nes_noise(uint16_t * reg, uint8_t mode, uint8_t run); +int16_t nes_noise(uint16_t* reg, uint8_t mode, uint8_t run); -int16_t run_trad_env(trad_env_t * env){ +int16_t run_trad_env(trad_env_t* env) { uint32_t tmp; - switch(env->env_phase){ + switch (env->env_phase) { case TRAD_ENV_PHASE_OFF: - env->env_counter = 0;; + env->env_counter = 0; + ; break; case TRAD_ENV_PHASE_ATTACK: tmp = env->env_counter + env->attack; - if(tmp < env->env_counter){ // overflow - tmp = ~((uint32_t) 0); // max out + if (tmp < env->env_counter) { // overflow + tmp = ~((uint32_t)0); // max out env->env_phase = TRAD_ENV_PHASE_DECAY; } env->env_counter = tmp; break; case TRAD_ENV_PHASE_DECAY: tmp = env->env_counter - env->decay; - if(tmp > env->env_counter){ // underflow - tmp = 0; //bottom out + if (tmp > env->env_counter) { // underflow + tmp = 0; // bottom out } env->env_counter = tmp; - if(env->env_counter <= env->sustain){ + if (env->env_counter <= env->sustain) { env->env_counter = env->sustain; env->env_phase = TRAD_ENV_PHASE_SUSTAIN; } break; case TRAD_ENV_PHASE_SUSTAIN: - if(env->sustain == 0) env->env_phase = TRAD_ENV_PHASE_OFF; + if (env->sustain == 0) env->env_phase = TRAD_ENV_PHASE_OFF; env->env_counter = env->sustain; break; case TRAD_ENV_PHASE_RELEASE: tmp = env->env_counter - env->release; - if(tmp > env->env_counter){ // underflow - tmp = 0; //bottom out + if (tmp > env->env_counter) { // underflow + tmp = 0; // bottom out env->env_phase = TRAD_ENV_PHASE_OFF; } env->env_counter = tmp; @@ -49,89 +50,92 @@ int16_t run_trad_env(trad_env_t * env){ return env->env_counter >> 17; } -int16_t run_trad_osc(trad_osc_t * osc){ - osc->undersampling_counter = (osc->undersampling_counter+1) % SYNTH_UNDERSAMPLING; - if(osc->undersampling_counter) return osc->prev_output; +int16_t run_trad_osc(trad_osc_t* osc) { + osc->undersampling_counter = + (osc->undersampling_counter + 1) % SYNTH_UNDERSAMPLING; + if (osc->undersampling_counter) return osc->prev_output; - int32_t ret; //lil bit buffer for operations + int32_t ret; // lil bit buffer for operations int32_t env = run_trad_env(&(osc->env)); - if(osc->env.env_phase == TRAD_ENV_PHASE_OFF){ - osc->counter = ((uint64_t) 1) << 63; + if (osc->env.env_phase == TRAD_ENV_PHASE_OFF) { + osc->counter = ((uint64_t)1) << 63; return 0; } - //run core sawtooth - //uint64_t incr = osc->freq * osc->bend; + // run core sawtooth + // uint64_t incr = osc->freq * osc->bend; uint64_t incr = osc->freq; osc->counter += incr; - osc->overflow_event = osc->counter_prev > osc->counter; //no neg f linfm for now + osc->overflow_event = + osc->counter_prev > osc->counter; // no neg f linfm for now osc->counter_prev = osc->counter; - if(osc->waveform >= 7){ - ret = nes_noise(&(osc->noise_reg), osc->waveform == 7, osc->overflow_event); + if (osc->waveform >= 7) { + ret = nes_noise(&(osc->noise_reg), osc->waveform == 7, + osc->overflow_event); } else { - //apply waveshaper - int32_t tmp = (osc->counter) >> (33+16); + // apply waveshaper + int32_t tmp = (osc->counter) >> (33 + 16); tmp *= 2; tmp -= 32767; ret = waveshaper(osc->waveform, tmp); } - //apply volume - ret = (ret * env)>>15; - ret = (ret * osc->vol)>>15; + // apply volume + ret = (ret * env) >> 15; + ret = (ret * osc->vol) >> 15; osc->prev_output = ret; return ret; } -int16_t nes_noise(uint16_t * reg, uint8_t mode, uint8_t run){ - if(run) { +int16_t nes_noise(uint16_t* reg, uint8_t mode, uint8_t run) { + if (run) { uint8_t fb = *reg; - if(mode){ - fb = fb>>6; + if (mode) { + fb = fb >> 6; } else { - fb = fb>>1; + fb = fb >> 1; } fb = (fb ^ (*reg)) & 1; *reg = (*reg >> 1); - *reg = (*reg) | (((uint16_t) fb) << 14); + *reg = (*reg) | (((uint16_t)fb) << 14); } - return ((int16_t) (((*reg) & 1))*2 - 1) * 32767; + return ((int16_t)(((*reg) & 1)) * 2 - 1) * 32767; } -int16_t fake_square(int16_t triangle, int16_t pwm, int16_t gain){ - //max gain (1<<14)-1 +int16_t fake_square(int16_t triangle, int16_t pwm, int16_t gain) { + // max gain (1<<14)-1 int32_t tmp = triangle; tmp += pwm; tmp *= gain; - if(tmp > 32767) tmp = 32767; - if(tmp < -32767) tmp = -32767; + if (tmp > 32767) tmp = 32767; + if (tmp < -32767) tmp = -32767; return tmp; } -int16_t waveshaper(uint8_t shape, int16_t in){ +int16_t waveshaper(uint8_t shape, int16_t in) { int32_t tmp = 0; - switch(shape){ - case TRAD_OSC_WAVE_SINE: // TODO: implement proper sine + switch (shape) { + case TRAD_OSC_WAVE_SINE: // TODO: implement proper sine case TRAD_OSC_WAVE_FAKE_SINE: tmp = waveshaper(TRAD_OSC_WAVE_TRI, in); - if(tmp > 0.){ + if (tmp > 0.) { tmp = 32767 - tmp; - tmp = (tmp*tmp)>>15; + tmp = (tmp * tmp) >> 15; tmp = 32767. - tmp; } else { tmp = 32767 + tmp; - tmp = (tmp*tmp)>>15; + tmp = (tmp * tmp) >> 15; tmp = tmp - 32767.; } break; case TRAD_OSC_WAVE_TRI: tmp = in; tmp += 16384; - if(tmp > 32767) tmp -= 65535; - if(tmp > 0) tmp = -tmp; + if (tmp > 32767) tmp -= 65535; + if (tmp > 0) tmp = -tmp; tmp = (2 * tmp) + 32767; break; case TRAD_OSC_WAVE_SAW: @@ -150,56 +154,57 @@ int16_t waveshaper(uint8_t shape, int16_t in){ tmp = fake_square(tmp, 20384, 100); break; } - if(tmp > 32767) tmp = 32767; - if(tmp < -32767) tmp = -32767; + if (tmp > 32767) tmp = 32767; + if (tmp < -32767) tmp = -32767; return tmp; } #define NAT_LOG_SEMITONE 0.05776226504666215 -void trad_osc_set_freq_semitone(trad_osc_t * osc, float tone){ +void trad_osc_set_freq_semitone(trad_osc_t* osc, float tone) { trad_osc_set_freq_Hz(osc, 440. * exp(tone * NAT_LOG_SEMITONE)); } -void trad_osc_set_freq_Hz(trad_osc_t * osc, float freq){ +void trad_osc_set_freq_Hz(trad_osc_t* osc, float freq) { uint64_t max = ~((uint64_t)0); - osc->freq = (freq/(SYNTH_SAMPLE_RATE)) * max; + osc->freq = (freq / (SYNTH_SAMPLE_RATE)) * max; } -void trad_osc_set_waveform(trad_osc_t * osc, uint8_t waveform){ +void trad_osc_set_waveform(trad_osc_t* osc, uint8_t waveform) { osc->waveform = waveform; } -void trad_osc_set_attack_ms(trad_osc_t * osc, float ms){ - osc->env.attack = (1000./ms/(SYNTH_SAMPLE_RATE)) * (~((uint32_t) 0)) ; +void trad_osc_set_attack_ms(trad_osc_t* osc, float ms) { + osc->env.attack = (1000. / ms / (SYNTH_SAMPLE_RATE)) * (~((uint32_t)0)); } -void trad_osc_set_decay_ms(trad_osc_t * osc, float ms){ - osc->env.decay = (1000./ms/(SYNTH_SAMPLE_RATE)) * ((~((uint32_t) 0)) - osc->env.sustain); +void trad_osc_set_decay_ms(trad_osc_t* osc, float ms) { + osc->env.decay = (1000. / ms / (SYNTH_SAMPLE_RATE)) * + ((~((uint32_t)0)) - osc->env.sustain); } -void trad_osc_set_sustain(trad_osc_t * osc, float sus){ +void trad_osc_set_sustain(trad_osc_t* osc, float sus) { uint32_t max = ~((uint32_t)0); osc->env.sustain = max * sus; } -void trad_osc_set_release_ms(trad_osc_t * osc, float ms){ - osc->env.release = (1000./ms/(SYNTH_SAMPLE_RATE)) * osc->env.sustain; +void trad_osc_set_release_ms(trad_osc_t* osc, float ms) { + osc->env.release = (1000. / ms / (SYNTH_SAMPLE_RATE)) * osc->env.sustain; } -void trad_env_stop(trad_osc_t * osc){ - if(osc->env.env_phase != TRAD_ENV_PHASE_OFF) osc->env.env_phase = TRAD_ENV_PHASE_RELEASE; +void trad_env_stop(trad_osc_t* osc) { + if (osc->env.env_phase != TRAD_ENV_PHASE_OFF) + osc->env.env_phase = TRAD_ENV_PHASE_RELEASE; } -void trad_env_fullstop(trad_osc_t * osc){ - osc->env.env_phase = TRAD_ENV_PHASE_OFF; //stop and skip decay phase +void trad_env_fullstop(trad_osc_t* osc) { + osc->env.env_phase = TRAD_ENV_PHASE_OFF; // stop and skip decay phase } -void trad_env_start(trad_osc_t * osc){ - osc->env.env_phase = TRAD_ENV_PHASE_ATTACK; //put into attack phase; +void trad_env_start(trad_osc_t* osc) { + osc->env.env_phase = TRAD_ENV_PHASE_ATTACK; // put into attack phase; } -void trad_osc_set_vol(trad_osc_t * osc, float volume){ +void trad_osc_set_vol(trad_osc_t* osc, float volume) { osc->vol = 32767 * volume; } - diff --git a/components/bl00mbox/buds/tinysynth/tinysynth.h b/components/bl00mbox/buds/tinysynth/tinysynth.h index aed43e16db..492d09105e 100644 --- a/components/bl00mbox/buds/tinysynth/tinysynth.h +++ b/components/bl00mbox/buds/tinysynth/tinysynth.h @@ -2,13 +2,13 @@ #include <stdint.h> #include <stdio.h> -//#include "radspa.h" -//#include "bl00mbox.h" +// #include "radspa.h" +// #include "bl00mbox.h" #define SAMPLE_RATE 48000 -#define TRAD_OSC_DECAY_STEP 0.01 -#define TRAD_OSC_ATTACK_POP_BLOCK 16 +#define TRAD_OSC_DECAY_STEP 0.01 +#define TRAD_OSC_ATTACK_POP_BLOCK 16 #define TRAD_ENV_PHASE_OFF 0 #define TRAD_ENV_PHASE_ATTACK 1 @@ -24,48 +24,47 @@ #define TRAD_OSC_WAVE_PULSE 5 #define TRAD_OSC_WAVE_BLIP 6 - typedef struct { - uint32_t env_counter; - uint32_t attack; - uint32_t decay; - uint32_t sustain; - uint32_t release; - uint8_t env_phase; - uint8_t skip_hold; + uint32_t env_counter; + uint32_t attack; + uint32_t decay; + uint32_t sustain; + uint32_t release; + uint8_t env_phase; + uint8_t skip_hold; } trad_env_t; typedef struct { - //user variables + // user variables - //internal data storage, not for user access - uint64_t freq; //in hertz, negative frequencies for linFM allowed - uint64_t bend; - uint32_t vol; //output volume - uint8_t waveform; //0: sine, 1: fast sine, 2: tri, 3: saw, - //4: square, 5: 33% pulse, 6: 25% pulse + // internal data storage, not for user access + uint64_t freq; // in hertz, negative frequencies for linFM allowed + uint64_t bend; + uint32_t vol; // output volume + uint8_t waveform; // 0: sine, 1: fast sine, 2: tri, 3: saw, + // 4: square, 5: 33% pulse, 6: 25% pulse - uint64_t counter; //state of central sawtooth oscillator. - uint64_t counter_prev; //previous state of central sawtooth oscillator. - int8_t overflow_event; //set to -1 when counter underflows (below -1), - //set to +1 when counter overflows (above 1) - //not reset or used by anything so far - uint8_t undersampling_counter; - int16_t prev_output; // for undersampling - uint16_t noise_reg; + uint64_t counter; // state of central sawtooth oscillator. + uint64_t counter_prev; // previous state of central sawtooth oscillator. + int8_t overflow_event; // set to -1 when counter underflows (below -1), + // set to +1 when counter overflows (above 1) + // not reset or used by anything so far + uint8_t undersampling_counter; + int16_t prev_output; // for undersampling + uint16_t noise_reg; trad_env_t env; } trad_osc_t; -int16_t run_trad_osc(trad_osc_t * osc); -void trad_osc_set_freq_semitone(trad_osc_t * osc, float bend); -void trad_osc_set_freq_Hz(trad_osc_t * osc, float freq); -void trad_osc_set_waveform(trad_osc_t * osc, uint8_t waveform); -void trad_osc_set_attack_ms(trad_osc_t * osc, float ms); -void trad_osc_set_decay_ms(trad_osc_t * osc, float ms); -void trad_osc_set_sustain(trad_osc_t * osc, float sus); -void trad_osc_set_release_ms(trad_osc_t * osc, float ms); -void trad_env_stop(trad_osc_t * osc); -void trad_env_fullstop(trad_osc_t * osc); -void trad_env_start(trad_osc_t * osc); +int16_t run_trad_osc(trad_osc_t* osc); +void trad_osc_set_freq_semitone(trad_osc_t* osc, float bend); +void trad_osc_set_freq_Hz(trad_osc_t* osc, float freq); +void trad_osc_set_waveform(trad_osc_t* osc, uint8_t waveform); +void trad_osc_set_attack_ms(trad_osc_t* osc, float ms); +void trad_osc_set_decay_ms(trad_osc_t* osc, float ms); +void trad_osc_set_sustain(trad_osc_t* osc, float sus); +void trad_osc_set_release_ms(trad_osc_t* osc, float ms); +void trad_env_stop(trad_osc_t* osc); +void trad_env_fullstop(trad_osc_t* osc); +void trad_env_start(trad_osc_t* osc); -void trad_osc_set_vol(trad_osc_t * osc, float volume); +void trad_osc_set_vol(trad_osc_t* osc, float volume); diff --git a/components/bl00mbox/include/bl00mbox.h b/components/bl00mbox/include/bl00mbox.h index 6d8cd0e76d..497e220e27 100644 --- a/components/bl00mbox/include/bl00mbox.h +++ b/components/bl00mbox/include/bl00mbox.h @@ -1,10 +1,10 @@ #pragma once -#include <stdint.h> #include <stdbool.h> +#include <stdint.h> #define SAMPLE_RATE 48000 uint16_t bl00mbox_sources_count(); -uint16_t bl00mbox_source_add(void * render_data, void * render_function); +uint16_t bl00mbox_source_add(void* render_data, void* render_function); void bl00mbox_source_remove(uint16_t index); -void bl00mbox_player_function(int16_t * rx, int16_t * tx, uint16_t len); +void bl00mbox_player_function(int16_t* rx, int16_t* tx, uint16_t len); diff --git a/components/bl00mbox/include/bud_registry.h b/components/bl00mbox/include/bud_registry.h index 5f1d5ec486..176ce63409 100644 --- a/components/bl00mbox/include/bud_registry.h +++ b/components/bl00mbox/include/bud_registry.h @@ -6,6 +6,4 @@ bud_t -bud_t buds[BUD_ENTRIES] = { - tinysynth_bud -} + bud_t buds[BUD_ENTRIES] = {tinysynth_bud} diff --git a/components/flow3r_bsp/flow3r_bsp.h b/components/flow3r_bsp/flow3r_bsp.h index 9492c4706a..47d11572e6 100644 --- a/components/flow3r_bsp/flow3r_bsp.h +++ b/components/flow3r_bsp/flow3r_bsp.h @@ -41,20 +41,19 @@ extern const char *flow3r_bsp_hw_name; #define FLOW3R_BSP_AUDIO_DMA_BUFFER_COUNT 4 typedef enum { - flow3r_bsp_audio_input_source_none = 0, - // Line in on riht jack. - flow3r_bsp_audio_input_source_line_in = 1, - // Headset microphone on left jack. - flow3r_bsp_audio_input_source_headset_mic = 2, - // Onboard microphone (enabled red LED). - flow3r_bsp_audio_input_source_onboard_mic = 3 + flow3r_bsp_audio_input_source_none = 0, + // Line in on riht jack. + flow3r_bsp_audio_input_source_line_in = 1, + // Headset microphone on left jack. + flow3r_bsp_audio_input_source_headset_mic = 2, + // Onboard microphone (enabled red LED). + flow3r_bsp_audio_input_source_onboard_mic = 3 } flow3r_bsp_audio_input_source_t; // Initialize the audio subsystem of the badge, including the codec and I2S data // channel. void flow3r_bsp_audio_init(void); - // Attempts to set target volume for the headphone output/onboard speakers // respectively, clamps/rounds if necessary and returns the actual volume // applied in hardware. @@ -74,9 +73,9 @@ float flow3r_bsp_audio_speaker_set_volume(bool mute, float dB); void flow3r_bsp_audio_headset_set_gain_dB(uint8_t gain_dB); typedef struct { - bool headphones; - bool headset; - bool line_in; + bool headphones; + bool headset; + bool line_in; } flow3r_bsp_audio_jacksense_state_t; // Polls hardware to check if headphones, headset or line in are plugged into @@ -88,14 +87,16 @@ void flow3r_bsp_audio_read_jacksense(flow3r_bsp_audio_jacksense_state_t *st); // enum. // // Note: The onboard digital mic turns on an LED on the top board if it receives -// a clock signal which is considered a good proxy for its capability of reading data. +// a clock signal which is considered a good proxy for its capability of reading +// data. // // TODO: check if sources are available void flow3r_bsp_audio_input_set_source(flow3r_bsp_audio_input_source_t source); // These route whatever is on the line in port directly to the headphones or // speaker respectively (enable = 1), or don't (enable = 0). Is affected by mute -// and coarse hardware volume settings, however software fine volume is not applied. +// and coarse hardware volume settings, however software fine volume is not +// applied. // // Good for testing, might deprecate later, idk~ void flow3r_bsp_audio_headphones_line_in_set_hardware_thru(bool enable); @@ -117,8 +118,11 @@ bool flow3r_bsp_audio_has_hardware_mute(void); // // The input is routed according to the configured source (via // flow3r_bp_audio_input_set_source). -esp_err_t flow3r_bsp_audio_read(void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait); -esp_err_t flow3r_bsp_audio_write(const void *src, size_t size, size_t *bytes_written, TickType_t ticks_to_wait); +esp_err_t flow3r_bsp_audio_read(void *dest, size_t size, size_t *bytes_read, + TickType_t ticks_to_wait); +esp_err_t flow3r_bsp_audio_write(const void *src, size_t size, + size_t *bytes_written, + TickType_t ticks_to_wait); // Write audio codec register. Obviously very unsafe. Have fun. void flow3r_bsp_audio_register_poke(uint8_t reg, uint8_t data); @@ -133,23 +137,25 @@ esp_err_t flow3r_bsp_leds_init(void); // Index is a value in [0, FLOW3R_BSP_LED_COUNT). // // RGB values are in [0, 0xff]. -void flow3r_bsp_leds_set_pixel(uint32_t index, uint32_t red, uint32_t green, uint32_t blue); +void flow3r_bsp_leds_set_pixel(uint32_t index, uint32_t red, uint32_t green, + uint32_t blue); // Transmit from internal buffer into LEDs. This will block in case there // already is a previous transmission happening. esp_err_t flow3r_bsp_leds_refresh(TickType_t timeout_ms); // A 'tripos' button is what we're calling the shoulder buttons. As the name -// indicates, it has three positions: left, middle (a.k.a. down/press) and right. +// indicates, it has three positions: left, middle (a.k.a. down/press) and +// right. typedef enum { - // Not pressed. - flow3r_bsp_tripos_none = 0, - // Pressed towards the left. - flow3r_bsp_tripos_left = -1, - // Pressed down. - flow3r_bsp_tripos_mid = 2, - // Pressed towards the right. - flow3r_bsp_tripos_right = 1, + // Not pressed. + flow3r_bsp_tripos_none = 0, + // Pressed towards the left. + flow3r_bsp_tripos_left = -1, + // Pressed down. + flow3r_bsp_tripos_mid = 2, + // Pressed towards the right. + flow3r_bsp_tripos_right = 1, } flow3r_bsp_tripos_state_t; // Initialize 'special purpose i/o', which is like gpio, but more :). This means @@ -198,11 +204,12 @@ void flow3r_bsp_spio_badgelink_right_enable(bool tip_on, bool ring_on); // Pin mapping information of programmable badge I/O. These are GPIO numbers // that can be used with the ESP-IDF API. typedef struct { - // Left jack, headphone/line out. - uint8_t badgelink_left_tip; - uint8_t badgelink_left_ring; - // Right jack, line in. - uint8_t badgelink_right_tip; - uint8_t badgelink_right_ring; + // Left jack, headphone/line out. + uint8_t badgelink_left_tip; + uint8_t badgelink_left_ring; + // Right jack, line in. + uint8_t badgelink_right_tip; + uint8_t badgelink_right_ring; } flow3r_bsp_spio_programmable_pins_t; -extern const flow3r_bsp_spio_programmable_pins_t flow3r_bsp_spio_programmable_pins; \ No newline at end of file +extern const flow3r_bsp_spio_programmable_pins_t + flow3r_bsp_spio_programmable_pins; \ No newline at end of file diff --git a/components/flow3r_bsp/flow3r_bsp_audio.c b/components/flow3r_bsp/flow3r_bsp_audio.c index bf3d5fa3a7..d23c41c933 100644 --- a/components/flow3r_bsp/flow3r_bsp_audio.c +++ b/components/flow3r_bsp/flow3r_bsp_audio.c @@ -5,7 +5,9 @@ #include "driver/i2s.h" #include "esp_log.h" -esp_err_t flow3r_bsp_audio_write(const void *src, size_t size, size_t *bytes_written, TickType_t ticks_to_wait) { +esp_err_t flow3r_bsp_audio_write(const void *src, size_t size, + size_t *bytes_written, + TickType_t ticks_to_wait) { return i2s_write(0, src, size, bytes_written, ticks_to_wait); } @@ -13,18 +15,17 @@ esp_err_t flow3r_bsp_audio_write(const void *src, size_t size, size_t *bytes_wri void flow3r_bsp_audio_init(void) { flow3r_bsp_max98091_init(); - vTaskDelay(100 / portTICK_PERIOD_MS); // dunno if necessary + vTaskDelay(100 / portTICK_PERIOD_MS); // dunno if necessary static const i2s_config_t i2s_config = { .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX, .sample_rate = FLOW3R_BSP_AUDIO_SAMPLE_RATE, .bits_per_sample = 16, .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB, - .intr_alloc_flags = 0, // default interrupt priority + .intr_alloc_flags = 0, // default interrupt priority .dma_buf_count = FLOW3R_BSP_AUDIO_DMA_BUFFER_COUNT, .dma_buf_len = FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE, - .use_apll = false - }; + .use_apll = false}; static const i2s_pin_config_t pin_config = { .bck_io_num = 10, .mck_io_num = 18, @@ -67,14 +68,13 @@ void flow3r_bsp_audio_line_in_set_hardware_thru(bool enable) { flow3r_bsp_max98091_line_in_set_hardware_thru(enable); } -bool flow3r_bsp_audio_has_hardware_mute(void) { - return true; -} +bool flow3r_bsp_audio_has_hardware_mute(void) { return true; } void flow3r_bsp_audio_register_poke(uint8_t reg, uint8_t data) { flow3r_bsp_max98091_register_poke(reg, data); } -esp_err_t flow3r_bsp_audio_read(void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait) { +esp_err_t flow3r_bsp_audio_read(void *dest, size_t size, size_t *bytes_read, + TickType_t ticks_to_wait) { return i2s_read(0, dest, size, bytes_read, ticks_to_wait); } diff --git a/components/flow3r_bsp/flow3r_bsp_display.c b/components/flow3r_bsp/flow3r_bsp_display.c index e689dcb2f1..fa4c2d1f5f 100644 --- a/components/flow3r_bsp/flow3r_bsp_display.c +++ b/components/flow3r_bsp/flow3r_bsp_display.c @@ -10,55 +10,55 @@ static const char *TAG = "flow3r-bsp-display"; flow3r_bsp_gc9a01_config_t gc9a01_config = { - .reset_used = 0, - .backlight_used = 1, + .reset_used = 0, + .backlight_used = 1, - .pin_sck = 41, - .pin_mosi = 42, - .pin_cs = 40, - .pin_dc = 38, - .pin_backlight = 46, + .pin_sck = 41, + .pin_mosi = 42, + .pin_cs = 40, + .pin_dc = 38, + .pin_backlight = 46, - .host = 2, + .host = 2, }; static flow3r_bsp_gc9a01_t gc9a01; static uint8_t gc9a01_initialized = 0; void flow3r_bsp_display_init(void) { - ESP_LOGI(TAG, "gc9a01 initializing..."); - esp_err_t ret = flow3r_bsp_gc9a01_init(&gc9a01, &gc9a01_config); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "gc9a01 init failed: %s", esp_err_to_name(ret)); - } else { - gc9a01_initialized = 1; - ESP_LOGI(TAG, "gc9a01 initialized"); - } + ESP_LOGI(TAG, "gc9a01 initializing..."); + esp_err_t ret = flow3r_bsp_gc9a01_init(&gc9a01, &gc9a01_config); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "gc9a01 init failed: %s", esp_err_to_name(ret)); + } else { + gc9a01_initialized = 1; + ESP_LOGI(TAG, "gc9a01 initialized"); + } } void flow3r_bsp_display_send_fb(uint16_t *fb_data) { - if (!gc9a01_initialized) { - return; - } - static bool had_error = false; + if (!gc9a01_initialized) { + return; + } + static bool had_error = false; - esp_err_t ret = flow3r_bsp_gc9a01_blit_full(&gc9a01, fb_data); - if (ret != ESP_OK) { - if (!had_error) { - ESP_LOGE(TAG, "display blit failed: %s", esp_err_to_name(ret)); - had_error = true; - } - } else { - if (had_error) { - ESP_LOGI(TAG, "display blit success!"); - had_error = false; - } - } + esp_err_t ret = flow3r_bsp_gc9a01_blit_full(&gc9a01, fb_data); + if (ret != ESP_OK) { + if (!had_error) { + ESP_LOGE(TAG, "display blit failed: %s", esp_err_to_name(ret)); + had_error = true; + } + } else { + if (had_error) { + ESP_LOGI(TAG, "display blit success!"); + had_error = false; + } + } } void flow3r_bsp_display_set_backlight(uint8_t percent) { - if (!gc9a01_initialized) { - return; - } - flow3r_bsp_gc9a01_backlight_set(&gc9a01, percent); + if (!gc9a01_initialized) { + return; + } + flow3r_bsp_gc9a01_backlight_set(&gc9a01, percent); } diff --git a/components/flow3r_bsp/flow3r_bsp_gc9a01.c b/components/flow3r_bsp/flow3r_bsp_gc9a01.c index 400bc53903..3f7cde1819 100644 --- a/components/flow3r_bsp/flow3r_bsp_gc9a01.c +++ b/components/flow3r_bsp/flow3r_bsp_gc9a01.c @@ -6,7 +6,8 @@ // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // -// 1. Redistributions of source code must retain the above copyright notice, this +// 1. Redistributions of source code must retain the above copyright notice, +// this // list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright notice, @@ -19,14 +20,15 @@ // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. #include <stdint.h> #include <string.h> @@ -38,47 +40,47 @@ #include "flow3r_bsp_gc9a01.h" #include "sdkconfig.h" -#define Cmd_SLPIN 0x10 -#define Cmd_SLPOUT 0x11 -#define Cmd_INVOFF 0x20 -#define Cmd_INVON 0x21 -#define Cmd_DISPOFF 0x28 -#define Cmd_DISPON 0x29 -#define Cmd_CASET 0x2A -#define Cmd_RASET 0x2B -#define Cmd_RAMWR 0x2C -#define Cmd_TEON 0x35 // Tearing effect line ON -#define Cmd_MADCTL 0x36 // Memory data access control -#define Cmd_COLMOD 0x3A // Pixel format set - -#define Cmd_DisplayFunctionControl 0xB6 -#define Cmd_PWCTR1 0xC1 // Power control 1 -#define Cmd_PWCTR2 0xC3 // Power control 2 -#define Cmd_PWCTR3 0xC4 // Power control 3 -#define Cmd_PWCTR4 0xC9 // Power control 4 -#define Cmd_PWCTR7 0xA7 // Power control 7 - -#define Cmd_FRAMERATE 0xE8 -#define Cmd_InnerReg1Enable 0xFE -#define Cmd_InnerReg2Enable 0xEF - -#define Cmd_GAMMA1 0xF0 // Set gamma 1 -#define Cmd_GAMMA2 0xF1 // Set gamma 2 -#define Cmd_GAMMA3 0xF2 // Set gamma 3 -#define Cmd_GAMMA4 0xF3 // Set gamma 4 - -#define ColorMode_RGB_16bit 0x50 -#define ColorMode_RGB_18bit 0x60 -#define ColorMode_MCU_12bit 0x03 -#define ColorMode_MCU_16bit 0x05 -#define ColorMode_MCU_18bit 0x06 - -#define MADCTL_MY 0x80 -#define MADCTL_MX 0x40 -#define MADCTL_MV 0x20 -#define MADCTL_ML 0x10 -#define MADCTL_BGR 0x08 -#define MADCTL_MH 0x04 +#define Cmd_SLPIN 0x10 +#define Cmd_SLPOUT 0x11 +#define Cmd_INVOFF 0x20 +#define Cmd_INVON 0x21 +#define Cmd_DISPOFF 0x28 +#define Cmd_DISPON 0x29 +#define Cmd_CASET 0x2A +#define Cmd_RASET 0x2B +#define Cmd_RAMWR 0x2C +#define Cmd_TEON 0x35 // Tearing effect line ON +#define Cmd_MADCTL 0x36 // Memory data access control +#define Cmd_COLMOD 0x3A // Pixel format set + +#define Cmd_DisplayFunctionControl 0xB6 +#define Cmd_PWCTR1 0xC1 // Power control 1 +#define Cmd_PWCTR2 0xC3 // Power control 2 +#define Cmd_PWCTR3 0xC4 // Power control 3 +#define Cmd_PWCTR4 0xC9 // Power control 4 +#define Cmd_PWCTR7 0xA7 // Power control 7 + +#define Cmd_FRAMERATE 0xE8 +#define Cmd_InnerReg1Enable 0xFE +#define Cmd_InnerReg2Enable 0xEF + +#define Cmd_GAMMA1 0xF0 // Set gamma 1 +#define Cmd_GAMMA2 0xF1 // Set gamma 2 +#define Cmd_GAMMA3 0xF2 // Set gamma 3 +#define Cmd_GAMMA4 0xF3 // Set gamma 4 + +#define ColorMode_RGB_16bit 0x50 +#define ColorMode_RGB_18bit 0x60 +#define ColorMode_MCU_12bit 0x03 +#define ColorMode_MCU_16bit 0x05 +#define ColorMode_MCU_18bit 0x06 + +#define MADCTL_MY 0x80 +#define MADCTL_MX 0x40 +#define MADCTL_MV 0x20 +#define MADCTL_ML 0x10 +#define MADCTL_BGR 0x08 +#define MADCTL_MH 0x04 static const char *TAG = "flow3r-bsp-gc9a01"; @@ -86,87 +88,92 @@ static const char *TAG = "flow3r-bsp-gc9a01"; // Provides enough data for the pre-transaction callback to be able to set the // DC pin as needed. typedef struct { - flow3r_bsp_gc9a01_t *gc9a01; + flow3r_bsp_gc9a01_t *gc9a01; - // DC pin will be set to this in pre-SPI callback. - int dc; + // DC pin will be set to this in pre-SPI callback. + int dc; } flow3r_bsp_gc9a01_tx_t; - // A full-framebuffer 'blit' operation descriptor. Keeps track of number of // underlying DMA SPI transactions left until blit is done. typedef struct { - flow3r_bsp_gc9a01_t *gc9a01; - const uint8_t *fb; - size_t left; + flow3r_bsp_gc9a01_t *gc9a01; + const uint8_t *fb; + size_t left; - flow3r_bsp_gc9a01_tx_t gc9a01_tx; - spi_transaction_t spi_tx; + flow3r_bsp_gc9a01_tx_t gc9a01_tx; + spi_transaction_t spi_tx; } flow3r_bsp_gc9a01_blit_t; /* - The LCD needs a bunch of command/argument values to be initialized. They are stored in this struct. + The LCD needs a bunch of command/argument values to be initialized. They are + stored in this struct. */ typedef struct { uint8_t cmd; uint8_t data[16]; - uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds. + uint8_t databytes; // No of data in data; bit 7 = delay after set; 0xFF = + // end of cmds. } flow3r_bsp_gc9a01_init_cmd_t; static const flow3r_bsp_gc9a01_init_cmd_t flow3r_bsp_gc9a01_init_cmds[] = { - {0xef,{0},0}, - {0xeb,{0x14},1}, - {0xfe,{0},0}, - {0xef,{0},0}, - {0xeb,{0x14},1}, - {0x84,{0x40},1}, - {0x85,{0xff},1}, - {0x86,{0xff},1}, - {0x87,{0xff},1}, - {0x88,{0x0a},1}, - {0x89,{0x21},1}, - {0x8a,{0x00},1}, - {0x8b,{0x80},1}, - {0x8c,{0x01},1}, - {0x8d,{0x01},1}, - {0x8e,{0xff},1}, - {0x8f,{0xff},1}, - {Cmd_DisplayFunctionControl,{0x00,0x20},2},// Scan direction S360 -> S1 - {0x90,{0x08,0x08,0x08,0x08},4}, - {0xbd,{0x06},1}, - {0xbc,{0x00},1}, - {0xff,{0x60,0x01,0x04},3}, - {Cmd_PWCTR2,{0x13},1}, - {Cmd_PWCTR3,{0x13},1}, - {Cmd_PWCTR4,{0x22},1}, - {0xbe,{0x11},1}, - {0xe1,{0x10,0x0e},2}, - {0xdf,{0x21,0x0c,0x02},3}, - {Cmd_GAMMA1,{0x45,0x09,0x08,0x08,0x26,0x2a},6}, - {Cmd_GAMMA2,{0x43,0x70,0x72,0x36,0x37,0x6f},6}, - {Cmd_GAMMA3,{0x45,0x09,0x08,0x08,0x26,0x2a},6}, - {Cmd_GAMMA4,{0x43,0x70,0x72,0x36,0x37,0x6f},6}, - {0xed,{0x1b,0x0b},2}, - {0xae,{0x77},1}, - {0xcd,{0x63},1}, - {0x70,{0x07,0x07,0x04,0x0e,0x0f,0x09,0x07,0x08,0x03},9}, - {Cmd_FRAMERATE,{0x34},1},// 4 dot inversion - {0x62,{0x18,0x0D,0x71,0xED,0x70,0x70,0x18,0x0F,0x71,0xEF,0x70,0x70},12}, - {0x63,{0x18,0x11,0x71,0xF1,0x70,0x70,0x18,0x13,0x71,0xF3,0x70,0x70},12}, - {0x64,{0x28,0x29,0xF1,0x01,0xF1,0x00,0x07},7}, - {0x66,{0x3C,0x00,0xCD,0x67,0x45,0x45,0x10,0x00,0x00,0x00},10}, - {0x67,{0x00,0x3C,0x00,0x00,0x00,0x01,0x54,0x10,0x32,0x98},10}, - {0x74,{0x10,0x85,0x80,0x00,0x00,0x4E,0x00},7}, - {0x98,{0x3e,0x07},2}, - {Cmd_TEON,{0},0},// Tearing effect line on - {0, {0}, 0xff},//END + {0xef, {0}, 0}, + {0xeb, {0x14}, 1}, + {0xfe, {0}, 0}, + {0xef, {0}, 0}, + {0xeb, {0x14}, 1}, + {0x84, {0x40}, 1}, + {0x85, {0xff}, 1}, + {0x86, {0xff}, 1}, + {0x87, {0xff}, 1}, + {0x88, {0x0a}, 1}, + {0x89, {0x21}, 1}, + {0x8a, {0x00}, 1}, + {0x8b, {0x80}, 1}, + {0x8c, {0x01}, 1}, + {0x8d, {0x01}, 1}, + {0x8e, {0xff}, 1}, + {0x8f, {0xff}, 1}, + {Cmd_DisplayFunctionControl, {0x00, 0x20}, 2}, // Scan direction S360 -> S1 + {0x90, {0x08, 0x08, 0x08, 0x08}, 4}, + {0xbd, {0x06}, 1}, + {0xbc, {0x00}, 1}, + {0xff, {0x60, 0x01, 0x04}, 3}, + {Cmd_PWCTR2, {0x13}, 1}, + {Cmd_PWCTR3, {0x13}, 1}, + {Cmd_PWCTR4, {0x22}, 1}, + {0xbe, {0x11}, 1}, + {0xe1, {0x10, 0x0e}, 2}, + {0xdf, {0x21, 0x0c, 0x02}, 3}, + {Cmd_GAMMA1, {0x45, 0x09, 0x08, 0x08, 0x26, 0x2a}, 6}, + {Cmd_GAMMA2, {0x43, 0x70, 0x72, 0x36, 0x37, 0x6f}, 6}, + {Cmd_GAMMA3, {0x45, 0x09, 0x08, 0x08, 0x26, 0x2a}, 6}, + {Cmd_GAMMA4, {0x43, 0x70, 0x72, 0x36, 0x37, 0x6f}, 6}, + {0xed, {0x1b, 0x0b}, 2}, + {0xae, {0x77}, 1}, + {0xcd, {0x63}, 1}, + {0x70, {0x07, 0x07, 0x04, 0x0e, 0x0f, 0x09, 0x07, 0x08, 0x03}, 9}, + {Cmd_FRAMERATE, {0x34}, 1}, // 4 dot inversion + {0x62, + {0x18, 0x0D, 0x71, 0xED, 0x70, 0x70, 0x18, 0x0F, 0x71, 0xEF, 0x70, 0x70}, + 12}, + {0x63, + {0x18, 0x11, 0x71, 0xF1, 0x70, 0x70, 0x18, 0x13, 0x71, 0xF3, 0x70, 0x70}, + 12}, + {0x64, {0x28, 0x29, 0xF1, 0x01, 0xF1, 0x00, 0x07}, 7}, + {0x66, {0x3C, 0x00, 0xCD, 0x67, 0x45, 0x45, 0x10, 0x00, 0x00, 0x00}, 10}, + {0x67, {0x00, 0x3C, 0x00, 0x00, 0x00, 0x01, 0x54, 0x10, 0x32, 0x98}, 10}, + {0x74, {0x10, 0x85, 0x80, 0x00, 0x00, 0x4E, 0x00}, 7}, + {0x98, {0x3e, 0x07}, 2}, + {Cmd_TEON, {0}, 0}, // Tearing effect line on + {0, {0}, 0xff}, // END }; -// This function is called (in irq context!) just before a transmission starts. It will -// set the D/C line to the value indicated in the tx's dc field. -static IRAM_ATTR void flow3r_bsp_gc9a01_pre_transfer_callback(spi_transaction_t *t) -{ - flow3r_bsp_gc9a01_tx_t *tx = (flow3r_bsp_gc9a01_tx_t *)t->user; +// This function is called (in irq context!) just before a transmission starts. +// It will set the D/C line to the value indicated in the tx's dc field. +static IRAM_ATTR void flow3r_bsp_gc9a01_pre_transfer_callback( + spi_transaction_t *t) { + flow3r_bsp_gc9a01_tx_t *tx = (flow3r_bsp_gc9a01_tx_t *)t->user; gpio_set_level(tx->gc9a01->config->pin_dc, tx->dc); } @@ -177,25 +184,24 @@ static IRAM_ATTR void flow3r_bsp_gc9a01_pre_transfer_callback(spi_transaction_t * mode for higher speed. The overhead of interrupt transactions is more than * just waiting for the transaction to complete. */ -esp_err_t flow3r_bsp_gc9a01_cmd_sync(flow3r_bsp_gc9a01_t *gc9a01, uint8_t cmd) -{ +esp_err_t flow3r_bsp_gc9a01_cmd_sync(flow3r_bsp_gc9a01_t *gc9a01, uint8_t cmd) { spi_transaction_t t; memset(&t, 0, sizeof(t)); t.length = 8; t.tx_buffer = &cmd; - // As we're running a synchronous transaction, we can allocate the TX object - // on the stack, as this frame is guaranteed to be valid until the - // transaction completes. - flow3r_bsp_gc9a01_tx_t tx = { - .gc9a01 = gc9a01, - .dc = 0, - }; + // As we're running a synchronous transaction, we can allocate the TX object + // on the stack, as this frame is guaranteed to be valid until the + // transaction completes. + flow3r_bsp_gc9a01_tx_t tx = { + .gc9a01 = gc9a01, + .dc = 0, + }; - t.user = (void*)&tx; + t.user = (void *)&tx; esp_err_t res = spi_device_polling_transmit(gc9a01->spi, &t); - return res; + return res; } /* Send data to the LCD. Uses spi_device_polling_transmit, which waits until the @@ -205,401 +211,422 @@ esp_err_t flow3r_bsp_gc9a01_cmd_sync(flow3r_bsp_gc9a01_t *gc9a01, uint8_t cmd) * mode for higher speed. The overhead of interrupt transactions is more than * just waiting for the transaction to complete. */ -esp_err_t flow3r_bsp_gc9a01_data_sync(flow3r_bsp_gc9a01_t *gc9a01, const uint8_t *data, int len) { +esp_err_t flow3r_bsp_gc9a01_data_sync(flow3r_bsp_gc9a01_t *gc9a01, + const uint8_t *data, int len) { if (len == 0) { - return ESP_OK; - } - - // As we're running a synchronous transaction, we can allocate the TX object - // on the stack, as this frame is guaranteed to be valid until the - // transaction completes. - flow3r_bsp_gc9a01_tx_t tx = { - .gc9a01 = gc9a01, - // DC == 1 for data. - .dc = 1, - }; + return ESP_OK; + } + + // As we're running a synchronous transaction, we can allocate the TX object + // on the stack, as this frame is guaranteed to be valid until the + // transaction completes. + flow3r_bsp_gc9a01_tx_t tx = { + .gc9a01 = gc9a01, + // DC == 1 for data. + .dc = 1, + }; /* - On certain MC's the max SPI DMA transfer length might be smaller than the buffer. We then have to split the transmissions. + On certain MC's the max SPI DMA transfer length might be smaller than the + buffer. We then have to split the transmissions. */ int offset = 0; do { spi_transaction_t t; - memset(&t, 0, sizeof(t)); //Zero out the transaction + memset(&t, 0, sizeof(t)); // Zero out the transaction - int tx_len = ((len - offset) < SPI_MAX_DMA_LEN) ? (len - offset) : SPI_MAX_DMA_LEN; - // Len is in bytes, transaction length is in bits. + int tx_len = ((len - offset) < SPI_MAX_DMA_LEN) ? (len - offset) + : SPI_MAX_DMA_LEN; + // Len is in bytes, transaction length is in bits. t.length = tx_len * 8; t.tx_buffer = data + offset; - t.user = (void*)&tx; + t.user = (void *)&tx; - // Transmit! + // Transmit! esp_err_t ret = spi_device_polling_transmit(gc9a01->spi, &t); - if (ret != ESP_OK) { - return ret; - } + if (ret != ESP_OK) { + return ret; + } offset += tx_len; - } - while (offset < len); + } while (offset < len); - return ESP_OK; + return ESP_OK; } -esp_err_t flow3r_bsp_gc9a01_data_byte_sync(flow3r_bsp_gc9a01_t *gc9a01, const uint8_t data) { - return flow3r_bsp_gc9a01_data_sync(gc9a01, &data, 1); +esp_err_t flow3r_bsp_gc9a01_data_byte_sync(flow3r_bsp_gc9a01_t *gc9a01, + const uint8_t data) { + return flow3r_bsp_gc9a01_data_sync(gc9a01, &data, 1); } -static esp_err_t flow3r_bsp_gc9a01_mem_access_mode_set(flow3r_bsp_gc9a01_t *gc9a01, uint8_t rotation, uint8_t vert_mirror, uint8_t horiz_mirror, uint8_t is_bgr) { - uint8_t val = 0; - rotation &= 7; - - switch (rotation) { - case 0: - val = 0; - break; - case 1: - val = MADCTL_MX; - break; - case 2: - val = MADCTL_MY; - break; - case 3: - val = MADCTL_MX | MADCTL_MY; - break; - case 4: - val = MADCTL_MV; - break; - case 5: - val = MADCTL_MV | MADCTL_MX; - break; - case 6: - val = MADCTL_MV | MADCTL_MY; - break; - case 7: - val = MADCTL_MV | MADCTL_MX | MADCTL_MY; - break; - } - - if (vert_mirror) - val = MADCTL_ML; - if (horiz_mirror) - val = MADCTL_MH; - - if (is_bgr) - val |= MADCTL_BGR; - - esp_err_t ret = flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_MADCTL); - if (ret != ESP_OK) { - return ret; - } - return flow3r_bsp_gc9a01_data_byte_sync(gc9a01, val); +static esp_err_t flow3r_bsp_gc9a01_mem_access_mode_set( + flow3r_bsp_gc9a01_t *gc9a01, uint8_t rotation, uint8_t vert_mirror, + uint8_t horiz_mirror, uint8_t is_bgr) { + uint8_t val = 0; + rotation &= 7; + + switch (rotation) { + case 0: + val = 0; + break; + case 1: + val = MADCTL_MX; + break; + case 2: + val = MADCTL_MY; + break; + case 3: + val = MADCTL_MX | MADCTL_MY; + break; + case 4: + val = MADCTL_MV; + break; + case 5: + val = MADCTL_MV | MADCTL_MX; + break; + case 6: + val = MADCTL_MV | MADCTL_MY; + break; + case 7: + val = MADCTL_MV | MADCTL_MX | MADCTL_MY; + break; + } + + if (vert_mirror) val = MADCTL_ML; + if (horiz_mirror) val = MADCTL_MH; + + if (is_bgr) val |= MADCTL_BGR; + + esp_err_t ret = flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_MADCTL); + if (ret != ESP_OK) { + return ret; + } + return flow3r_bsp_gc9a01_data_byte_sync(gc9a01, val); } -static esp_err_t flow3r_bsp_gc9a01_color_mode_set(flow3r_bsp_gc9a01_t *gc9a01, uint8_t color_mode) { - esp_err_t ret = flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_COLMOD); - if (ret != ESP_OK) { - return ret; - } - return flow3r_bsp_gc9a01_data_byte_sync(gc9a01, color_mode & 0x77); +static esp_err_t flow3r_bsp_gc9a01_color_mode_set(flow3r_bsp_gc9a01_t *gc9a01, + uint8_t color_mode) { + esp_err_t ret = flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_COLMOD); + if (ret != ESP_OK) { + return ret; + } + return flow3r_bsp_gc9a01_data_byte_sync(gc9a01, color_mode & 0x77); } -static esp_err_t flow3r_bsp_gc9a01_inversion_mode_set(flow3r_bsp_gc9a01_t *gc9a01, uint8_t mode) { - if (mode) - return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_INVON); - else - return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_INVOFF); +static esp_err_t flow3r_bsp_gc9a01_inversion_mode_set( + flow3r_bsp_gc9a01_t *gc9a01, uint8_t mode) { + if (mode) + return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_INVON); + else + return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_INVOFF); } -static esp_err_t flow3r_bsp_gc9a01_sleep_mode_set(flow3r_bsp_gc9a01_t *gc9a01, uint8_t mode) { - if (mode) - return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_SLPIN); - else - return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_SLPOUT); - vTaskDelay(500 / portTICK_PERIOD_MS); +static esp_err_t flow3r_bsp_gc9a01_sleep_mode_set(flow3r_bsp_gc9a01_t *gc9a01, + uint8_t mode) { + if (mode) + return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_SLPIN); + else + return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_SLPOUT); + vTaskDelay(500 / portTICK_PERIOD_MS); } -static esp_err_t flow3r_bsp_gc9a01_power_set(flow3r_bsp_gc9a01_t *gc9a01, uint8_t mode) { - if (mode) - return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_DISPON); - else - return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_DISPOFF); +static esp_err_t flow3r_bsp_gc9a01_power_set(flow3r_bsp_gc9a01_t *gc9a01, + uint8_t mode) { + if (mode) + return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_DISPON); + else + return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_DISPOFF); } -static esp_err_t flow3r_bsp_gc9a01_column_set(flow3r_bsp_gc9a01_t *gc9a01, uint16_t start, uint16_t end) { - esp_err_t ret; - ret = flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_CASET); - if (ret != ESP_OK) { - return ret; - } - ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, start >> 8); - if (ret != ESP_OK) { - return ret; - } - ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, start & 0xFF); - if (ret != ESP_OK) { - return ret; - } - ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, end >> 8); - if (ret != ESP_OK) { - return ret; - } - return flow3r_bsp_gc9a01_data_byte_sync(gc9a01, end & 0xFF); +static esp_err_t flow3r_bsp_gc9a01_column_set(flow3r_bsp_gc9a01_t *gc9a01, + uint16_t start, uint16_t end) { + esp_err_t ret; + ret = flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_CASET); + if (ret != ESP_OK) { + return ret; + } + ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, start >> 8); + if (ret != ESP_OK) { + return ret; + } + ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, start & 0xFF); + if (ret != ESP_OK) { + return ret; + } + ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, end >> 8); + if (ret != ESP_OK) { + return ret; + } + return flow3r_bsp_gc9a01_data_byte_sync(gc9a01, end & 0xFF); } -static esp_err_t flow3r_bsp_gc9a01_row_set(flow3r_bsp_gc9a01_t *gc9a01, uint16_t start, uint16_t end) { - esp_err_t ret; - ret = flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_RASET); - if (ret != ESP_OK) { - return ret; - } - ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, start >> 8); - if (ret != ESP_OK) { - return ret; - } - ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, start & 0xFF); - if (ret != ESP_OK) { - return ret; - } - ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, end >> 8); - if (ret != ESP_OK) { - return ret; - } - return flow3r_bsp_gc9a01_data_byte_sync(gc9a01, end & 0xFF); +static esp_err_t flow3r_bsp_gc9a01_row_set(flow3r_bsp_gc9a01_t *gc9a01, + uint16_t start, uint16_t end) { + esp_err_t ret; + ret = flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_RASET); + if (ret != ESP_OK) { + return ret; + } + ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, start >> 8); + if (ret != ESP_OK) { + return ret; + } + ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, start & 0xFF); + if (ret != ESP_OK) { + return ret; + } + ret = flow3r_bsp_gc9a01_data_byte_sync(gc9a01, end >> 8); + if (ret != ESP_OK) { + return ret; + } + return flow3r_bsp_gc9a01_data_byte_sync(gc9a01, end & 0xFF); } -esp_err_t flow3r_bsp_gc9a01_backlight_set(flow3r_bsp_gc9a01_t *gc9a01, uint8_t value) { - if (gc9a01->config->backlight_used == 0) { - return ESP_OK; - } - if (value > 100) { - value = 100; - } - - uint16_t max_duty = (1 << (int)gc9a01->bl_timer_config.duty_resolution) - 1; - uint16_t duty; - if (value >= 100) { - duty = max_duty; - } else { - duty = value * (max_duty / (float)100); - } - - gc9a01->bl_channel_config.duty = duty; - return ledc_channel_config(&gc9a01->bl_channel_config); +esp_err_t flow3r_bsp_gc9a01_backlight_set(flow3r_bsp_gc9a01_t *gc9a01, + uint8_t value) { + if (gc9a01->config->backlight_used == 0) { + return ESP_OK; + } + if (value > 100) { + value = 100; + } + + uint16_t max_duty = (1 << (int)gc9a01->bl_timer_config.duty_resolution) - 1; + uint16_t duty; + if (value >= 100) { + duty = max_duty; + } else { + duty = value * (max_duty / (float)100); + } + + gc9a01->bl_channel_config.duty = duty; + return ledc_channel_config(&gc9a01->bl_channel_config); } -esp_err_t flow3r_bsp_gc9a01_init(flow3r_bsp_gc9a01_t *gc9a01, flow3r_bsp_gc9a01_config_t *config) { - memset(gc9a01, 0, sizeof(flow3r_bsp_gc9a01_t)); - gc9a01->config = config; - - // Configure DC pin. - gpio_config_t gpiocfg = { - .pin_bit_mask = ((uint64_t)1UL << gc9a01->config->pin_dc), - .mode = GPIO_MODE_OUTPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, - .pull_down_en = GPIO_PULLDOWN_DISABLE, - .intr_type = GPIO_INTR_DISABLE, - }; - esp_err_t res = gpio_config(&gpiocfg); - if (res != ESP_OK) { - return res; - } - gpio_set_level(gc9a01->config->pin_dc, 0); - - // Configure Reset pin if used. - if (gc9a01->config->reset_used) { - gpiocfg.pin_bit_mask |= ((uint64_t)1UL << gc9a01->config->pin_rst); - res = gpio_config(&gpiocfg); - if (res != ESP_OK) { - return res; - } - gpio_set_level(gc9a01->config->pin_rst, 1); - } - - // Configure SPI bus. - // - // TODO(q3k): don't do this here, do this higher up in the BSP. Even if - // nothing else sits on this bus. +esp_err_t flow3r_bsp_gc9a01_init(flow3r_bsp_gc9a01_t *gc9a01, + flow3r_bsp_gc9a01_config_t *config) { + memset(gc9a01, 0, sizeof(flow3r_bsp_gc9a01_t)); + gc9a01->config = config; + + // Configure DC pin. + gpio_config_t gpiocfg = { + .pin_bit_mask = ((uint64_t)1UL << gc9a01->config->pin_dc), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + esp_err_t res = gpio_config(&gpiocfg); + if (res != ESP_OK) { + return res; + } + gpio_set_level(gc9a01->config->pin_dc, 0); + + // Configure Reset pin if used. + if (gc9a01->config->reset_used) { + gpiocfg.pin_bit_mask |= ((uint64_t)1UL << gc9a01->config->pin_rst); + res = gpio_config(&gpiocfg); + if (res != ESP_OK) { + return res; + } + gpio_set_level(gc9a01->config->pin_rst, 1); + } + + // Configure SPI bus. + // + // TODO(q3k): don't do this here, do this higher up in the BSP. Even if + // nothing else sits on this bus. spi_bus_config_t buscfg = { .mosi_io_num = gc9a01->config->pin_mosi, .miso_io_num = GPIO_NUM_NC, .sclk_io_num = gc9a01->config->pin_sck, .quadwp_io_num = GPIO_NUM_NC, .quadhd_io_num = GPIO_NUM_NC, - .max_transfer_sz = 250*250*2, + .max_transfer_sz = 250 * 250 * 2, }; - // Configure SPI device on bus. + // Configure SPI device on bus. spi_device_interface_config_t devcfg = { - .clock_speed_hz = 80*1000*1000, + .clock_speed_hz = 80 * 1000 * 1000, .mode = 0, .spics_io_num = gc9a01->config->pin_cs, .queue_size = 7, .pre_cb = flow3r_bsp_gc9a01_pre_transfer_callback, }; - - esp_err_t ret = spi_bus_initialize(gc9a01->config->host, &buscfg, SPI_DMA_CH_AUTO); - if (ret != ESP_OK) { - return ret; - } + + esp_err_t ret = + spi_bus_initialize(gc9a01->config->host, &buscfg, SPI_DMA_CH_AUTO); + if (ret != ESP_OK) { + return ret; + } ret = spi_bus_add_device(gc9a01->config->host, &devcfg, &gc9a01->spi); - if (ret != ESP_OK) { - goto cleanup_spi_bus; - } - - // Configure backlight timer/channel if used. - if (gc9a01->config->backlight_used) { - gc9a01->bl_timer_config.speed_mode = LEDC_LOW_SPEED_MODE; - gc9a01->bl_timer_config.duty_resolution = LEDC_TIMER_8_BIT; - gc9a01->bl_timer_config.timer_num = LEDC_TIMER_0; - gc9a01->bl_timer_config.freq_hz=1000; - gc9a01->bl_timer_config.clk_cfg=LEDC_AUTO_CLK; - ret = ledc_timer_config(&gc9a01->bl_timer_config); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - - gc9a01->bl_channel_config.gpio_num = gc9a01->config->pin_backlight; - gc9a01->bl_channel_config.speed_mode = LEDC_LOW_SPEED_MODE; - gc9a01->bl_channel_config.channel = LEDC_CHANNEL_0; - gc9a01->bl_channel_config.intr_type = LEDC_INTR_DISABLE; - gc9a01->bl_channel_config.timer_sel = LEDC_TIMER_0; - gc9a01->bl_channel_config.duty = 0; - gc9a01->bl_channel_config.hpoint = 0; - ret = ledc_channel_config(&gc9a01->bl_channel_config); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - } - - // Issue reset if used. - if (gc9a01->config->reset_used) { - gpio_set_level(gc9a01->config->pin_rst, 0); - vTaskDelay(10 / portTICK_PERIOD_MS); - gpio_set_level(gc9a01->config->pin_rst, 1); - vTaskDelay(150 / portTICK_PERIOD_MS); - } - - // Send initialization commands. - int ix = 0; - while (flow3r_bsp_gc9a01_init_cmds[ix].databytes != 0xff) { - const flow3r_bsp_gc9a01_init_cmd_t *cmd = &flow3r_bsp_gc9a01_init_cmds[ix]; + if (ret != ESP_OK) { + goto cleanup_spi_bus; + } + + // Configure backlight timer/channel if used. + if (gc9a01->config->backlight_used) { + gc9a01->bl_timer_config.speed_mode = LEDC_LOW_SPEED_MODE; + gc9a01->bl_timer_config.duty_resolution = LEDC_TIMER_8_BIT; + gc9a01->bl_timer_config.timer_num = LEDC_TIMER_0; + gc9a01->bl_timer_config.freq_hz = 1000; + gc9a01->bl_timer_config.clk_cfg = LEDC_AUTO_CLK; + ret = ledc_timer_config(&gc9a01->bl_timer_config); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + + gc9a01->bl_channel_config.gpio_num = gc9a01->config->pin_backlight; + gc9a01->bl_channel_config.speed_mode = LEDC_LOW_SPEED_MODE; + gc9a01->bl_channel_config.channel = LEDC_CHANNEL_0; + gc9a01->bl_channel_config.intr_type = LEDC_INTR_DISABLE; + gc9a01->bl_channel_config.timer_sel = LEDC_TIMER_0; + gc9a01->bl_channel_config.duty = 0; + gc9a01->bl_channel_config.hpoint = 0; + ret = ledc_channel_config(&gc9a01->bl_channel_config); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + } + + // Issue reset if used. + if (gc9a01->config->reset_used) { + gpio_set_level(gc9a01->config->pin_rst, 0); + vTaskDelay(10 / portTICK_PERIOD_MS); + gpio_set_level(gc9a01->config->pin_rst, 1); + vTaskDelay(150 / portTICK_PERIOD_MS); + } + + // Send initialization commands. + int ix = 0; + while (flow3r_bsp_gc9a01_init_cmds[ix].databytes != 0xff) { + const flow3r_bsp_gc9a01_init_cmd_t *cmd = + &flow3r_bsp_gc9a01_init_cmds[ix]; flow3r_bsp_gc9a01_cmd_sync(gc9a01, cmd->cmd); flow3r_bsp_gc9a01_data_sync(gc9a01, cmd->data, cmd->databytes & 0x1F); if (cmd->databytes & 0x80) { - vTaskDelay(100 / portTICK_PERIOD_MS); + vTaskDelay(100 / portTICK_PERIOD_MS); } ix++; - } - - ret = flow3r_bsp_gc9a01_mem_access_mode_set(gc9a01, 0, 0, 0, 1); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - ret = flow3r_bsp_gc9a01_color_mode_set(gc9a01, ColorMode_MCU_16bit); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - ret = flow3r_bsp_gc9a01_inversion_mode_set(gc9a01, 1); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - ret = flow3r_bsp_gc9a01_sleep_mode_set(gc9a01, 0); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - vTaskDelay(120 / portTICK_PERIOD_MS); - - ret = flow3r_bsp_gc9a01_power_set(gc9a01, 1); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - vTaskDelay(20 / portTICK_PERIOD_MS); - - // We always write the entire framebuffer at once. - ret = flow3r_bsp_gc9a01_column_set(gc9a01, 0, 239); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - ret = flow3r_bsp_gc9a01_row_set(gc9a01, 0, 239); - if (ret != ESP_OK) { - goto cleanup_spi_device; - } - - return ret; + } + + ret = flow3r_bsp_gc9a01_mem_access_mode_set(gc9a01, 0, 0, 0, 1); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + ret = flow3r_bsp_gc9a01_color_mode_set(gc9a01, ColorMode_MCU_16bit); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + ret = flow3r_bsp_gc9a01_inversion_mode_set(gc9a01, 1); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + ret = flow3r_bsp_gc9a01_sleep_mode_set(gc9a01, 0); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + vTaskDelay(120 / portTICK_PERIOD_MS); + + ret = flow3r_bsp_gc9a01_power_set(gc9a01, 1); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + vTaskDelay(20 / portTICK_PERIOD_MS); + + // We always write the entire framebuffer at once. + ret = flow3r_bsp_gc9a01_column_set(gc9a01, 0, 239); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + ret = flow3r_bsp_gc9a01_row_set(gc9a01, 0, 239); + if (ret != ESP_OK) { + goto cleanup_spi_device; + } + + return ret; cleanup_spi_device: - spi_bus_remove_device(gc9a01->spi); + spi_bus_remove_device(gc9a01->spi); cleanup_spi_bus: - spi_bus_free(gc9a01->config->host); - return ret; + spi_bus_free(gc9a01->config->host); + return ret; } static esp_err_t flow3r_bsp_gc9a01_blit_next(flow3r_bsp_gc9a01_blit_t *blit) { - size_t size = blit->left; - if (size > SPI_MAX_DMA_LEN) { - size = SPI_MAX_DMA_LEN; - } - - blit->gc9a01_tx.gc9a01 = blit->gc9a01; - blit->gc9a01_tx.dc = 1; - - // Memzero spi_tx as it gets written by the SPI driver after each - // transaction. - memset(&blit->spi_tx, 0, sizeof(spi_transaction_t)); - blit->spi_tx.length = size * 8; - blit->spi_tx.tx_buffer = blit->fb; - blit->spi_tx.user = &blit->gc9a01_tx; - - blit->left -= size; - blit->fb += size; - - esp_err_t res = spi_device_queue_trans(blit->gc9a01->spi, &blit->spi_tx, portMAX_DELAY); - if (res != ESP_OK) { - - ESP_LOGE(TAG, "spi_device_queue_trans (size %d, buf %p, dma capab: %d, largest block: %d): %s", - size, blit->fb, esp_ptr_dma_capable(blit->fb), heap_caps_get_largest_free_block(MALLOC_CAP_DMA), esp_err_to_name(res)); - } - return res; + size_t size = blit->left; + if (size > SPI_MAX_DMA_LEN) { + size = SPI_MAX_DMA_LEN; + } + + blit->gc9a01_tx.gc9a01 = blit->gc9a01; + blit->gc9a01_tx.dc = 1; + + // Memzero spi_tx as it gets written by the SPI driver after each + // transaction. + memset(&blit->spi_tx, 0, sizeof(spi_transaction_t)); + blit->spi_tx.length = size * 8; + blit->spi_tx.tx_buffer = blit->fb; + blit->spi_tx.user = &blit->gc9a01_tx; + + blit->left -= size; + blit->fb += size; + + esp_err_t res = + spi_device_queue_trans(blit->gc9a01->spi, &blit->spi_tx, portMAX_DELAY); + if (res != ESP_OK) { + ESP_LOGE(TAG, + "spi_device_queue_trans (size %d, buf %p, dma capab: %d, " + "largest block: %d): %s", + size, blit->fb, esp_ptr_dma_capable(blit->fb), + heap_caps_get_largest_free_block(MALLOC_CAP_DMA), + esp_err_to_name(res)); + } + return res; } -static esp_err_t flow3r_bsp_gc9a01_blit_start(flow3r_bsp_gc9a01_t *gc9a01, flow3r_bsp_gc9a01_blit_t *blit, const uint16_t *fb) { - memset(blit, 0, sizeof(flow3r_bsp_gc9a01_blit_t)); +static esp_err_t flow3r_bsp_gc9a01_blit_start(flow3r_bsp_gc9a01_t *gc9a01, + flow3r_bsp_gc9a01_blit_t *blit, + const uint16_t *fb) { + memset(blit, 0, sizeof(flow3r_bsp_gc9a01_blit_t)); - blit->gc9a01 = gc9a01; - blit->fb = (const uint8_t *)fb; - blit->left = 2 * 240 * 240; + blit->gc9a01 = gc9a01; + blit->fb = (const uint8_t *)fb; + blit->left = 2 * 240 * 240; - return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_RAMWR); + return flow3r_bsp_gc9a01_cmd_sync(gc9a01, Cmd_RAMWR); } static uint8_t flow3r_bsp_gc9a01_blit_done(flow3r_bsp_gc9a01_blit_t *blit) { - return blit->left == 0; + return blit->left == 0; } -static esp_err_t flow3r_bsp_gc9a01_blit_wait_done(flow3r_bsp_gc9a01_blit_t *blit, TickType_t ticks_to_wait) { - spi_transaction_t *tx_done; - esp_err_t ret = spi_device_get_trans_result(blit->gc9a01->spi, &tx_done, ticks_to_wait); - // TODO(q3k): validate that tx_done corresponds to the spi_transaction_tx in - // bilt. If not, this means we got some sequencing failure to deal with. - return ret; +static esp_err_t flow3r_bsp_gc9a01_blit_wait_done( + flow3r_bsp_gc9a01_blit_t *blit, TickType_t ticks_to_wait) { + spi_transaction_t *tx_done; + esp_err_t ret = + spi_device_get_trans_result(blit->gc9a01->spi, &tx_done, ticks_to_wait); + // TODO(q3k): validate that tx_done corresponds to the spi_transaction_tx in + // bilt. If not, this means we got some sequencing failure to deal with. + return ret; } -esp_err_t flow3r_bsp_gc9a01_blit_full(flow3r_bsp_gc9a01_t *gc9a01, const uint16_t *fb) { - flow3r_bsp_gc9a01_blit_t blit; - esp_err_t res = flow3r_bsp_gc9a01_blit_start(gc9a01, &blit, fb); - if (res != ESP_OK) { - return res; - } - - while (!flow3r_bsp_gc9a01_blit_done(&blit)) { - res = flow3r_bsp_gc9a01_blit_next(&blit); - if (res != ESP_OK) { - return res; - } - res = flow3r_bsp_gc9a01_blit_wait_done(&blit, portMAX_DELAY); - if (res != ESP_OK) { - return res; - } - } - return ESP_OK; +esp_err_t flow3r_bsp_gc9a01_blit_full(flow3r_bsp_gc9a01_t *gc9a01, + const uint16_t *fb) { + flow3r_bsp_gc9a01_blit_t blit; + esp_err_t res = flow3r_bsp_gc9a01_blit_start(gc9a01, &blit, fb); + if (res != ESP_OK) { + return res; + } + + while (!flow3r_bsp_gc9a01_blit_done(&blit)) { + res = flow3r_bsp_gc9a01_blit_next(&blit); + if (res != ESP_OK) { + return res; + } + res = flow3r_bsp_gc9a01_blit_wait_done(&blit, portMAX_DELAY); + if (res != ESP_OK) { + return res; + } + } + return ESP_OK; } diff --git a/components/flow3r_bsp/flow3r_bsp_gc9a01.h b/components/flow3r_bsp/flow3r_bsp_gc9a01.h index 3822568371..a125431d02 100644 --- a/components/flow3r_bsp/flow3r_bsp_gc9a01.h +++ b/components/flow3r_bsp/flow3r_bsp_gc9a01.h @@ -2,45 +2,45 @@ #include <stdint.h> -#include "driver/spi_master.h" #include "driver/ledc.h" +#include "driver/spi_master.h" // Configuration structure for display. // // The display expects to be the only device on an SPI bus. typedef struct { - // Boolean: whether the display as a reset pin connected. - uint8_t reset_used; - // Boolean: whether the display as backlight control connected to the ESP - // LED Control peripheral. - uint8_t backlight_used; + // Boolean: whether the display as a reset pin connected. + uint8_t reset_used; + // Boolean: whether the display as backlight control connected to the ESP + // LED Control peripheral. + uint8_t backlight_used; - // Reset pin, if reset_used. - uint8_t pin_rst; - // SPI SCK pin. - uint8_t pin_sck; - // SPI MOSI pin. - uint8_t pin_mosi; - // SPI CS pin. - uint8_t pin_cs; - // Data/Command pin. - uint8_t pin_dc; - // Backlight control pin, if backlight_used. - uint8_t pin_backlight; + // Reset pin, if reset_used. + uint8_t pin_rst; + // SPI SCK pin. + uint8_t pin_sck; + // SPI MOSI pin. + uint8_t pin_mosi; + // SPI CS pin. + uint8_t pin_cs; + // Data/Command pin. + uint8_t pin_dc; + // Backlight control pin, if backlight_used. + uint8_t pin_backlight; - // Nubmer of SPI host device (ie. bus) that the display will use. - spi_host_device_t host; + // Nubmer of SPI host device (ie. bus) that the display will use. + spi_host_device_t host; } flow3r_bsp_gc9a01_config_t; typedef struct { - const flow3r_bsp_gc9a01_config_t *config; + const flow3r_bsp_gc9a01_config_t *config; - // Allocatged SPI device handle on configured bus. - spi_device_handle_t spi; + // Allocatged SPI device handle on configured bus. + spi_device_handle_t spi; - // Only if using backlight. - ledc_channel_config_t bl_channel_config; - ledc_timer_config_t bl_timer_config; + // Only if using backlight. + ledc_channel_config_t bl_channel_config; + ledc_timer_config_t bl_timer_config; } flow3r_bsp_gc9a01_t; @@ -55,7 +55,8 @@ typedef struct { // // An error will be returned if initialization failed. Initialization can be // re-tried. -esp_err_t flow3r_bsp_gc9a01_init(flow3r_bsp_gc9a01_t *gc9a01, flow3r_bsp_gc9a01_config_t *config); +esp_err_t flow3r_bsp_gc9a01_init(flow3r_bsp_gc9a01_t *gc9a01, + flow3r_bsp_gc9a01_config_t *config); // Send a full-sized framebuffer to the display using interrupts/DMA, blocking // FreeRTOS task until done. @@ -63,7 +64,9 @@ esp_err_t flow3r_bsp_gc9a01_init(flow3r_bsp_gc9a01_t *gc9a01, flow3r_bsp_gc9a01_ // This must not be called if another blit is being performed. The user code // should sequence access and make sure not more than one blit is performed // simultaneously. -esp_err_t flow3r_bsp_gc9a01_blit_full(flow3r_bsp_gc9a01_t *gc9a01, const uint16_t *fb); +esp_err_t flow3r_bsp_gc9a01_blit_full(flow3r_bsp_gc9a01_t *gc9a01, + const uint16_t *fb); // Set backlight for display, using integer percent value (0-100, clamped). -esp_err_t flow3r_bsp_gc9a01_backlight_set(flow3r_bsp_gc9a01_t *gc9a01, uint8_t value); \ No newline at end of file +esp_err_t flow3r_bsp_gc9a01_backlight_set(flow3r_bsp_gc9a01_t *gc9a01, + uint8_t value); \ No newline at end of file diff --git a/components/flow3r_bsp/flow3r_bsp_i2c.c b/components/flow3r_bsp/flow3r_bsp_i2c.c index c8c879ed24..c351f66106 100644 --- a/components/flow3r_bsp/flow3r_bsp_i2c.c +++ b/components/flow3r_bsp/flow3r_bsp_i2c.c @@ -1,9 +1,9 @@ #include "flow3r_bsp_i2c.h" #include "driver/i2c.h" +#include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" -#include "esp_log.h" #include <string.h> @@ -13,24 +13,24 @@ static const char *TAG = "flow3r-bsp-i2c"; #if defined(CONFIG_FLOW3R_HW_GEN_P3) const flow3r_i2c_addressdef flow3r_i2c_addresses = { - .codec = 0x10, - .touch_top = 0x2d, - .touch_bottom = 0x2c, - .portexp = { 0x6e, 0x6d }, + .codec = 0x10, + .touch_top = 0x2d, + .touch_bottom = 0x2c, + .portexp = {0x6e, 0x6d}, }; #elif defined(CONFIG_FLOW3R_HW_GEN_P4) const flow3r_i2c_addressdef flow3r_i2c_addresses = { - .codec = 0x10, - .touch_top = 0x2c, - .touch_bottom = 0x2d, - .portexp = { 0x6e, 0x6d }, + .codec = 0x10, + .touch_top = 0x2c, + .touch_bottom = 0x2d, + .portexp = {0x6e, 0x6d}, }; #elif defined(CONFIG_FLOW3R_HW_GEN_P6) const flow3r_i2c_addressdef flow3r_i2c_addresses = { - .codec = 0x10, - .touch_top = 0x2c, - .touch_bottom = 0x2d, - .portexp = { 0x6e, 0x6d }, + .codec = 0x10, + .touch_top = 0x2c, + .touch_bottom = 0x2d, + .portexp = {0x6e, 0x6d}, }; #else #error "i2c not implemented for this badge generation" @@ -46,63 +46,69 @@ static i2c_config_t i2c_conf = { }; void flow3r_bsp_i2c_init(void) { - if (mutex != NULL) { - return; - } + if (mutex != NULL) { + return; + } - mutex = xSemaphoreCreateMutex(); - assert(mutex != NULL); + mutex = xSemaphoreCreateMutex(); + assert(mutex != NULL); assert(i2c_param_config(I2C_NUM_0, &i2c_conf) == ESP_OK); - assert(i2c_driver_install(I2C_NUM_0, i2c_conf.mode, 0, 0, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED) == ESP_OK); + assert(i2c_driver_install(I2C_NUM_0, i2c_conf.mode, 0, 0, + ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED) == + ESP_OK); - flow3r_bsp_i2c_scan(NULL); + flow3r_bsp_i2c_scan(NULL); } // Take I2C bus lock. -static void flow3r_bsp_i2c_get(void) { - xSemaphoreTake(mutex, portMAX_DELAY); -} +static void flow3r_bsp_i2c_get(void) { xSemaphoreTake(mutex, portMAX_DELAY); } // Release I2C bus lock. -static void flow3r_bsp_i2c_put(void) { - xSemaphoreGive(mutex); -} +static void flow3r_bsp_i2c_put(void) { xSemaphoreGive(mutex); } -esp_err_t flow3r_bsp_i2c_write_to_device(uint8_t address, const uint8_t *buffer, size_t write_size, TickType_t ticks_to_wait) { - flow3r_bsp_i2c_get(); - esp_err_t res = i2c_master_write_to_device(I2C_NUM_0, address, buffer, write_size, ticks_to_wait); - flow3r_bsp_i2c_put(); - return res; +esp_err_t flow3r_bsp_i2c_write_to_device(uint8_t address, const uint8_t *buffer, + size_t write_size, + TickType_t ticks_to_wait) { + flow3r_bsp_i2c_get(); + esp_err_t res = i2c_master_write_to_device(I2C_NUM_0, address, buffer, + write_size, ticks_to_wait); + flow3r_bsp_i2c_put(); + return res; } -esp_err_t flow3r_bsp_i2c_write_read_device(uint8_t address, const uint8_t *write_buffer, size_t write_size, uint8_t *read_buffer, size_t read_size, TickType_t ticks_to_wait) { - flow3r_bsp_i2c_get(); - esp_err_t res = i2c_master_write_read_device(I2C_NUM_0, address, write_buffer, write_size, read_buffer, read_size, ticks_to_wait); - flow3r_bsp_i2c_put(); - return res; +esp_err_t flow3r_bsp_i2c_write_read_device( + uint8_t address, const uint8_t *write_buffer, size_t write_size, + uint8_t *read_buffer, size_t read_size, TickType_t ticks_to_wait) { + flow3r_bsp_i2c_get(); + esp_err_t res = i2c_master_write_read_device( + I2C_NUM_0, address, write_buffer, write_size, read_buffer, read_size, + ticks_to_wait); + flow3r_bsp_i2c_put(); + return res; } void flow3r_bsp_i2c_scan(flow3r_bsp_i2c_scan_result_t *res) { - if (res != NULL) { - memset(res, 0, sizeof(flow3r_bsp_i2c_scan_result_t)); - } - ESP_LOGI(TAG, "Scan: starting..."); - for (uint8_t i = 1; i < 127; i++) { - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); + if (res != NULL) { + memset(res, 0, sizeof(flow3r_bsp_i2c_scan_result_t)); + } + ESP_LOGI(TAG, "Scan: starting..."); + for (uint8_t i = 1; i < 127; i++) { + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1); - i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_PERIOD_MS); + i2c_master_stop(cmd); + esp_err_t ret = + i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_PERIOD_MS); i2c_cmd_link_delete(cmd); - if (ret == ESP_OK) { - ESP_LOGI(TAG, "Scan: detected %02x", i); - if (res != NULL) { - size_t ix = i / 32; - size_t offs = i % 32; - res->res[ix] |= (1 << offs); - } - } - } - ESP_LOGI(TAG, "Scan: done."); + if (ret == ESP_OK) { + ESP_LOGI(TAG, "Scan: detected %02x", i); + if (res != NULL) { + size_t ix = i / 32; + size_t offs = i % 32; + res->res[ix] |= (1 << offs); + } + } + } + ESP_LOGI(TAG, "Scan: done."); } diff --git a/components/flow3r_bsp/flow3r_bsp_i2c.h b/components/flow3r_bsp/flow3r_bsp_i2c.h index 37d0c3b845..2505d156ee 100644 --- a/components/flow3r_bsp/flow3r_bsp_i2c.h +++ b/components/flow3r_bsp/flow3r_bsp_i2c.h @@ -9,14 +9,14 @@ typedef uint8_t flow3r_i2c_address; // I2C addresses of different devices on a flow3r badge. Not all fields are // applicable to all badge generations. typedef struct { - // Address of the audio codec. - flow3r_i2c_address codec; - // Address of the captouch controller on the top board. - flow3r_i2c_address touch_top; - // Address of the captouch controller on the bottom board. - flow3r_i2c_address touch_bottom; - // Addresses of the port expander chips. - flow3r_i2c_address portexp[2]; + // Address of the audio codec. + flow3r_i2c_address codec; + // Address of the captouch controller on the top board. + flow3r_i2c_address touch_top; + // Address of the captouch controller on the bottom board. + flow3r_i2c_address touch_bottom; + // Addresses of the port expander chips. + flow3r_i2c_address portexp[2]; } flow3r_i2c_addressdef; // Global constant defining addresses for the badge generation that this @@ -30,15 +30,20 @@ void flow3r_bsp_i2c_init(void); // Perform a write transaction to a I2C device. // // This can be called concurrently from different tassks. -esp_err_t flow3r_bsp_i2c_write_to_device(flow3r_i2c_address address, const uint8_t *buffer, size_t write_size, TickType_t ticks_to_wait); +esp_err_t flow3r_bsp_i2c_write_to_device(flow3r_i2c_address address, + const uint8_t *buffer, + size_t write_size, + TickType_t ticks_to_wait); // Perform a write-then read transaction on an I2C device. // // This can be called concurrently from different tassks. -esp_err_t flow3r_bsp_i2c_write_read_device(flow3r_i2c_address address, const uint8_t *write_buffer, size_t write_size, uint8_t *read_buffer, size_t read_size, TickType_t ticks_to_wait); +esp_err_t flow3r_bsp_i2c_write_read_device( + flow3r_i2c_address address, const uint8_t *write_buffer, size_t write_size, + uint8_t *read_buffer, size_t read_size, TickType_t ticks_to_wait); typedef struct { - uint32_t res[4]; // Bitfield of addresses from 0 to 127. + uint32_t res[4]; // Bitfield of addresses from 0 to 127. } flow3r_bsp_i2c_scan_result_t; void flow3r_bsp_i2c_scan(flow3r_bsp_i2c_scan_result_t *res); \ No newline at end of file diff --git a/components/flow3r_bsp/flow3r_bsp_leds.c b/components/flow3r_bsp/flow3r_bsp_leds.c index 38ab78c86c..2d96cb7fe4 100644 --- a/components/flow3r_bsp/flow3r_bsp_leds.c +++ b/components/flow3r_bsp/flow3r_bsp_leds.c @@ -3,13 +3,14 @@ #include "flow3r_bsp_rmtled.h" esp_err_t flow3r_bsp_leds_init(void) { - return flow3r_bsp_rmtled_init(FLOW3R_BSP_LED_COUNT); + return flow3r_bsp_rmtled_init(FLOW3R_BSP_LED_COUNT); } -void flow3r_bsp_leds_set_pixel(uint32_t index, uint32_t red, uint32_t green, uint32_t blue) { - flow3r_bsp_rmtled_set_pixel(index, red, green, blue); +void flow3r_bsp_leds_set_pixel(uint32_t index, uint32_t red, uint32_t green, + uint32_t blue) { + flow3r_bsp_rmtled_set_pixel(index, red, green, blue); } esp_err_t flow3r_bsp_leds_refresh(TickType_t timeout_ms) { - return flow3r_bsp_rmtled_refresh(timeout_ms); + return flow3r_bsp_rmtled_refresh(timeout_ms); } diff --git a/components/flow3r_bsp/flow3r_bsp_max7321.c b/components/flow3r_bsp/flow3r_bsp_max7321.c index 47329c2d15..41e5d47fbe 100644 --- a/components/flow3r_bsp/flow3r_bsp_max7321.c +++ b/components/flow3r_bsp/flow3r_bsp_max7321.c @@ -2,10 +2,10 @@ #include "flow3r_bsp_i2c.h" /* The MAX7321 doesn't have any input/output pin configuration methods. Instead, - * it has a ~40k pullup resistor to VCC and a programmable shunt transistor to VEE. - * Writing a byte to the IC closes each shunt with a LO at its index. + * it has a ~40k pullup resistor to VCC and a programmable shunt transistor to + * VEE. Writing a byte to the IC closes each shunt with a LO at its index. * Reading a byte returns the state of each pin. - * + * * This means that changing a single output bit cannot be changed without some * information about the other pins. Also a output pin set to HI can read as LO * if there's an outside shunt. @@ -14,20 +14,22 @@ // TODO(q3k): unhardcode #define TIMEOUT_MS 1000 -esp_err_t flow3r_bsp_max7321_init(flow3r_bsp_max7321_t *max, flow3r_i2c_address addr) { - max->addr = addr; - max->is_output_pin = 0; - max->read_state = 0xff; - max->output_state = 0xff; +esp_err_t flow3r_bsp_max7321_init(flow3r_bsp_max7321_t *max, + flow3r_i2c_address addr) { + max->addr = addr; + max->is_output_pin = 0; + max->read_state = 0xff; + max->output_state = 0xff; - return flow3r_bsp_max7321_update(max); + return flow3r_bsp_max7321_update(max); } -void flow3r_bsp_max7321_set_pinmode_output(flow3r_bsp_max7321_t *max, uint32_t pin, bool output) { - if(output) { - max->is_output_pin |= (1<<pin); +void flow3r_bsp_max7321_set_pinmode_output(flow3r_bsp_max7321_t *max, + uint32_t pin, bool output) { + if (output) { + max->is_output_pin |= (1 << pin); } else { - max->is_output_pin &= ~(1<<pin); + max->is_output_pin &= ~(1 << pin); } } @@ -35,25 +37,28 @@ bool flow3r_bsp_max7321_get_pin(flow3r_bsp_max7321_t *max, uint32_t pin) { return ((max->read_state) >> pin) & 1; } -void flow3r_bsp_max7321_set_pin(flow3r_bsp_max7321_t *max, uint32_t pin, bool on) { - if(((max->is_output_pin) >> pin) & 1){ - if(on){ - max->output_state |= 1<<pin; +void flow3r_bsp_max7321_set_pin(flow3r_bsp_max7321_t *max, uint32_t pin, + bool on) { + if (((max->is_output_pin) >> pin) & 1) { + if (on) { + max->output_state |= 1 << pin; } else { - max->output_state &= ~(1<<pin); + max->output_state &= ~(1 << pin); } - } + } } esp_err_t flow3r_bsp_max7321_update(flow3r_bsp_max7321_t *max) { uint8_t rx = 0; uint8_t tx = (~(max->is_output_pin)) | max->output_state; - esp_err_t ret = flow3r_bsp_i2c_write_read_device(max->addr, &tx, sizeof(tx), &rx, sizeof(rx), TIMEOUT_MS / portTICK_PERIOD_MS); - if (ret != ESP_OK) { - return ret; - } - max->read_state = rx; + esp_err_t ret = flow3r_bsp_i2c_write_read_device( + max->addr, &tx, sizeof(tx), &rx, sizeof(rx), + TIMEOUT_MS / portTICK_PERIOD_MS); + if (ret != ESP_OK) { + return ret; + } + max->read_state = rx; - return ESP_OK; + return ESP_OK; } \ No newline at end of file diff --git a/components/flow3r_bsp/flow3r_bsp_max7321.h b/components/flow3r_bsp/flow3r_bsp_max7321.h index 79ae185e41..c32cbb8f3a 100644 --- a/components/flow3r_bsp/flow3r_bsp_max7321.h +++ b/components/flow3r_bsp/flow3r_bsp_max7321.h @@ -8,16 +8,19 @@ #include "esp_err.h" typedef struct { - flow3r_i2c_address addr; - // mask for pins we wish to use as outputs - uint8_t is_output_pin; + flow3r_i2c_address addr; + // mask for pins we wish to use as outputs + uint8_t is_output_pin; uint8_t read_state; - // goal output state + // goal output state uint8_t output_state; } flow3r_bsp_max7321_t; -esp_err_t flow3r_bsp_max7321_init(flow3r_bsp_max7321_t *max, flow3r_i2c_address addr); -void flow3r_bsp_max7321_set_pinmode_output(flow3r_bsp_max7321_t *max, uint32_t pin, bool output); +esp_err_t flow3r_bsp_max7321_init(flow3r_bsp_max7321_t *max, + flow3r_i2c_address addr); +void flow3r_bsp_max7321_set_pinmode_output(flow3r_bsp_max7321_t *max, + uint32_t pin, bool output); bool flow3r_bsp_max7321_get_pin(flow3r_bsp_max7321_t *max, uint32_t pin); -void flow3r_bsp_max7321_set_pin(flow3r_bsp_max7321_t *max, uint32_t pin, bool on); +void flow3r_bsp_max7321_set_pin(flow3r_bsp_max7321_t *max, uint32_t pin, + bool on); esp_err_t flow3r_bsp_max7321_update(flow3r_bsp_max7321_t *max); \ No newline at end of file diff --git a/components/flow3r_bsp/flow3r_bsp_max98091.c b/components/flow3r_bsp/flow3r_bsp_max98091.c index 4794f4835a..7e80667133 100644 --- a/components/flow3r_bsp/flow3r_bsp_max98091.c +++ b/components/flow3r_bsp/flow3r_bsp_max98091.c @@ -1,49 +1,51 @@ #include "flow3r_bsp_max98091.h" +#include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_log.h" -#include "flow3r_bsp_max98091_hwregs.h" #include "flow3r_bsp_i2c.h" +#include "flow3r_bsp_max98091_hwregs.h" #define TIMEOUT_MS 1000 static const char *TAG = "flow3r-bsp-max98091"; -static uint8_t max98091_read(const uint8_t reg) -{ +static uint8_t max98091_read(const uint8_t reg) { const uint8_t tx[] = {reg}; uint8_t rx[1]; - flow3r_bsp_i2c_write_read_device(flow3r_i2c_addresses.codec, tx, sizeof(tx), rx, sizeof(rx), TIMEOUT_MS / portTICK_PERIOD_MS); + flow3r_bsp_i2c_write_read_device(flow3r_i2c_addresses.codec, tx, sizeof(tx), + rx, sizeof(rx), + TIMEOUT_MS / portTICK_PERIOD_MS); // TODO(q3k): handle error return rx[0]; } -static esp_err_t max98091_write(const uint8_t reg, const uint8_t data) -{ +static esp_err_t max98091_write(const uint8_t reg, const uint8_t data) { const uint8_t tx[] = {reg, data}; - return flow3r_bsp_i2c_write_to_device(flow3r_i2c_addresses.codec, tx, sizeof(tx), TIMEOUT_MS / portTICK_PERIOD_MS); + return flow3r_bsp_i2c_write_to_device(flow3r_i2c_addresses.codec, tx, + sizeof(tx), + TIMEOUT_MS / portTICK_PERIOD_MS); } -static esp_err_t max98091_check(const uint8_t reg, const uint8_t data) -{ +static esp_err_t max98091_check(const uint8_t reg, const uint8_t data) { esp_err_t ret = max98091_write(reg, data); if (ret != ESP_OK) { return ret; } - + switch (reg) { - // Do not attempt to read back registers that are write-only. - case MAX98091_SOFTWARE_RESET: - case MAX98091_DAI_INTERFACE: - case MAX98091_DAC_PATH: - case MAX98091_LINE_TO_ADC: - return ESP_OK; + // Do not attempt to read back registers that are write-only. + case MAX98091_SOFTWARE_RESET: + case MAX98091_DAI_INTERFACE: + case MAX98091_DAC_PATH: + case MAX98091_LINE_TO_ADC: + return ESP_OK; } uint8_t readback = max98091_read(reg); if (readback != data) { - ESP_LOGE(TAG, "Write of %02X failed: wanted %02x, got %02x", reg, data, readback); + ESP_LOGE(TAG, "Write of %02X failed: wanted %02x, got %02x", reg, data, + readback); } return ESP_OK; } @@ -54,29 +56,28 @@ void flow3r_bsp_max98091_register_poke(uint8_t reg, uint8_t data) { void flow3r_bsp_max98091_headphones_line_in_set_hardware_thru(bool enable) { // Enable headphone mixer. - max98091_check(MAX98091_HPCONTROL, - MAX98091_ON(HPCONTROL_MIXHP_RSEL) | - MAX98091_ON(HPCONTROL_MIXHP_LSEL)); + max98091_check(MAX98091_HPCONTROL, MAX98091_ON(HPCONTROL_MIXHP_RSEL) | + MAX98091_ON(HPCONTROL_MIXHP_LSEL)); // Line A + DAC_L -> Left HP max98091_check(MAX98091_LEFT_HP_MIXER, - MAX98091_BOOL(LEFT_HP_MIXER_LINE_A, enable) | - MAX98091_ON(LEFT_HP_MIXER_LEFT_DAC)); + MAX98091_BOOL(LEFT_HP_MIXER_LINE_A, enable) | + MAX98091_ON(LEFT_HP_MIXER_LEFT_DAC)); // Line B + DAC_R -> Right HP max98091_check(MAX98091_RIGHT_HP_MIXER, - MAX98091_BOOL(RIGHT_HP_MIXER_LINE_B, enable) | - MAX98091_ON(RIGHT_HP_MIXER_RIGHT_DAC)); + MAX98091_BOOL(RIGHT_HP_MIXER_LINE_B, enable) | + MAX98091_ON(RIGHT_HP_MIXER_RIGHT_DAC)); } void flow3r_bsp_max98091_speaker_line_in_set_hardware_thru(bool enable) { // TODO(q3k): left/right DAC seem to have been swapped before, double-check. // Line A -> Left Speaker max98091_check(MAX98091_LEFT_SPK_MIXER, - MAX98091_BOOL(LEFT_SPK_MIXER_LINE_A, enable) | - MAX98091_ON(LEFT_SPK_MIXER_LEFT_DAC)); + MAX98091_BOOL(LEFT_SPK_MIXER_LINE_A, enable) | + MAX98091_ON(LEFT_SPK_MIXER_LEFT_DAC)); // Line B -> Right Speaker max98091_check(MAX98091_RIGHT_SPK_MIXER, - MAX98091_BOOL(RIGHT_SPK_MIXER_LINE_B, enable) | - MAX98091_ON(RIGHT_SPK_MIXER_RIGHT_DAC)); + MAX98091_BOOL(RIGHT_SPK_MIXER_LINE_B, enable) | + MAX98091_ON(RIGHT_SPK_MIXER_RIGHT_DAC)); } void flow3r_bsp_max98091_line_in_set_hardware_thru(bool enable) { @@ -87,69 +88,75 @@ void flow3r_bsp_max98091_line_in_set_hardware_thru(bool enable) { static void onboard_mic_set_power(bool enable) { // DMICCLK = 3 => f_DMC = f_PCLK / 5 max98091_check(MAX98091_DIGITAL_MIC_ENABLE, - MAX98091_BITS(DIGITAL_MIC_ENABLE_DMICCLK, 3) | - MAX98091_BOOL(DIGITAL_MIC_ENABLE_DIGMIC1L, enable) | - MAX98091_BOOL(DIGITAL_MIC_ENABLE_DIGMIC1R, enable)); + MAX98091_BITS(DIGITAL_MIC_ENABLE_DMICCLK, 3) | + MAX98091_BOOL(DIGITAL_MIC_ENABLE_DIGMIC1L, enable) | + MAX98091_BOOL(DIGITAL_MIC_ENABLE_DIGMIC1R, enable)); } -void flow3r_bsp_max98091_input_set_source(flow3r_bsp_audio_input_source_t source){ +void flow3r_bsp_max98091_input_set_source( + flow3r_bsp_audio_input_source_t source) { switch (source) { - case flow3r_bsp_audio_input_source_none: - onboard_mic_set_power(false); - max98091_check(MAX98091_LEFT_ADC_MIXER, 0); - max98091_check(MAX98091_RIGHT_ADC_MIXER, 0); - break; - case flow3r_bsp_audio_input_source_line_in: - onboard_mic_set_power(false); - max98091_check(MAX98091_LEFT_ADC_MIXER, MAX98091_ON(LEFT_ADC_MIXER_LINE_IN_A)); - max98091_check(MAX98091_RIGHT_ADC_MIXER, MAX98091_ON(RIGHT_ADC_MIXER_LINE_IN_B)); - break; - case flow3r_bsp_audio_input_source_headset_mic: - onboard_mic_set_power(false); - max98091_check(MAX98091_LEFT_ADC_MIXER, MAX98091_ON(LEFT_ADC_MIXER_MICROPHONE_1)); - max98091_check(MAX98091_RIGHT_ADC_MIXER, MAX98091_ON(RIGHT_ADC_MIXER_MICROPHONE_1)); - break; - case flow3r_bsp_audio_input_source_onboard_mic: - onboard_mic_set_power(true); - max98091_check(MAX98091_LEFT_ADC_MIXER, 0); - max98091_check(MAX98091_RIGHT_ADC_MIXER, 0); - break; + case flow3r_bsp_audio_input_source_none: + onboard_mic_set_power(false); + max98091_check(MAX98091_LEFT_ADC_MIXER, 0); + max98091_check(MAX98091_RIGHT_ADC_MIXER, 0); + break; + case flow3r_bsp_audio_input_source_line_in: + onboard_mic_set_power(false); + max98091_check(MAX98091_LEFT_ADC_MIXER, + MAX98091_ON(LEFT_ADC_MIXER_LINE_IN_A)); + max98091_check(MAX98091_RIGHT_ADC_MIXER, + MAX98091_ON(RIGHT_ADC_MIXER_LINE_IN_B)); + break; + case flow3r_bsp_audio_input_source_headset_mic: + onboard_mic_set_power(false); + max98091_check(MAX98091_LEFT_ADC_MIXER, + MAX98091_ON(LEFT_ADC_MIXER_MICROPHONE_1)); + max98091_check(MAX98091_RIGHT_ADC_MIXER, + MAX98091_ON(RIGHT_ADC_MIXER_MICROPHONE_1)); + break; + case flow3r_bsp_audio_input_source_onboard_mic: + onboard_mic_set_power(true); + max98091_check(MAX98091_LEFT_ADC_MIXER, 0); + max98091_check(MAX98091_RIGHT_ADC_MIXER, 0); + break; } } -void flow3r_bsp_max98091_headset_set_gain_dB(uint8_t gain_dB){ - if(gain_dB > 50) gain_dB = 50; +void flow3r_bsp_max98091_headset_set_gain_dB(uint8_t gain_dB) { + if (gain_dB > 50) gain_dB = 50; uint8_t pa1en = 0b01; - if(gain_dB > 30){ + if (gain_dB > 30) { gain_dB -= 30; pa1en = 0b11; - } else if(gain_dB > 20){ + } else if (gain_dB > 20) { gain_dB -= 20; pa1en = 0b10; } max98091_check(MAX98091_MIC1_INPUT_LEVEL, - MAX98091_BITS(MIC1_INPUT_LEVEL_PGAM1, 0x14 - gain_dB) | - MAX98091_BITS(MIC1_INPUT_LEVEL_PA1EN, pa1en)); + MAX98091_BITS(MIC1_INPUT_LEVEL_PGAM1, 0x14 - gain_dB) | + MAX98091_BITS(MIC1_INPUT_LEVEL_PA1EN, pa1en)); } -void flow3r_bsp_max98091_read_jacksense(flow3r_bsp_audio_jacksense_state_t *st) { +void flow3r_bsp_max98091_read_jacksense( + flow3r_bsp_audio_jacksense_state_t *st) { // TODO: read port expander st->line_in = false; uint8_t jck = max98091_read(MAX98091_JACK_STATUS); switch (jck) { - case 6: - st->headphones = false; - st->headset = false; - return; - case 0: - st->headphones = true; - st->headset = false; - return; - case 2: - st->headphones = true; - st->headset = true; + case 6: + st->headphones = false; + st->headset = false; + return; + case 0: + st->headphones = true; + st->headset = false; + return; + case 2: + st->headphones = true; + st->headset = true; } } @@ -159,24 +166,39 @@ typedef struct { } volume_step_t; static const uint8_t speaker_map_len = 40; -static const volume_step_t speaker_map[] = {{0x3F, +14}, {0x3E, +13.5}, {0x3D, +13}, {0x3C, +12.5}, {0x3B, +12}, {0x3A, +11.5}, {0x39, +11}, {0x38, +10.5}, {0x37, +10}, {0x36, +9.5}, {0x35, +9}, {0x34, +8}, {0x33, +7}, {0x32, +6}, {0x31, +5}, {0x30, +4}, {0x2F, +3}, {0x2E, +2}, {0x2D, +1}, {0x2C, +0}, {0x2B, -1}, {0x2A, -2}, {0x29, -3}, {0x28, -4}, {0x27, -5}, {0x26, -6}, {0x25, -8}, {0x24, -10}, {0x23, -12}, {0x22, -14}, {0x21, -17}, {0x20, -20}, {0x1F, -23}, {0x1E, -26}, {0x1D, -29}, {0x1C, -32}, {0x1B, -36}, {0x1A, -40}, {0x19, -44}, {0x18, -48}}; +static const volume_step_t speaker_map[] = { + {0x3F, +14}, {0x3E, +13.5}, {0x3D, +13}, {0x3C, +12.5}, {0x3B, +12}, + {0x3A, +11.5}, {0x39, +11}, {0x38, +10.5}, {0x37, +10}, {0x36, +9.5}, + {0x35, +9}, {0x34, +8}, {0x33, +7}, {0x32, +6}, {0x31, +5}, + {0x30, +4}, {0x2F, +3}, {0x2E, +2}, {0x2D, +1}, {0x2C, +0}, + {0x2B, -1}, {0x2A, -2}, {0x29, -3}, {0x28, -4}, {0x27, -5}, + {0x26, -6}, {0x25, -8}, {0x24, -10}, {0x23, -12}, {0x22, -14}, + {0x21, -17}, {0x20, -20}, {0x1F, -23}, {0x1E, -26}, {0x1D, -29}, + {0x1C, -32}, {0x1B, -36}, {0x1A, -40}, {0x19, -44}, {0x18, -48}}; static const volume_step_t *find_speaker_volume(float vol_dB) { uint8_t map_index = speaker_map_len - 1; - for(; map_index; map_index--){ - if(speaker_map[map_index].volume_dB >= vol_dB) + for (; map_index; map_index--) { + if (speaker_map[map_index].volume_dB >= vol_dB) return &speaker_map[map_index]; } return &speaker_map[0]; } static const uint8_t headphones_map_len = 32; -static const volume_step_t headphones_map[] = {{0x1F, +3}, {0x1E, +2.5}, {0x1D, +2}, {0x1C, +1.5}, {0x1B, +1}, {0x1A, +0}, {0x19, -1}, {0x18, -2}, {0x17, -3}, {0x16, -4}, {0x15, -5}, {0x14, -7}, {0x13, -9}, {0x12, -11}, {0x11, -13}, {0x10, -15}, {0x0F, -17}, {0x0E, -19}, {0x0D, -22}, {0x0C, -25}, {0x0B, -28}, {0x0A, -31}, {0x09, -34}, {0x08, -37}, {0x07, -40}, {0x06, -43}, {0x06, -47}, {0x04, -51}, {0x03, -55}, {0x02, -59}, {0x01, -63}, {0x00, -67}}; +static const volume_step_t headphones_map[] = { + {0x1F, +3}, {0x1E, +2.5}, {0x1D, +2}, {0x1C, +1.5}, {0x1B, +1}, + {0x1A, +0}, {0x19, -1}, {0x18, -2}, {0x17, -3}, {0x16, -4}, + {0x15, -5}, {0x14, -7}, {0x13, -9}, {0x12, -11}, {0x11, -13}, + {0x10, -15}, {0x0F, -17}, {0x0E, -19}, {0x0D, -22}, {0x0C, -25}, + {0x0B, -28}, {0x0A, -31}, {0x09, -34}, {0x08, -37}, {0x07, -40}, + {0x06, -43}, {0x06, -47}, {0x04, -51}, {0x03, -55}, {0x02, -59}, + {0x01, -63}, {0x00, -67}}; static const volume_step_t *find_headphones_volume(float vol_dB) { uint8_t map_index = headphones_map_len - 1; - for(; map_index; map_index--){ - if(headphones_map[map_index].volume_dB >= vol_dB) + for (; map_index; map_index--) { + if (headphones_map[map_index].volume_dB >= vol_dB) return &headphones_map[map_index]; } return &headphones_map[0]; @@ -186,11 +208,11 @@ float flow3r_bsp_max98091_headphones_set_volume(bool mute, float dB) { const volume_step_t *step = find_headphones_volume(dB); ESP_LOGI(TAG, "Setting headphones volume: %d/%02x", mute, step->raw_value); max98091_check(MAX98091_LEFT_HP_VOLUME, - MAX98091_BOOL(LEFT_HP_VOLUME_HPLM, mute) | - MAX98091_BITS(LEFT_HP_VOLUME_HPVOLL, step->raw_value)); + MAX98091_BOOL(LEFT_HP_VOLUME_HPLM, mute) | + MAX98091_BITS(LEFT_HP_VOLUME_HPVOLL, step->raw_value)); max98091_check(MAX98091_RIGHT_HP_VOLUME, - MAX98091_BOOL(RIGHT_HP_VOLUME_HPRM, mute) | - MAX98091_BITS(RIGHT_HP_VOLUME_HPVOLR, step->raw_value)); + MAX98091_BOOL(RIGHT_HP_VOLUME_HPRM, mute) | + MAX98091_BITS(RIGHT_HP_VOLUME_HPVOLR, step->raw_value)); return step->volume_dB; } @@ -198,11 +220,11 @@ float flow3r_bsp_max98091_speaker_set_volume(bool mute, float dB) { const volume_step_t *step = find_speaker_volume(dB); ESP_LOGI(TAG, "Setting speakers volume: %d/%02x", mute, step->raw_value); max98091_check(MAX98091_LEFT_SPK_VOLUME, - MAX98091_BOOL(LEFT_SPK_VOLUME_SPLM, mute) | - MAX98091_BITS(LEFT_SPK_VOLUME_SPVOLL, step->raw_value)); + MAX98091_BOOL(LEFT_SPK_VOLUME_SPLM, mute) | + MAX98091_BITS(LEFT_SPK_VOLUME_SPVOLL, step->raw_value)); max98091_check(MAX98091_RIGHT_SPK_VOLUME, - MAX98091_BOOL(RIGHT_SPK_VOLUME_SPRM, mute) | - MAX98091_BITS(RIGHT_SPK_VOLUME_SPVOLR, step->raw_value)); + MAX98091_BOOL(RIGHT_SPK_VOLUME_SPRM, mute) | + MAX98091_BITS(RIGHT_SPK_VOLUME_SPVOLR, step->raw_value)); return step->volume_dB; } void flow3r_bsp_max98091_init(void) { @@ -216,7 +238,7 @@ void flow3r_bsp_max98091_init(void) { // pclk = mclk / 1 POKE(SYSTEM_CLOCK, MAX98091_BITS(SYSTEM_CLOCK_PSCLK, 1)); // music, dc filter in record and playback - POKE(FILTER_CONFIGURATION, (1 << 7) | (1 << 6) | (1 << 5) | (1 << 2)); + POKE(FILTER_CONFIGURATION, (1 << 7) | (1 << 6) | (1 << 5) | (1 << 2)); // Sets up DAI for left-justified slave mode operation. POKE(DAI_INTERFACE, 1 << 2); // Sets up the DAC to speaker path @@ -235,32 +257,35 @@ void flow3r_bsp_max98091_init(void) { POKE(DAC_CONTROL, 1 << 0); // enable micbias, line input amps, ADCs - POKE(INPUT_ENABLE, (1<<4) |(1<<3) | (1<<2) | (1<<1) | (1<<0)); + POKE(INPUT_ENABLE, (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0)); // IN3 SE -> Line A, IN4 SE -> Line B - POKE(LINE_INPUT_CONFIG, (1<<3) | (1<<2)); + POKE(LINE_INPUT_CONFIG, (1 << 3) | (1 << 2)); // 64x oversampling, dithering, high performance ADC - POKE(ADC_CONTROL, (1<<1) | (1<<0)); + POKE(ADC_CONTROL, (1 << 1) | (1 << 0)); POKE(DIGITAL_MIC_ENABLE, 0); // IN5/IN6 to MIC1 - POKE(INPUT_MODE, (1<<0)); + POKE(INPUT_MODE, (1 << 0)); flow3r_bsp_max98091_line_in_set_hardware_thru(0); flow3r_bsp_max98091_headset_set_gain_dB(0); flow3r_bsp_max98091_input_set_source(flow3r_bsp_audio_input_source_none); // output enable: enable dacs - POKE(OUTPUT_ENABLE, (1<<1) | (1<<0)); + POKE(OUTPUT_ENABLE, (1 << 1) | (1 << 0)); // power up - POKE(DEVICE_SHUTDOWN, 1<<7); + POKE(DEVICE_SHUTDOWN, 1 << 7); // enable outputs, dacs - POKE(OUTPUT_ENABLE, (1<<7) | (1<<6) | (1<<5) | (1<<4) | (1<<1) | (1<<0)); + POKE(OUTPUT_ENABLE, + (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0)); // disable all digital filters except for dc blocking POKE(DSP_FILTER_ENABLE, 0x0); // jack detect enable - POKE(JACK_DETECT, 1<<7); + POKE(JACK_DETECT, 1 << 7); // TODO(q3k): mute this - ESP_LOGI(TAG, "Codec initilialied! Don't worry about the readback errors above."); + ESP_LOGI( + TAG, + "Codec initilialied! Don't worry about the readback errors above."); #undef POKE } \ No newline at end of file diff --git a/components/flow3r_bsp/flow3r_bsp_max98091.h b/components/flow3r_bsp/flow3r_bsp_max98091.h index a90ebb7c99..c203053f0b 100644 --- a/components/flow3r_bsp/flow3r_bsp_max98091.h +++ b/components/flow3r_bsp/flow3r_bsp_max98091.h @@ -12,7 +12,8 @@ float flow3r_bsp_max98091_headphones_set_volume(bool mute, float dB); float flow3r_bsp_max98091_speaker_set_volume(bool mute, float dB); void flow3r_bsp_max98091_headset_set_gain_dB(uint8_t gain_dB); void flow3r_bsp_max98091_read_jacksense(flow3r_bsp_audio_jacksense_state_t *st); -void flow3r_bsp_max98091_input_set_source(flow3r_bsp_audio_input_source_t source); +void flow3r_bsp_max98091_input_set_source( + flow3r_bsp_audio_input_source_t source); void flow3r_bsp_max98091_headphones_line_in_set_hardware_thru(bool enable); void flow3r_bsp_max98091_speaker_line_in_set_hardware_thru(bool enable); void flow3r_bsp_max98091_line_in_set_hardware_thru(bool enable); diff --git a/components/flow3r_bsp/flow3r_bsp_max98091_hwregs.h b/components/flow3r_bsp/flow3r_bsp_max98091_hwregs.h index b55ace4f2d..949baddf0e 100644 --- a/components/flow3r_bsp/flow3r_bsp_max98091_hwregs.h +++ b/components/flow3r_bsp/flow3r_bsp_max98091_hwregs.h @@ -4,7 +4,8 @@ // // https://www.analog.com/media/en/technical-documentation/data-sheets/MAX98091.pdf -#define MAX98091_BITS(n, v) (((v) & ((1 << MAX98091_##n##_WIDTH)-1)) << (MAX98091_##n##_BASE)) +#define MAX98091_BITS(n, v) \ + (((v) & ((1 << MAX98091_##n##_WIDTH) - 1)) << (MAX98091_##n##_BASE)) #define MAX98091_BOOL(n, v) MAX98091_BITS(n, v ? 1 : 0) #define MAX98091_ON(n) MAX98091_BOOL(n, true) #define MAX98091_OFF(n) MAX98091_BOOL(n, false) @@ -139,7 +140,6 @@ #define MAX98091_RIGHT_SPK_VOLUME_SPRM_BASE 7 #define MAX98091_RIGHT_SPK_VOLUME_SPRM_WIDTH 1 - #define MAX98091_JACK_DETECT 0x3D #define MAX98091_INPUT_ENABLE 0x3E diff --git a/components/flow3r_bsp/flow3r_bsp_rmtled.c b/components/flow3r_bsp/flow3r_bsp_rmtled.c index fb9a90f1db..90cb441624 100644 --- a/components/flow3r_bsp/flow3r_bsp_rmtled.c +++ b/components/flow3r_bsp/flow3r_bsp_rmtled.c @@ -43,16 +43,19 @@ typedef struct { static flow3r_bsp_rmtled_t rmtled = {0}; -static void IRAM_ATTR _rmtled_adapter(const void *src, rmt_item32_t *dest, size_t src_size, - size_t wanted_num, size_t *translated_size, size_t *item_num) -{ +static void IRAM_ATTR _rmtled_adapter(const void *src, rmt_item32_t *dest, + size_t src_size, size_t wanted_num, + size_t *translated_size, + size_t *item_num) { if (src == NULL || dest == NULL) { *translated_size = 0; *item_num = 0; return; } - const rmt_item32_t bit0 = {{{ rmtled.ws2812_t0h_ticks, 1, rmtled.ws2812_t0l_ticks, 0 }}}; //Logical 0 - const rmt_item32_t bit1 = {{{ rmtled.ws2812_t1h_ticks, 1, rmtled.ws2812_t1l_ticks, 0 }}}; //Logical 1 + const rmt_item32_t bit0 = {{{rmtled.ws2812_t0h_ticks, 1, + rmtled.ws2812_t0l_ticks, 0}}}; // Logical 0 + const rmt_item32_t bit1 = {{{rmtled.ws2812_t1h_ticks, 1, + rmtled.ws2812_t1l_ticks, 0}}}; // Logical 1 size_t size = 0; size_t num = 0; uint8_t *psrc = (uint8_t *)src; @@ -61,9 +64,9 @@ static void IRAM_ATTR _rmtled_adapter(const void *src, rmt_item32_t *dest, size_ for (int i = 0; i < 8; i++) { // MSB first if (*psrc & (1 << (7 - i))) { - pdest->val = bit1.val; + pdest->val = bit1.val; } else { - pdest->val = bit0.val; + pdest->val = bit0.val; } num++; pdest++; @@ -75,7 +78,6 @@ static void IRAM_ATTR _rmtled_adapter(const void *src, rmt_item32_t *dest, size_ *item_num = num; } - esp_err_t flow3r_bsp_rmtled_init(uint16_t num_leds) { if (rmtled.buffer != NULL) { return ESP_ERR_INVALID_STATE; @@ -90,13 +92,16 @@ esp_err_t flow3r_bsp_rmtled_init(uint16_t num_leds) { ESP_LOGE(TAG, "rmt_config failed: %s", esp_err_to_name(ret)); return ret; } - if ((ret = rmt_driver_install(config.channel, 0, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED)) != ESP_OK) { + if ((ret = rmt_driver_install( + config.channel, 0, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED)) != + ESP_OK) { ESP_LOGE(TAG, "rmt_driver_install failed: %s", esp_err_to_name(ret)); return ret; } uint32_t counter_clk_hz = 0; - if ((ret = rmt_get_counter_clock(config.channel, &counter_clk_hz)) != ESP_OK) { + if ((ret = rmt_get_counter_clock(config.channel, &counter_clk_hz)) != + ESP_OK) { ESP_LOGE(TAG, "rmt_get_counter_clock failed: %s", esp_err_to_name(ret)); return ret; } @@ -116,7 +121,8 @@ esp_err_t flow3r_bsp_rmtled_init(uint16_t num_leds) { } rmtled.transmitting = false; - if ((ret = rmt_translator_init(config.channel, _rmtled_adapter)) != ESP_OK) { + if ((ret = rmt_translator_init(config.channel, _rmtled_adapter)) != + ESP_OK) { ESP_LOGE(TAG, "rmt_translator_init: %s", esp_err_to_name(ret)); return ret; } @@ -124,8 +130,8 @@ esp_err_t flow3r_bsp_rmtled_init(uint16_t num_leds) { return ESP_OK; } -void flow3r_bsp_rmtled_set_pixel(uint32_t index, uint32_t red, uint32_t green, uint32_t blue) -{ +void flow3r_bsp_rmtled_set_pixel(uint32_t index, uint32_t red, uint32_t green, + uint32_t blue) { if (rmtled.buffer == NULL) { return; } @@ -144,14 +150,16 @@ esp_err_t flow3r_bsp_rmtled_refresh(int32_t timeout_ms) { } if (rmtled.transmitting) { - esp_err_t ret = rmt_wait_tx_done(rmtled.chan, pdMS_TO_TICKS(timeout_ms)); + esp_err_t ret = + rmt_wait_tx_done(rmtled.chan, pdMS_TO_TICKS(timeout_ms)); if (ret != ESP_OK) { ESP_LOGE(TAG, "rmt_wait_tx_done: %s", esp_err_to_name(ret)); } rmtled.transmitting = false; } - esp_err_t ret = rmt_write_sample(rmtled.chan, rmtled.buffer, rmtled.num_leds * 3, true); + esp_err_t ret = + rmt_write_sample(rmtled.chan, rmtled.buffer, rmtled.num_leds * 3, true); rmtled.transmitting = true; return ret; } \ No newline at end of file diff --git a/components/flow3r_bsp/flow3r_bsp_rmtled.h b/components/flow3r_bsp/flow3r_bsp_rmtled.h index 569b8507af..c612872379 100644 --- a/components/flow3r_bsp/flow3r_bsp_rmtled.h +++ b/components/flow3r_bsp/flow3r_bsp_rmtled.h @@ -3,5 +3,6 @@ #include "esp_err.h" esp_err_t flow3r_bsp_rmtled_init(uint16_t num_leds); -void flow3r_bsp_rmtled_set_pixel(uint32_t index, uint32_t red, uint32_t green, uint32_t blue); +void flow3r_bsp_rmtled_set_pixel(uint32_t index, uint32_t red, uint32_t green, + uint32_t blue); esp_err_t flow3r_bsp_rmtled_refresh(int32_t timeout_ms); \ No newline at end of file diff --git a/components/flow3r_bsp/flow3r_bsp_spio.c b/components/flow3r_bsp/flow3r_bsp_spio.c index 61dc03ea5f..779acfb022 100644 --- a/components/flow3r_bsp/flow3r_bsp_spio.c +++ b/components/flow3r_bsp/flow3r_bsp_spio.c @@ -8,106 +8,126 @@ static const char *TAG = "flow3r-spio"; typedef enum { - flow3r_bsp_iochip_esp32 = 0, - flow3r_bsp_iochip_portexp = 1, - flow3r_bsp_iochip_dummy = 2, + flow3r_bsp_iochip_esp32 = 0, + flow3r_bsp_iochip_portexp = 1, + flow3r_bsp_iochip_dummy = 2, } flow3r_bsp_iochip_t; - typedef struct { - flow3r_bsp_iochip_t chip; - uint32_t pin; - bool output; - bool pullup; - bool invert; + flow3r_bsp_iochip_t chip; + uint32_t pin; + bool output; + bool pullup; + bool invert; } flow3r_bsp_iopin_t; -#define IESP(pinno, pullup_, ...) { .chip = flow3r_bsp_iochip_esp32, .pin = pinno, .output = false, .pullup = pullup_ , __VA_ARGS__} -#define IPEX(pinno, pexno, ...) { .chip = flow3r_bsp_iochip_portexp, .pin = pinno + pexno * 8, .output = false, __VA_ARGS__ } -#define OPEX(pinno, pexno, ...) { .chip = flow3r_bsp_iochip_portexp, .pin = pinno + pexno * 8, .output = true, __VA_ARGS__ } -#define IODUMMY { .chip = flow3r_bsp_iochip_dummy } +#define IESP(pinno, pullup_, ...) \ + { \ + .chip = flow3r_bsp_iochip_esp32, .pin = pinno, .output = false, \ + .pullup = pullup_, __VA_ARGS__ \ + } +#define IPEX(pinno, pexno, ...) \ + { \ + .chip = flow3r_bsp_iochip_portexp, .pin = pinno + pexno * 8, \ + .output = false, __VA_ARGS__ \ + } +#define OPEX(pinno, pexno, ...) \ + { \ + .chip = flow3r_bsp_iochip_portexp, .pin = pinno + pexno * 8, \ + .output = true, __VA_ARGS__ \ + } +#define IODUMMY \ + { .chip = flow3r_bsp_iochip_dummy } typedef struct { - flow3r_bsp_iopin_t left; - flow3r_bsp_iopin_t mid; - flow3r_bsp_iopin_t right; + flow3r_bsp_iopin_t left; + flow3r_bsp_iopin_t mid; + flow3r_bsp_iopin_t right; } flow3r_bsp_iodef_tripos_t; typedef struct { - flow3r_bsp_iopin_t tip_badgelink_enable; - flow3r_bsp_iopin_t tip_badgelink_data; - flow3r_bsp_iopin_t ring_badgelink_enable; - flow3r_bsp_iopin_t ring_badgelink_data; + flow3r_bsp_iopin_t tip_badgelink_enable; + flow3r_bsp_iopin_t tip_badgelink_data; + flow3r_bsp_iopin_t ring_badgelink_enable; + flow3r_bsp_iopin_t ring_badgelink_data; } flow3r_bsp_iodef_trrs_t; typedef struct { - flow3r_bsp_iodef_tripos_t tripos_left; - flow3r_bsp_iodef_tripos_t tripos_right; - flow3r_bsp_iodef_trrs_t trrs_left; - flow3r_bsp_iodef_trrs_t trrs_right; + flow3r_bsp_iodef_tripos_t tripos_left; + flow3r_bsp_iodef_tripos_t tripos_right; + flow3r_bsp_iodef_trrs_t trrs_left; + flow3r_bsp_iodef_trrs_t trrs_right; - flow3r_bsp_iopin_t charger_state; - flow3r_bsp_iopin_t jacksense_right; + flow3r_bsp_iopin_t charger_state; + flow3r_bsp_iopin_t jacksense_right; } flow3r_bsp_iodef_t; #if defined(CONFIG_FLOW3R_HW_GEN_P4) || defined(CONFIG_FLOW3R_HW_GEN_P3) static const flow3r_bsp_iodef_t iodef = { - .tripos_left = { - .left = IESP(3, true, .invert = true), - .mid = IESP(0, false, .invert = true), - .right = IPEX(0, 1, .invert = true), - }, - .tripos_right = { - .left = IPEX(6, 1, .invert = true), - .mid = IPEX(5, 1, .invert = true), - .right = IPEX(7, 1, .invert = true), - }, - .trrs_left = { - .tip_badgelink_enable = OPEX(6, 0, .invert = true), - .ring_badgelink_enable = OPEX(7, 0, .invert = true), - }, - .trrs_right = { - .tip_badgelink_enable = OPEX(5, 0, .invert = true), - .ring_badgelink_enable = OPEX(4, 0, .invert = true), - }, - .charger_state = IODUMMY, - .jacksense_right = IODUMMY, + .tripos_left = + { + .left = IESP(3, true, .invert = true), + .mid = IESP(0, false, .invert = true), + .right = IPEX(0, 1, .invert = true), + }, + .tripos_right = + { + .left = IPEX(6, 1, .invert = true), + .mid = IPEX(5, 1, .invert = true), + .right = IPEX(7, 1, .invert = true), + }, + .trrs_left = + { + .tip_badgelink_enable = OPEX(6, 0, .invert = true), + .ring_badgelink_enable = OPEX(7, 0, .invert = true), + }, + .trrs_right = + { + .tip_badgelink_enable = OPEX(5, 0, .invert = true), + .ring_badgelink_enable = OPEX(4, 0, .invert = true), + }, + .charger_state = IODUMMY, + .jacksense_right = IODUMMY, }; const flow3r_bsp_spio_programmable_pins_t flow3r_bsp_spio_programmable_pins = { - .badgelink_left_tip = 6, - .badgelink_left_ring = 7, - .badgelink_right_tip = 4, - .badgelink_right_ring = 5, + .badgelink_left_tip = 6, + .badgelink_left_ring = 7, + .badgelink_right_tip = 4, + .badgelink_right_ring = 5, }; #define PORTEXP_MAX7321S #elif defined(CONFIG_FLOW3R_HW_GEN_P6) static const flow3r_bsp_iodef_t iodef = { - .tripos_left = { - .left = IPEX(7, 1, .invert = true), - .mid = IESP(0, false, .invert = true), - .right = IPEX(0, 1, .invert = true), - }, - .tripos_right = { - .left = IPEX(4, 1, .invert = true), - .mid = IESP(3, true, .invert = true), - .right = IPEX(5, 1, .invert = true), - }, - .trrs_left = { - .tip_badgelink_enable = OPEX(6, 0), - .ring_badgelink_enable = OPEX(5, 0), - }, - .trrs_right = { - .tip_badgelink_enable = OPEX(4, 0), - .ring_badgelink_enable = OPEX(3, 0), - }, - .charger_state = IPEX(2, 1), - .jacksense_right = IPEX(6, 1), + .tripos_left = + { + .left = IPEX(7, 1, .invert = true), + .mid = IESP(0, false, .invert = true), + .right = IPEX(0, 1, .invert = true), + }, + .tripos_right = + { + .left = IPEX(4, 1, .invert = true), + .mid = IESP(3, true, .invert = true), + .right = IPEX(5, 1, .invert = true), + }, + .trrs_left = + { + .tip_badgelink_enable = OPEX(6, 0), + .ring_badgelink_enable = OPEX(5, 0), + }, + .trrs_right = + { + .tip_badgelink_enable = OPEX(4, 0), + .ring_badgelink_enable = OPEX(3, 0), + }, + .charger_state = IPEX(2, 1), + .jacksense_right = IPEX(6, 1), }; const flow3r_bsp_spio_programmable_pins_t flow3r_bsp_spio_programmable_pins = { - .badgelink_left_tip = 7, - .badgelink_left_ring = 6, - .badgelink_right_tip = 4, - .badgelink_right_ring = 5, + .badgelink_left_tip = 7, + .badgelink_left_ring = 6, + .badgelink_right_tip = 4, + .badgelink_right_ring = 5, }; #define PORTEXP_MAX7321S #else @@ -119,30 +139,35 @@ const flow3r_bsp_spio_programmable_pins_t flow3r_bsp_spio_programmable_pins = { static flow3r_bsp_max7321_t portexps[2]; static esp_err_t _portexp_init(void) { - esp_err_t ret = flow3r_bsp_max7321_init(&portexps[0], flow3r_i2c_addresses.portexp[0]); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to initialize portexp 0: %s", esp_err_to_name(ret)); - return ret; - } - ret = flow3r_bsp_max7321_init(&portexps[1], flow3r_i2c_addresses.portexp[1]); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to initialize portexp 1: %s", esp_err_to_name(ret)); - return ret; - } - return ESP_OK; + esp_err_t ret = + flow3r_bsp_max7321_init(&portexps[0], flow3r_i2c_addresses.portexp[0]); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize portexp 0: %s", + esp_err_to_name(ret)); + return ret; + } + ret = + flow3r_bsp_max7321_init(&portexps[1], flow3r_i2c_addresses.portexp[1]); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize portexp 1: %s", + esp_err_to_name(ret)); + return ret; + } + return ESP_OK; } static uint32_t _portexp_pin(const flow3r_bsp_iopin_t *iopin) { - return iopin->pin % 8; + return iopin->pin % 8; } static flow3r_bsp_max7321_t *_portexp_max(const flow3r_bsp_iopin_t *iopin) { - uint32_t ix = (iopin->pin >> 3) % 2; - return &portexps[ix]; + uint32_t ix = (iopin->pin >> 3) % 2; + return &portexps[ix]; } static void _iopin_portexp_init(const flow3r_bsp_iopin_t *iopin) { - flow3r_bsp_max7321_set_pinmode_output(_portexp_max(iopin), _portexp_pin(iopin), iopin->output); + flow3r_bsp_max7321_set_pinmode_output(_portexp_max(iopin), + _portexp_pin(iopin), iopin->output); } static bool _iopin_portexp_get_pin(const flow3r_bsp_iopin_t *iopin) { @@ -154,148 +179,150 @@ static void _iopin_portexp_set_pin(const flow3r_bsp_iopin_t *iopin, bool on) { } static esp_err_t _portexp_update(void) { - esp_err_t ret = flow3r_bsp_max7321_update(&portexps[0]); - if (ret != ESP_OK) { - return ret; - } - ret = flow3r_bsp_max7321_update(&portexps[1]); - if (ret != ESP_OK) { - return ret; - } - return ESP_OK; + esp_err_t ret = flow3r_bsp_max7321_update(&portexps[0]); + if (ret != ESP_OK) { + return ret; + } + ret = flow3r_bsp_max7321_update(&portexps[1]); + if (ret != ESP_OK) { + return ret; + } + return ESP_OK; } #endif static esp_err_t _iopin_esp32_init(const flow3r_bsp_iopin_t *iopin) { - gpio_config_t cfg = { - .pin_bit_mask = 1 << iopin->pin, - .mode = GPIO_MODE_INPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, - .pull_down_en = GPIO_PULLDOWN_DISABLE, - .intr_type = GPIO_INTR_DISABLE - }; - if (iopin->output) { - // Untested, so unimplemented. - return ESP_ERR_NOT_SUPPORTED; - } - if (iopin->pullup) { + gpio_config_t cfg = {.pin_bit_mask = 1 << iopin->pin, + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE}; + if (iopin->output) { + // Untested, so unimplemented. + return ESP_ERR_NOT_SUPPORTED; + } + if (iopin->pullup) { cfg.pull_up_en = GPIO_PULLUP_ENABLE; - } + } return gpio_config(&cfg); } static esp_err_t _iopin_init(const flow3r_bsp_iopin_t *iopin) { - switch (iopin->chip) { - case flow3r_bsp_iochip_esp32: - return _iopin_esp32_init(iopin); - case flow3r_bsp_iochip_portexp: - _iopin_portexp_init(iopin); - return ESP_OK; - case flow3r_bsp_iochip_dummy: - return ESP_OK; - } - return ESP_ERR_NOT_SUPPORTED; + switch (iopin->chip) { + case flow3r_bsp_iochip_esp32: + return _iopin_esp32_init(iopin); + case flow3r_bsp_iochip_portexp: + _iopin_portexp_init(iopin); + return ESP_OK; + case flow3r_bsp_iochip_dummy: + return ESP_OK; + } + return ESP_ERR_NOT_SUPPORTED; } static bool _iopin_esp32_get_pin(const flow3r_bsp_iopin_t *iopin) { - return gpio_get_level(iopin->pin); + return gpio_get_level(iopin->pin); } static bool _iopin_get_pin(const flow3r_bsp_iopin_t *iopin) { - bool res = false; - switch (iopin->chip) { - case flow3r_bsp_iochip_esp32: - res = _iopin_esp32_get_pin(iopin); - break; - case flow3r_bsp_iochip_portexp: - res = _iopin_portexp_get_pin(iopin); - break; - case flow3r_bsp_iochip_dummy: - res = false; - } - if (iopin->invert) { - res = !res; - } - return res; + bool res = false; + switch (iopin->chip) { + case flow3r_bsp_iochip_esp32: + res = _iopin_esp32_get_pin(iopin); + break; + case flow3r_bsp_iochip_portexp: + res = _iopin_portexp_get_pin(iopin); + break; + case flow3r_bsp_iochip_dummy: + res = false; + } + if (iopin->invert) { + res = !res; + } + return res; } static void _iopin_set_pin(const flow3r_bsp_iopin_t *iopin, bool on) { - if (iopin->invert) { - on = !on; - } - - switch (iopin->chip) { - case flow3r_bsp_iochip_esp32: - // Not implemented. - break; - case flow3r_bsp_iochip_portexp: - _iopin_portexp_set_pin(iopin, on); - break; - case flow3r_bsp_iochip_dummy: - break; - } + if (iopin->invert) { + on = !on; + } + + switch (iopin->chip) { + case flow3r_bsp_iochip_esp32: + // Not implemented. + break; + case flow3r_bsp_iochip_portexp: + _iopin_portexp_set_pin(iopin, on); + break; + case flow3r_bsp_iochip_dummy: + break; + } } -#define INITIO(name) ret = _iopin_init(&iodef.name); if (ret != ESP_OK) { return ret; } +#define INITIO(name) \ + ret = _iopin_init(&iodef.name); \ + if (ret != ESP_OK) { \ + return ret; \ + } esp_err_t flow3r_bsp_spio_init(void) { - esp_err_t ret = _portexp_init(); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Port Expander initialzation failed: %s", esp_err_to_name(ret)); - return ret; - } - INITIO(tripos_left.left); - INITIO(tripos_left.mid); - INITIO(tripos_left.right); - INITIO(tripos_right.left); - INITIO(tripos_right.mid); - INITIO(tripos_right.right); - INITIO(trrs_left.tip_badgelink_enable); - INITIO(trrs_left.ring_badgelink_enable); - INITIO(trrs_right.tip_badgelink_enable); - INITIO(trrs_right.ring_badgelink_enable); - - return _portexp_update(); -} - -esp_err_t flow3r_bsp_spio_update(void) { - return _portexp_update(); + esp_err_t ret = _portexp_init(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Port Expander initialzation failed: %s", + esp_err_to_name(ret)); + return ret; + } + INITIO(tripos_left.left); + INITIO(tripos_left.mid); + INITIO(tripos_left.right); + INITIO(tripos_right.left); + INITIO(tripos_right.mid); + INITIO(tripos_right.right); + INITIO(trrs_left.tip_badgelink_enable); + INITIO(trrs_left.ring_badgelink_enable); + INITIO(trrs_right.tip_badgelink_enable); + INITIO(trrs_right.ring_badgelink_enable); + + return _portexp_update(); } -static flow3r_bsp_tripos_state_t _tripos_get(const flow3r_bsp_iodef_tripos_t *io) { - if (_iopin_get_pin(&io->mid)) { - return flow3r_bsp_tripos_mid; - } - if (_iopin_get_pin(&io->left)) { - return flow3r_bsp_tripos_left; - } - if (_iopin_get_pin(&io->right)) { - return flow3r_bsp_tripos_right; - } - return flow3r_bsp_tripos_none; +esp_err_t flow3r_bsp_spio_update(void) { return _portexp_update(); } + +static flow3r_bsp_tripos_state_t _tripos_get( + const flow3r_bsp_iodef_tripos_t *io) { + if (_iopin_get_pin(&io->mid)) { + return flow3r_bsp_tripos_mid; + } + if (_iopin_get_pin(&io->left)) { + return flow3r_bsp_tripos_left; + } + if (_iopin_get_pin(&io->right)) { + return flow3r_bsp_tripos_right; + } + return flow3r_bsp_tripos_none; } flow3r_bsp_tripos_state_t flow3r_bsp_spio_left_button_get(void) { - return _tripos_get(&iodef.tripos_left); + return _tripos_get(&iodef.tripos_left); } flow3r_bsp_tripos_state_t flow3r_bsp_spio_right_button_get(void) { - return _tripos_get(&iodef.tripos_right); + return _tripos_get(&iodef.tripos_right); } bool flow3r_bsp_spio_charger_state_get(void) { - return _iopin_get_pin(&iodef.charger_state); + return _iopin_get_pin(&iodef.charger_state); } bool flow3r_bsp_spio_jacksense_right_get(void) { - return _iopin_get_pin(&iodef.jacksense_right); + return _iopin_get_pin(&iodef.jacksense_right); } void flow3r_bsp_spio_badgelink_left_enable(bool tip_on, bool ring_on) { - _iopin_set_pin(&iodef.trrs_left.ring_badgelink_enable, ring_on); - _iopin_set_pin(&iodef.trrs_left.tip_badgelink_enable, tip_on); + _iopin_set_pin(&iodef.trrs_left.ring_badgelink_enable, ring_on); + _iopin_set_pin(&iodef.trrs_left.tip_badgelink_enable, tip_on); } void flow3r_bsp_spio_badgelink_right_enable(bool tip_on, bool ring_on) { - _iopin_set_pin(&iodef.trrs_right.ring_badgelink_enable, ring_on); - _iopin_set_pin(&iodef.trrs_right.tip_badgelink_enable, tip_on); + _iopin_set_pin(&iodef.trrs_right.ring_badgelink_enable, ring_on); + _iopin_set_pin(&iodef.trrs_right.tip_badgelink_enable, tip_on); } diff --git a/components/micropython/include/mpconfigboard.h b/components/micropython/include/mpconfigboard.h index fab8e7180c..66690b5291 100644 --- a/components/micropython/include/mpconfigboard.h +++ b/components/micropython/include/mpconfigboard.h @@ -8,7 +8,6 @@ #define MICROPY_HW_I2C0_SDA (8) #define MICROPY_HW_ESP32S3_EXTENDED_IO (0) - #define MICROPY_ESP_IDF_4 1 #define MICROPY_VFS_POSIX 1 diff --git a/components/st3m/st3m_audio.c b/components/st3m/st3m_audio.c index 3abb26f9ce..9084597757 100644 --- a/components/st3m/st3m_audio.c +++ b/components/st3m/st3m_audio.c @@ -1,15 +1,15 @@ #include "st3m_audio.h" -#include <stdio.h> #include <math.h> +#include <stdio.h> #include <string.h> #include "flow3r_bsp.h" +#include "esp_log.h" #include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "freertos/semphr.h" -#include "esp_log.h" +#include "freertos/task.h" static const char *TAG = "st3m-audio"; @@ -22,7 +22,8 @@ static bool _headphones_connected(void); // used for exp(vol_dB * NAT_LOG_DB) #define NAT_LOG_DB 0.1151292546497023 -// placeholder for "fake mute" -inf dB (we know floats can do that but we have trust issues when using NAN) +// placeholder for "fake mute" -inf dB (we know floats can do that but we have +// trust issues when using NAN) #define SILLY_LOW_VOLUME_DB (-10000.) const static float headphones_maximum_volume_system_dB = 3; @@ -49,9 +50,7 @@ typedef struct st3m_audio_output { // Apply output settings to actual output channel, calculate software volume if // output is active. -static void _output_apply(st3m_audio_output_t *out) { - out->apply(out); -} +static void _output_apply(st3m_audio_output_t *out) { out->apply(out); } static void _output_set_mute(st3m_audio_output_t *out, bool mute) { out->mute = mute; @@ -82,7 +81,8 @@ static float _output_adjust_volume(st3m_audio_output_t *out, float vol_dB) { return out->volume; } -static float _output_set_maximum_volume(st3m_audio_output_t *out, float vol_dB) { +static float _output_set_maximum_volume(st3m_audio_output_t *out, + float vol_dB) { if (vol_dB > out->volume_max_system) { vol_dB = out->volume_max_system; } @@ -97,11 +97,12 @@ static float _output_set_maximum_volume(st3m_audio_output_t *out, float vol_dB) return vol_dB; } -static float _output_set_minimum_volume(st3m_audio_output_t *out, float vol_dB) { +static float _output_set_minimum_volume(st3m_audio_output_t *out, + float vol_dB) { if (vol_dB > out->volume_max) { vol_dB = out->volume_max; } - if (vol_dB+1 < SILLY_LOW_VOLUME_DB) { + if (vol_dB + 1 < SILLY_LOW_VOLUME_DB) { vol_dB = SILLY_LOW_VOLUME_DB + 1.; } out->volume_min = vol_dB; @@ -115,18 +116,18 @@ static float _output_set_minimum_volume(st3m_audio_output_t *out, float vol_dB) static float _output_get_volume_relative(st3m_audio_output_t *out) { float ret = out->volume; // fake mute - if(ret < out->volume_min) return 0; + if (ret < out->volume_min) return 0; float vol_range = out->volume_max - out->volume_min; // shift to above zero ret -= out->volume_min; // restrict to 0..1 range ret /= vol_range; // shift to 0.01 to 0.99 range to distinguish from fake mute - return (ret*0.99) + 0.01; + return (ret * 0.99) + 0.01; } // Output apply function for headphones. -static void _audio_headphones_apply(st3m_audio_output_t *out){ +static void _audio_headphones_apply(st3m_audio_output_t *out) { bool mute = out->mute; float vol_dB = out->volume; @@ -135,17 +136,20 @@ static void _audio_headphones_apply(st3m_audio_output_t *out){ mute = true; } - float hardware_volume_dB = flow3r_bsp_audio_headphones_set_volume(mute, vol_dB); + float hardware_volume_dB = + flow3r_bsp_audio_headphones_set_volume(mute, vol_dB); // do the fine steps in software - // note: synchronizing both hw and software volume changes is somewhat tricky + // note: synchronizing both hw and software volume changes is somewhat + // tricky float software_volume_dB = vol_dB - hardware_volume_dB; - if(software_volume_dB > 0) software_volume_dB = 0; - out->volume_software = (int32_t) (32768 * exp(software_volume_dB * NAT_LOG_DB)); + if (software_volume_dB > 0) software_volume_dB = 0; + out->volume_software = + (int32_t)(32768 * exp(software_volume_dB * NAT_LOG_DB)); } // Output apply function for speaker. -static void _audio_speaker_apply(st3m_audio_output_t *out){ +static void _audio_speaker_apply(st3m_audio_output_t *out) { bool mute = out->mute; float vol_dB = out->volume; @@ -154,13 +158,16 @@ static void _audio_speaker_apply(st3m_audio_output_t *out){ mute = true; } - float hardware_volume_dB = flow3r_bsp_audio_speaker_set_volume(mute, vol_dB); + float hardware_volume_dB = + flow3r_bsp_audio_speaker_set_volume(mute, vol_dB); // do the fine steps in software - // note: synchronizing both hw and software volume changes is somewhat tricky + // note: synchronizing both hw and software volume changes is somewhat + // tricky float software_volume_dB = vol_dB - hardware_volume_dB; - if(software_volume_dB > 0) software_volume_dB = 0; - out->volume_software = (int32_t) (32768. * exp(software_volume_dB * NAT_LOG_DB)); + if (software_volume_dB > 0) software_volume_dB = 0; + out->volume_software = + (int32_t)(32768. * exp(software_volume_dB * NAT_LOG_DB)); } // Global state structure. Guarded by state_mutex. @@ -192,28 +199,31 @@ SemaphoreHandle_t state_mutex; #define UNLOCK xSemaphoreGiveRecursive(state_mutex) static st3m_audio_state_t state = { - .jacksense = { - .headphones = false, - .headset = false, - .line_in = false, - }, + .jacksense = + { + .headphones = false, + .headset = false, + .line_in = false, + }, .headphones_detection_override = false, - .headphones = { - .volume = 0, - .mute = false, - .volume_max = headphones_maximum_volume_system_dB, - .volume_min = headphones_maximum_volume_system_dB - 70, - .volume_max_system = headphones_maximum_volume_system_dB, - .apply = _audio_headphones_apply, - }, - .speaker = { - .volume = 0, - .mute = false, - .volume_max = speaker_maximum_volume_system_dB, - .volume_min = speaker_maximum_volume_system_dB - 60, - .volume_max_system = speaker_maximum_volume_system_dB, - .apply = _audio_speaker_apply, - }, + .headphones = + { + .volume = 0, + .mute = false, + .volume_max = headphones_maximum_volume_system_dB, + .volume_min = headphones_maximum_volume_system_dB - 70, + .volume_max_system = headphones_maximum_volume_system_dB, + .apply = _audio_headphones_apply, + }, + .speaker = + { + .volume = 0, + .mute = false, + .volume_max = speaker_maximum_volume_system_dB, + .volume_min = speaker_maximum_volume_system_dB - 60, + .volume_max_system = speaker_maximum_volume_system_dB, + .apply = _audio_speaker_apply, + }, .source = st3m_audio_input_source_none, .headset_gain = 0, @@ -238,16 +248,18 @@ static void _update_jacksense() { // Update volume to trigger mutes if needed. But only do that if the // jacks actually changed. LOCK; - if (memcmp(&state.jacksense, &st, sizeof(flow3r_bsp_audio_jacksense_state_t)) != 0) { - memcpy(&state.jacksense, &st, sizeof(flow3r_bsp_audio_jacksense_state_t)); + if (memcmp(&state.jacksense, &st, + sizeof(flow3r_bsp_audio_jacksense_state_t)) != 0) { + memcpy(&state.jacksense, &st, + sizeof(flow3r_bsp_audio_jacksense_state_t)); _output_apply(&state.speaker); _output_apply(&state.headphones); } UNLOCK; } -void st3m_audio_player_function_dummy(int16_t * rx, int16_t * tx, uint16_t len){ - for(uint16_t i = 0; i < len; i++){ +void st3m_audio_player_function_dummy(int16_t *rx, int16_t *tx, uint16_t len) { + for (uint16_t i = 0; i < len; i++) { tx[i] = 0; } } @@ -264,8 +276,10 @@ void st3m_audio_init(void) { _output_apply(&state.speaker); _output_apply(&state.headphones); - xTaskCreate(&_audio_player_task, "audio", 3000, NULL, configMAX_PRIORITIES - 1, NULL); - xTaskCreate(&_jacksense_update_task, "jacksense", 2048, NULL, configMAX_PRIORITIES - 2, NULL); + xTaskCreate(&_audio_player_task, "audio", 3000, NULL, + configMAX_PRIORITIES - 1, NULL); + xTaskCreate(&_jacksense_update_task, "jacksense", 2048, NULL, + configMAX_PRIORITIES - 2, NULL); ESP_LOGI(TAG, "Audio task started"); } @@ -280,38 +294,43 @@ static void _audio_player_task(void *data) { bool hwmute = flow3r_bsp_audio_has_hardware_mute(); - while(true) { + while (true) { count = 0; flow3r_bsp_audio_read(buffer_rx, sizeof(buffer_rx), &count, 1000); if (count != sizeof(buffer_rx)) { - ESP_LOGE(TAG, "audio_read: count (%d) != length (%d)\n", count, sizeof(buffer_rx)); + ESP_LOGE(TAG, "audio_read: count (%d) != length (%d)\n", count, + sizeof(buffer_rx)); abort(); } LOCK; bool headphones = _headphones_connected(); st3m_audio_player_function_t function = state.function; - int32_t software_volume = headphones ? state.headphones.volume_software : state.speaker.volume_software; - bool software_mute = headphones ? state.headphones.mute : state.speaker.mute; + int32_t software_volume = headphones ? state.headphones.volume_software + : state.speaker.volume_software; + bool software_mute = + headphones ? state.headphones.mute : state.speaker.mute; bool input_thru_mute = state.input_thru_mute; int32_t input_thru_vol_int = state.input_thru_vol_int; UNLOCK; - (*function)(buffer_rx, buffer_tx, FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE*2); + (*function)(buffer_rx, buffer_tx, FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE * 2); if (!hwmute && software_mute) { // Software muting needed. Only used on P1. - for(int i = 0; i < (FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE * 2); i += 2){ + for (int i = 0; i < (FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE * 2); + i += 2) { buffer_tx[i] = 0; } } else { - for(int i = 0; i < (FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE * 2); i += 2){ + for (int i = 0; i < (FLOW3R_BSP_AUDIO_DMA_BUFFER_SIZE * 2); + i += 2) { int32_t acc = buffer_tx[i]; acc = (acc * software_volume) >> 15; - if(!input_thru_mute){ - acc += (((int32_t) buffer_rx[i]) * input_thru_vol_int) >> 15; + if (!input_thru_mute) { + acc += (((int32_t)buffer_rx[i]) * input_thru_vol_int) >> 15; } buffer_tx[i] = acc; } @@ -319,24 +338,23 @@ static void _audio_player_task(void *data) { flow3r_bsp_audio_write(buffer_tx, sizeof(buffer_tx), &count, 1000); if (count != sizeof(buffer_tx)) { - ESP_LOGE(TAG, "audio_write: count (%d) != length (%d)\n", count, sizeof(buffer_tx)); + ESP_LOGE(TAG, "audio_write: count (%d) != length (%d)\n", count, + sizeof(buffer_tx)); abort(); } - } } -static void _jacksense_update_task(void * data) { +static void _jacksense_update_task(void *data) { (void)data; TickType_t last_wake = xTaskGetTickCount(); - while(1) { - vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(100)); // 10 Hz + while (1) { + vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(100)); // 10 Hz _update_jacksense(); } } - // BSP wrappers that don't need locking. void st3m_audio_headphones_line_in_set_hardware_thru(bool enable) { @@ -353,20 +371,24 @@ void st3m_audio_line_in_set_hardware_thru(bool enable) { // Locked global state getters. -#define GETTER(ty, name, accessor) ty st3m_##name(void) {\ - LOCK; \ - ty res = accessor; \ - UNLOCK; \ - return res; \ +#define GETTER(ty, name, accessor) \ + ty st3m_##name(void) { \ + LOCK; \ + ty res = accessor; \ + UNLOCK; \ + return res; \ } GETTER(bool, audio_headset_is_connected, state.jacksense.headset) -GETTER(bool, audio_headphones_is_connected, state.jacksense.headphones || state.headphones_detection_override) +GETTER(bool, audio_headphones_is_connected, + state.jacksense.headphones || state.headphones_detection_override) GETTER(float, audio_headphones_get_volume_dB, state.headphones.volume) GETTER(float, audio_speaker_get_volume_dB, state.speaker.volume) -GETTER(float, audio_headphones_get_minimum_volume_dB, state.headphones.volume_min) +GETTER(float, audio_headphones_get_minimum_volume_dB, + state.headphones.volume_min) GETTER(float, audio_speaker_get_minimum_volume_dB, state.speaker.volume_min) -GETTER(float, audio_headphones_get_maximum_volume_dB, state.headphones.volume_max) +GETTER(float, audio_headphones_get_maximum_volume_dB, + state.headphones.volume_max) GETTER(float, audio_speaker_get_maximum_volume_dB, state.speaker.volume_max) GETTER(bool, audio_headphones_get_mute, state.headphones.mute) GETTER(bool, audio_speaker_get_mute, state.speaker.mute) @@ -376,10 +398,9 @@ GETTER(float, audio_input_thru_get_volume_dB, state.input_thru_vol) GETTER(bool, audio_input_thru_get_mute, state.input_thru_mute) #undef GETTER - // Locked global API functions. -uint8_t st3m_audio_headset_set_gain_dB(uint8_t gain_dB){ +uint8_t st3m_audio_headset_set_gain_dB(uint8_t gain_dB) { flow3r_bsp_audio_headset_set_gain_dB(gain_dB); LOCK; state.headset_gain = gain_dB; @@ -389,18 +410,22 @@ uint8_t st3m_audio_headset_set_gain_dB(uint8_t gain_dB){ void st3m_audio_input_set_source(st3m_audio_input_source_t source) { switch (source) { - case st3m_audio_input_source_none: - flow3r_bsp_audio_input_set_source(flow3r_bsp_audio_input_source_none); - break; - case st3m_audio_input_source_line_in: - flow3r_bsp_audio_input_set_source(flow3r_bsp_audio_input_source_line_in); - break; - case st3m_audio_input_source_headset_mic: - flow3r_bsp_audio_input_set_source(flow3r_bsp_audio_input_source_headset_mic); - break; - case st3m_audio_input_source_onboard_mic: - flow3r_bsp_audio_input_set_source(flow3r_bsp_audio_input_source_onboard_mic); - break; + case st3m_audio_input_source_none: + flow3r_bsp_audio_input_set_source( + flow3r_bsp_audio_input_source_none); + break; + case st3m_audio_input_source_line_in: + flow3r_bsp_audio_input_set_source( + flow3r_bsp_audio_input_source_line_in); + break; + case st3m_audio_input_source_headset_mic: + flow3r_bsp_audio_input_set_source( + flow3r_bsp_audio_input_source_headset_mic); + break; + case st3m_audio_input_source_onboard_mic: + flow3r_bsp_audio_input_set_source( + flow3r_bsp_audio_input_source_onboard_mic); + break; } LOCK; state.source = source; @@ -413,16 +438,16 @@ void st3m_audio_input_thru_set_mute(bool mute) { UNLOCK; } -float st3m_audio_input_thru_set_volume_dB(float vol_dB){ - if(vol_dB > 0) vol_dB = 0; +float st3m_audio_input_thru_set_volume_dB(float vol_dB) { + if (vol_dB > 0) vol_dB = 0; LOCK; - state.input_thru_vol_int = (int32_t) (32768. * exp(vol_dB * NAT_LOG_DB)); + state.input_thru_vol_int = (int32_t)(32768. * exp(vol_dB * NAT_LOG_DB)); state.input_thru_vol = vol_dB; UNLOCK; return vol_dB; } -void st3m_audio_set_player_function(st3m_audio_player_function_t fun){ +void st3m_audio_set_player_function(st3m_audio_player_function_t fun) { LOCK; state.function = fun; UNLOCK; @@ -437,26 +462,33 @@ bool st3m_audio_headphones_are_connected(void) { // Locked output getters/setters. -#define LOCKED0(body) LOCK; body; UNLOCK -#define LOCKED(ty, body) LOCK; ty res = body; UNLOCK; return res - -void st3m_audio_headphones_set_mute(bool mute){ +#define LOCKED0(body) \ + LOCK; \ + body; \ + UNLOCK +#define LOCKED(ty, body) \ + LOCK; \ + ty res = body; \ + UNLOCK; \ + return res + +void st3m_audio_headphones_set_mute(bool mute) { LOCKED0(_output_set_mute(&state.headphones, mute)); } -void st3m_audio_speaker_set_mute(bool mute){ +void st3m_audio_speaker_set_mute(bool mute) { LOCKED0(_output_set_mute(&state.speaker, mute)); } -float st3m_audio_speaker_set_volume_dB(float vol_dB){ +float st3m_audio_speaker_set_volume_dB(float vol_dB) { LOCKED(float, _output_set_volume(&state.speaker, vol_dB)); } -float st3m_audio_headphones_set_volume_dB(float vol_dB){ +float st3m_audio_headphones_set_volume_dB(float vol_dB) { LOCKED(float, _output_set_volume(&state.headphones, vol_dB)); } -void st3m_audio_headphones_detection_override(bool enable){ +void st3m_audio_headphones_detection_override(bool enable) { LOCK; state.headphones_detection_override = enable; _output_apply(&state.headphones); @@ -464,27 +496,27 @@ void st3m_audio_headphones_detection_override(bool enable){ UNLOCK; } -float st3m_audio_headphones_adjust_volume_dB(float vol_dB){ +float st3m_audio_headphones_adjust_volume_dB(float vol_dB) { LOCKED(float, _output_adjust_volume(&state.headphones, vol_dB)); } -float st3m_audio_speaker_adjust_volume_dB(float vol_dB){ +float st3m_audio_speaker_adjust_volume_dB(float vol_dB) { LOCKED(float, _output_adjust_volume(&state.speaker, vol_dB)); } -float st3m_audio_headphones_set_maximum_volume_dB(float vol_dB){ +float st3m_audio_headphones_set_maximum_volume_dB(float vol_dB) { LOCKED(float, _output_set_maximum_volume(&state.headphones, vol_dB)); } -float st3m_audio_headphones_set_minimum_volume_dB(float vol_dB){ +float st3m_audio_headphones_set_minimum_volume_dB(float vol_dB) { LOCKED(float, _output_set_minimum_volume(&state.headphones, vol_dB)); } -float st3m_audio_speaker_set_maximum_volume_dB(float vol_dB){ +float st3m_audio_speaker_set_maximum_volume_dB(float vol_dB) { LOCKED(float, _output_set_maximum_volume(&state.speaker, vol_dB)); } -float st3m_audio_speaker_set_minimum_volume_dB(float vol_dB){ +float st3m_audio_speaker_set_minimum_volume_dB(float vol_dB) { LOCKED(float, _output_set_minimum_volume(&state.speaker, vol_dB)); } @@ -492,7 +524,7 @@ float st3m_audio_speaker_get_volume_relative(void) { LOCKED(float, _output_get_volume_relative(&state.speaker)); } -float st3m_audio_headphones_get_volume_relative(){ +float st3m_audio_headphones_get_volume_relative() { LOCKED(float, _output_get_volume_relative(&state.headphones)); } @@ -500,38 +532,41 @@ float st3m_audio_headphones_get_volume_relative(){ // sure we don't race between output detection and applying the function to the // current output. -#define DISPATCH_TY_TY(ty, ty2, name) ty st3m_audio_##name(ty2 arg) { \ - ty res; \ - LOCK; \ - if (_headphones_connected()) { \ +#define DISPATCH_TY_TY(ty, ty2, name) \ + ty st3m_audio_##name(ty2 arg) { \ + ty res; \ + LOCK; \ + if (_headphones_connected()) { \ res = st3m_audio_headphones_##name(arg); \ - } else { \ - res = st3m_audio_speaker_##name(arg); \ - } \ - UNLOCK; \ - return res; \ + } else { \ + res = st3m_audio_speaker_##name(arg); \ + } \ + UNLOCK; \ + return res; \ } -#define DISPATCH_TY_VOID(ty, name) ty st3m_audio_##name(void) { \ - ty res; \ - LOCK; \ - if (_headphones_connected()) { \ +#define DISPATCH_TY_VOID(ty, name) \ + ty st3m_audio_##name(void) { \ + ty res; \ + LOCK; \ + if (_headphones_connected()) { \ res = st3m_audio_headphones_##name(); \ - } else { \ - res = st3m_audio_speaker_##name(); \ - } \ - UNLOCK; \ - return res; \ + } else { \ + res = st3m_audio_speaker_##name(); \ + } \ + UNLOCK; \ + return res; \ } -#define DISPATCH_VOID_TY(ty, name) void st3m_audio_##name(ty arg) { \ - LOCK; \ - if (_headphones_connected()) { \ +#define DISPATCH_VOID_TY(ty, name) \ + void st3m_audio_##name(ty arg) { \ + LOCK; \ + if (_headphones_connected()) { \ st3m_audio_headphones_##name(arg); \ - } else { \ - st3m_audio_speaker_##name(arg); \ - } \ - UNLOCK; \ + } else { \ + st3m_audio_speaker_##name(arg); \ + } \ + UNLOCK; \ } DISPATCH_TY_TY(float, float, adjust_volume_dB) diff --git a/components/st3m/st3m_audio.h b/components/st3m/st3m_audio.h index 76fdcb5e37..727493ff37 100644 --- a/components/st3m/st3m_audio.h +++ b/components/st3m/st3m_audio.h @@ -1,31 +1,32 @@ #pragma once -#include <stdint.h> #include <stdbool.h> +#include <stdint.h> typedef enum { - st3m_audio_input_source_none = 0, - // Line in on riht jack. - st3m_audio_input_source_line_in = 1, - // Headset microphone on left jack. - st3m_audio_input_source_headset_mic = 2, - // Onboard microphone (enabled red LED). - st3m_audio_input_source_onboard_mic = 3 + st3m_audio_input_source_none = 0, + // Line in on riht jack. + st3m_audio_input_source_line_in = 1, + // Headset microphone on left jack. + st3m_audio_input_source_headset_mic = 2, + // Onboard microphone (enabled red LED). + st3m_audio_input_source_onboard_mic = 3 } st3m_audio_input_source_t; -typedef void (*st3m_audio_player_function_t)(int16_t * tx, int16_t * rx, uint16_t len); +typedef void (*st3m_audio_player_function_t)(int16_t* tx, int16_t* rx, + uint16_t len); /* The default audio task takes a function of prototype - * &st3m_audio_player_function_t, loops it and sets software volume/adds software - * thru. tx is the stereo zipped l/r output, rx is the stereo zipped input, each - * buffer the size of len. + * &st3m_audio_player_function_t, loops it and sets software volume/adds + * software thru. tx is the stereo zipped l/r output, rx is the stereo zipped + * input, each buffer the size of len. */ void st3m_audio_set_player_function(st3m_audio_player_function_t fun); -/* Dummy for st3m_audio_set_player_function that just writes zeros to the output. - * Default state. +/* Dummy for st3m_audio_set_player_function that just writes zeros to the + * output. Default state. */ -void st3m_audio_player_function_dummy(int16_t * rx, int16_t * tx, uint16_t len); +void st3m_audio_player_function_dummy(int16_t* rx, int16_t* tx, uint16_t len); /* Initializes I2S bus, the audio task and required data structures. * Expects an initialized I2C bus, will fail ungracefully otherwise (TODO). @@ -72,7 +73,7 @@ float st3m_audio_headphones_adjust_volume_dB(float vol_dB); float st3m_audio_speaker_adjust_volume_dB(float vol_dB); float st3m_audio_adjust_volume_dB(float vol_dB); -/* Returns volume as set with st3m_audio_{headphones/speaker}_set_volume_dB. The +/* Returns volume as set with st3m_audio_{headphones/speaker}_set_volume_dB. The * unspecified variant automatically chooses the adequate channel (**). */ float st3m_audio_headphones_get_volume_dB(void); @@ -98,16 +99,16 @@ bool st3m_audio_speaker_get_mute(void); bool st3m_audio_get_mute(void); /* Set the minimum and maximum allowed volume levels for speakers and headphones - * respectively. Clamps with hardware limitations. Maximum clamps below the minimum - * value, minimum clamps above the maximum. Returns clamped value. + * respectively. Clamps with hardware limitations. Maximum clamps below the + * minimum value, minimum clamps above the maximum. Returns clamped value. */ float st3m_audio_headphones_set_minimum_volume_dB(float vol_dB); float st3m_audio_headphones_set_maximum_volume_dB(float vol_dB); float st3m_audio_speaker_set_minimum_volume_dB(float vol_dB); float st3m_audio_speaker_set_maximum_volume_dB(float vol_dB); -/* Returns the minimum and maximum allowed volume levels for speakers and headphones - * respectively. Change with +/* Returns the minimum and maximum allowed volume levels for speakers and + * headphones respectively. Change with * st3m_audio_{headphones/speaker}_set_{minimum/maximum}_volume_dB. */ float st3m_audio_headphones_get_minimum_volume_dB(void); @@ -132,7 +133,8 @@ float st3m_audio_get_volume_relative(void); /* These route whatever is on the line in port directly to the headphones or * speaker respectively (enable = 1), or don't (enable = 0). Is affected by mute - * and coarse hardware volume settings, however software fine volume is not applied. + * and coarse hardware volume settings, however software fine volume is not + * applied. * * Good for testing, might deprecate later, idk~ */ @@ -140,11 +142,12 @@ void st3m_audio_headphones_line_in_set_hardware_thru(bool enable); void st3m_audio_speaker_line_in_set_hardware_thru(bool enable); void st3m_audio_line_in_set_hardware_thru(bool enable); -/* The codec can transmit audio data from different sources. This function enables - * one or no source as provided by the st3m_audio_input_source_t enum. +/* The codec can transmit audio data from different sources. This function + * enables one or no source as provided by the st3m_audio_input_source_t enum. * * Note: The onboard digital mic turns on an LED on the top board if it receives - * a clock signal which is considered a good proxy for its capability of reading data. + * a clock signal which is considered a good proxy for its capability of reading + * data. * * TODO: check if sources are available */ @@ -173,14 +176,15 @@ HEADPHONE PORT POLICY Under normal circumstances it is an important feature to have a reliable speaker mute when plugging in headphones. However, since the headphone port on the badge -can also be used for badge link, there are legimate cases where it is desirable to -have the speakers unmuted while a cable is plugged into the jack. +can also be used for badge link, there are legimate cases where it is desirable +to have the speakers unmuted while a cable is plugged into the jack. -As a person who plugs in the headphones on the tram, doesn't put them on, turns on -music to check if it's not accidentially playing on speakers and then finally puts -on headphones (temporarily, of course, intermittent checks if the speakers didn't -magically turn on are scheduled according to our general anxiety level) we wish to -make it difficult to accidentially have sound coming from the speakers. +As a person who plugs in the headphones on the tram, doesn't put them on, turns +on music to check if it's not accidentially playing on speakers and then finally +puts on headphones (temporarily, of course, intermittent checks if the speakers +didn't magically turn on are scheduled according to our general anxiety level) +we wish to make it difficult to accidentially have sound coming from the +speakers. Our proposed logic is as follows (excluding boot conditions): @@ -192,19 +196,20 @@ of digital data before the software can react to the state change. 2) If the software detects that the headphone jack has changed from unplugged to plugged it *always* turns off speakers, no exceptions. -3) If a user wishes to TX on headphone badge link, they must confirm a warning that -having headphones plugged in may potentially cause hearing damage *every time*. +3) If a user wishes to TX on headphone badge link, they must confirm a warning +that having headphones plugged in may potentially cause hearing damage *every +time*. -4) If a user wishes to RX or TX on headphone badge link while playing sound on the -onboard speakers, they must confirm a warning *every time*. +4) If a user wishes to RX or TX on headphone badge link while playing sound on +the onboard speakers, they must confirm a warning *every time*. We understand that these means seem extreme, but we find them to be a sensible default configuration to make sure people can safely operate the device without needing to refer to a manual. (TX here means any state that is not constantly ~GND with whatever impedance. -While there are current limiting resistors (value TBD at the time of writing, but -presumably 100R-470R) in series with the GPIOs, they still can generate quite some -volume with standard 40Ohm-ish headphones. Ideally the analog switch will never -switch to the GPIOs without a cable plugged in.) +While there are current limiting resistors (value TBD at the time of writing, +but presumably 100R-470R) in series with the GPIOs, they still can generate +quite some volume with standard 40Ohm-ish headphones. Ideally the analog switch +will never switch to the GPIOs without a cable plugged in.) */ diff --git a/components/st3m/st3m_board_startup.c b/components/st3m/st3m_board_startup.c index 6159a9da9d..808e9d7ea0 100644 --- a/components/st3m/st3m_board_startup.c +++ b/components/st3m/st3m_board_startup.c @@ -1,18 +1,18 @@ -#include "st3m_gfx.h" -#include "st3m_fs.h" -#include "st3m_audio.h" -#include "st3m_scope.h" -#include "st3m_leds.h" -#include "st3m_io.h" -#include "st3m_console.h" -#include "st3m_usb.h" #include "bl00mbox.h" #include "flow3r_bsp.h" +#include "st3m_audio.h" +#include "st3m_console.h" +#include "st3m_fs.h" +#include "st3m_gfx.h" +#include "st3m_io.h" +#include "st3m_leds.h" #include "st3m_mode.h" +#include "st3m_scope.h" +#include "st3m_usb.h" +#include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_log.h" // Called by micropython via MICROPY_BOARD_STARTUP. void st3m_board_startup(void) { @@ -26,7 +26,7 @@ void st3m_board_startup(void) { } // Display should've flushed by now. Turn on backlight. flow3r_bsp_display_set_backlight(100); - + st3m_mode_init(); st3m_mode_set(st3m_mode_kind_starting, "st3m"); st3m_mode_update_display(NULL); @@ -49,11 +49,10 @@ void st3m_board_startup(void) { puts("| _| | . | | | |_ | _| . | .'| . | . | -_|"); puts("|_| |_|___|_____|___|_| |___|__,|___|_ |___|"); puts(" |___|"); - + // Load bearing delay. USB crashes otherwise? // TODO(q3k): debug this - vTaskDelay(100/portTICK_PERIOD_MS); - + vTaskDelay(100 / portTICK_PERIOD_MS); flow3r_bsp_i2c_init(); st3m_mode_set(st3m_mode_kind_starting, "fs"); diff --git a/components/st3m/st3m_colors.c b/components/st3m/st3m_colors.c index 7d4cb9b7da..9069755e38 100644 --- a/components/st3m/st3m_colors.c +++ b/components/st3m/st3m_colors.c @@ -5,14 +5,11 @@ st3m_rgb_t st3m_hsv_to_rgb(st3m_hsv_t hsv) { double r = 0, g = 0, b = 0; - if (hsv.s == 0) - { + if (hsv.s == 0) { r = hsv.v; g = hsv.v; b = hsv.v; - } - else - { + } else { int i; double f, p, q, t; @@ -28,8 +25,7 @@ st3m_rgb_t st3m_hsv_to_rgb(st3m_hsv_t hsv) { q = hsv.v * (1.0 - (hsv.s * f)); t = hsv.v * (1.0 - (hsv.s * (1.0 - f))); - switch (i) - { + switch (i) { case 0: r = hsv.v; g = t; @@ -66,13 +62,12 @@ st3m_rgb_t st3m_hsv_to_rgb(st3m_hsv_t hsv) { b = q; break; } - } st3m_rgb_t rgb = { - .r = r * 255, - .g = g * 255, - .b = b * 255, - }; - return rgb; + .r = r * 255, + .g = g * 255, + .b = b * 255, + }; + return rgb; } diff --git a/components/st3m/st3m_colors.h b/components/st3m/st3m_colors.h index 78216d7e52..450218be76 100644 --- a/components/st3m/st3m_colors.h +++ b/components/st3m/st3m_colors.h @@ -3,15 +3,15 @@ #include <stdint.h> typedef struct { - // From 0 to 255. + // From 0 to 255. - uint8_t r; - uint8_t g; - uint8_t b; + uint8_t r; + uint8_t g; + uint8_t b; } st3m_rgb_t; typedef struct { - // From 0.0 to 1.0. + // From 0.0 to 1.0. double h; double s; diff --git a/components/st3m/st3m_console.c b/components/st3m/st3m_console.c index d1f61e8a31..9824802280 100644 --- a/components/st3m/st3m_console.c +++ b/components/st3m/st3m_console.c @@ -1,13 +1,13 @@ #include "st3m_console.h" -#include <sys/fcntl.h> #include <sys/errno.h> +#include <sys/fcntl.h> -#include "esp_vfs.h" -#include "esp_vfs_dev.h" +#include "driver/uart.h" #include "esp_log.h" #include "esp_timer.h" -#include "driver/uart.h" +#include "esp_vfs.h" +#include "esp_vfs_dev.h" #include "freertos/FreeRTOS.h" #include "freertos/ringbuf.h" #include "freertos/semphr.h" @@ -16,101 +16,103 @@ static const char *TAG = "st3m-console"; static const uart_port_t uart_num = UART_NUM_0; static esp_vfs_t _vfs; - typedef struct { - int flags; + int flags; } st3m_console_file_t; typedef struct { - SemaphoreHandle_t mu; - RingbufHandle_t txbuf; - RingbufHandle_t rxbuf; + SemaphoreHandle_t mu; + RingbufHandle_t txbuf; + RingbufHandle_t rxbuf; - st3m_console_file_t fstdin; - st3m_console_file_t fstdout; - st3m_console_file_t fstderr; + st3m_console_file_t fstdin; + st3m_console_file_t fstdout; + st3m_console_file_t fstderr; - int64_t cdc_last_read; + int64_t cdc_last_read; - int64_t cdc_first_read_at; + int64_t cdc_first_read_at; } st3m_console_state_t; static st3m_console_state_t _state = { - .mu = NULL, - .txbuf = NULL, - .rxbuf = NULL, + .mu = NULL, + .txbuf = NULL, + .rxbuf = NULL, - .fstdin = { 0 }, - .fstdout = { 0 }, - .fstderr = { 0 }, + .fstdin = {0}, + .fstdout = {0}, + .fstderr = {0}, - .cdc_last_read = 0, - .cdc_first_read_at = 0, + .cdc_last_read = 0, + .cdc_first_read_at = 0, }; static void _uart0_write(const char *buffer, size_t bufsize) { - // Skip entire writes that would block, as we don't want to limit stdout - // speed to 115200 baud. - size_t free_bytes = 0; - uart_get_tx_buffer_free_size(uart_num, &free_bytes); - if (bufsize > free_bytes) { - return; - } - - uart_write_bytes(uart_num, buffer, bufsize); + // Skip entire writes that would block, as we don't want to limit stdout + // speed to 115200 baud. + size_t free_bytes = 0; + uart_get_tx_buffer_free_size(uart_num, &free_bytes); + if (bufsize > free_bytes) { + return; + } + + uart_write_bytes(uart_num, buffer, bufsize); } int st3m_uart0_debug(const char *fmt, ...) { - va_list args; + va_list args; va_start(args, fmt); - char buf[256]; - memset(buf, 0, 256); - vsnprintf(buf, 256, fmt, args); - _uart0_write(buf, strlen(buf)); + char buf[256]; + memset(buf, 0, 256); + vsnprintf(buf, 256, fmt, args); + _uart0_write(buf, strlen(buf)); - va_end(args); - return strlen(buf); + va_end(args); + return strlen(buf); } void st3m_console_init(void) { - assert(_state.mu == NULL); - _state.mu = xSemaphoreCreateMutex(); - assert(_state.mu != NULL); - - _state.txbuf = xRingbufferCreate(1024, RINGBUF_TYPE_BYTEBUF); - assert(_state.txbuf != NULL); - _state.rxbuf = xRingbufferCreate(1024, RINGBUF_TYPE_BYTEBUF); - assert(_state.rxbuf != NULL); - - // Initialize debug UART0. - uart_config_t uart_config = { - .baud_rate = 115200, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, - .rx_flow_ctrl_thresh = 122, - }; - ESP_ERROR_CHECK(uart_driver_install(uart_num, 256, 256, 0, NULL, ESP_INTR_FLAG_LOWMED)); - ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); - const char *msg = "\r\n\r\nThis is flow3r's UART0. Good luck with whatever brings you here.\r\n"; - _uart0_write(msg, strlen(msg)); - - esp_err_t res = esp_vfs_register("/console", &_vfs, NULL); - if (res != ESP_OK) { - ESP_LOGE(TAG, "vfs mount failed"); - abort(); - } - - // Switch over to new console. From now on, all stdio is going through this - // code. - ESP_LOGI(TAG, "switching to st3m console..."); - fflush(stdout); - freopen("/console/stdin", "r", stdin); - freopen("/console/stdout", "w", stdout); - freopen("/console/stderr", "w", stderr); - ESP_LOGI(TAG, "st3m console running"); + assert(_state.mu == NULL); + _state.mu = xSemaphoreCreateMutex(); + assert(_state.mu != NULL); + + _state.txbuf = xRingbufferCreate(1024, RINGBUF_TYPE_BYTEBUF); + assert(_state.txbuf != NULL); + _state.rxbuf = xRingbufferCreate(1024, RINGBUF_TYPE_BYTEBUF); + assert(_state.rxbuf != NULL); + + // Initialize debug UART0. + uart_config_t uart_config = { + .baud_rate = 115200, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .rx_flow_ctrl_thresh = 122, + }; + ESP_ERROR_CHECK( + uart_driver_install(uart_num, 256, 256, 0, NULL, ESP_INTR_FLAG_LOWMED)); + ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); + const char *msg = + "\r\n\r\nThis is flow3r's UART0. Good luck with whatever brings you " + "here.\r\n"; + _uart0_write(msg, strlen(msg)); + + esp_err_t res = esp_vfs_register("/console", &_vfs, NULL); + if (res != ESP_OK) { + ESP_LOGE(TAG, "vfs mount failed"); + abort(); + } + + // Switch over to new console. From now on, all stdio is going through this + // code. + ESP_LOGI(TAG, "switching to st3m console..."); + fflush(stdout); + freopen("/console/stdin", "r", stdin); + freopen("/console/stdout", "w", stdout); + freopen("/console/stderr", "w", stderr); + ESP_LOGI(TAG, "st3m console running"); } // From Micropython. Used to let it know there's an interrupt. @@ -119,23 +121,23 @@ extern int mp_interrupt_char; // Called by st3m_usb_cdc when it receives some data from the host. void st3m_console_cdc_on_rx(const uint8_t *buffer, size_t bufsize) { - bool interrupted = false; - const uint8_t *bufbytes = buffer; - for (size_t i = 0; i < bufsize; i++) { - if (bufbytes[i] == mp_interrupt_char) { - interrupted = true; - break; - } - } - - if (interrupted) { - mp_sched_keyboard_interrupt(); - } - - // We can't really do much if the ringbuffer is full, at least not with - // TinyUSB? Because I think CDC-ACM lets you apply backpressure, but I"m not - // sure that is exposed via TinyUSB's API... - xRingbufferSend(_state.rxbuf, buffer, bufsize, 0); + bool interrupted = false; + const uint8_t *bufbytes = buffer; + for (size_t i = 0; i < bufsize; i++) { + if (bufbytes[i] == mp_interrupt_char) { + interrupted = true; + break; + } + } + + if (interrupted) { + mp_sched_keyboard_interrupt(); + } + + // We can't really do much if the ringbuffer is full, at least not with + // TinyUSB? Because I think CDC-ACM lets you apply backpressure, but I"m not + // sure that is exposed via TinyUSB's API... + xRingbufferSend(_state.rxbuf, buffer, bufsize, 0); } void st3m_gfx_splash(const char *c); @@ -143,247 +145,251 @@ void st3m_gfx_splash(const char *c); // Called by st3m_usb_cdc when it has the opportunity to send some data to the // host. size_t st3m_console_cdc_on_txpoll(uint8_t *buffer, size_t bufsize) { - // I have no idea why this is needed, but it is. Otherwise a large backlog - // of data cuases the IN endpoint to get stuck. - // - // I've spend three days debugging this. - // - // No, I'm not fine. Thanks for asking, though. I appreciate it. - if (bufsize > 0) { - bufsize -= 1; - } - - int64_t now = esp_timer_get_time(); - - xSemaphoreTake(_state.mu, portMAX_DELAY); - - // Make note of when the host attempts to drain the buffer - both the last - // time, and the first time. - if (bufsize > 0) { - _state.cdc_last_read = now; - } - if (_state.cdc_first_read_at == 0) { - _state.cdc_first_read_at = now; - } - int64_t since_first_read = now - _state.cdc_first_read_at; - xSemaphoreGive(_state.mu); - - // Do not transmit to host for the first second of a connection, as Linux - // tends to echo that back at us. - if (since_first_read < 1000000) { - return 0; - } - - size_t res = 0; - void *data = xRingbufferReceiveUpTo(_state.txbuf, &res, 0, bufsize); - if (data == NULL) { - return 0; - } - memcpy(buffer, data, res); - vRingbufferReturnItem(_state.txbuf, data); - return res; + // I have no idea why this is needed, but it is. Otherwise a large backlog + // of data cuases the IN endpoint to get stuck. + // + // I've spend three days debugging this. + // + // No, I'm not fine. Thanks for asking, though. I appreciate it. + if (bufsize > 0) { + bufsize -= 1; + } + + int64_t now = esp_timer_get_time(); + + xSemaphoreTake(_state.mu, portMAX_DELAY); + + // Make note of when the host attempts to drain the buffer - both the last + // time, and the first time. + if (bufsize > 0) { + _state.cdc_last_read = now; + } + if (_state.cdc_first_read_at == 0) { + _state.cdc_first_read_at = now; + } + int64_t since_first_read = now - _state.cdc_first_read_at; + xSemaphoreGive(_state.mu); + + // Do not transmit to host for the first second of a connection, as Linux + // tends to echo that back at us. + if (since_first_read < 1000000) { + return 0; + } + + size_t res = 0; + void *data = xRingbufferReceiveUpTo(_state.txbuf, &res, 0, bufsize); + if (data == NULL) { + return 0; + } + memcpy(buffer, data, res); + vRingbufferReturnItem(_state.txbuf, data); + return res; } void st3m_console_cdc_on_detach(void) { - xSemaphoreTake(_state.mu, portMAX_DELAY); - _state.cdc_first_read_at = 0; - xSemaphoreGive(_state.mu); + xSemaphoreTake(_state.mu, portMAX_DELAY); + _state.cdc_first_read_at = 0; + xSemaphoreGive(_state.mu); } static st3m_console_file_t *_file_for_fd(int fd) { - switch (fd) { - case 0: - return &_state.fstdin; - case 1: - return &_state.fstdout; - case 2: - return &_state.fstderr; - } - return NULL; + switch (fd) { + case 0: + return &_state.fstdin; + case 1: + return &_state.fstdout; + case 2: + return &_state.fstderr; + } + return NULL; } -#define FILE_GET(f, fd) st3m_console_file_t *f = _file_for_fd(fd); if (f == NULL) { errno = EBADF; return -1; } +#define FILE_GET(f, fd) \ + st3m_console_file_t *f = _file_for_fd(fd); \ + if (f == NULL) { \ + errno = EBADF; \ + return -1; \ + } static int _console_close(int fd) { - FILE_GET(f, fd); + FILE_GET(f, fd); return 0; } static int _console_fcntl(int fd, int cmd, int arg) { - FILE_GET(f, fd); - - xSemaphoreTake(_state.mu, portMAX_DELAY); - int result = 0; - switch (cmd) { - case F_GETFL: - result = f->flags; - break; - case F_SETFL: - f->flags = arg; - break; - default: - result = -1; - errno = ENOSYS; - break; - } - xSemaphoreGive(_state.mu); - return result; + FILE_GET(f, fd); + + xSemaphoreTake(_state.mu, portMAX_DELAY); + int result = 0; + switch (cmd) { + case F_GETFL: + result = f->flags; + break; + case F_SETFL: + f->flags = arg; + break; + default: + result = -1; + errno = ENOSYS; + break; + } + xSemaphoreGive(_state.mu); + return result; } static int _console_fstat(int fd, struct stat *st) { - FILE_GET(f, fd); + FILE_GET(f, fd); - memset(st, 0, sizeof(*st)); - st->st_mode = S_IFCHR; - return 0; + memset(st, 0, sizeof(*st)); + st->st_mode = S_IFCHR; + return 0; } static int _console_open(const char *path, int flags, int mode) { - xSemaphoreTake(_state.mu, portMAX_DELAY); - if (strcmp(path, "/stdin") == 0) { - _state.fstdin.flags = flags | O_NONBLOCK; - xSemaphoreGive(_state.mu); - return 0; - } - if (strcmp(path, "/stdout") == 0) { - _state.fstdout.flags = flags; - xSemaphoreGive(_state.mu); - return 1; - } - if (strcmp(path, "/stderr") == 0) { - _state.fstderr.flags = flags; - xSemaphoreGive(_state.mu); - return 2; - } - errno = ENOENT; - xSemaphoreGive(_state.mu); - return -1; + xSemaphoreTake(_state.mu, portMAX_DELAY); + if (strcmp(path, "/stdin") == 0) { + _state.fstdin.flags = flags | O_NONBLOCK; + xSemaphoreGive(_state.mu); + return 0; + } + if (strcmp(path, "/stdout") == 0) { + _state.fstdout.flags = flags; + xSemaphoreGive(_state.mu); + return 1; + } + if (strcmp(path, "/stderr") == 0) { + _state.fstderr.flags = flags; + xSemaphoreGive(_state.mu); + return 2; + } + errno = ENOENT; + xSemaphoreGive(_state.mu); + return -1; } static ssize_t _console_read(int fd, void *data, size_t size) { - FILE_GET(f, fd); - if (size == 0) { - return 0; - } - - if (fd != 0) { - return 0; - } - - xSemaphoreTake(_state.mu, portMAX_DELAY); - bool nonblock = (f->flags & O_NONBLOCK) > 0; - xSemaphoreGive(_state.mu); - - TickType_t nticks = portMAX_DELAY; - if (nonblock) { - nticks = 0; - } - - size_t res = 0; - void *ready = xRingbufferReceiveUpTo(_state.rxbuf, &res, nticks, size); - if (ready == NULL) { - assert(nonblock); - errno = EAGAIN; - return -1; - } - memcpy(data, ready, res); - - vRingbufferReturnItem(_state.rxbuf, ready); - return res; + FILE_GET(f, fd); + if (size == 0) { + return 0; + } + + if (fd != 0) { + return 0; + } + + xSemaphoreTake(_state.mu, portMAX_DELAY); + bool nonblock = (f->flags & O_NONBLOCK) > 0; + xSemaphoreGive(_state.mu); + + TickType_t nticks = portMAX_DELAY; + if (nonblock) { + nticks = 0; + } + + size_t res = 0; + void *ready = xRingbufferReceiveUpTo(_state.rxbuf, &res, nticks, size); + if (ready == NULL) { + assert(nonblock); + errno = EAGAIN; + return -1; + } + memcpy(data, ready, res); + + vRingbufferReturnItem(_state.rxbuf, ready); + return res; } static bool _draining(void) { - int64_t now = esp_timer_get_time(); - xSemaphoreTake(_state.mu, portMAX_DELAY); - bool res = (now - _state.cdc_last_read) < 100000; // 100ms is consrvative enough. - xSemaphoreGive(_state.mu); - return res; + int64_t now = esp_timer_get_time(); + xSemaphoreTake(_state.mu, portMAX_DELAY); + bool res = + (now - _state.cdc_last_read) < 100000; // 100ms is consrvative enough. + xSemaphoreGive(_state.mu); + return res; } -bool st3m_console_active(void) { - return _draining(); -} +bool st3m_console_active(void) { return _draining(); } static ssize_t _console_write(int fd, const void *data, size_t size) { - FILE_GET(f, fd); - if (size == 0) { - return 0; - } - - xSemaphoreTake(_state.mu, portMAX_DELAY); - bool nonblock = (f->flags & O_NONBLOCK) > 0; - xSemaphoreGive(_state.mu); - - // Convert \n to \r\n where appropriate. - // First, calculate new length. - const char *bytes = data; - bool prev_cr = false; - size_t rnsize = 0; - for (size_t i = 0; i < size; i ++) { - char c = bytes[i]; - if (c == '\n' && !prev_cr) { - rnsize += 2; - } else { - rnsize += 1; - } - - prev_cr = c == '\r'; - } - - // Allocate appropriately-sized buffer. - char *rnbytes = malloc(rnsize); - if (rnbytes == NULL) { - return size; - } - - // And convert \n to \r\n. - size_t rnix = 0; - for (size_t i = 0; i < size; i ++) { - char c = bytes[i]; - if (c == '\n' && !prev_cr) { - rnbytes[rnix] = '\r'; - rnbytes[rnix+1] = '\n'; - rnix += 2; - } else { - rnbytes[rnix] = c; - rnix += 1; - } - - prev_cr = c == '\r'; - } - - - // Always transmit to UART0 for debugging. - _uart0_write(rnbytes, rnsize); - - // Transmit to CDC console. - if (nonblock) { - BaseType_t res = xRingbufferSend(_state.txbuf, rnbytes, rnsize, 0); - if (res == pdFALSE) { - errno = EAGAIN; - free(rnbytes); - return -1; - } - } else { - // Interruptible blocking write. We need to cancel the write if the - // ringbuffer is not being drained actively. - // - // I mean, we don't need to, but in case of a disconnected CDC-ACM we - // just pretend there's no-one receiving us so the writes truly are - // blocking, ie. blocking into /dev/null. - for (;;) { - BaseType_t res = xRingbufferSend(_state.txbuf, rnbytes, rnsize, 10 / portTICK_PERIOD_MS); - if (res == pdTRUE) { - break; - } - - if (!_draining()) { - break; - } - } - } - free(rnbytes); - return size; + FILE_GET(f, fd); + if (size == 0) { + return 0; + } + + xSemaphoreTake(_state.mu, portMAX_DELAY); + bool nonblock = (f->flags & O_NONBLOCK) > 0; + xSemaphoreGive(_state.mu); + + // Convert \n to \r\n where appropriate. + // First, calculate new length. + const char *bytes = data; + bool prev_cr = false; + size_t rnsize = 0; + for (size_t i = 0; i < size; i++) { + char c = bytes[i]; + if (c == '\n' && !prev_cr) { + rnsize += 2; + } else { + rnsize += 1; + } + + prev_cr = c == '\r'; + } + + // Allocate appropriately-sized buffer. + char *rnbytes = malloc(rnsize); + if (rnbytes == NULL) { + return size; + } + + // And convert \n to \r\n. + size_t rnix = 0; + for (size_t i = 0; i < size; i++) { + char c = bytes[i]; + if (c == '\n' && !prev_cr) { + rnbytes[rnix] = '\r'; + rnbytes[rnix + 1] = '\n'; + rnix += 2; + } else { + rnbytes[rnix] = c; + rnix += 1; + } + + prev_cr = c == '\r'; + } + + // Always transmit to UART0 for debugging. + _uart0_write(rnbytes, rnsize); + + // Transmit to CDC console. + if (nonblock) { + BaseType_t res = xRingbufferSend(_state.txbuf, rnbytes, rnsize, 0); + if (res == pdFALSE) { + errno = EAGAIN; + free(rnbytes); + return -1; + } + } else { + // Interruptible blocking write. We need to cancel the write if the + // ringbuffer is not being drained actively. + // + // I mean, we don't need to, but in case of a disconnected CDC-ACM we + // just pretend there's no-one receiving us so the writes truly are + // blocking, ie. blocking into /dev/null. + for (;;) { + BaseType_t res = xRingbufferSend(_state.txbuf, rnbytes, rnsize, + 10 / portTICK_PERIOD_MS); + if (res == pdTRUE) { + break; + } + + if (!_draining()) { + break; + } + } + } + free(rnbytes); + return size; } static esp_vfs_t _vfs = { diff --git a/components/st3m/st3m_counter.c b/components/st3m/st3m_counter.c index 8897be26d2..984d71c365 100644 --- a/components/st3m/st3m_counter.c +++ b/components/st3m/st3m_counter.c @@ -2,94 +2,93 @@ #include <string.h> -#include "esp_timer.h" #include "esp_log.h" - +#include "esp_timer.h" void st3m_counter_rate_init(st3m_counter_rate_t *rate) { - memset(rate, 0, sizeof(st3m_counter_rate_t)); + memset(rate, 0, sizeof(st3m_counter_rate_t)); } void st3m_counter_rate_sample(st3m_counter_rate_t *rate) { - rate->sample_us[rate->write_offs] = esp_timer_get_time(); - rate->write_offs = (rate->write_offs + 1) % 10; - if (rate->read_offs == rate->write_offs) { - rate->read_offs = (rate->write_offs + 1) % 10; - } + rate->sample_us[rate->write_offs] = esp_timer_get_time(); + rate->write_offs = (rate->write_offs + 1) % 10; + if (rate->read_offs == rate->write_offs) { + rate->read_offs = (rate->write_offs + 1) % 10; + } } int64_t st3m_counter_rate_average(st3m_counter_rate_t *rate) { - int start = rate->read_offs - 1; - int end = rate->write_offs - 1; - if (start < 0) start = 9; - if (end < 0) end = 9; - - int i = start; - int64_t sum = 0; - int64_t count = 0; - while (i != end) { - int j = (i + 1) % 10; - int64_t diff = rate->sample_us[j] - rate->sample_us[i]; - sum += diff; - count++; - - i = j; - } - - if (count == 0) { - return INT64_MAX; - } - return sum / count; + int start = rate->read_offs - 1; + int end = rate->write_offs - 1; + if (start < 0) start = 9; + if (end < 0) end = 9; + + int i = start; + int64_t sum = 0; + int64_t count = 0; + while (i != end) { + int j = (i + 1) % 10; + int64_t diff = rate->sample_us[j] - rate->sample_us[i]; + sum += diff; + count++; + + i = j; + } + + if (count == 0) { + return INT64_MAX; + } + return sum / count; } uint8_t st3m_counter_rate_report(st3m_counter_rate_t *rate, int seconds) { - int64_t now = esp_timer_get_time(); - if ((now - rate->last_report) > seconds * 1000000) { - rate->last_report = now; - return 1; - } - return 0; + int64_t now = esp_timer_get_time(); + if ((now - rate->last_report) > seconds * 1000000) { + rate->last_report = now; + return 1; + } + return 0; } void st3m_counter_timer_init(st3m_counter_timer_t *timer) { - memset(timer, 0, sizeof(st3m_counter_timer_t)); + memset(timer, 0, sizeof(st3m_counter_timer_t)); } void st3m_counter_timer_sample(st3m_counter_timer_t *timer, int64_t val) { - timer->sample[timer->write_offs] = val; - timer->write_offs = (timer->write_offs + 1) % 10; - if (timer->read_offs == timer->write_offs) { - timer->read_offs = (timer->write_offs + 1) % 10; - } + timer->sample[timer->write_offs] = val; + timer->write_offs = (timer->write_offs + 1) % 10; + if (timer->read_offs == timer->write_offs) { + timer->read_offs = (timer->write_offs + 1) % 10; + } } int64_t st3m_counter_timer_average(st3m_counter_timer_t *timer) { - int start = timer->read_offs - 1; - int end = timer->write_offs - 1; - if (start < 0) start = 9; - if (end < 0) end = 9; - - int i = start; - int64_t sum = 0; - int64_t count = 0; - while (i != end) { - int j = (i + 1) % 10; - sum += timer->sample[i]; - count++; - i = j; - } - - if (count == 0) { - return INT64_MAX; - } - return sum / count; + int start = timer->read_offs - 1; + int end = timer->write_offs - 1; + if (start < 0) start = 9; + if (end < 0) end = 9; + + int i = start; + int64_t sum = 0; + int64_t count = 0; + while (i != end) { + int j = (i + 1) % 10; + sum += timer->sample[i]; + count++; + i = j; + } + + if (count == 0) { + return INT64_MAX; + } + return sum / count; } uint8_t st3m_counter_timer_report(st3m_counter_timer_t *timer, int seconds) { - int64_t now = esp_timer_get_time(); - if ((now - timer->last_report) > seconds * 1000000) { - timer->last_report = now; - return 1; - } - return 0; + int64_t now = esp_timer_get_time(); + if ((now - timer->last_report) > seconds * 1000000) { + timer->last_report = now; + return 1; + } + return 0; } \ No newline at end of file diff --git a/components/st3m/st3m_counter.h b/components/st3m/st3m_counter.h index 803ee658cf..2973e5d7f1 100644 --- a/components/st3m/st3m_counter.h +++ b/components/st3m/st3m_counter.h @@ -3,11 +3,11 @@ #include <stdint.h> typedef struct { - int64_t sample_us[10]; - int64_t last_report; + int64_t sample_us[10]; + int64_t last_report; - int write_offs; - int read_offs; + int write_offs; + int read_offs; } st3m_counter_rate_t; void st3m_counter_rate_init(st3m_counter_rate_t *rate); @@ -16,11 +16,11 @@ int64_t st3m_counter_rate_average(st3m_counter_rate_t *rate); uint8_t st3m_counter_rate_report(st3m_counter_rate_t *rate, int seconds); typedef struct { - int64_t sample[10]; - int64_t last_report; + int64_t sample[10]; + int64_t last_report; - int write_offs; - int read_offs; + int write_offs; + int read_offs; } st3m_counter_timer_t; void st3m_counter_timer_init(st3m_counter_timer_t *rate); diff --git a/components/st3m/st3m_fs.c b/components/st3m/st3m_fs.c index e39803ed8e..3df9f668ff 100644 --- a/components/st3m/st3m_fs.c +++ b/components/st3m/st3m_fs.c @@ -1,28 +1,26 @@ #include "st3m_fs.h" +#include "esp_system.h" #include "esp_vfs.h" #include "esp_vfs_fat.h" -#include "esp_system.h" - static const char *TAG = "st3m-fs"; // Handle of the wear levelling library instance static wl_handle_t s_wl_handle = WL_INVALID_HANDLE; - void st3m_fs_init(void) { - const esp_vfs_fat_mount_config_t mount_config = { + const esp_vfs_fat_mount_config_t mount_config = { .max_files = 4, .format_if_mount_failed = true, - .allocation_unit_size = CONFIG_WL_SECTOR_SIZE - }; + .allocation_unit_size = CONFIG_WL_SECTOR_SIZE}; - esp_err_t err = esp_vfs_fat_spiflash_mount("", "vfs", &mount_config, &s_wl_handle); + esp_err_t err = + esp_vfs_fat_spiflash_mount("", "vfs", &mount_config, &s_wl_handle); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to mount FAT FS: %s", esp_err_to_name(err)); return; } - ESP_LOGI(TAG, "Mounted Flash VFS Partition at /"); + ESP_LOGI(TAG, "Mounted Flash VFS Partition at /"); } diff --git a/components/st3m/st3m_gfx.c b/components/st3m/st3m_gfx.c index 61db39bb79..e78c8ffb68 100644 --- a/components/st3m/st3m_gfx.c +++ b/components/st3m/st3m_gfx.c @@ -2,15 +2,15 @@ #include <string.h> -#include "esp_system.h" #include "esp_log.h" +#include "esp_system.h" #include "esp_task.h" #include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" -#include "ctx_config.h" #include "ctx.h" +#include "ctx_config.h" #include "flow3r_bsp.h" #include "st3m_counter.h" @@ -21,7 +21,8 @@ static const char *TAG = "st3m-gfx"; // framebuffers. // // These live in SPIRAM, as we don't have enough space in SRAM/IRAM. -EXT_RAM_BSS_ATTR static st3m_framebuffer_desc_t framebuffer_descs[ST3M_GFX_NBUFFERS]; +EXT_RAM_BSS_ATTR static st3m_framebuffer_desc_t + framebuffer_descs[ST3M_GFX_NBUFFERS]; static st3m_ctx_desc_t dctx_descs[ST3M_GFX_NCTX]; @@ -50,7 +51,8 @@ static TaskHandle_t rast_task; // Attempt to receive from a queue forever, but log an error if it takes longer // than two seconds to get something. -static void xQueueReceiveNotifyStarved(QueueHandle_t q, void *dst, const char *desc) { +static void xQueueReceiveNotifyStarved(QueueHandle_t q, void *dst, + const char *desc) { uint8_t starved = 0; for (;;) { if (xQueueReceive(q, dst, pdMS_TO_TICKS(2000)) == pdTRUE) { @@ -70,19 +72,20 @@ static void st3m_gfx_crtc_task(void *_arg) { int descno; int64_t start = esp_timer_get_time(); - xQueueReceiveNotifyStarved(framebuffer_blitq, &descno, "crtc task starved!"); + xQueueReceiveNotifyStarved(framebuffer_blitq, &descno, + "crtc task starved!"); int64_t end = esp_timer_get_time(); - st3m_counter_timer_sample(&blit_read_time, end-start); + st3m_counter_timer_sample(&blit_read_time, end - start); start = esp_timer_get_time(); flow3r_bsp_display_send_fb(framebuffer_descs[descno].buffer); end = esp_timer_get_time(); - st3m_counter_timer_sample(&blit_work_time, end-start); + st3m_counter_timer_sample(&blit_work_time, end - start); start = esp_timer_get_time(); xQueueSend(framebuffer_freeq, &descno, portMAX_DELAY); end = esp_timer_get_time(); - st3m_counter_timer_sample(&blit_write_time, end-start); + st3m_counter_timer_sample(&blit_write_time, end - start); st3m_counter_rate_sample(&blit_rate); @@ -92,8 +95,14 @@ static void st3m_gfx_crtc_task(void *_arg) { float work = st3m_counter_timer_average(&blit_work_time) / 1000.0; float write = st3m_counter_timer_average(&blit_write_time) / 1000.0; // Mark variables as used even if debug is disabled. - (void)rate; (void)read; (void)work; (void)write; - ESP_LOGD(TAG, "blitting: %.3f/sec, read %.3fms, work %.3fms, write %.3fms", rate, read, work, write); + (void)rate; + (void)read; + (void)work; + (void)write; + ESP_LOGD( + TAG, + "blitting: %.3f/sec, read %.3fms, work %.3fms, write %.3fms", + rate, read, work, write); } } } @@ -104,49 +113,60 @@ static void st3m_gfx_rast_task(void *_arg) { while (true) { int fb_descno, dctx_descno; int64_t start = esp_timer_get_time(); - xQueueReceiveNotifyStarved(framebuffer_freeq, &fb_descno, "rast task starved (freeq)!"); + xQueueReceiveNotifyStarved(framebuffer_freeq, &fb_descno, + "rast task starved (freeq)!"); st3m_framebuffer_desc_t *fb = &framebuffer_descs[fb_descno]; int64_t end = esp_timer_get_time(); - st3m_counter_timer_sample(&rast_read_fb_time, end-start); + st3m_counter_timer_sample(&rast_read_fb_time, end - start); start = esp_timer_get_time(); - xQueueReceiveNotifyStarved(dctx_rastq, &dctx_descno, "rast task starved (dctx)!"); + xQueueReceiveNotifyStarved(dctx_rastq, &dctx_descno, + "rast task starved (dctx)!"); st3m_ctx_desc_t *draw = &dctx_descs[dctx_descno]; end = esp_timer_get_time(); - st3m_counter_timer_sample(&rast_read_dctx_time, end-start); + st3m_counter_timer_sample(&rast_read_dctx_time, end - start); // Render drawctx into fbctx. start = esp_timer_get_time(); ctx_render_ctx(draw->ctx, fb->ctx); ctx_drawlist_clear(draw->ctx); end = esp_timer_get_time(); - st3m_counter_timer_sample(&rast_work_time, end-start); + st3m_counter_timer_sample(&rast_work_time, end - start); start = esp_timer_get_time(); xQueueSend(dctx_freeq, &dctx_descno, portMAX_DELAY); xQueueSend(framebuffer_blitq, &fb_descno, portMAX_DELAY); end = esp_timer_get_time(); - st3m_counter_timer_sample(&rast_write_time, end-start); + st3m_counter_timer_sample(&rast_write_time, end - start); st3m_counter_rate_sample(&rast_rate); - if (st3m_counter_rate_report(&rast_rate, 1)) { float rate = 1000000.0 / st3m_counter_rate_average(&rast_rate); - float read_fb = st3m_counter_timer_average(&rast_read_fb_time) / 1000.0; - float read_dctx = st3m_counter_timer_average(&rast_read_dctx_time) / 1000.0; + float read_fb = + st3m_counter_timer_average(&rast_read_fb_time) / 1000.0; + float read_dctx = + st3m_counter_timer_average(&rast_read_dctx_time) / 1000.0; float work = st3m_counter_timer_average(&rast_work_time) / 1000.0; float write = st3m_counter_timer_average(&rast_write_time) / 1000.0; // Mark variables as used even if debug is disabled. - (void)rate; (void)read_fb; (void)read_dctx; (void)work; (void)write; - ESP_LOGD(TAG, "rasterization: %.3f/sec, read fb %.3fms, read dctx %.3fms, work %.3fms, write %.3fms", rate, read_fb, read_dctx, work, write); + (void)rate; + (void)read_fb; + (void)read_dctx; + (void)work; + (void)write; + ESP_LOGD(TAG, + "rasterization: %.3f/sec, read fb %.3fms, read dctx " + "%.3fms, work %.3fms, write %.3fms", + rate, read_fb, read_dctx, work, write); } } } void st3m_gfx_splash(const char *text) { const char *lines[] = { - text, NULL, + text, + NULL, }; st3m_gfx_textview_t tv = { .title = NULL, @@ -162,19 +182,19 @@ void st3m_gfx_show_textview(st3m_gfx_textview_t *tv) { st3m_ctx_desc_t *target = st3m_gfx_drawctx_free_get(portMAX_DELAY); - // Draw background. + // Draw background. ctx_rgb(target->ctx, 0, 0, 0); ctx_rectangle(target->ctx, -120, -120, 240, 240); ctx_fill(target->ctx); int y = -30; - ctx_gray(target->ctx, 1.0); + ctx_gray(target->ctx, 1.0); ctx_text_align(target->ctx, CTX_TEXT_ALIGN_CENTER); ctx_text_baseline(target->ctx, CTX_TEXT_BASELINE_MIDDLE); ctx_font_size(target->ctx, 20.0); - // Draw title, if any. + // Draw title, if any. if (tv->title != NULL) { ctx_move_to(target->ctx, 0, y); ctx_text(target->ctx, tv->title); @@ -182,7 +202,7 @@ void st3m_gfx_show_textview(st3m_gfx_textview_t *tv) { } ctx_font_size(target->ctx, 15.0); - ctx_gray(target->ctx, 0.8); + ctx_gray(target->ctx, 0.8); // Draw messages. const char **lines = tv->lines; @@ -216,34 +236,32 @@ void st3m_gfx_init(void) { flow3r_bsp_display_init(); // Create framebuffer queues. - framebuffer_freeq = xQueueCreate(ST3M_GFX_NBUFFERS+1, sizeof(int)); + framebuffer_freeq = xQueueCreate(ST3M_GFX_NBUFFERS + 1, sizeof(int)); assert(framebuffer_freeq != NULL); - framebuffer_blitq = xQueueCreate(ST3M_GFX_NBUFFERS+1, sizeof(int)); + framebuffer_blitq = xQueueCreate(ST3M_GFX_NBUFFERS + 1, sizeof(int)); assert(framebuffer_blitq != NULL); // Create drawlist ctx queues. - dctx_freeq = xQueueCreate(ST3M_GFX_NCTX+1, sizeof(int)); + dctx_freeq = xQueueCreate(ST3M_GFX_NCTX + 1, sizeof(int)); assert(dctx_freeq != NULL); - dctx_rastq = xQueueCreate(ST3M_GFX_NCTX+1, sizeof(int)); + dctx_rastq = xQueueCreate(ST3M_GFX_NCTX + 1, sizeof(int)); assert(dctx_rastq != NULL); - for (int i = 0; i < ST3M_GFX_NBUFFERS; i++) { // Setup framebuffer descriptor. st3m_framebuffer_desc_t *fb_desc = &framebuffer_descs[i]; fb_desc->num = i; fb_desc->ctx = ctx_new_for_framebuffer( - fb_desc->buffer, - FLOW3R_BSP_DISPLAY_WIDTH, - FLOW3R_BSP_DISPLAY_HEIGHT, - FLOW3R_BSP_DISPLAY_WIDTH * 2, - CTX_FORMAT_RGB565_BYTESWAPPED - ); + fb_desc->buffer, FLOW3R_BSP_DISPLAY_WIDTH, + FLOW3R_BSP_DISPLAY_HEIGHT, FLOW3R_BSP_DISPLAY_WIDTH * 2, + CTX_FORMAT_RGB565_BYTESWAPPED); assert(fb_desc->ctx != NULL); - // Rotate by 180 deg and translate x and y by 120 px to have (0,0) at the center of the screen + // Rotate by 180 deg and translate x and y by 120 px to have (0,0) at + // the center of the screen int32_t offset_x = FLOW3R_BSP_DISPLAY_WIDTH / 2; int32_t offset_y = FLOW3R_BSP_DISPLAY_HEIGHT / 2; - ctx_apply_transform(fb_desc->ctx, -1, 0, offset_x, 0, -1, offset_y, 0, 0, 1); + ctx_apply_transform(fb_desc->ctx, -1, 0, offset_x, 0, -1, offset_y, 0, + 0, 1); // Push descriptor to freeq. BaseType_t res = xQueueSend(framebuffer_freeq, &i, 0); @@ -254,7 +272,8 @@ void st3m_gfx_init(void) { // Setup dctx descriptor. st3m_ctx_desc_t *dctx_desc = &dctx_descs[i]; dctx_desc->num = i; - dctx_desc->ctx = ctx_new_drawlist(FLOW3R_BSP_DISPLAY_WIDTH, FLOW3R_BSP_DISPLAY_HEIGHT); + dctx_desc->ctx = ctx_new_drawlist(FLOW3R_BSP_DISPLAY_WIDTH, + FLOW3R_BSP_DISPLAY_HEIGHT); assert(dctx_desc->ctx != NULL); // Push descriptor to freeq. @@ -263,11 +282,13 @@ void st3m_gfx_init(void) { } // Start crtc. - BaseType_t res = xTaskCreate(st3m_gfx_crtc_task, "crtc", 4096, NULL, ESP_TASK_PRIO_MIN+3, &crtc_task); + BaseType_t res = xTaskCreate(st3m_gfx_crtc_task, "crtc", 4096, NULL, + ESP_TASK_PRIO_MIN + 3, &crtc_task); assert(res == pdPASS); // Start rast. - res = xTaskCreate(st3m_gfx_rast_task, "rast", 8192, NULL, ESP_TASK_PRIO_MIN+1, &rast_task); + res = xTaskCreate(st3m_gfx_rast_task, "rast", 8192, NULL, + ESP_TASK_PRIO_MIN + 1, &rast_task); assert(res == pdPASS); } diff --git a/components/st3m/st3m_gfx.h b/components/st3m/st3m_gfx.h index 4a526503b8..bef77d80d5 100644 --- a/components/st3m/st3m_gfx.h +++ b/components/st3m/st3m_gfx.h @@ -2,8 +2,8 @@ #include "freertos/FreeRTOS.h" -#include "ctx_config.h" #include "ctx.h" +#include "ctx_config.h" // Each buffer takes ~116kB SPIRAM. While one framebuffer is being blitted, the // other one is being written to by the rasterizer. @@ -14,11 +14,11 @@ // A framebuffer descriptor, pointing at a framebuffer. typedef struct { - // The numeric ID of this descriptor. - int num; - // SPIRAM buffer. - uint16_t buffer[240*240]; - Ctx *ctx; + // The numeric ID of this descriptor. + int num; + // SPIRAM buffer. + uint16_t buffer[240 * 240]; + Ctx *ctx; } st3m_framebuffer_desc_t; // Initialize the gfx subsystem of st3m, includng the rasterization and @@ -27,9 +27,9 @@ void st3m_gfx_init(void); // A drawlist ctx descriptor, pointing to a drawlist-backed Ctx. typedef struct { - // The numeric ID of this descriptor. - int num; - Ctx *ctx; + // The numeric ID of this descriptor. + int num; + Ctx *ctx; } st3m_ctx_desc_t; // Get a free drawlist ctx to draw into. @@ -55,8 +55,8 @@ uint8_t st3m_gfx_drawctx_pipe_full(void); void st3m_gfx_flush(void); typedef struct { - const char *title; - const char **lines; + const char *title; + const char **lines; } st3m_gfx_textview_t; void st3m_gfx_show_textview(st3m_gfx_textview_t *tv); diff --git a/components/st3m/st3m_io.c b/components/st3m/st3m_io.c index a02abc3b66..7c78b0d513 100644 --- a/components/st3m/st3m_io.c +++ b/components/st3m/st3m_io.c @@ -2,71 +2,64 @@ static const char *TAG = "st3m-io"; -#include "esp_log.h" #include "esp_err.h" +#include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "st3m_audio.h" -#include "flow3r_bsp_i2c.h" #include "flow3r_bsp.h" +#include "flow3r_bsp_i2c.h" +#include "st3m_audio.h" -static void _update_button_state(){ +static void _update_button_state() { esp_err_t ret = flow3r_bsp_spio_update(); if (ret != ESP_OK) { ESP_LOGE(TAG, "update failed: %s", esp_err_to_name(ret)); } } -void init_buttons(){ +void init_buttons() { esp_err_t ret = flow3r_bsp_spio_init(); if (ret != ESP_OK) { ESP_LOGE(TAG, "init failed: %s", esp_err_to_name(ret)); - for (;;) {} + for (;;) { + } } } -bool st3m_io_charger_state_get() { - return flow3r_bsp_spio_charger_state_get(); -} +bool st3m_io_charger_state_get() { return flow3r_bsp_spio_charger_state_get(); } -bool st3m_io_line_in_jacksense_get(){ +bool st3m_io_line_in_jacksense_get() { return flow3r_bsp_spio_jacksense_right_get(); } static bool menu_button_left = false; -void st3m_io_menu_button_set_left(bool left){ - menu_button_left = left; -} +void st3m_io_menu_button_set_left(bool left) { menu_button_left = left; } st3m_tripos st3m_io_menu_button_get() { - if (menu_button_left) - return flow3r_bsp_spio_left_button_get(); + if (menu_button_left) return flow3r_bsp_spio_left_button_get(); return flow3r_bsp_spio_right_button_get(); } -st3m_tripos st3m_io_application_button_get(){ - if (menu_button_left) - return flow3r_bsp_spio_right_button_get(); +st3m_tripos st3m_io_application_button_get() { + if (menu_button_left) return flow3r_bsp_spio_right_button_get(); return flow3r_bsp_spio_left_button_get(); } -st3m_tripos st3m_io_left_button_get(){ +st3m_tripos st3m_io_left_button_get() { return flow3r_bsp_spio_left_button_get(); } -st3m_tripos st3m_io_right_button_get(){ +st3m_tripos st3m_io_right_button_get() { return flow3r_bsp_spio_right_button_get(); } -bool st3m_io_menu_button_get_left(){ - return menu_button_left; -} +bool st3m_io_menu_button_get_left() { return menu_button_left; } static uint8_t badge_link_enabled = 0; -uint8_t st3m_io_badge_link_get_active(uint8_t pin_mask){ +uint8_t st3m_io_badge_link_get_active(uint8_t pin_mask) { return badge_link_enabled & pin_mask; } @@ -77,12 +70,14 @@ static int8_t st3m_io_badge_link_set(uint8_t mask, bool state) { bool right_ring = (mask & BADGE_LINK_PIN_MASK_LINE_IN_RING) > 0; // Apply request to badge_link_enabled. - if(state) { + if (state) { if (left_tip || left_ring) { - if(!st3m_audio_headphones_are_connected()) { + if (!st3m_audio_headphones_are_connected()) { left_tip = false; left_ring = false; - ESP_LOGE(TAG, "cannot enable line out badge link without cable plugged in for safety reasons"); + ESP_LOGE(TAG, + "cannot enable line out badge link without cable " + "plugged in for safety reasons"); } } if (left_tip) badge_link_enabled |= BADGE_LINK_PIN_MASK_LINE_OUT_TIP; @@ -91,9 +86,11 @@ static int8_t st3m_io_badge_link_set(uint8_t mask, bool state) { if (right_ring) badge_link_enabled |= BADGE_LINK_PIN_MASK_LINE_IN_RING; } else { if (!left_tip) badge_link_enabled &= ~BADGE_LINK_PIN_MASK_LINE_OUT_TIP; - if (!left_ring) badge_link_enabled &= ~BADGE_LINK_PIN_MASK_LINE_OUT_RING; + if (!left_ring) + badge_link_enabled &= ~BADGE_LINK_PIN_MASK_LINE_OUT_RING; if (!right_tip) badge_link_enabled &= ~BADGE_LINK_PIN_MASK_LINE_IN_TIP; - if (!right_ring) badge_link_enabled &= ~BADGE_LINK_PIN_MASK_LINE_IN_RING; + if (!right_ring) + badge_link_enabled &= ~BADGE_LINK_PIN_MASK_LINE_IN_RING; } // Convert badge_link_enabled back to {left,right}_{tip,ring}, but this @@ -108,11 +105,11 @@ static int8_t st3m_io_badge_link_set(uint8_t mask, bool state) { return st3m_io_badge_link_get_active(mask); } -uint8_t st3m_io_badge_link_disable(uint8_t pin_mask){ +uint8_t st3m_io_badge_link_disable(uint8_t pin_mask) { return st3m_io_badge_link_set(pin_mask, 0); } -uint8_t st3m_io_badge_link_enable(uint8_t pin_mask){ +uint8_t st3m_io_badge_link_enable(uint8_t pin_mask) { return st3m_io_badge_link_set(pin_mask, 1); } @@ -121,26 +118,27 @@ void captouch_read_cycle(void); void captouch_init(void); void captouch_force_calibration(void); -static void _task(void * data){ +static void _task(void *data) { TickType_t last_wake = xTaskGetTickCount(); - while(1) { - vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(10)); // 100 Hz + while (1) { + vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(10)); // 100 Hz captouch_read_cycle(); _update_button_state(); } } void st3m_io_init(void) { - esp_err_t ret = flow3r_bsp_spio_init(); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "spio init failed: %s", esp_err_to_name(ret)); - for (;;) {} - } - - captouch_init(); - captouch_force_calibration(); - st3m_io_badge_link_disable(BADGE_LINK_PIN_MASK_ALL); - - xTaskCreate(&_task, "io", 4096, NULL, configMAX_PRIORITIES-1, NULL); - ESP_LOGI(TAG, "IO task started"); + esp_err_t ret = flow3r_bsp_spio_init(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "spio init failed: %s", esp_err_to_name(ret)); + for (;;) { + } + } + + captouch_init(); + captouch_force_calibration(); + st3m_io_badge_link_disable(BADGE_LINK_PIN_MASK_ALL); + + xTaskCreate(&_task, "io", 4096, NULL, configMAX_PRIORITIES - 1, NULL); + ESP_LOGI(TAG, "IO task started"); } diff --git a/components/st3m/st3m_io.h b/components/st3m/st3m_io.h index 27b630d2b0..75448a3fa8 100644 --- a/components/st3m/st3m_io.h +++ b/components/st3m/st3m_io.h @@ -5,10 +5,10 @@ void st3m_io_init(void); -/* UI sugar: People might prefer using one button for in-application stuff and the other - * for entering main menu/volume control, depending on their handedness and how they hold - * the badge. This function allows them configure this and is meant to be only be used by - * the OS user config handler. +/* UI sugar: People might prefer using one button for in-application stuff and + * the other for entering main menu/volume control, depending on their + * handedness and how they hold the badge. This function allows them configure + * this and is meant to be only be used by the OS user config handler. * * Set to 1 to use the left shoulder button as the menu button, 0 for the right */ @@ -20,16 +20,17 @@ void st3m_io_menu_button_set_left(bool left); bool st3m_io_menu_button_get_left(); // A 'tripos' button is what we're calling the shoulder buttons. As the name -// indicates, it has three positions: left, middle (a.k.a. down/press) and right. +// indicates, it has three positions: left, middle (a.k.a. down/press) and +// right. typedef enum { - // Not pressed. - st3m_tripos_none = 0, - // Pressed towards the left. - st3m_tripos_left = -1, - // Pressed down. - st3m_tripos_mid = 2, - // Pressed towards the right. - st3m_tripos_right = 1, + // Not pressed. + st3m_tripos_none = 0, + // Pressed towards the left. + st3m_tripos_left = -1, + // Pressed down. + st3m_tripos_mid = 2, + // Pressed towards the right. + st3m_tripos_right = 1, } st3m_tripos; /* Read the state of the menu/application button. @@ -43,29 +44,36 @@ st3m_tripos st3m_io_application_button_get(); st3m_tripos st3m_io_left_button_get(); st3m_tripos st3m_io_right_button_get(); -#define BADGE_LINK_PIN_MASK_LINE_IN_TIP 0b0001 -#define BADGE_LINK_PIN_MASK_LINE_IN_RING 0b0010 -#define BADGE_LINK_PIN_MASK_LINE_OUT_TIP 0b0100 -#define BADGE_LINK_PIN_MASK_LINE_OUT_RING 0b1000 -#define BADGE_LINK_PIN_MASK_LINE_IN ((BADGE_LINK_PIN_MASK_LINE_IN_TIP) | (BADGE_LINK_PIN_MASK_LINE_IN_RING)) -#define BADGE_LINK_PIN_MASK_LINE_OUT ((BADGE_LINK_PIN_MASK_LINE_OUT_TIP) | (BADGE_LINK_PIN_MASK_LINE_OUT_RING)) -#define BADGE_LINK_PIN_MASK_ALL ((BADGE_LINK_PIN_MASK_LINE_IN) | (BADGE_LINK_PIN_MASK_LINE_OUT)) +#define BADGE_LINK_PIN_MASK_LINE_IN_TIP 0b0001 +#define BADGE_LINK_PIN_MASK_LINE_IN_RING 0b0010 +#define BADGE_LINK_PIN_MASK_LINE_OUT_TIP 0b0100 +#define BADGE_LINK_PIN_MASK_LINE_OUT_RING 0b1000 +#define BADGE_LINK_PIN_MASK_LINE_IN \ + ((BADGE_LINK_PIN_MASK_LINE_IN_TIP) | (BADGE_LINK_PIN_MASK_LINE_IN_RING)) +#define BADGE_LINK_PIN_MASK_LINE_OUT \ + ((BADGE_LINK_PIN_MASK_LINE_OUT_TIP) | (BADGE_LINK_PIN_MASK_LINE_OUT_RING)) +#define BADGE_LINK_PIN_MASK_ALL \ + ((BADGE_LINK_PIN_MASK_LINE_IN) | (BADGE_LINK_PIN_MASK_LINE_OUT)) -/* Gets active badge links ports. Mask with BADGE_LINK_PIN_MASK_LINE_{IN/OUT}_{TIP/RING}. The corresponding - * GPIO indices are listed in BADGE_LINK_PIN_INDEX_LINE_{OUT/IN}_{TIP/RING}. +/* Gets active badge links ports. Mask with + * BADGE_LINK_PIN_MASK_LINE_{IN/OUT}_{TIP/RING}. The corresponding GPIO indices + * are listed in BADGE_LINK_PIN_INDEX_LINE_{OUT/IN}_{TIP/RING}. */ uint8_t st3m_io_badge_link_get_active(uint8_t pin_mask); -/* Disables badge link ports. Mask with BADGE_LINK_PIN_MASK_LINE_{IN/OUT}_{TIP/RING}. The corresponding - * GPIO indices are listed in BADGE_LINK_PIN_INDEX_LINE_{OUT/IN}_{TIP/RING}. - * Returns the output of st3m_io_badge_link_get_active after execution. +/* Disables badge link ports. Mask with + * BADGE_LINK_PIN_MASK_LINE_{IN/OUT}_{TIP/RING}. The corresponding GPIO indices + * are listed in BADGE_LINK_PIN_INDEX_LINE_{OUT/IN}_{TIP/RING}. Returns the + * output of st3m_io_badge_link_get_active after execution. */ uint8_t st3m_io_badge_link_disable(uint8_t pin_mask); -/* Enables badge link ports. Mask with BADGE_LINK_PIN_MASK_LINE_{IN/OUT}_{TIP/RING}. The corresponding - * GPIO indices are listed in BADGE_LINK_PIN_INDEX_LINE_{OUT/IN}_{TIP/RING}_PIN. - * Returns the output of st3m_io_badge_link_get_active after execution. +/* Enables badge link ports. Mask with + * BADGE_LINK_PIN_MASK_LINE_{IN/OUT}_{TIP/RING}. The corresponding GPIO indices + * are listed in BADGE_LINK_PIN_INDEX_LINE_{OUT/IN}_{TIP/RING}_PIN. Returns the + * output of st3m_io_badge_link_get_active after execution. * - * Do NOT connect headphones to a badge link port. You might hear a ringing for a while. Warn user. + * Do NOT connect headphones to a badge link port. You might hear a ringing for + * a while. Warn user. */ uint8_t st3m_io_badge_link_enable(uint8_t pin_mask); diff --git a/components/st3m/st3m_leds.c b/components/st3m/st3m_leds.c index 4184a124d5..304b3a51b8 100644 --- a/components/st3m/st3m_leds.c +++ b/components/st3m/st3m_leds.c @@ -1,15 +1,15 @@ #include "st3m_leds.h" +#include <math.h> #include <stdio.h> #include <string.h> -#include <math.h> #include "esp_log.h" #include "esp_system.h" #include "esp_task.h" #include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "freertos/semphr.h" +#include "freertos/task.h" #include "flow3r_bsp.h" #include "st3m_colors.h" @@ -44,29 +44,29 @@ SemaphoreHandle_t mutex_incoming; #define LOCK_INCOMING xSemaphoreTake(mutex_incoming, portMAX_DELAY) #define UNLOCK_INCOMING xSemaphoreGive(mutex_incoming) -static void set_single_led(uint8_t index, st3m_rgb_t c){ - index = ((39-index) + 1 + 32)%40; +static void set_single_led(uint8_t index, st3m_rgb_t c) { + index = ((39 - index) + 1 + 32) % 40; flow3r_bsp_leds_set_pixel(index, c.r, c.g, c.b); } -uint8_t led_get_slew(int16_t old, int16_t new, int16_t slew){ - if(new > old + slew){ +uint8_t led_get_slew(int16_t old, int16_t new, int16_t slew) { + if (new > old + slew) { return old + slew; - } else if(new > old) { + } else if (new > old) { return new; } - if(new < old - slew){ + if (new < old - slew) { return old - slew; - } else if(new < old) { + } else if (new < old) { return new; } return old; } -static void leds_update_target(){ +static void leds_update_target() { LOCK_INCOMING; - for(int i = 0; i < 40; i++){ + for (int i = 0; i < 40; i++) { state.target[i].r = state.target_buffer[i].r; state.target[i].g = state.target_buffer[i].g; state.target[i].b = state.target_buffer[i].b; @@ -75,13 +75,12 @@ static void leds_update_target(){ UNLOCK_INCOMING; } -void st3m_leds_update_hardware(){ +void st3m_leds_update_hardware() { LOCK; - if(state.auto_update) - leds_update_target(); + if (state.auto_update) leds_update_target(); - for(int i = 0; i < 40; i++){ + for (int i = 0; i < 40; i++) { st3m_rgb_t c = state.target[i]; c.r = c.r * state.brightness / 255; c.g = c.g * state.brightness / 255; @@ -107,15 +106,16 @@ void st3m_leds_update_hardware(){ } } -void st3m_leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, uint8_t blue){ +void st3m_leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, + uint8_t blue) { LOCK_INCOMING; state.target_buffer[index].r = red; state.target_buffer[index].g = green; state.target_buffer[index].b = blue; UNLOCK_INCOMING; } - -void st3m_leds_set_single_hsv(uint8_t index, float hue, float sat, float val){ + +void st3m_leds_set_single_hsv(uint8_t index, float hue, float sat, float val) { st3m_hsv_t hsv = { .h = hue, .s = sat, @@ -126,44 +126,42 @@ void st3m_leds_set_single_hsv(uint8_t index, float hue, float sat, float val){ UNLOCK_INCOMING; } -void st3m_leds_set_all_rgb(uint8_t red, uint8_t green, uint8_t blue){ - for(int i = 0; i<40; i++){ +void st3m_leds_set_all_rgb(uint8_t red, uint8_t green, uint8_t blue) { + for (int i = 0; i < 40; i++) { st3m_leds_set_single_rgb(i, red, green, blue); } } -void st3m_leds_set_all_hsv(float h, float s, float v){ - for(int i = 0; i<40; i++){ +void st3m_leds_set_all_hsv(float h, float s, float v) { + for (int i = 0; i < 40; i++) { st3m_leds_set_single_hsv(i, h, s, v); } } -void st3m_leds_update(){ - leds_update_target(); -} +void st3m_leds_update() { leds_update_target(); } static void _leds_task(void *_data) { (void)_data; TickType_t last_wake = xTaskGetTickCount(); - while (true) { - vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(100)); // 10 Hz + while (true) { + vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(100)); // 10 Hz st3m_leds_update_hardware(); } -} +} -void st3m_leds_init(){ +void st3m_leds_init() { mutex = xSemaphoreCreateMutex(); assert(mutex != NULL); mutex_incoming = xSemaphoreCreateMutex(); assert(mutex_incoming != NULL); - memset(&state, 0, sizeof(state)); + memset(&state, 0, sizeof(state)); state.brightness = 69; state.slew_rate = 255; state.auto_update = false; - for(uint16_t i = 0; i<256; i++){ + for (uint16_t i = 0; i < 256; i++) { state.gamma_red.lut[i] = i; state.gamma_green.lut[i] = i; state.gamma_blue.lut[i] = i; @@ -175,62 +173,62 @@ void st3m_leds_init(){ abort(); } - xTaskCreate(_leds_task, "leds", 4096, NULL, ESP_TASK_PRIO_MIN+4, NULL); + xTaskCreate(_leds_task, "leds", 4096, NULL, ESP_TASK_PRIO_MIN + 4, NULL); ESP_LOGI(TAG, "LED task started"); } -void st3m_leds_set_brightness(uint8_t b){ +void st3m_leds_set_brightness(uint8_t b) { LOCK; state.brightness = b; UNLOCK; } -uint8_t st3m_leds_get_brightness(){ +uint8_t st3m_leds_get_brightness() { LOCK; uint8_t res = state.brightness; UNLOCK; return res; } -void st3m_leds_set_slew_rate(uint8_t s){ +void st3m_leds_set_slew_rate(uint8_t s) { LOCK; state.slew_rate = s; UNLOCK; } -uint8_t st3m_leds_get_slew_rate(){ +uint8_t st3m_leds_get_slew_rate() { LOCK; uint8_t res = state.slew_rate; UNLOCK; return res; } -void st3m_leds_set_auto_update(bool on){ +void st3m_leds_set_auto_update(bool on) { LOCK; state.auto_update = on; UNLOCK; } -bool st3m_leds_get_auto_update(){ +bool st3m_leds_get_auto_update() { LOCK; bool res = state.auto_update; UNLOCK; return res; } -void st3m_leds_set_gamma(float red, float green, float blue){ +void st3m_leds_set_gamma(float red, float green, float blue) { LOCK; - for(uint16_t i = 0; i<256; i++){ - if(i == 0){ + for (uint16_t i = 0; i < 256; i++) { + if (i == 0) { state.gamma_red.lut[i] = 0; state.gamma_green.lut[i] = 0; state.gamma_blue.lut[i] = 0; } - float step = ((float) i) / 255.; - state.gamma_red.lut[i] = (uint8_t) (254.*(pow(step, red))+1); - state.gamma_green.lut[i] = (uint8_t) (254.*(pow(step, green))+1); - state.gamma_blue.lut[i] = (uint8_t) (254.*(pow(step, blue))+1); + float step = ((float)i) / 255.; + state.gamma_red.lut[i] = (uint8_t)(254. * (pow(step, red)) + 1); + state.gamma_green.lut[i] = (uint8_t)(254. * (pow(step, green)) + 1); + state.gamma_blue.lut[i] = (uint8_t)(254. * (pow(step, blue)) + 1); } UNLOCK; } diff --git a/components/st3m/st3m_leds.h b/components/st3m/st3m_leds.h index a9a1da141d..7bb587a7fa 100644 --- a/components/st3m/st3m_leds.h +++ b/components/st3m/st3m_leds.h @@ -1,7 +1,7 @@ #pragma once -#include <stdint.h> #include <stdbool.h> +#include <stdint.h> // LEDs are triple-buffered: // @@ -21,7 +21,8 @@ void st3m_leds_init(); // // After you're ready setting up your blink, call st3m_leds_update, or enable // autoupdates. -void st3m_leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, uint8_t blue); +void st3m_leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, + uint8_t blue); void st3m_leds_set_single_hsv(uint8_t index, float hue, float sat, float value); void st3m_leds_set_all_rgb(uint8_t red, uint8_t green, uint8_t blue); void st3m_leds_set_all_hsv(float hue, float sat, float value); @@ -32,8 +33,8 @@ void st3m_leds_set_all_hsv(float hue, float sat, float value); void st3m_leds_set_brightness(uint8_t brightness); uint8_t st3m_leds_get_brightness(); -// Set/get maximum change rate of brightness. Set to 1-3 for fade effects, set to -// 255 to disable. Currently clocks at 10Hz. +// Set/get maximum change rate of brightness. Set to 1-3 for fade effects, set +// to 255 to disable. Currently clocks at 10Hz. void st3m_leds_set_slew_rate(uint8_t slew_rate); uint8_t st3m_leds_get_slew_rate(); @@ -41,9 +42,9 @@ uint8_t st3m_leds_get_slew_rate(); // buffer, effectively scheduling the LED state to be presented to the user. void st3m_leds_update(); -// Bend the rgb curves with an exponent each. (1,1,1) is default, (2,2,2) works well too -// If someone wants to do color calibration, this is ur friend -void st3m_leds_set_gamma(float red, float green, float blue); +// Bend the rgb curves with an exponent each. (1,1,1) is default, (2,2,2) works +// well too If someone wants to do color calibration, this is ur friend +void st3m_leds_set_gamma(float red, float green, float blue); // Make st3m_leds_update be periodically called by the background task for slew // rate animation when set to 1. This adds user changes immediately. Useful with diff --git a/components/st3m/st3m_mode.c b/components/st3m/st3m_mode.c index 8f258af885..e250a636d9 100644 --- a/components/st3m/st3m_mode.c +++ b/components/st3m/st3m_mode.c @@ -2,10 +2,10 @@ #include <string.h> +#include "esp_system.h" #include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "freertos/semphr.h" -#include "esp_system.h" +#include "freertos/task.h" #include "st3m_gfx.h" #include "st3m_io.h" @@ -38,18 +38,18 @@ void st3m_mode_set(st3m_mode_kind_t kind, const char *message) { } void st3m_mode_update_display(bool *restartable) { - if (restartable != NULL) - *restartable = false; + if (restartable != NULL) *restartable = false; xSemaphoreTake(_mu, portMAX_DELAY); switch (_mode.kind) { - case st3m_mode_kind_app: - case st3m_mode_kind_invalid: - // Nothing to do. - break; - case st3m_mode_kind_starting: { - const char *lines[] = { - _mode.message, NULL, + case st3m_mode_kind_app: + case st3m_mode_kind_invalid: + // Nothing to do. + break; + case st3m_mode_kind_starting: { + const char *lines[] = { + _mode.message, + NULL, }; st3m_gfx_textview_t tv = { .title = "Starting...", @@ -58,11 +58,11 @@ void st3m_mode_update_display(bool *restartable) { st3m_gfx_show_textview(&tv); break; } - case st3m_mode_kind_disk: - st3m_gfx_splash("Disk Mode"); - break; - case st3m_mode_kind_repl: { - const char *lines[] = { + case st3m_mode_kind_disk: + st3m_gfx_splash("Disk Mode"); + break; + case st3m_mode_kind_repl: { + const char *lines[] = { "Send Ctrl-D over USB", "or press left shoulder button", "to restart.", @@ -73,12 +73,11 @@ void st3m_mode_update_display(bool *restartable) { .lines = lines, }; st3m_gfx_show_textview(&tv); - if (restartable != NULL) - *restartable = true; + if (restartable != NULL) *restartable = true; break; } - case st3m_mode_kind_fatal: { - const char *msg = _mode.message; + case st3m_mode_kind_fatal: { + const char *msg = _mode.message; if (msg == NULL) { msg = ""; } @@ -93,8 +92,7 @@ void st3m_mode_update_display(bool *restartable) { .lines = lines, }; st3m_gfx_show_textview(&tv); - if (restartable != NULL) - *restartable = true; + if (restartable != NULL) *restartable = true; break; } } @@ -112,7 +110,6 @@ static void _task(void *arg) { bool restartable = false; st3m_mode_update_display(&restartable); - if (restartable) { st3m_tripos tp = st3m_io_left_button_get(); if (tp == st3m_tripos_mid) { diff --git a/components/st3m/st3m_mode.h b/components/st3m/st3m_mode.h index 29b5aa3fec..57a00c6264 100644 --- a/components/st3m/st3m_mode.h +++ b/components/st3m/st3m_mode.h @@ -3,19 +3,19 @@ #include <stdbool.h> typedef enum { - st3m_mode_kind_invalid = 0, - st3m_mode_kind_starting = 1, - st3m_mode_kind_app = 2, - st3m_mode_kind_repl = 3, - st3m_mode_kind_disk = 4, - st3m_mode_kind_fatal = 5, + st3m_mode_kind_invalid = 0, + st3m_mode_kind_starting = 1, + st3m_mode_kind_app = 2, + st3m_mode_kind_repl = 3, + st3m_mode_kind_disk = 4, + st3m_mode_kind_fatal = 5, } st3m_mode_kind_t; typedef struct { - st3m_mode_kind_t kind; + st3m_mode_kind_t kind; - // Valid if mode != app. - char *message; + // Valid if mode != app. + char *message; } st3m_mode_t; // Must be called exactly once before using any other st3m_mode_* functions. diff --git a/components/st3m/st3m_scope.c b/components/st3m/st3m_scope.c index a59e55070a..23143fafca 100644 --- a/components/st3m/st3m_scope.c +++ b/components/st3m/st3m_scope.c @@ -6,13 +6,15 @@ #include "freertos/FreeRTOS.h" #include "freertos/atomic.h" -#include "ctx_config.h" #include "ctx.h" +#include "ctx_config.h" -st3m_scope_t scope = { 0, }; -static const char* TAG = "st3m-scope"; +st3m_scope_t scope = { + 0, +}; +static const char *TAG = "st3m-scope"; -void st3m_scope_init(void){ +void st3m_scope_init(void) { if (scope.write_buffer != NULL) { return; } @@ -22,7 +24,8 @@ void st3m_scope_init(void){ scope.exchange_buffer = malloc(sizeof(int16_t) * scope.buffer_size); scope.read_buffer = malloc(sizeof(int16_t) * scope.buffer_size); - if (scope.write_buffer == NULL || scope.exchange_buffer == NULL || scope.read_buffer == NULL) { + if (scope.write_buffer == NULL || scope.exchange_buffer == NULL || + scope.read_buffer == NULL) { if (scope.write_buffer != NULL) { free(scope.write_buffer); scope.write_buffer = NULL; @@ -43,14 +46,13 @@ void st3m_scope_init(void){ memset(scope.exchange_buffer, 0, sizeof(int16_t) * scope.buffer_size); memset(scope.read_buffer, 0, sizeof(int16_t) * scope.buffer_size); - - scope.write_head_position = 0; + scope.write_head_position = 0; scope.prev_write_attempt = 0; ESP_LOGI(TAG, "initialized"); } -void st3m_scope_write(int16_t value){ - if(scope.write_buffer == NULL) { +void st3m_scope_write(int16_t value) { + if (scope.write_buffer == NULL) { return; } @@ -70,30 +72,31 @@ void st3m_scope_write(int16_t value){ } if (scope.write_head_position >= scope.buffer_size) { - scope.write_buffer = Atomic_SwapPointers_p32((void *volatile *)&scope.exchange_buffer, scope.write_buffer); + scope.write_buffer = Atomic_SwapPointers_p32( + (void *volatile *)&scope.exchange_buffer, scope.write_buffer); scope.write_head_position = 0; } else { scope.write_buffer[scope.write_head_position] = value; - scope.write_head_position++; + scope.write_head_position++; } } -void st3m_scope_draw(Ctx *ctx){ +void st3m_scope_draw(Ctx *ctx) { if (scope.write_buffer == NULL) { return; } - scope.read_buffer = Atomic_SwapPointers_p32((void *volatile *)&scope.exchange_buffer, scope.read_buffer); - + scope.read_buffer = Atomic_SwapPointers_p32( + (void *volatile *)&scope.exchange_buffer, scope.read_buffer); - // How much to divide the values persisted in the buffer to scale to - // -120+120. + // How much to divide the values persisted in the buffer to scale to + // -120+120. // // shift == 5 -> division by 2^5 -> division by 32. This seems to work for // the value currently emitted by the audio stack. int16_t shift = 5; - // How many samples to skip for each drawn line segment. + // How many samples to skip for each drawn line segment. // // decimate == 1 works, but is a waste of CPU cycles both at draw and // rasterization time. @@ -104,7 +107,7 @@ void st3m_scope_draw(Ctx *ctx){ int x = -120; int y = scope.read_buffer[0] >> shift; - ctx_move_to(ctx, x, y); + ctx_move_to(ctx, x, y); for (size_t i = 1; i < scope.buffer_size; i += decimate) { x += decimate; int y = scope.read_buffer[i] >> shift; @@ -115,5 +118,4 @@ void st3m_scope_draw(Ctx *ctx){ ctx_line_to(ctx, 130, -130); ctx_line_to(ctx, -130, -130); ctx_line_to(ctx, -130, 0); - } diff --git a/components/st3m/st3m_scope.h b/components/st3m/st3m_scope.h index fc8d4074e6..c359ff0427 100644 --- a/components/st3m/st3m_scope.h +++ b/components/st3m/st3m_scope.h @@ -11,22 +11,22 @@ #include "st3m_gfx.h" typedef struct { - // Scope buffer size, in samples. Currently always 240 (same as screen - // width). + // Scope buffer size, in samples. Currently always 240 (same as screen + // width). size_t buffer_size; - // Triple-buffering for lockless exhange between free-running writer and - // reader. The exchange buffer is swapped to/from by the reader/writer - // whenever they're done with a whole sample buffer. - int16_t *write_buffer; - int16_t *exchange_buffer; - int16_t *read_buffer; + // Triple-buffering for lockless exhange between free-running writer and + // reader. The exchange buffer is swapped to/from by the reader/writer + // whenever they're done with a whole sample buffer. + int16_t *write_buffer; + int16_t *exchange_buffer; + int16_t *read_buffer; - // Offset where the write handler should write the next sample. + // Offset where the write handler should write the next sample. uint32_t write_head_position; - // Previous sample that was attempted to be written. Used for - // zero-detection. - int16_t prev_write_attempt; + // Previous sample that was attempted to be written. Used for + // zero-detection. + int16_t prev_write_attempt; } st3m_scope_t; // Initialize global scope. Must be performed before any other access to scope @@ -48,4 +48,3 @@ void st3m_scope_write(int16_t value); // The user is responsible for setting a color and running a fill/stroke // afterwards. void st3m_scope_draw(Ctx *ctx); - diff --git a/components/st3m/st3m_usb.c b/components/st3m/st3m_usb.c index 1727351547..f3bd482758 100644 --- a/components/st3m/st3m_usb.c +++ b/components/st3m/st3m_usb.c @@ -3,8 +3,8 @@ static const char *TAG = "st3m-usb"; #include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "freertos/semphr.h" +#include "freertos/task.h" #include "esp_log.h" #include "esp_mac.h" @@ -17,46 +17,47 @@ static usb_phy_handle_t phy_hdl; static bool _connected = false; static void _usb_task(void *_arg) { - (void)_arg; + (void)_arg; while (true) { - tud_task_ext(10, false); + tud_task_ext(10, false); - xSemaphoreTake(_mu, portMAX_DELAY); - st3m_usb_mode_kind_t mode = _mode; - xSemaphoreGive(_mu); + xSemaphoreTake(_mu, portMAX_DELAY); + st3m_usb_mode_kind_t mode = _mode; + xSemaphoreGive(_mu); - if (mode == st3m_usb_mode_kind_app) { - st3m_usb_cdc_txpoll(); - } + if (mode == st3m_usb_mode_kind_app) { + st3m_usb_cdc_txpoll(); + } } } // Generate USB serial from on-board chip ID / MAC. static void _generate_serial(void) { - uint8_t mac[6]; - memset(mac, 0, 6); - // Ignore error. Worst case we get zeroes. - esp_read_mac(mac, ESP_MAC_WIFI_STA); - - char serial[13]; - snprintf(serial, 13, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - st3m_usb_descriptors_set_serial(serial); + uint8_t mac[6]; + memset(mac, 0, 6); + // Ignore error. Worst case we get zeroes. + esp_read_mac(mac, ESP_MAC_WIFI_STA); + + char serial[13]; + snprintf(serial, 13, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], + mac[3], mac[4], mac[5]); + st3m_usb_descriptors_set_serial(serial); } void st3m_usb_init(void) { - assert(_mu == NULL); - _mu = xSemaphoreCreateMutex(); - assert(_mu != NULL); - _mode = st3m_usb_mode_kind_disabled; + assert(_mu == NULL); + _mu = xSemaphoreCreateMutex(); + assert(_mu != NULL); + _mode = st3m_usb_mode_kind_disabled; - _generate_serial(); - st3m_usb_cdc_init(); + _generate_serial(); + st3m_usb_cdc_init(); usb_phy_config_t phy_conf = { .controller = USB_PHY_CTRL_OTG, .otg_mode = USB_OTG_MODE_DEVICE, .target = USB_PHY_TARGET_INT, }; - // TODO(q3k): set self-powered based on battery state? + // TODO(q3k): set self-powered based on battery state? ESP_ERROR_CHECK(usb_new_phy(&phy_conf, &phy_hdl)); usb_phy_action(phy_hdl, USB_PHY_ACTION_HOST_FORCE_DISCONN); @@ -65,81 +66,81 @@ void st3m_usb_init(void) { assert(false); } - xTaskCreate(_usb_task, "usb", 4096, NULL, 5, NULL); - ESP_LOGI(TAG, "USB stack started"); + xTaskCreate(_usb_task, "usb", 4096, NULL, 5, NULL); + ESP_LOGI(TAG, "USB stack started"); } void st3m_usb_mode_switch(st3m_usb_mode_t *mode) { - xSemaphoreTake(_mu, portMAX_DELAY); - - bool running = false; - switch (_mode) { - case st3m_usb_mode_kind_app: - case st3m_usb_mode_kind_disk: - running = true; - break; - default: - break; - } - - bool should_run = false; - switch (mode->kind) { - case st3m_usb_mode_kind_app: - case st3m_usb_mode_kind_disk: - should_run = true; - break; - default: - break; - } - - if (running) { - ESP_LOGI(TAG, "stopping and disconnecting"); + xSemaphoreTake(_mu, portMAX_DELAY); + + bool running = false; + switch (_mode) { + case st3m_usb_mode_kind_app: + case st3m_usb_mode_kind_disk: + running = true; + break; + default: + break; + } + + bool should_run = false; + switch (mode->kind) { + case st3m_usb_mode_kind_app: + case st3m_usb_mode_kind_disk: + should_run = true; + break; + default: + break; + } + + if (running) { + ESP_LOGI(TAG, "stopping and disconnecting"); usb_phy_action(phy_hdl, USB_PHY_ACTION_HOST_FORCE_DISCONN); - vTaskDelay(1000 / portTICK_PERIOD_MS); - } - - st3m_usb_descriptors_switch(mode); - if (mode->kind == st3m_usb_mode_kind_disk) { - assert(mode->disk != NULL); - st3m_usb_msc_set_conf(mode->disk); - } - if (mode->kind == st3m_usb_mode_kind_app) { - assert(mode->app != NULL); - st3m_usb_cdc_set_conf(mode->app); - } - - if (should_run) { - ESP_LOGI(TAG, "reconnecting and starting"); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } + + st3m_usb_descriptors_switch(mode); + if (mode->kind == st3m_usb_mode_kind_disk) { + assert(mode->disk != NULL); + st3m_usb_msc_set_conf(mode->disk); + } + if (mode->kind == st3m_usb_mode_kind_app) { + assert(mode->app != NULL); + st3m_usb_cdc_set_conf(mode->app); + } + + if (should_run) { + ESP_LOGI(TAG, "reconnecting and starting"); usb_phy_action(phy_hdl, USB_PHY_ACTION_HOST_ALLOW_CONN); - } + } - _mode = mode->kind; - _connected = false; - xSemaphoreGive(_mu); + _mode = mode->kind; + _connected = false; + xSemaphoreGive(_mu); } bool st3m_usb_connected(void) { - xSemaphoreTake(_mu, portMAX_DELAY); - bool res = _connected; - xSemaphoreGive(_mu); - return res; + xSemaphoreTake(_mu, portMAX_DELAY); + bool res = _connected; + xSemaphoreGive(_mu); + return res; } void tud_mount_cb(void) { - ESP_LOGI(TAG, "USB attached"); - xSemaphoreTake(_mu, portMAX_DELAY); - _connected = true; - xSemaphoreGive(_mu); + ESP_LOGI(TAG, "USB attached"); + xSemaphoreTake(_mu, portMAX_DELAY); + _connected = true; + xSemaphoreGive(_mu); } void tud_suspend_cb(bool remote_wakeup_en) { - ESP_LOGI(TAG, "USB detached"); - - xSemaphoreTake(_mu, portMAX_DELAY); - st3m_usb_mode_kind_t mode = _mode; - _connected = false; - xSemaphoreGive(_mu); - if (mode == st3m_usb_mode_kind_app) { - st3m_usb_cdc_detached(); - } + ESP_LOGI(TAG, "USB detached"); + + xSemaphoreTake(_mu, portMAX_DELAY); + st3m_usb_mode_kind_t mode = _mode; + _connected = false; + xSemaphoreGive(_mu); + if (mode == st3m_usb_mode_kind_app) { + st3m_usb_cdc_detached(); + } } \ No newline at end of file diff --git a/components/st3m/st3m_usb.h b/components/st3m/st3m_usb.h index 0095a8927c..2361ab22c3 100644 --- a/components/st3m/st3m_usb.h +++ b/components/st3m/st3m_usb.h @@ -35,66 +35,70 @@ // each mode, and thus things are a bit simplistic. This might change in the // future. -#include <stdint.h> #include <stdbool.h> #include <stddef.h> +#include <stdint.h> typedef enum { - // Device should not enumerate. - st3m_usb_mode_kind_disabled = 0, - // Device should appear as a 'flow3er' with a CDC-ACM (serial) endpoint. - st3m_usb_mode_kind_app = 1, - // Device should appear as a 'flower (disk mode)' with a MSC (mass storage) - // endpoint. - st3m_usb_mode_kind_disk = 2, + // Device should not enumerate. + st3m_usb_mode_kind_disabled = 0, + // Device should appear as a 'flow3er' with a CDC-ACM (serial) endpoint. + st3m_usb_mode_kind_app = 1, + // Device should appear as a 'flower (disk mode)' with a MSC (mass storage) + // endpoint. + st3m_usb_mode_kind_disk = 2, } st3m_usb_mode_kind_t; // Description of the device in disk mode. typedef struct { - // Number of blocks. - size_t block_size; - // Size of each block (usually 512 bytes). - size_t block_count; - // Product ID, padded with zeroes. - uint8_t product_id[16]; + // Number of blocks. + size_t block_size; + // Size of each block (usually 512 bytes). + size_t block_count; + // Product ID, padded with zeroes. + uint8_t product_id[16]; - // Optional. Called whenever the host asks if the device is ready. Defaults to always ready. - bool (*fn_ready)(uint8_t lun); - // Optional. Called whenever the hosts executes a scsi start/stop. Defaults to 'yeah sure' stub. - bool (*fn_start_stop)(uint8_t lun, uint8_t power_condition, bool start, bool load_eject); - // Required. Called when the host wishes to read from an LBA/offset. Address - // = lba*block_size+offset. Must return however many bytes were actually - // read. - int32_t (*fn_read10)(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize); - // Optional. Called when the host wishes to write to an LBA/offset. Defaults - // to ignoring writes. - int32_t (*fn_write10)(uint8_t lun, uint32_t lba, uint32_t offset, const void* buffer, uint32_t bufsize); + // Optional. Called whenever the host asks if the device is ready. Defaults + // to always ready. + bool (*fn_ready)(uint8_t lun); + // Optional. Called whenever the hosts executes a scsi start/stop. Defaults + // to 'yeah sure' stub. + bool (*fn_start_stop)(uint8_t lun, uint8_t power_condition, bool start, + bool load_eject); + // Required. Called when the host wishes to read from an LBA/offset. Address + // = lba*block_size+offset. Must return however many bytes were actually + // read. + int32_t (*fn_read10)(uint8_t lun, uint32_t lba, uint32_t offset, + void *buffer, uint32_t bufsize); + // Optional. Called when the host wishes to write to an LBA/offset. Defaults + // to ignoring writes. + int32_t (*fn_write10)(uint8_t lun, uint32_t lba, uint32_t offset, + const void *buffer, uint32_t bufsize); } st3m_usb_msc_conf_t; // Description of the device in application mode. typedef struct { - // Required. Called whenever the host wrote some bytes. All bytes must be - // processed. - void (*fn_rx)(const uint8_t *buffer, size_t bufsize); - // Required. Called whenever the host requests bytes to read. Must return - // how many bytes are actually available to transmit to the host. - size_t (*fn_txpoll)(uint8_t *buffer, size_t bufsize); - // Optional. Called whenever the host has detached from the device. - void (*fn_detach)(void); + // Required. Called whenever the host wrote some bytes. All bytes must be + // processed. + void (*fn_rx)(const uint8_t *buffer, size_t bufsize); + // Required. Called whenever the host requests bytes to read. Must return + // how many bytes are actually available to transmit to the host. + size_t (*fn_txpoll)(uint8_t *buffer, size_t bufsize); + // Optional. Called whenever the host has detached from the device. + void (*fn_detach)(void); } st3m_usb_app_conf_t; // Main configuration structure, passed by pointer to st3m_usb_mode_switch. // Describes a requested configuration mode of the USB subsystem. typedef struct { - st3m_usb_mode_kind_t kind; + st3m_usb_mode_kind_t kind; - // Only valid if kind == disk. - st3m_usb_msc_conf_t *disk; - // Only valid if kind == app. - st3m_usb_app_conf_t *app; + // Only valid if kind == disk. + st3m_usb_msc_conf_t *disk; + // Only valid if kind == app. + st3m_usb_app_conf_t *app; } st3m_usb_mode_t; - // Immediately switch to a given mode, blocking until that mode is active. A // mode being active does not indicate that the device is connected to a host. void st3m_usb_mode_switch(st3m_usb_mode_t *target); @@ -115,11 +119,11 @@ void st3m_usb_cdc_txpoll(void); void st3m_usb_cdc_detached(void); typedef enum { - st3m_usb_interface_disk_msc, - st3m_usb_interface_disk_total, + st3m_usb_interface_disk_msc, + st3m_usb_interface_disk_total, } st3m_usb_interface_disk_t; typedef enum { - st3m_usb_interface_app_cdc, - st3m_usb_interface_app_total, + st3m_usb_interface_app_cdc, + st3m_usb_interface_app_total, } st3m_usb_interface_app_t; diff --git a/components/st3m/st3m_usb_cdc.c b/components/st3m/st3m_usb_cdc.c index b4495d35e6..26e7f4c2e9 100644 --- a/components/st3m/st3m_usb_cdc.c +++ b/components/st3m/st3m_usb_cdc.c @@ -1,18 +1,16 @@ #include "st3m_usb.h" -#include "tusb.h" #include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "freertos/semphr.h" +#include "freertos/task.h" +#include "tusb.h" #include "esp_log.h" -static st3m_usb_app_conf_t _conf = { 0 }; +static st3m_usb_app_conf_t _conf = {0}; static SemaphoreHandle_t _write_mu; -void st3m_usb_cdc_init(void) { - _write_mu = xSemaphoreCreateMutex(); -} +void st3m_usb_cdc_init(void) { _write_mu = xSemaphoreCreateMutex(); } void st3m_usb_cdc_set_conf(st3m_usb_app_conf_t *conf) { assert(conf->fn_rx != NULL); @@ -45,7 +43,7 @@ void st3m_usb_cdc_txpoll(void) { return; } - for (;;) { + for (;;) { uint32_t space = tud_cdc_n_write_available(st3m_usb_interface_app_cdc); if (space == 0) { return; diff --git a/components/st3m/st3m_usb_descriptors.c b/components/st3m/st3m_usb_descriptors.c index 1785010c59..1128f3bfcc 100644 --- a/components/st3m/st3m_usb_descriptors.c +++ b/components/st3m/st3m_usb_descriptors.c @@ -1,144 +1,162 @@ #include "st3m_usb.h" -#include "tusb.h" #include "esp_log.h" +#include "tusb.h" static const char *TAG = "st3m-usb-descs"; -static const char * const _manufacturer = "flow3r.garden"; -static const char * const _product_app = "flow3r"; -static const char * const _product_disk = "flow3r (disk mode)"; -static const char * const _cdc_repl = "MicroPython REPL"; -static char _serial[32+1] = {0}; +static const char *const _manufacturer = "flow3r.garden"; +static const char *const _product_app = "flow3r"; +static const char *const _product_disk = "flow3r (disk mode)"; +static const char *const _cdc_repl = "MicroPython REPL"; +static char _serial[32 + 1] = {0}; typedef struct { - const tusb_desc_device_t dev; - const uint8_t *cfg; - size_t num_str; - const char *str[16]; + const tusb_desc_device_t dev; + const uint8_t *cfg; + size_t num_str; + const char *str[16]; } st3m_usb_descriptor_set_t; -static const uint32_t st3m_usb_configdesc_len_app = TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN; +static const uint32_t st3m_usb_configdesc_len_app = + TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN; #define EPNUM_CDC_0_NOTIF 0x81 -#define EPNUM_CDC_0_OUT 0x02 -#define EPNUM_CDC_0_IN 0x82 +#define EPNUM_CDC_0_OUT 0x02 +#define EPNUM_CDC_0_IN 0x82 static uint8_t _cfg_desc_app[] = { - TUD_CONFIG_DESCRIPTOR(1, st3m_usb_interface_app_total, 0, st3m_usb_configdesc_len_app, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), - TUD_CDC_DESCRIPTOR(st3m_usb_interface_app_cdc, 4, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, 64), + TUD_CONFIG_DESCRIPTOR(1, st3m_usb_interface_app_total, 0, + st3m_usb_configdesc_len_app, + TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), + TUD_CDC_DESCRIPTOR(st3m_usb_interface_app_cdc, 4, EPNUM_CDC_0_NOTIF, 8, + EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, 64), }; static st3m_usb_descriptor_set_t _descset_app = { - .dev = { - .bLength = sizeof(tusb_desc_device_t), - .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, - - .bDeviceClass = 0x00, - .bDeviceSubClass = 0x00, - .bDeviceProtocol = 0x00, - - .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, - .idVendor = 0x303a, - .idProduct = 0x4042, - - .bcdDevice = 0x0100, - - .iManufacturer = 0x01, - .iProduct = 0x02, - .iSerialNumber = 0x03, - - .bNumConfigurations = 0x01, - }, - .cfg = _cfg_desc_app, - .num_str = 5, - .str = { - (char[]){0x09, 0x04}, // EN - _manufacturer, - _product_app, - _serial, - _cdc_repl, - }, + .dev = + { + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, + + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, + .idVendor = 0x303a, + .idProduct = 0x4042, + + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x01, + }, + .cfg = _cfg_desc_app, + .num_str = 5, + .str = + { + (char[]){0x09, 0x04}, // EN + _manufacturer, + _product_app, + _serial, + _cdc_repl, + }, }; -static const uint32_t st3m_usb_configdesc_len_disk = TUD_CONFIG_DESC_LEN + TUD_MSC_DESC_LEN; +static const uint32_t st3m_usb_configdesc_len_disk = + TUD_CONFIG_DESC_LEN + TUD_MSC_DESC_LEN; - // Use separate endpoint numbers otherwise TinyUSB seems to get very confused... - #define EPNUM_1_MSC_OUT 0x03 - #define EPNUM_1_MSC_IN 0x83 +// Use separate endpoint numbers otherwise TinyUSB seems to get very confused... +#define EPNUM_1_MSC_OUT 0x03 +#define EPNUM_1_MSC_IN 0x83 static uint8_t _cfg_desc_disk[] = { - TUD_CONFIG_DESCRIPTOR(1, st3m_usb_interface_disk_total, 0, st3m_usb_configdesc_len_disk, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), - TUD_MSC_DESCRIPTOR(st3m_usb_interface_disk_msc, 0, EPNUM_1_MSC_OUT, EPNUM_1_MSC_IN, 64), + TUD_CONFIG_DESCRIPTOR(1, st3m_usb_interface_disk_total, 0, + st3m_usb_configdesc_len_disk, + TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), + TUD_MSC_DESCRIPTOR(st3m_usb_interface_disk_msc, 0, EPNUM_1_MSC_OUT, + EPNUM_1_MSC_IN, 64), }; static st3m_usb_descriptor_set_t _descset_disk = { - .dev = { - .bLength = sizeof(tusb_desc_device_t), - .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, - - .bDeviceClass = 0x00, - .bDeviceSubClass = 0x00, - .bDeviceProtocol = 0x00, - - .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, - .idVendor = 0x303a, - .idProduct = 0x4023, - - .bcdDevice = 0x0100, - - .iManufacturer = 0x01, - .iProduct = 0x02, - .iSerialNumber = 0x03, - - .bNumConfigurations = 0x01, - }, - .cfg = _cfg_desc_disk, - .num_str = 4, - .str = { - (char[]){0x09, 0x04}, // EN - _manufacturer, - _product_disk, - _serial, - }, + .dev = + { + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, + + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, + .idVendor = 0x303a, + .idProduct = 0x4023, + + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x01, + }, + .cfg = _cfg_desc_disk, + .num_str = 4, + .str = + { + (char[]){0x09, 0x04}, // EN + _manufacturer, + _product_disk, + _serial, + }, }; static st3m_usb_descriptor_set_t *_descset_current = &_descset_app; uint8_t const *tud_descriptor_device_cb(void) { - return (uint8_t const *)&_descset_current->dev; + return (uint8_t const *)&_descset_current->dev; } uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { - return _descset_current->cfg; + return _descset_current->cfg; } -uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) -{ - (void) langid; // Unused, this driver supports only one language in string descriptors +uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { + (void)langid; // Unused, this driver supports only one language in string + // descriptors uint8_t chr_count; static uint16_t _desc_str[64]; - const char **descs = _descset_current->str; + const char **descs = _descset_current->str; if (index == 0) { memcpy(&_desc_str[1], descs[0], 2); chr_count = 1; } else { if (index >= _descset_current->num_str) { - ESP_LOGW(TAG, "String index (%u) is out of bounds, check your string descriptor", index); + ESP_LOGW(TAG, + "String index (%u) is out of bounds, check your string " + "descriptor", + index); return NULL; } if (descs[index] == NULL) { - ESP_LOGW(TAG, "String index (%u) points to NULL, check your string descriptor", index); + ESP_LOGW(TAG, + "String index (%u) points to NULL, check your string " + "descriptor", + index); return NULL; } const char *str = descs[index]; - chr_count = strnlen(str, 63); // Buffer len - header + chr_count = strnlen(str, 63); // Buffer len - header // Convert ASCII string into UTF-16 for (uint8_t i = 0; i < chr_count; i++) { @@ -146,26 +164,27 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) } } - // First byte is length in bytes (including header), second byte is descriptor type (TUSB_DESC_STRING) - _desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2 * chr_count + 2); + // First byte is length in bytes (including header), second byte is + // descriptor type (TUSB_DESC_STRING) + _desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * chr_count + 2); return _desc_str; } void st3m_usb_descriptors_switch(st3m_usb_mode_t *mode) { - switch (mode->kind) { - case st3m_usb_mode_kind_app: - _descset_current = &_descset_app; - break; - case st3m_usb_mode_kind_disk: - _descset_current = &_descset_disk; - break; - default: - break; - } + switch (mode->kind) { + case st3m_usb_mode_kind_app: + _descset_current = &_descset_app; + break; + case st3m_usb_mode_kind_disk: + _descset_current = &_descset_disk; + break; + default: + break; + } } void st3m_usb_descriptors_set_serial(const char *serial) { - memset(_serial, 0, 33); - strncpy(_serial, serial, 32); + memset(_serial, 0, 33); + strncpy(_serial, serial, 32); } \ No newline at end of file diff --git a/components/st3m/st3m_usb_msc.c b/components/st3m/st3m_usb_msc.c index 7167c630c0..2bb9a88a7b 100644 --- a/components/st3m/st3m_usb_msc.c +++ b/components/st3m/st3m_usb_msc.c @@ -1,116 +1,121 @@ -#include <stdint.h> #include <stdbool.h> +#include <stdint.h> #include <string.h> #include "st3m_usb.h" -#include "tusb.h" #include "esp_log.h" +#include "tusb.h" -static st3m_usb_msc_conf_t _conf = { 0 }; +static st3m_usb_msc_conf_t _conf = {0}; void st3m_usb_msc_set_conf(st3m_usb_msc_conf_t *conf) { - assert(conf->block_count != 0); - assert(conf->block_size != 0); - assert(conf->fn_read10 != NULL); - memcpy(&_conf, conf, sizeof(_conf)); + assert(conf->block_count != 0); + assert(conf->block_size != 0); + assert(conf->fn_read10 != NULL); + memcpy(&_conf, conf, sizeof(_conf)); } static st3m_usb_msc_conf_t *_get_conf(void) { - assert(_conf.block_count != 0); - assert(_conf.block_size != 0); - assert(_conf.fn_read10 != NULL); - return &_conf; + assert(_conf.block_count != 0); + assert(_conf.block_size != 0); + assert(_conf.fn_read10 != NULL); + return &_conf; } // TinyUSB callback. -void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) -{ - (void) lun; +void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], + uint8_t product_id[16], uint8_t product_rev[4]) { + (void)lun; - st3m_usb_msc_conf_t *conf = _get_conf(); + st3m_usb_msc_conf_t *conf = _get_conf(); const char vid[] = "flow3r"; const char rev[] = "1.0"; - memcpy(vendor_id , vid, strlen(vid)); - memcpy(product_id , conf->product_id, 16); + memcpy(vendor_id, vid, strlen(vid)); + memcpy(product_id, conf->product_id, 16); memcpy(product_rev, rev, strlen(rev)); } // TinyUSB callback. -bool tud_msc_test_unit_ready_cb(uint8_t lun) -{ - (void) lun; - st3m_usb_msc_conf_t *conf = _get_conf(); - if (conf->fn_ready != NULL) { - return conf->fn_ready(lun); - } +bool tud_msc_test_unit_ready_cb(uint8_t lun) { + (void)lun; + st3m_usb_msc_conf_t *conf = _get_conf(); + if (conf->fn_ready != NULL) { + return conf->fn_ready(lun); + } return true; } // TinyUSB callback. -void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) { - (void) lun; - st3m_usb_msc_conf_t *conf = _get_conf(); +void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, + uint16_t *block_size) { + (void)lun; + st3m_usb_msc_conf_t *conf = _get_conf(); *block_count = conf->block_count; - *block_size = conf->block_size; + *block_size = conf->block_size; } // TinyUSB callback. -bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) { - st3m_usb_msc_conf_t *conf = _get_conf(); - if (conf->fn_start_stop != NULL) { - return conf->fn_start_stop(lun, power_condition, start, load_eject); - } - return true; +bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, + bool load_eject) { + st3m_usb_msc_conf_t *conf = _get_conf(); + if (conf->fn_start_stop != NULL) { + return conf->fn_start_stop(lun, power_condition, start, load_eject); + } + return true; } // TinyUSB callback. -int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { - st3m_usb_msc_conf_t *conf = _get_conf(); - return conf->fn_read10(lun, lba, offset, buffer, bufsize); +int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, + void *buffer, uint32_t bufsize) { + st3m_usb_msc_conf_t *conf = _get_conf(); + return conf->fn_read10(lun, lba, offset, buffer, bufsize); } // TinyUSB callback. -int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) { - st3m_usb_msc_conf_t *conf = _get_conf(); - if (conf->fn_write10 != NULL) { - return conf->fn_write10(lun, lba, offset, buffer, bufsize); - } - - if (lba >= conf->block_count) { - return -1; - } - return (int32_t) bufsize; +int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, + uint8_t *buffer, uint32_t bufsize) { + st3m_usb_msc_conf_t *conf = _get_conf(); + if (conf->fn_write10 != NULL) { + return conf->fn_write10(lun, lba, offset, buffer, bufsize); + } + + if (lba >= conf->block_count) { + return -1; + } + return (int32_t)bufsize; } // TinyUSB callback. -int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) { - void const* response = NULL; +int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, + uint16_t bufsize) { + void const *response = NULL; int32_t resplen = 0; // most scsi handled is input bool in_xfer = true; switch (scsi_cmd[0]) { - default: - // Set Sense = Invalid Command Operation - tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); - - // negative means error -> tinyusb could stall and/or response with failed status - resplen = -1; - break; + default: + // Set Sense = Invalid Command Operation + tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); + + // negative means error -> tinyusb could stall and/or response with + // failed status + resplen = -1; + break; } // return resplen must not larger than bufsize - if ( resplen > bufsize ) { - resplen = bufsize; - } + if (resplen > bufsize) { + resplen = bufsize; + } - if ( response && (resplen > 0) ) { - if(in_xfer) { - memcpy(buffer, response, (size_t) resplen); + if (response && (resplen > 0)) { + if (in_xfer) { + memcpy(buffer, response, (size_t)resplen); } else { // SCSI output } diff --git a/components/st3m/tusb_config.h b/components/st3m/tusb_config.h index c4fc055a1a..8d6ddda19f 100644 --- a/components/st3m/tusb_config.h +++ b/components/st3m/tusb_config.h @@ -6,16 +6,16 @@ extern "C" { #endif -#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED -#define CFG_TUSB_OS OPT_OS_FREERTOS +#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED +#define CFG_TUSB_OS OPT_OS_FREERTOS #define CFG_TUSB_MEM_SECTION -#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) #define CFG_TUSB_DEBUG 0 int st3m_uart0_debug(const char *fmt, ...); #define CFG_TUSB_DEBUG_PRINTF st3m_uart0_debug -#define CFG_TUD_ENDPOINT0_SIZE 64 +#define CFG_TUD_ENDPOINT0_SIZE 64 #define CFG_TUD_CDC 1 #define CFG_TUD_CDC_RX_BUFSIZE 64 @@ -24,8 +24,6 @@ int st3m_uart0_debug(const char *fmt, ...); #define CFG_TUD_MSC 1 #define CFG_TUD_MSC_EP_BUFSIZE 512 - #ifdef __cplusplus } #endif - diff --git a/usermodule/mp_audio.c b/usermodule/mp_audio.c index 24e2b934ad..0084461609 100644 --- a/usermodule/mp_audio.c +++ b/usermodule/mp_audio.c @@ -2,111 +2,115 @@ #include <stdio.h> #include <string.h> -#include "py/runtime.h" -#include "py/mphal.h" -#include "mphalport.h" -#include "modmachine.h" #include "extmod/virtpin.h" #include "machine_rtc.h" +#include "modmachine.h" +#include "mphalport.h" #include "py/builtin.h" +#include "py/mphal.h" #include "py/runtime.h" -#include "st3m_audio.h" #include "flow3r_bsp.h" +#include "st3m_audio.h" -// documentation: these are all super thin wrappers for the c api in components/st3m/st3m_audio.h +// documentation: these are all super thin wrappers for the c api in +// components/st3m/st3m_audio.h STATIC mp_obj_t mp_headset_is_connected() { return mp_obj_new_int(st3m_audio_headset_is_connected()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headset_is_connected_obj, mp_headset_is_connected); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headset_is_connected_obj, + mp_headset_is_connected); STATIC mp_obj_t mp_headphones_are_connected() { return mp_obj_new_int(st3m_audio_headphones_are_connected()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_are_connected_obj, mp_headphones_are_connected); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_are_connected_obj, + mp_headphones_are_connected); STATIC mp_obj_t mp_headphones_detection_override(mp_obj_t enable) { st3m_audio_headphones_detection_override(mp_obj_get_int(enable)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_detection_override_obj, mp_headphones_detection_override); - - +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_detection_override_obj, + mp_headphones_detection_override); STATIC mp_obj_t mp_headphones_set_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_headphones_set_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_headphones_set_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_set_volume_dB_obj, mp_headphones_set_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_set_volume_dB_obj, + mp_headphones_set_volume_dB); STATIC mp_obj_t mp_speaker_set_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_speaker_set_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_speaker_set_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_set_volume_dB_obj, mp_speaker_set_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_set_volume_dB_obj, + mp_speaker_set_volume_dB); STATIC mp_obj_t mp_set_volume_dB(mp_obj_t vol_dB) { return mp_obj_new_float(st3m_audio_set_volume_dB(mp_obj_get_float(vol_dB))); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_set_volume_dB_obj, mp_set_volume_dB); - - STATIC mp_obj_t mp_headphones_adjust_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_headphones_adjust_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_headphones_adjust_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_adjust_volume_dB_obj, mp_headphones_adjust_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_adjust_volume_dB_obj, + mp_headphones_adjust_volume_dB); STATIC mp_obj_t mp_speaker_adjust_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_speaker_adjust_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_speaker_adjust_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_adjust_volume_dB_obj, mp_speaker_adjust_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_adjust_volume_dB_obj, + mp_speaker_adjust_volume_dB); STATIC mp_obj_t mp_adjust_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_adjust_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_adjust_volume_dB(mp_obj_get_float(vol_dB))); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_adjust_volume_dB_obj, mp_adjust_volume_dB); - - STATIC mp_obj_t mp_headphones_get_volume_dB() { return mp_obj_new_float(st3m_audio_headphones_get_volume_dB()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_volume_dB_obj, mp_headphones_get_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_volume_dB_obj, + mp_headphones_get_volume_dB); STATIC mp_obj_t mp_speaker_get_volume_dB() { return mp_obj_new_float(st3m_audio_speaker_get_volume_dB()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_volume_dB_obj, mp_speaker_get_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_volume_dB_obj, + mp_speaker_get_volume_dB); STATIC mp_obj_t mp_get_volume_dB() { return mp_obj_new_float(st3m_audio_get_volume_dB()); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_get_volume_dB_obj, mp_get_volume_dB); - - STATIC mp_obj_t mp_headphones_get_mute() { return mp_obj_new_int(st3m_audio_headphones_get_mute()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_mute_obj, mp_headphones_get_mute); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_mute_obj, + mp_headphones_get_mute); STATIC mp_obj_t mp_speaker_get_mute() { return mp_obj_new_int(st3m_audio_speaker_get_mute()); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_mute_obj, mp_speaker_get_mute); -STATIC mp_obj_t mp_get_mute() { - return mp_obj_new_int(st3m_audio_get_mute()); -} +STATIC mp_obj_t mp_get_mute() { return mp_obj_new_int(st3m_audio_get_mute()); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_get_mute_obj, mp_get_mute); - - STATIC mp_obj_t mp_headphones_set_mute(mp_obj_t mute) { st3m_audio_headphones_set_mute(mp_obj_get_int(mute)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_set_mute_obj, mp_headphones_set_mute); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_set_mute_obj, + mp_headphones_set_mute); STATIC mp_obj_t mp_speaker_set_mute(mp_obj_t mute) { st3m_audio_speaker_set_mute(mp_obj_get_int(mute)); @@ -120,88 +124,96 @@ STATIC mp_obj_t mp_set_mute(mp_obj_t mute) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_set_mute_obj, mp_set_mute); - - STATIC mp_obj_t mp_headphones_set_minimum_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_headphones_set_minimum_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_headphones_set_minimum_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_set_minimum_volume_dB_obj, mp_headphones_set_minimum_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_set_minimum_volume_dB_obj, + mp_headphones_set_minimum_volume_dB); STATIC mp_obj_t mp_speaker_set_minimum_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_speaker_set_minimum_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_speaker_set_minimum_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_set_minimum_volume_dB_obj, mp_speaker_set_minimum_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_set_minimum_volume_dB_obj, + mp_speaker_set_minimum_volume_dB); STATIC mp_obj_t mp_headphones_set_maximum_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_headphones_set_maximum_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_headphones_set_maximum_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_set_maximum_volume_dB_obj, mp_headphones_set_maximum_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_set_maximum_volume_dB_obj, + mp_headphones_set_maximum_volume_dB); STATIC mp_obj_t mp_speaker_set_maximum_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_speaker_set_maximum_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_speaker_set_maximum_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_set_maximum_volume_dB_obj, mp_speaker_set_maximum_volume_dB); - - +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_set_maximum_volume_dB_obj, + mp_speaker_set_maximum_volume_dB); STATIC mp_obj_t mp_headphones_get_minimum_volume_dB() { return mp_obj_new_float(st3m_audio_headphones_get_minimum_volume_dB()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_minimum_volume_dB_obj, mp_headphones_get_minimum_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_minimum_volume_dB_obj, + mp_headphones_get_minimum_volume_dB); STATIC mp_obj_t mp_speaker_get_minimum_volume_dB() { return mp_obj_new_float(st3m_audio_speaker_get_minimum_volume_dB()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_minimum_volume_dB_obj, mp_speaker_get_minimum_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_minimum_volume_dB_obj, + mp_speaker_get_minimum_volume_dB); STATIC mp_obj_t mp_headphones_get_maximum_volume_dB() { return mp_obj_new_float(st3m_audio_headphones_get_maximum_volume_dB()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_maximum_volume_dB_obj, mp_headphones_get_maximum_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_maximum_volume_dB_obj, + mp_headphones_get_maximum_volume_dB); STATIC mp_obj_t mp_speaker_get_maximum_volume_dB() { return mp_obj_new_float(st3m_audio_speaker_get_maximum_volume_dB()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_maximum_volume_dB_obj, mp_speaker_get_maximum_volume_dB); - - +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_maximum_volume_dB_obj, + mp_speaker_get_maximum_volume_dB); STATIC mp_obj_t mp_headphones_get_volume_relative() { return mp_obj_new_float(st3m_audio_headphones_get_volume_relative()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_volume_relative_obj, mp_headphones_get_volume_relative); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headphones_get_volume_relative_obj, + mp_headphones_get_volume_relative); STATIC mp_obj_t mp_speaker_get_volume_relative() { return mp_obj_new_float(st3m_audio_speaker_get_volume_relative()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_volume_relative_obj, mp_speaker_get_volume_relative); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_speaker_get_volume_relative_obj, + mp_speaker_get_volume_relative); STATIC mp_obj_t mp_get_volume_relative() { return mp_obj_new_float(st3m_audio_get_volume_relative()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_get_volume_relative_obj, mp_get_volume_relative); - - +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_get_volume_relative_obj, + mp_get_volume_relative); STATIC mp_obj_t mp_headphones_line_in_set_hardware_thru(mp_obj_t enable) { st3m_audio_headphones_line_in_set_hardware_thru(mp_obj_get_int(enable)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_line_in_set_hardware_thru_obj, mp_headphones_line_in_set_hardware_thru); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headphones_line_in_set_hardware_thru_obj, + mp_headphones_line_in_set_hardware_thru); STATIC mp_obj_t mp_speaker_line_in_set_hardware_thru(mp_obj_t enable) { st3m_audio_speaker_line_in_set_hardware_thru(mp_obj_get_int(enable)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_line_in_set_hardware_thru_obj, mp_speaker_line_in_set_hardware_thru); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_speaker_line_in_set_hardware_thru_obj, + mp_speaker_line_in_set_hardware_thru); STATIC mp_obj_t mp_line_in_set_hardware_thru(mp_obj_t enable) { st3m_audio_line_in_set_hardware_thru(mp_obj_get_int(enable)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_line_in_set_hardware_thru_obj, mp_line_in_set_hardware_thru); - - +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_line_in_set_hardware_thru_obj, + mp_line_in_set_hardware_thru); STATIC mp_obj_t mp_input_set_source(mp_obj_t enable) { st3m_audio_input_set_source(mp_obj_get_int(enable)); @@ -214,50 +226,54 @@ STATIC mp_obj_t mp_input_get_source(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_input_get_source_obj, mp_input_get_source); - - STATIC mp_obj_t mp_headset_set_gain_dB(mp_obj_t gain_dB) { st3m_audio_headset_set_gain_dB(mp_obj_get_int(gain_dB)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headset_set_gain_dB_obj, mp_headset_set_gain_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_headset_set_gain_dB_obj, + mp_headset_set_gain_dB); STATIC mp_obj_t mp_headset_get_gain_dB(void) { return mp_obj_new_int(st3m_audio_headset_get_gain_dB()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headset_get_gain_dB_obj, mp_headset_get_gain_dB); - - +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_headset_get_gain_dB_obj, + mp_headset_get_gain_dB); STATIC mp_obj_t mp_input_thru_set_volume_dB(mp_obj_t vol_dB) { - return mp_obj_new_float(st3m_audio_input_thru_set_volume_dB(mp_obj_get_float(vol_dB))); + return mp_obj_new_float( + st3m_audio_input_thru_set_volume_dB(mp_obj_get_float(vol_dB))); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_input_thru_set_volume_dB_obj, mp_input_thru_set_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_input_thru_set_volume_dB_obj, + mp_input_thru_set_volume_dB); STATIC mp_obj_t mp_input_thru_get_volume_dB() { return mp_obj_new_float(st3m_audio_input_thru_get_volume_dB()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_input_thru_get_volume_dB_obj, mp_input_thru_get_volume_dB); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_input_thru_get_volume_dB_obj, + mp_input_thru_get_volume_dB); STATIC mp_obj_t mp_input_thru_set_mute(mp_obj_t mute) { st3m_audio_input_thru_set_mute(mp_obj_get_int(mute)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_input_thru_set_mute_obj, mp_input_thru_set_mute); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_input_thru_set_mute_obj, + mp_input_thru_set_mute); STATIC mp_obj_t mp_input_thru_get_mute() { return mp_obj_new_int(st3m_audio_input_thru_get_mute()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_input_thru_get_mute_obj, mp_input_thru_get_mute); - +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_input_thru_get_mute_obj, + mp_input_thru_get_mute); STATIC mp_obj_t mp_codec_i2c_write(mp_obj_t reg_in, mp_obj_t data_in) { -#if defined(CONFIG_FLOW3R_HW_GEN_P3) || defined(CONFIG_FLOW3R_HW_GEN_P4) || defined(CONFIG_FLOW3R_HW_GEN_P6) +#if defined(CONFIG_FLOW3R_HW_GEN_P3) || defined(CONFIG_FLOW3R_HW_GEN_P4) || \ + defined(CONFIG_FLOW3R_HW_GEN_P6) uint8_t reg = mp_obj_get_int(reg_in); uint8_t data = mp_obj_get_int(data_in); flow3r_bsp_audio_register_poke(reg, data); #elif defined(CONFIG_FLOW3R_HW_GEN_P1) - mp_raise_NotImplementedError(MP_ERROR_TEXT("not implemented for p1 badges")); + mp_raise_NotImplementedError( + MP_ERROR_TEXT("not implemented for p1 badges")); #else #error "audio not implemented for this badge generation" #endif @@ -265,77 +281,115 @@ STATIC mp_obj_t mp_codec_i2c_write(mp_obj_t reg_in, mp_obj_t data_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_codec_i2c_write_obj, mp_codec_i2c_write); - - STATIC const mp_rom_map_elem_t mp_module_audio_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audio) }, - { MP_ROM_QSTR(MP_QSTR_headset_is_connected), MP_ROM_PTR(&mp_headset_is_connected_obj) }, - { MP_ROM_QSTR(MP_QSTR_headphones_are_connected), MP_ROM_PTR(&mp_headphones_are_connected_obj) }, - { MP_ROM_QSTR(MP_QSTR_headphones_detection_override), MP_ROM_PTR(&mp_headphones_detection_override_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_set_volume_dB), MP_ROM_PTR(&mp_headphones_set_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_set_volume_dB), MP_ROM_PTR(&mp_speaker_set_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_volume_dB), MP_ROM_PTR(&mp_set_volume_dB_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_adjust_volume_dB), MP_ROM_PTR(&mp_headphones_adjust_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_adjust_volume_dB), MP_ROM_PTR(&mp_speaker_adjust_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_adjust_volume_dB), MP_ROM_PTR(&mp_adjust_volume_dB_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_get_volume_dB), MP_ROM_PTR(&mp_headphones_get_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_get_volume_dB), MP_ROM_PTR(&mp_speaker_get_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_volume_dB), MP_ROM_PTR(&mp_get_volume_dB_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_get_mute), MP_ROM_PTR(&mp_headphones_get_mute_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_get_mute), MP_ROM_PTR(&mp_speaker_get_mute_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_mute), MP_ROM_PTR(&mp_get_mute_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_set_mute), MP_ROM_PTR(&mp_headphones_set_mute_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_set_mute), MP_ROM_PTR(&mp_speaker_set_mute_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_mute), MP_ROM_PTR(&mp_set_mute_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_set_minimum_volume_dB), MP_ROM_PTR(&mp_headphones_set_minimum_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_set_minimum_volume_dB), MP_ROM_PTR(&mp_speaker_set_minimum_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_headphones_set_maximum_volume_dB), MP_ROM_PTR(&mp_headphones_set_maximum_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_set_maximum_volume_dB), MP_ROM_PTR(&mp_speaker_set_maximum_volume_dB_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_get_minimum_volume_dB), MP_ROM_PTR(&mp_headphones_get_minimum_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_get_minimum_volume_dB), MP_ROM_PTR(&mp_speaker_get_minimum_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_headphones_get_maximum_volume_dB), MP_ROM_PTR(&mp_headphones_get_maximum_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_get_maximum_volume_dB), MP_ROM_PTR(&mp_speaker_get_maximum_volume_dB_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_get_volume_relative), MP_ROM_PTR(&mp_headphones_get_volume_relative_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_get_volume_relative), MP_ROM_PTR(&mp_speaker_get_volume_relative_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_volume_relative), MP_ROM_PTR(&mp_get_volume_relative_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headphones_line_in_set_hardware_thru), MP_ROM_PTR(&mp_headphones_line_in_set_hardware_thru_obj) }, - { MP_ROM_QSTR(MP_QSTR_speaker_line_in_set_hardware_thru), MP_ROM_PTR(&mp_speaker_line_in_set_hardware_thru_obj) }, - { MP_ROM_QSTR(MP_QSTR_line_in_set_hardware_thru), MP_ROM_PTR(&mp_line_in_set_hardware_thru_obj) }, - - { MP_ROM_QSTR(MP_QSTR_input_set_source), MP_ROM_PTR(&mp_input_set_source_obj) }, - { MP_ROM_QSTR(MP_QSTR_input_get_source), MP_ROM_PTR(&mp_input_get_source_obj) }, - - { MP_ROM_QSTR(MP_QSTR_headset_set_gain_dB), MP_ROM_PTR(&mp_headset_set_gain_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_headset_get_gain_dB), MP_ROM_PTR(&mp_headset_get_gain_dB_obj) }, - - { MP_ROM_QSTR(MP_QSTR_input_thru_set_volume_dB), MP_ROM_PTR(&mp_input_thru_set_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_input_thru_get_volume_dB), MP_ROM_PTR(&mp_input_thru_get_volume_dB_obj) }, - { MP_ROM_QSTR(MP_QSTR_input_thru_set_mute), MP_ROM_PTR(&mp_input_thru_set_mute_obj) }, - { MP_ROM_QSTR(MP_QSTR_input_thru_get_mute), MP_ROM_PTR(&mp_input_thru_get_mute_obj) }, - - { MP_ROM_QSTR(MP_QSTR_codec_i2c_write), MP_ROM_PTR(&mp_codec_i2c_write_obj) }, - - { MP_ROM_QSTR(MP_QSTR_INPUT_SOURCE_NONE), MP_ROM_INT(st3m_audio_input_source_none) }, - { MP_ROM_QSTR(MP_QSTR_INPUT_SOURCE_LINE_IN), MP_ROM_INT(st3m_audio_input_source_line_in) }, - { MP_ROM_QSTR(MP_QSTR_INPUT_SOURCE_HEADSET_MIC), MP_ROM_INT(st3m_audio_input_source_headset_mic) }, - { MP_ROM_QSTR(MP_QSTR_INPUT_SOURCE_ONBOARD_MIC), MP_ROM_INT(st3m_audio_input_source_onboard_mic) }, + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audio)}, + {MP_ROM_QSTR(MP_QSTR_headset_is_connected), + MP_ROM_PTR(&mp_headset_is_connected_obj)}, + {MP_ROM_QSTR(MP_QSTR_headphones_are_connected), + MP_ROM_PTR(&mp_headphones_are_connected_obj)}, + {MP_ROM_QSTR(MP_QSTR_headphones_detection_override), + MP_ROM_PTR(&mp_headphones_detection_override_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_set_volume_dB), + MP_ROM_PTR(&mp_headphones_set_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_set_volume_dB), + MP_ROM_PTR(&mp_speaker_set_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_volume_dB), MP_ROM_PTR(&mp_set_volume_dB_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_adjust_volume_dB), + MP_ROM_PTR(&mp_headphones_adjust_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_adjust_volume_dB), + MP_ROM_PTR(&mp_speaker_adjust_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_adjust_volume_dB), + MP_ROM_PTR(&mp_adjust_volume_dB_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_get_volume_dB), + MP_ROM_PTR(&mp_headphones_get_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_get_volume_dB), + MP_ROM_PTR(&mp_speaker_get_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_volume_dB), MP_ROM_PTR(&mp_get_volume_dB_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_get_mute), + MP_ROM_PTR(&mp_headphones_get_mute_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_get_mute), + MP_ROM_PTR(&mp_speaker_get_mute_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_mute), MP_ROM_PTR(&mp_get_mute_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_set_mute), + MP_ROM_PTR(&mp_headphones_set_mute_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_set_mute), + MP_ROM_PTR(&mp_speaker_set_mute_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_mute), MP_ROM_PTR(&mp_set_mute_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_set_minimum_volume_dB), + MP_ROM_PTR(&mp_headphones_set_minimum_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_set_minimum_volume_dB), + MP_ROM_PTR(&mp_speaker_set_minimum_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_headphones_set_maximum_volume_dB), + MP_ROM_PTR(&mp_headphones_set_maximum_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_set_maximum_volume_dB), + MP_ROM_PTR(&mp_speaker_set_maximum_volume_dB_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_get_minimum_volume_dB), + MP_ROM_PTR(&mp_headphones_get_minimum_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_get_minimum_volume_dB), + MP_ROM_PTR(&mp_speaker_get_minimum_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_headphones_get_maximum_volume_dB), + MP_ROM_PTR(&mp_headphones_get_maximum_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_get_maximum_volume_dB), + MP_ROM_PTR(&mp_speaker_get_maximum_volume_dB_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_get_volume_relative), + MP_ROM_PTR(&mp_headphones_get_volume_relative_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_get_volume_relative), + MP_ROM_PTR(&mp_speaker_get_volume_relative_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_volume_relative), + MP_ROM_PTR(&mp_get_volume_relative_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headphones_line_in_set_hardware_thru), + MP_ROM_PTR(&mp_headphones_line_in_set_hardware_thru_obj)}, + {MP_ROM_QSTR(MP_QSTR_speaker_line_in_set_hardware_thru), + MP_ROM_PTR(&mp_speaker_line_in_set_hardware_thru_obj)}, + {MP_ROM_QSTR(MP_QSTR_line_in_set_hardware_thru), + MP_ROM_PTR(&mp_line_in_set_hardware_thru_obj)}, + + {MP_ROM_QSTR(MP_QSTR_input_set_source), + MP_ROM_PTR(&mp_input_set_source_obj)}, + {MP_ROM_QSTR(MP_QSTR_input_get_source), + MP_ROM_PTR(&mp_input_get_source_obj)}, + + {MP_ROM_QSTR(MP_QSTR_headset_set_gain_dB), + MP_ROM_PTR(&mp_headset_set_gain_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_headset_get_gain_dB), + MP_ROM_PTR(&mp_headset_get_gain_dB_obj)}, + + {MP_ROM_QSTR(MP_QSTR_input_thru_set_volume_dB), + MP_ROM_PTR(&mp_input_thru_set_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_input_thru_get_volume_dB), + MP_ROM_PTR(&mp_input_thru_get_volume_dB_obj)}, + {MP_ROM_QSTR(MP_QSTR_input_thru_set_mute), + MP_ROM_PTR(&mp_input_thru_set_mute_obj)}, + {MP_ROM_QSTR(MP_QSTR_input_thru_get_mute), + MP_ROM_PTR(&mp_input_thru_get_mute_obj)}, + + {MP_ROM_QSTR(MP_QSTR_codec_i2c_write), MP_ROM_PTR(&mp_codec_i2c_write_obj)}, + + {MP_ROM_QSTR(MP_QSTR_INPUT_SOURCE_NONE), + MP_ROM_INT(st3m_audio_input_source_none)}, + {MP_ROM_QSTR(MP_QSTR_INPUT_SOURCE_LINE_IN), + MP_ROM_INT(st3m_audio_input_source_line_in)}, + {MP_ROM_QSTR(MP_QSTR_INPUT_SOURCE_HEADSET_MIC), + MP_ROM_INT(st3m_audio_input_source_headset_mic)}, + {MP_ROM_QSTR(MP_QSTR_INPUT_SOURCE_ONBOARD_MIC), + MP_ROM_INT(st3m_audio_input_source_onboard_mic)}, }; -STATIC MP_DEFINE_CONST_DICT(mp_module_audio_globals, mp_module_audio_globals_table); +STATIC MP_DEFINE_CONST_DICT(mp_module_audio_globals, + mp_module_audio_globals_table); const mp_obj_module_t mp_module_audio = { - .base = { &mp_type_module }, + .base = {&mp_type_module}, .globals = (mp_obj_dict_t *)&mp_module_audio_globals, }; MP_REGISTER_MODULE(MP_QSTR_audio, mp_module_audio); - diff --git a/usermodule/mp_badge_link.c b/usermodule/mp_badge_link.c index cb1bfc8e8e..25c3932791 100644 --- a/usermodule/mp_badge_link.c +++ b/usermodule/mp_badge_link.c @@ -2,17 +2,16 @@ #include <stdio.h> #include <string.h> -#include "py/runtime.h" -#include "py/mphal.h" -#include "mphalport.h" -#include "modmachine.h" #include "extmod/virtpin.h" #include "machine_rtc.h" +#include "modmachine.h" +#include "mphalport.h" #include "py/builtin.h" +#include "py/mphal.h" #include "py/runtime.h" -#include "st3m_io.h" #include "flow3r_bsp.h" +#include "st3m_io.h" // Badgelink API. // @@ -40,43 +39,47 @@ typedef struct _badgelink_jack_t { const mp_obj_type_t badgelink_jack_type; - STATIC badgelink_jack_t left = { .base = {&badgelink_jack_type}, .left = true, - .tip = { - .base = {&badgelink_jack_pin_type}, - .left = true, - .tip = true, - .pin = mp_const_none, - }, - .ring = { - .base = {&badgelink_jack_pin_type}, - .left = true, - .tip = false, - .pin = mp_const_none, - }, + .tip = + { + .base = {&badgelink_jack_pin_type}, + .left = true, + .tip = true, + .pin = mp_const_none, + }, + .ring = + { + .base = {&badgelink_jack_pin_type}, + .left = true, + .tip = false, + .pin = mp_const_none, + }, }; STATIC badgelink_jack_t right = { .base = {&badgelink_jack_type}, .left = false, - .tip = { - .base = {&badgelink_jack_pin_type}, - .left = false, - .tip = true, - .pin = mp_const_none, - }, - .ring = { - .base = {&badgelink_jack_pin_type}, - .left = false, - .tip = false, - .pin = mp_const_none, - }, + .tip = + { + .base = {&badgelink_jack_pin_type}, + .left = false, + .tip = true, + .pin = mp_const_none, + }, + .ring = + { + .base = {&badgelink_jack_pin_type}, + .left = false, + .tip = false, + .pin = mp_const_none, + }, }; -STATIC void badgelink_jack_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void badgelink_jack_pin_print(const mp_print_t *print, mp_obj_t self_in, + mp_print_kind_t kind) { badgelink_jack_pin_t *self = MP_OBJ_TO_PTR(self_in); if (self->left) { @@ -92,7 +95,8 @@ STATIC void badgelink_jack_pin_print(const mp_print_t *print, mp_obj_t self_in, } } -STATIC void badgelink_jack_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void badgelink_jack_print(const mp_print_t *print, mp_obj_t self_in, + mp_print_kind_t kind) { badgelink_jack_t *self = MP_OBJ_TO_PTR(self_in); if (self->left) @@ -102,9 +106,11 @@ STATIC void badgelink_jack_print(const mp_print_t *print, mp_obj_t self_in, mp_p } // From machine_pin.c. Used to make a machine.Pin below. -mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args); +mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, + const mp_obj_t *args); -STATIC void badgelink_jack_pin_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { +STATIC void badgelink_jack_pin_attr(mp_obj_t self_in, qstr attr, + mp_obj_t *dest) { badgelink_jack_pin_t *self = MP_OBJ_TO_PTR(self_in); if (dest[0] != MP_OBJ_NULL) { return; @@ -112,20 +118,21 @@ STATIC void badgelink_jack_pin_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) if (self->pin == mp_const_none) { bool left = self->left; bool tip = self->tip; - uint32_t unum = left - ? (tip - ? flow3r_bsp_spio_programmable_pins.badgelink_left_tip - : flow3r_bsp_spio_programmable_pins.badgelink_left_ring) - : (tip - ? flow3r_bsp_spio_programmable_pins.badgelink_right_tip - : flow3r_bsp_spio_programmable_pins.badgelink_right_ring); + uint32_t unum = + left ? (tip ? flow3r_bsp_spio_programmable_pins.badgelink_left_tip + : flow3r_bsp_spio_programmable_pins.badgelink_left_ring) + : (tip ? flow3r_bsp_spio_programmable_pins.badgelink_right_tip + : flow3r_bsp_spio_programmable_pins + .badgelink_right_ring); mp_obj_t num = mp_obj_new_int_from_uint(unum); self->pin = mp_pin_make_new(NULL, 1, 0, &num); } switch (attr) { - case MP_QSTR_pin: dest[0] = self->pin; break; - default: - dest[1] = MP_OBJ_SENTINEL; + case MP_QSTR_pin: + dest[0] = self->pin; + break; + default: + dest[1] = MP_OBJ_SENTINEL; } } @@ -135,10 +142,14 @@ STATIC void badgelink_jack_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { return; } switch (attr) { - case MP_QSTR_tip: dest[0] = MP_OBJ_FROM_PTR(&self->tip); break; - case MP_QSTR_ring: dest[0] = MP_OBJ_FROM_PTR(&self->ring); break; - default: - dest[1] = MP_OBJ_SENTINEL; + case MP_QSTR_tip: + dest[0] = MP_OBJ_FROM_PTR(&self->tip); + break; + case MP_QSTR_ring: + dest[0] = MP_OBJ_FROM_PTR(&self->ring); + break; + default: + dest[1] = MP_OBJ_SENTINEL; } } @@ -174,7 +185,8 @@ STATIC mp_obj_t badgelink_jack_active(mp_obj_t self_in) { uint8_t val = st3m_io_badge_link_get_active(pin_mask); return mp_obj_new_bool(val == pin_mask); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(badgelink_jack_active_obj, badgelink_jack_active); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(badgelink_jack_active_obj, + badgelink_jack_active); // Shared between Jack and JackPin. STATIC mp_obj_t badgelink_jack_enable(mp_obj_t self_in) { @@ -182,7 +194,8 @@ STATIC mp_obj_t badgelink_jack_enable(mp_obj_t self_in) { uint8_t val = st3m_io_badge_link_enable(pin_mask); return mp_obj_new_bool(val == pin_mask); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(badgelink_jack_enable_obj, badgelink_jack_enable); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(badgelink_jack_enable_obj, + badgelink_jack_enable); // Shared between Jack and JackPin. STATIC mp_obj_t badgelink_jack_disable(mp_obj_t self_in) { @@ -190,48 +203,41 @@ STATIC mp_obj_t badgelink_jack_disable(mp_obj_t self_in) { st3m_io_badge_link_disable(pin_mask); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(badgelink_jack_disable_obj, badgelink_jack_disable); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(badgelink_jack_disable_obj, + badgelink_jack_disable); // Shared between Jack and JackPin. STATIC const mp_rom_map_elem_t badgelink_jack_locals_dict_table[] = { // instance methods - { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&badgelink_jack_enable_obj) }, - { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&badgelink_jack_disable_obj) }, - { MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&badgelink_jack_active_obj) }, + {MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&badgelink_jack_enable_obj)}, + {MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&badgelink_jack_disable_obj)}, + {MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&badgelink_jack_active_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(badgelink_jack_locals_dict, badgelink_jack_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - badgelink_jack_pin_type, - MP_QSTR_JackPin, - MP_TYPE_FLAG_NONE, - print, badgelink_jack_pin_print, - attr, badgelink_jack_pin_attr, - locals_dict, &badgelink_jack_locals_dict -); - -MP_DEFINE_CONST_OBJ_TYPE( - badgelink_jack_type, - MP_QSTR_Jack, - MP_TYPE_FLAG_NONE, - print, badgelink_jack_print, - attr, badgelink_jack_attr, - locals_dict, &badgelink_jack_locals_dict -); +STATIC MP_DEFINE_CONST_DICT(badgelink_jack_locals_dict, + badgelink_jack_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE(badgelink_jack_pin_type, MP_QSTR_JackPin, + MP_TYPE_FLAG_NONE, print, badgelink_jack_pin_print, + attr, badgelink_jack_pin_attr, locals_dict, + &badgelink_jack_locals_dict); + +MP_DEFINE_CONST_OBJ_TYPE(badgelink_jack_type, MP_QSTR_Jack, MP_TYPE_FLAG_NONE, + print, badgelink_jack_print, attr, badgelink_jack_attr, + locals_dict, &badgelink_jack_locals_dict); STATIC const mp_rom_map_elem_t mp_module_badge_link_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_link) }, - { MP_ROM_QSTR(MP_QSTR_left), MP_ROM_PTR(&left) }, - { MP_ROM_QSTR(MP_QSTR_right), MP_ROM_PTR(&right) }, + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_link)}, + {MP_ROM_QSTR(MP_QSTR_left), MP_ROM_PTR(&left)}, + {MP_ROM_QSTR(MP_QSTR_right), MP_ROM_PTR(&right)}, }; -STATIC MP_DEFINE_CONST_DICT(mp_module_badge_link_globals, mp_module_badge_link_globals_table); +STATIC MP_DEFINE_CONST_DICT(mp_module_badge_link_globals, + mp_module_badge_link_globals_table); const mp_obj_module_t mp_module_badge_link = { - .base = { &mp_type_module }, + .base = {&mp_type_module}, .globals = (mp_obj_dict_t *)&mp_module_badge_link_globals, }; MP_REGISTER_MODULE(MP_QSTR_badge_link, mp_module_badge_link); - diff --git a/usermodule/mp_bl00mbox.c b/usermodule/mp_bl00mbox.c index f41cb4c7f2..e223c1c306 100644 --- a/usermodule/mp_bl00mbox.c +++ b/usermodule/mp_bl00mbox.c @@ -1,7 +1,7 @@ #include <stdio.h> -#include "py/runtime.h" #include "py/obj.h" +#include "py/runtime.h" #include "bl00mbox.h" #include "tinysynth.h" @@ -18,7 +18,8 @@ typedef struct _synth_tinysynth_obj_t { const mp_obj_type_t synth_tinysynth_type; -STATIC void tinysynth_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void tinysynth_print(const mp_print_t *print, mp_obj_t self_in, + mp_print_kind_t kind) { (void)kind; synth_tinysynth_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_print_str(print, "tinysynth("); @@ -26,9 +27,11 @@ STATIC void tinysynth_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ mp_print_str(print, ")"); } -STATIC mp_obj_t tinysynth_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t tinysynth_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, 1, true); - synth_tinysynth_obj_t *self = m_new_obj_with_finaliser(synth_tinysynth_obj_t); + synth_tinysynth_obj_t *self = + m_new_obj_with_finaliser(synth_tinysynth_obj_t); self->base.type = &synth_tinysynth_type; self->osc.vol = 32767; @@ -126,44 +129,35 @@ STATIC mp_obj_t tinysynth_deinit(mp_obj_t self_in) { MP_DEFINE_CONST_FUN_OBJ_1(tinysynth_deinit_obj, tinysynth_deinit); STATIC const mp_rom_map_elem_t tinysynth_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&tinysynth_start_obj) }, - { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&tinysynth_stop_obj) }, - { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&tinysynth_freq_obj) }, - { MP_ROM_QSTR(MP_QSTR_tone), MP_ROM_PTR(&tinysynth_tone_obj) }, - { MP_ROM_QSTR(MP_QSTR_waveform), MP_ROM_PTR(&tinysynth_waveform_obj) }, - { MP_ROM_QSTR(MP_QSTR_attack_ms), MP_ROM_PTR(&tinysynth_attack_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR_decay_ms), MP_ROM_PTR(&tinysynth_decay_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR_volume), MP_ROM_PTR(&tinysynth_sustain_obj) }, - { MP_ROM_QSTR(MP_QSTR_sustain), MP_ROM_PTR(&tinysynth_sustain_obj) }, - { MP_ROM_QSTR(MP_QSTR_release_ms), MP_ROM_PTR(&tinysynth_release_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&tinysynth_deinit_obj) }, + {MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&tinysynth_start_obj)}, + {MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&tinysynth_stop_obj)}, + {MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&tinysynth_freq_obj)}, + {MP_ROM_QSTR(MP_QSTR_tone), MP_ROM_PTR(&tinysynth_tone_obj)}, + {MP_ROM_QSTR(MP_QSTR_waveform), MP_ROM_PTR(&tinysynth_waveform_obj)}, + {MP_ROM_QSTR(MP_QSTR_attack_ms), MP_ROM_PTR(&tinysynth_attack_ms_obj)}, + {MP_ROM_QSTR(MP_QSTR_decay_ms), MP_ROM_PTR(&tinysynth_decay_ms_obj)}, + {MP_ROM_QSTR(MP_QSTR_volume), MP_ROM_PTR(&tinysynth_sustain_obj)}, + {MP_ROM_QSTR(MP_QSTR_sustain), MP_ROM_PTR(&tinysynth_sustain_obj)}, + {MP_ROM_QSTR(MP_QSTR_release_ms), MP_ROM_PTR(&tinysynth_release_ms_obj)}, + {MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&tinysynth_deinit_obj)}, }; STATIC MP_DEFINE_CONST_DICT(tinysynth_locals_dict, tinysynth_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - synth_tinysynth_type, - MP_QSTR_bl00mbox, - MP_TYPE_FLAG_NONE, - make_new, tinysynth_make_new, - print, tinysynth_print, - locals_dict, &tinysynth_locals_dict - ); +MP_DEFINE_CONST_OBJ_TYPE(synth_tinysynth_type, MP_QSTR_bl00mbox, + MP_TYPE_FLAG_NONE, make_new, tinysynth_make_new, print, + tinysynth_print, locals_dict, &tinysynth_locals_dict); STATIC const mp_map_elem_t bl00mbox_globals_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_bl00mbox) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_tinysynth), (mp_obj_t)&synth_tinysynth_type }, + {MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_bl00mbox)}, + {MP_OBJ_NEW_QSTR(MP_QSTR_tinysynth), (mp_obj_t)&synth_tinysynth_type}, }; -STATIC MP_DEFINE_CONST_DICT ( - mp_module_bl00mbox_globals, - bl00mbox_globals_table -); +STATIC MP_DEFINE_CONST_DICT(mp_module_bl00mbox_globals, bl00mbox_globals_table); const mp_obj_module_t bl00mbox_user_cmodule = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&mp_module_bl00mbox_globals, + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t *)&mp_module_bl00mbox_globals, }; MP_REGISTER_MODULE(MP_QSTR_bl00mbox, bl00mbox_user_cmodule); diff --git a/usermodule/mp_captouch.c b/usermodule/mp_captouch.c index 9e27c595ea..b4f76a1f45 100644 --- a/usermodule/mp_captouch.c +++ b/usermodule/mp_captouch.c @@ -1,158 +1,170 @@ -#include "py/runtime.h" #include "py/builtin.h" +#include "py/runtime.h" #include "badge23/captouch.h" #include <string.h> -STATIC mp_obj_t mp_captouch_calibration_active(void) -{ +STATIC mp_obj_t mp_captouch_calibration_active(void) { return mp_obj_new_int(captouch_calibration_active()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_captouch_calibration_active_obj, mp_captouch_calibration_active); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_captouch_calibration_active_obj, + mp_captouch_calibration_active); typedef struct { - mp_obj_base_t base; - mp_obj_t petal; + mp_obj_base_t base; + mp_obj_t petal; } mp_captouch_petal_pads_state_t; const mp_obj_type_t captouch_petal_pads_state_type; typedef struct { - mp_obj_base_t base; - mp_obj_t captouch; - mp_obj_t pads; - size_t ix; + mp_obj_base_t base; + mp_obj_t captouch; + mp_obj_t pads; + size_t ix; } mp_captouch_petal_state_t; const mp_obj_type_t captouch_petal_state_type; typedef struct { - mp_obj_base_t base; - mp_obj_t petals; - captouch_state_t underlying; + mp_obj_base_t base; + mp_obj_t petals; + captouch_state_t underlying; } mp_captouch_state_t; const mp_obj_type_t captouch_state_type; -STATIC void mp_captouch_petal_pads_state_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { - mp_captouch_petal_pads_state_t *self = MP_OBJ_TO_PTR(self_in); - if (dest[0] != MP_OBJ_NULL) { - return; - } - - mp_captouch_petal_state_t *petal = MP_OBJ_TO_PTR(self->petal); - mp_captouch_state_t *captouch = MP_OBJ_TO_PTR(petal->captouch); - captouch_petal_state_t *state = &captouch->underlying.petals[petal->ix]; - bool top = (petal->ix % 2) == 0; - - if (top) { - switch (attr) { - case MP_QSTR_base: dest[0] = mp_obj_new_bool(state->pads.base_pressed); break; - case MP_QSTR_cw: dest[0] = mp_obj_new_bool(state->pads.cw_pressed); break; - case MP_QSTR_ccw: dest[0] = mp_obj_new_bool(state->pads.ccw_pressed); break; - } - } else { - switch (attr) { - case MP_QSTR_tip: dest[0] = mp_obj_new_bool(state->pads.tip_pressed); break; - case MP_QSTR_base: dest[0] = mp_obj_new_bool(state->pads.base_pressed); break; - } - } +STATIC void mp_captouch_petal_pads_state_attr(mp_obj_t self_in, qstr attr, + mp_obj_t *dest) { + mp_captouch_petal_pads_state_t *self = MP_OBJ_TO_PTR(self_in); + if (dest[0] != MP_OBJ_NULL) { + return; + } + + mp_captouch_petal_state_t *petal = MP_OBJ_TO_PTR(self->petal); + mp_captouch_state_t *captouch = MP_OBJ_TO_PTR(petal->captouch); + captouch_petal_state_t *state = &captouch->underlying.petals[petal->ix]; + bool top = (petal->ix % 2) == 0; + + if (top) { + switch (attr) { + case MP_QSTR_base: + dest[0] = mp_obj_new_bool(state->pads.base_pressed); + break; + case MP_QSTR_cw: + dest[0] = mp_obj_new_bool(state->pads.cw_pressed); + break; + case MP_QSTR_ccw: + dest[0] = mp_obj_new_bool(state->pads.ccw_pressed); + break; + } + } else { + switch (attr) { + case MP_QSTR_tip: + dest[0] = mp_obj_new_bool(state->pads.tip_pressed); + break; + case MP_QSTR_base: + dest[0] = mp_obj_new_bool(state->pads.base_pressed); + break; + } + } } -STATIC void mp_captouch_petal_state_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { - mp_captouch_petal_state_t *self = MP_OBJ_TO_PTR(self_in); - if (dest[0] != MP_OBJ_NULL) { - return; - } - - mp_captouch_state_t *captouch = MP_OBJ_TO_PTR(self->captouch); - captouch_petal_state_t *state = &captouch->underlying.petals[self->ix]; - - bool top = (self->ix % 2) == 0; +STATIC void mp_captouch_petal_state_attr(mp_obj_t self_in, qstr attr, + mp_obj_t *dest) { + mp_captouch_petal_state_t *self = MP_OBJ_TO_PTR(self_in); + if (dest[0] != MP_OBJ_NULL) { + return; + } + + mp_captouch_state_t *captouch = MP_OBJ_TO_PTR(self->captouch); + captouch_petal_state_t *state = &captouch->underlying.petals[self->ix]; + + bool top = (self->ix % 2) == 0; + + switch (attr) { + case MP_QSTR_top: + dest[0] = mp_obj_new_bool(top); + break; + case MP_QSTR_bottom: + dest[0] = mp_obj_new_bool(!top); + break; + case MP_QSTR_pressed: + dest[0] = mp_obj_new_bool(state->pressed); + break; + case MP_QSTR_pads: + dest[0] = self->pads; + break; + } +} - switch (attr) { - case MP_QSTR_top: dest[0] = mp_obj_new_bool(top); break; - case MP_QSTR_bottom: dest[0] = mp_obj_new_bool(!top); break; - case MP_QSTR_pressed: dest[0] = mp_obj_new_bool(state->pressed); break; - case MP_QSTR_pads: dest[0] = self->pads; break; - } +STATIC void mp_captouch_state_attr(mp_obj_t self_in, qstr attr, + mp_obj_t *dest) { + mp_captouch_state_t *self = MP_OBJ_TO_PTR(self_in); + if (dest[0] != MP_OBJ_NULL) { + return; + } + + switch (attr) { + case MP_QSTR_petals: + dest[0] = self->petals; + break; + } } -STATIC void mp_captouch_state_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { - mp_captouch_state_t *self = MP_OBJ_TO_PTR(self_in); - if (dest[0] != MP_OBJ_NULL) { - return; - } +MP_DEFINE_CONST_OBJ_TYPE(captouch_petal_pads_state_type, + MP_QSTR_CaptouchPetalPadsState, MP_TYPE_FLAG_NONE, + attr, mp_captouch_petal_pads_state_attr); - switch (attr) { - case MP_QSTR_petals: dest[0] = self->petals; break; - } -} +MP_DEFINE_CONST_OBJ_TYPE(captouch_petal_state_type, MP_QSTR_CaptouchPetalState, + MP_TYPE_FLAG_NONE, attr, mp_captouch_petal_state_attr); -MP_DEFINE_CONST_OBJ_TYPE( - captouch_petal_pads_state_type, - MP_QSTR_CaptouchPetalPadsState, - MP_TYPE_FLAG_NONE, - attr, mp_captouch_petal_pads_state_attr -); - -MP_DEFINE_CONST_OBJ_TYPE( - captouch_petal_state_type, - MP_QSTR_CaptouchPetalState, - MP_TYPE_FLAG_NONE, - attr, mp_captouch_petal_state_attr -); - -MP_DEFINE_CONST_OBJ_TYPE( - captouch_state_type, - MP_QSTR_CaptouchState, - MP_TYPE_FLAG_NONE, - attr, mp_captouch_state_attr -); +MP_DEFINE_CONST_OBJ_TYPE(captouch_state_type, MP_QSTR_CaptouchState, + MP_TYPE_FLAG_NONE, attr, mp_captouch_state_attr); STATIC mp_obj_t mp_captouch_state_new(const captouch_state_t *underlying) { - mp_captouch_state_t *captouch = m_new_obj(mp_captouch_state_t); - captouch->base.type = &captouch_state_type; - memcpy(&captouch->underlying, underlying, sizeof(captouch_state_t)); - - captouch->petals = mp_obj_new_list(0, NULL); - for (int i = 0; i < 10; i++) { - mp_captouch_petal_state_t *petal = m_new_obj(mp_captouch_petal_state_t); - petal->base.type = &captouch_petal_state_type; - petal->captouch = MP_OBJ_FROM_PTR(captouch); - petal->ix = i; - - mp_captouch_petal_pads_state_t *pads = m_new_obj(mp_captouch_petal_pads_state_t); - pads->base.type = &captouch_petal_pads_state_type; - pads->petal = MP_OBJ_FROM_PTR(petal); - petal->pads = MP_OBJ_FROM_PTR(pads); - - mp_obj_list_append(captouch->petals, MP_OBJ_FROM_PTR(petal)); - } - - return MP_OBJ_FROM_PTR(captouch); + mp_captouch_state_t *captouch = m_new_obj(mp_captouch_state_t); + captouch->base.type = &captouch_state_type; + memcpy(&captouch->underlying, underlying, sizeof(captouch_state_t)); + + captouch->petals = mp_obj_new_list(0, NULL); + for (int i = 0; i < 10; i++) { + mp_captouch_petal_state_t *petal = m_new_obj(mp_captouch_petal_state_t); + petal->base.type = &captouch_petal_state_type; + petal->captouch = MP_OBJ_FROM_PTR(captouch); + petal->ix = i; + + mp_captouch_petal_pads_state_t *pads = + m_new_obj(mp_captouch_petal_pads_state_t); + pads->base.type = &captouch_petal_pads_state_type; + pads->petal = MP_OBJ_FROM_PTR(petal); + petal->pads = MP_OBJ_FROM_PTR(pads); + + mp_obj_list_append(captouch->petals, MP_OBJ_FROM_PTR(petal)); + } + + return MP_OBJ_FROM_PTR(captouch); } STATIC mp_obj_t mp_captouch_read(void) { - captouch_state_t st; - read_captouch_ex(&st); - return mp_captouch_state_new(&st); + captouch_state_t st; + read_captouch_ex(&st); + return mp_captouch_state_new(&st); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_captouch_read_obj, mp_captouch_read); STATIC const mp_rom_map_elem_t globals_table[] = { - { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_captouch_read_obj) }, - { MP_ROM_QSTR(MP_QSTR_calibration_active), MP_ROM_PTR(&mp_captouch_calibration_active_obj) } -}; + {MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_captouch_read_obj)}, + {MP_ROM_QSTR(MP_QSTR_calibration_active), + MP_ROM_PTR(&mp_captouch_calibration_active_obj)}}; STATIC MP_DEFINE_CONST_DICT(globals, globals_table); - const mp_obj_module_t mp_module_captouch_user_cmodule = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t *)&globals, + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t *)&globals, }; MP_REGISTER_MODULE(MP_QSTR_captouch, mp_module_captouch_user_cmodule); \ No newline at end of file diff --git a/usermodule/mp_hardware.c b/usermodule/mp_hardware.c index 0df60f5952..91ba52e281 100644 --- a/usermodule/mp_hardware.c +++ b/usermodule/mp_hardware.c @@ -2,40 +2,41 @@ #include <stdio.h> #include <string.h> -#include "py/runtime.h" -#include "py/mphal.h" -#include "mphalport.h" -#include "modmachine.h" #include "extmod/virtpin.h" #include "machine_rtc.h" +#include "modmachine.h" +#include "mphalport.h" #include "py/builtin.h" +#include "py/mphal.h" #include "py/runtime.h" #include "badge23/captouch.h" #include "flow3r_bsp.h" +#include "st3m_console.h" #include "st3m_gfx.h" -#include "st3m_scope.h" #include "st3m_io.h" -#include "st3m_console.h" +#include "st3m_scope.h" #include "st3m_usb.h" -#include "ctx_config.h" #include "ctx.h" +#include "ctx_config.h" mp_obj_t mp_ctx_from_ctx(Ctx *ctx); STATIC mp_obj_t mp_captouch_calibration_active(void) { return mp_obj_new_int(captouch_calibration_active()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_captouch_calibration_active_obj, mp_captouch_calibration_active); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_captouch_calibration_active_obj, + mp_captouch_calibration_active); STATIC mp_obj_t mp_display_set_backlight(mp_obj_t percent_in) { uint8_t percent = mp_obj_get_int(percent_in); flow3r_bsp_display_set_backlight(percent); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_display_set_backlight_obj, mp_display_set_backlight); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_display_set_backlight_obj, + mp_display_set_backlight); STATIC mp_obj_t mp_get_captouch(size_t n_args, const mp_obj_t *args) { uint16_t captouch = read_captouch(); @@ -44,24 +45,27 @@ STATIC mp_obj_t mp_get_captouch(size_t n_args, const mp_obj_t *args) { return mp_obj_new_int(output); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_get_captouch_obj, 1, 2, mp_get_captouch); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_get_captouch_obj, 1, 2, + mp_get_captouch); -STATIC mp_obj_t mp_captouch_get_petal_pad_raw(mp_obj_t petal_in, mp_obj_t pad_in) { +STATIC mp_obj_t mp_captouch_get_petal_pad_raw(mp_obj_t petal_in, + mp_obj_t pad_in) { uint8_t petal = mp_obj_get_int(petal_in); uint8_t pad = mp_obj_get_int(pad_in); uint16_t output = captouch_get_petal_pad_raw(petal, pad); return mp_obj_new_int(output); } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_captouch_get_petal_pad_raw_obj, mp_captouch_get_petal_pad_raw); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_captouch_get_petal_pad_raw_obj, + mp_captouch_get_petal_pad_raw); STATIC mp_obj_t mp_captouch_get_petal_pad(mp_obj_t petal_in, mp_obj_t pad_in) { uint8_t petal = mp_obj_get_int(petal_in); uint8_t pad = mp_obj_get_int(pad_in); return mp_obj_new_int(captouch_get_petal_pad(petal, pad)); - } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_captouch_get_petal_pad_obj, mp_captouch_get_petal_pad); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_captouch_get_petal_pad_obj, + mp_captouch_get_petal_pad); STATIC mp_obj_t mp_captouch_get_petal_rad(mp_obj_t petal_in) { uint8_t petal = mp_obj_get_int(petal_in); @@ -69,7 +73,8 @@ STATIC mp_obj_t mp_captouch_get_petal_rad(mp_obj_t petal_in) { return mp_obj_new_int(ret); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_captouch_get_petal_rad_obj, mp_captouch_get_petal_rad); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_captouch_get_petal_rad_obj, + mp_captouch_get_petal_rad); STATIC mp_obj_t mp_captouch_get_petal_phi(mp_obj_t petal_in) { uint8_t petal = mp_obj_get_int(petal_in); @@ -77,41 +82,48 @@ STATIC mp_obj_t mp_captouch_get_petal_phi(mp_obj_t petal_in) { return mp_obj_new_int(ret); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_captouch_get_petal_phi_obj, mp_captouch_get_petal_phi); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_captouch_get_petal_phi_obj, + mp_captouch_get_petal_phi); -STATIC mp_obj_t mp_captouch_set_petal_pad_threshold(mp_obj_t petal_in, mp_obj_t pad_in, mp_obj_t thres_in) { +STATIC mp_obj_t mp_captouch_set_petal_pad_threshold(mp_obj_t petal_in, + mp_obj_t pad_in, + mp_obj_t thres_in) { uint8_t petal = mp_obj_get_int(petal_in); uint8_t pad = mp_obj_get_int(pad_in); uint16_t thres = mp_obj_get_int(thres_in); captouch_set_petal_pad_threshold(petal, pad, thres); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mp_captouch_set_petal_pad_threshold_obj, mp_captouch_set_petal_pad_threshold); +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mp_captouch_set_petal_pad_threshold_obj, + mp_captouch_set_petal_pad_threshold); STATIC mp_obj_t mp_captouch_autocalib(void) { captouch_force_calibration(); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_captouch_autocalib_obj, mp_captouch_autocalib); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_captouch_autocalib_obj, + mp_captouch_autocalib); STATIC mp_obj_t mp_captouch_set_calibration_afe_target(mp_obj_t target_in) { uint16_t target = mp_obj_get_int(target_in); captouch_set_calibration_afe_target(target); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_captouch_set_calibration_afe_target_obj, mp_captouch_set_calibration_afe_target); - +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_captouch_set_calibration_afe_target_obj, + mp_captouch_set_calibration_afe_target); STATIC mp_obj_t mp_menu_button_set_left(mp_obj_t left) { st3m_io_menu_button_set_left(mp_obj_get_int(left)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_menu_button_set_left_obj, mp_menu_button_set_left); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_menu_button_set_left_obj, + mp_menu_button_set_left); STATIC mp_obj_t mp_menu_button_get_left() { return mp_obj_new_int(st3m_io_menu_button_get_left()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_menu_button_get_left_obj, mp_menu_button_get_left); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_menu_button_get_left_obj, + mp_menu_button_get_left); STATIC mp_obj_t mp_menu_button_get() { return mp_obj_new_int(st3m_io_menu_button_get()); @@ -121,7 +133,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_menu_button_get_obj, mp_menu_button_get); STATIC mp_obj_t mp_application_button_get() { return mp_obj_new_int(st3m_io_application_button_get()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_application_button_get_obj, mp_application_button_get); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_application_button_get_obj, + mp_application_button_get); STATIC mp_obj_t mp_left_button_get() { return mp_obj_new_int(st3m_io_left_button_get()); @@ -133,10 +146,9 @@ STATIC mp_obj_t mp_right_button_get() { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_right_button_get_obj, mp_right_button_get); - - STATIC mp_obj_t mp_version(void) { - mp_obj_t str = mp_obj_new_str(flow3r_bsp_hw_name, strlen(flow3r_bsp_hw_name)); + mp_obj_t str = + mp_obj_new_str(flow3r_bsp_hw_name, strlen(flow3r_bsp_hw_name)); return str; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_version_obj, mp_version); @@ -181,13 +193,15 @@ STATIC mp_obj_t mp_display_pipe_full(void) { } return mp_const_false; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_display_pipe_full_obj, mp_display_pipe_full); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_display_pipe_full_obj, + mp_display_pipe_full); STATIC mp_obj_t mp_display_pipe_flush(void) { st3m_gfx_flush(); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_display_pipe_flush_obj, mp_display_pipe_flush); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_display_pipe_flush_obj, + mp_display_pipe_flush); STATIC mp_obj_t mp_scope_draw(mp_obj_t ctx_in) { // TODO(q3k): check in_ctx? Or just drop from API? @@ -203,7 +217,7 @@ STATIC mp_obj_t mp_i2c_scan(void) { flow3r_bsp_i2c_scan_result_t scan; flow3r_bsp_i2c_scan(&scan); - mp_obj_t res = mp_obj_new_list(0, NULL); + mp_obj_t res = mp_obj_new_list(0, NULL); for (int i = 0; i < 127; i++) { size_t ix = i / 32; size_t offs = i % 32; @@ -250,51 +264,69 @@ STATIC mp_obj_t mp_usb_console_active(void) { } return mp_obj_new_bool(value); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_usb_console_active_obj, mp_usb_console_active); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_usb_console_active_obj, + mp_usb_console_active); STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hardware) }, - - { MP_ROM_QSTR(MP_QSTR_captouch_calibration_active), MP_ROM_PTR(&mp_captouch_calibration_active_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_captouch), MP_ROM_PTR(&mp_get_captouch_obj) }, - { MP_ROM_QSTR(MP_QSTR_captouch_get_petal_pad_raw), MP_ROM_PTR(&mp_captouch_get_petal_pad_raw_obj) }, - { MP_ROM_QSTR(MP_QSTR_captouch_get_petal_pad), MP_ROM_PTR(&mp_captouch_get_petal_pad_obj) }, - { MP_ROM_QSTR(MP_QSTR_captouch_get_petal_rad), MP_ROM_PTR(&mp_captouch_get_petal_rad_obj) }, - { MP_ROM_QSTR(MP_QSTR_captouch_get_petal_phi), MP_ROM_PTR(&mp_captouch_get_petal_phi_obj) }, - { MP_ROM_QSTR(MP_QSTR_captouch_set_petal_pad_threshold), MP_ROM_PTR(&mp_captouch_set_petal_pad_threshold_obj) }, - { MP_ROM_QSTR(MP_QSTR_captouch_autocalib), MP_ROM_PTR(&mp_captouch_autocalib_obj) }, - { MP_ROM_QSTR(MP_QSTR_captouch_set_calibration_afe_target), MP_ROM_PTR(&mp_captouch_set_calibration_afe_target_obj) }, - - { MP_ROM_QSTR(MP_QSTR_menu_button_get), MP_ROM_PTR(&mp_menu_button_get_obj) }, - { MP_ROM_QSTR(MP_QSTR_application_button_get), MP_ROM_PTR(&mp_application_button_get_obj) }, - { MP_ROM_QSTR(MP_QSTR_left_button_get), MP_ROM_PTR(&mp_left_button_get_obj) }, - { MP_ROM_QSTR(MP_QSTR_right_button_get), MP_ROM_PTR(&mp_right_button_get_obj) }, - { MP_ROM_QSTR(MP_QSTR_menu_button_set_left), MP_ROM_PTR(&mp_menu_button_set_left_obj) }, - { MP_ROM_QSTR(MP_QSTR_menu_button_get_left), MP_ROM_PTR(&mp_menu_button_get_left_obj) }, - - { MP_ROM_QSTR(MP_QSTR_display_update), MP_ROM_PTR(&mp_display_update_obj) }, - { MP_ROM_QSTR(MP_QSTR_freertos_sleep), MP_ROM_PTR(&mp_freertos_sleep_obj) }, - { MP_ROM_QSTR(MP_QSTR_display_pipe_full), MP_ROM_PTR(&mp_display_pipe_full_obj) }, - { MP_ROM_QSTR(MP_QSTR_display_pipe_flush), MP_ROM_PTR(&mp_display_pipe_flush_obj) }, - { MP_ROM_QSTR(MP_QSTR_display_set_backlight), MP_ROM_PTR(&mp_display_set_backlight_obj) }, - { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_version_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_ctx), MP_ROM_PTR(&mp_get_ctx_obj) }, - { MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&mp_usb_connected_obj) }, - { MP_ROM_QSTR(MP_QSTR_usb_console_active), MP_ROM_PTR(&mp_usb_console_active_obj) }, - { MP_ROM_QSTR(MP_QSTR_i2c_scan), MP_ROM_PTR(&mp_i2c_scan_obj) }, - - { MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_LEFT), MP_ROM_INT(st3m_tripos_left) }, - { MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_RIGHT), MP_ROM_INT(st3m_tripos_right) }, - { MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_DOWN), MP_ROM_INT(st3m_tripos_mid) }, - { MP_ROM_QSTR(MP_QSTR_BUTTON_NOT_PRESSED), MP_ROM_INT(st3m_tripos_none) }, - - { MP_ROM_QSTR(MP_QSTR_scope_draw), MP_ROM_PTR(&mp_scope_draw_obj) }, + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hardware)}, + + {MP_ROM_QSTR(MP_QSTR_captouch_calibration_active), + MP_ROM_PTR(&mp_captouch_calibration_active_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_captouch), MP_ROM_PTR(&mp_get_captouch_obj)}, + {MP_ROM_QSTR(MP_QSTR_captouch_get_petal_pad_raw), + MP_ROM_PTR(&mp_captouch_get_petal_pad_raw_obj)}, + {MP_ROM_QSTR(MP_QSTR_captouch_get_petal_pad), + MP_ROM_PTR(&mp_captouch_get_petal_pad_obj)}, + {MP_ROM_QSTR(MP_QSTR_captouch_get_petal_rad), + MP_ROM_PTR(&mp_captouch_get_petal_rad_obj)}, + {MP_ROM_QSTR(MP_QSTR_captouch_get_petal_phi), + MP_ROM_PTR(&mp_captouch_get_petal_phi_obj)}, + {MP_ROM_QSTR(MP_QSTR_captouch_set_petal_pad_threshold), + MP_ROM_PTR(&mp_captouch_set_petal_pad_threshold_obj)}, + {MP_ROM_QSTR(MP_QSTR_captouch_autocalib), + MP_ROM_PTR(&mp_captouch_autocalib_obj)}, + {MP_ROM_QSTR(MP_QSTR_captouch_set_calibration_afe_target), + MP_ROM_PTR(&mp_captouch_set_calibration_afe_target_obj)}, + + {MP_ROM_QSTR(MP_QSTR_menu_button_get), MP_ROM_PTR(&mp_menu_button_get_obj)}, + {MP_ROM_QSTR(MP_QSTR_application_button_get), + MP_ROM_PTR(&mp_application_button_get_obj)}, + {MP_ROM_QSTR(MP_QSTR_left_button_get), MP_ROM_PTR(&mp_left_button_get_obj)}, + {MP_ROM_QSTR(MP_QSTR_right_button_get), + MP_ROM_PTR(&mp_right_button_get_obj)}, + {MP_ROM_QSTR(MP_QSTR_menu_button_set_left), + MP_ROM_PTR(&mp_menu_button_set_left_obj)}, + {MP_ROM_QSTR(MP_QSTR_menu_button_get_left), + MP_ROM_PTR(&mp_menu_button_get_left_obj)}, + + {MP_ROM_QSTR(MP_QSTR_display_update), MP_ROM_PTR(&mp_display_update_obj)}, + {MP_ROM_QSTR(MP_QSTR_freertos_sleep), MP_ROM_PTR(&mp_freertos_sleep_obj)}, + {MP_ROM_QSTR(MP_QSTR_display_pipe_full), + MP_ROM_PTR(&mp_display_pipe_full_obj)}, + {MP_ROM_QSTR(MP_QSTR_display_pipe_flush), + MP_ROM_PTR(&mp_display_pipe_flush_obj)}, + {MP_ROM_QSTR(MP_QSTR_display_set_backlight), + MP_ROM_PTR(&mp_display_set_backlight_obj)}, + {MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_version_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_ctx), MP_ROM_PTR(&mp_get_ctx_obj)}, + {MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&mp_usb_connected_obj)}, + {MP_ROM_QSTR(MP_QSTR_usb_console_active), + MP_ROM_PTR(&mp_usb_console_active_obj)}, + {MP_ROM_QSTR(MP_QSTR_i2c_scan), MP_ROM_PTR(&mp_i2c_scan_obj)}, + + {MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_LEFT), MP_ROM_INT(st3m_tripos_left)}, + {MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_RIGHT), MP_ROM_INT(st3m_tripos_right)}, + {MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_DOWN), MP_ROM_INT(st3m_tripos_mid)}, + {MP_ROM_QSTR(MP_QSTR_BUTTON_NOT_PRESSED), MP_ROM_INT(st3m_tripos_none)}, + + {MP_ROM_QSTR(MP_QSTR_scope_draw), MP_ROM_PTR(&mp_scope_draw_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(mp_module_hardware_globals, mp_module_hardware_globals_table); +STATIC MP_DEFINE_CONST_DICT(mp_module_hardware_globals, + mp_module_hardware_globals_table); const mp_obj_module_t mp_module_hardware = { - .base = { &mp_type_module }, + .base = {&mp_type_module}, .globals = (mp_obj_dict_t *)&mp_module_hardware_globals, }; diff --git a/usermodule/mp_kernel.c b/usermodule/mp_kernel.c index 9160ee1281..85bc54f3a0 100644 --- a/usermodule/mp_kernel.c +++ b/usermodule/mp_kernel.c @@ -4,10 +4,10 @@ #include <stdio.h> #include <string.h> -#include "py/runtime.h" #include "py/obj.h" +#include "py/runtime.h" -#if ( configUSE_TRACE_FACILITY != 1 ) +#if (configUSE_TRACE_FACILITY != 1) #error config_USE_TRACE_FACILITY must be set #endif @@ -38,36 +38,39 @@ typedef struct _task_obj_t { const mp_obj_type_t task_type; -STATIC void task_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void task_print(const mp_print_t *print, mp_obj_t self_in, + mp_print_kind_t kind) { (void)kind; task_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_print_str(print, "Task(name="); mp_print_str(print, self->name); mp_print_str(print, ",state="); switch (self->state) { - case eRunning: - mp_print_str(print, "RUNNING"); - break; - case eReady: - mp_print_str(print, "READY"); - break; - case eBlocked: - mp_print_str(print, "BLOCKED"); - break; - case eSuspended: - mp_print_str(print, "SUSPENDED"); - break; - case eDeleted: - mp_print_str(print, "DELETED"); - break; - case eInvalid: - mp_print_str(print, "INVALID"); - break; - default: - mp_print_str(print, "???"); - break; + case eRunning: + mp_print_str(print, "RUNNING"); + break; + case eReady: + mp_print_str(print, "READY"); + break; + case eBlocked: + mp_print_str(print, "BLOCKED"); + break; + case eSuspended: + mp_print_str(print, "SUSPENDED"); + break; + case eDeleted: + mp_print_str(print, "DELETED"); + break; + case eInvalid: + mp_print_str(print, "INVALID"); + break; + default: + mp_print_str(print, "???"); + break; } - mp_printf(print, ",number=%d,stack_left=%d,run_time=%d,core_affinity=%d)", self->number, self->stack_left, self->run_time, self->core_affinity); + mp_printf(print, ",number=%d,stack_left=%d,run_time=%d,core_affinity=%d)", + self->number, self->stack_left, self->run_time, + self->core_affinity); } STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { @@ -76,22 +79,29 @@ STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { return; } switch (attr) { - case MP_QSTR_name: dest[0] = mp_obj_new_str(self->name, strlen(self->name)); break; - case MP_QSTR_state: dest[0] = MP_OBJ_NEW_SMALL_INT(self->state); break; - case MP_QSTR_number: dest[0] = mp_obj_new_int_from_uint(self->number); break; - case MP_QSTR_stack_left: dest[0] = mp_obj_new_int_from_uint(self->stack_left); break; - case MP_QSTR_run_time: dest[0] = mp_obj_new_int_from_uint(self->run_time); break; - case MP_QSTR_core_affinity: dest[0] = mp_obj_new_int_from_uint(self->core_affinity); break; + case MP_QSTR_name: + dest[0] = mp_obj_new_str(self->name, strlen(self->name)); + break; + case MP_QSTR_state: + dest[0] = MP_OBJ_NEW_SMALL_INT(self->state); + break; + case MP_QSTR_number: + dest[0] = mp_obj_new_int_from_uint(self->number); + break; + case MP_QSTR_stack_left: + dest[0] = mp_obj_new_int_from_uint(self->stack_left); + break; + case MP_QSTR_run_time: + dest[0] = mp_obj_new_int_from_uint(self->run_time); + break; + case MP_QSTR_core_affinity: + dest[0] = mp_obj_new_int_from_uint(self->core_affinity); + break; } } -MP_DEFINE_CONST_OBJ_TYPE( - task_type, - MP_QSTR_task, - MP_TYPE_FLAG_NONE, - print, task_print, - attr, task_attr -); +MP_DEFINE_CONST_OBJ_TYPE(task_type, MP_QSTR_task, MP_TYPE_FLAG_NONE, print, + task_print, attr, task_attr); /// snapshot of the FreeRTOS scheduler state. Will not update dynamically, /// instead needs to be re-created by calling scheduler_snapsot() again. @@ -109,30 +119,33 @@ typedef struct _scheduler_snapshot_obj_t { const mp_obj_type_t scheduler_snapshot_type; -STATIC void scheduler_snapshot_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void scheduler_snapshot_print(const mp_print_t *print, mp_obj_t self_in, + mp_print_kind_t kind) { (void)kind; scheduler_snapshot_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "SchedulerSnapshot(tasks=[...], total_runtime=%d)", self->total_runtime); + mp_printf(print, "SchedulerSnapshot(tasks=[...], total_runtime=%d)", + self->total_runtime); } -STATIC void scheduler_snapshot_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { +STATIC void scheduler_snapshot_attr(mp_obj_t self_in, qstr attr, + mp_obj_t *dest) { scheduler_snapshot_obj_t *self = MP_OBJ_TO_PTR(self_in); if (dest[0] != MP_OBJ_NULL) { return; } switch (attr) { - case MP_QSTR_total_runtime: dest[0] = mp_obj_new_int_from_uint(self->total_runtime); break; - case MP_QSTR_tasks: dest[0] = self->tasks; break; + case MP_QSTR_total_runtime: + dest[0] = mp_obj_new_int_from_uint(self->total_runtime); + break; + case MP_QSTR_tasks: + dest[0] = self->tasks; + break; } } -MP_DEFINE_CONST_OBJ_TYPE( - scheduler_snapshot_type, - MP_QSTR_scheduler_snapshot, - MP_TYPE_FLAG_NONE, - print, scheduler_snapshot_print, - attr, scheduler_snapshot_attr -); +MP_DEFINE_CONST_OBJ_TYPE(scheduler_snapshot_type, MP_QSTR_scheduler_snapshot, + MP_TYPE_FLAG_NONE, print, scheduler_snapshot_print, + attr, scheduler_snapshot_attr); STATIC mp_obj_t mp_scheduler_snapshot(void) { mp_obj_t tasks_out = mp_obj_new_list(0, NULL); @@ -144,23 +157,24 @@ STATIC mp_obj_t mp_scheduler_snapshot(void) { return mp_const_none; } uint32_t total_runtime; - UBaseType_t ntasks_returned = uxTaskGetSystemState(tasks, ntasks, &total_runtime); + UBaseType_t ntasks_returned = + uxTaskGetSystemState(tasks, ntasks, &total_runtime); for (UBaseType_t i = 0; i < ntasks_returned; i++) { task_obj_t *task = m_new_obj(task_obj_t); task->base.type = &task_type; - strncpy(task->name, tasks[i].pcTaskName, configMAX_TASK_NAME_LEN-1); + strncpy(task->name, tasks[i].pcTaskName, configMAX_TASK_NAME_LEN - 1); task->number = tasks[i].xTaskNumber; task->stack_left = tasks[i].usStackHighWaterMark; task->run_time = tasks[i].ulRunTimeCounter; task->state = tasks[i].eCurrentState; task->core_affinity = 0b11; switch (tasks[i].xCoreID) { - case 0: - task->core_affinity = 1; - break; - case 1: - task->core_affinity = 2; - break; + case 0: + task->core_affinity = 1; + break; + case 1: + task->core_affinity = 2; + break; } mp_obj_list_append(tasks_out, MP_OBJ_FROM_PTR(task)); } @@ -172,22 +186,27 @@ STATIC mp_obj_t mp_scheduler_snapshot(void) { return snap; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_scheduler_snapshot_obj, mp_scheduler_snapshot); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_scheduler_snapshot_obj, + mp_scheduler_snapshot); typedef struct _heap_kind_stats_obj_t { mp_obj_base_t base; - qstr kind; + qstr kind; multi_heap_info_t info; } heap_kind_stats_obj_t; const mp_obj_type_t heap_kind_stats_type; -STATIC void heap_kind_stats_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void heap_kind_stats_print(const mp_print_t *print, mp_obj_t self_in, + mp_print_kind_t kind) { (void)kind; heap_kind_stats_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "HeapKindStats(kind=%s,total_free_bytes=%d,total_allocated_bytes=%d,largest_free_block=%d)", - qstr_str(self->kind), self->info.total_free_bytes, self->info.total_allocated_bytes, self->info.largest_free_block); + mp_printf(print, + "HeapKindStats(kind=%s,total_free_bytes=%d,total_allocated_bytes=" + "%d,largest_free_block=%d)", + qstr_str(self->kind), self->info.total_free_bytes, + self->info.total_allocated_bytes, self->info.largest_free_block); } STATIC void heap_kind_stats_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { @@ -196,19 +215,22 @@ STATIC void heap_kind_stats_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { return; } switch (attr) { - case MP_QSTR_total_free_bytes: dest[0] = mp_obj_new_int_from_uint(self->info.total_free_bytes); break; - case MP_QSTR_total_allocated_bytes: dest[0] = mp_obj_new_int_from_uint(self->info.total_allocated_bytes); break; - case MP_QSTR_largest_free_block: dest[0] = mp_obj_new_int_from_uint(self->info.largest_free_block); break; + case MP_QSTR_total_free_bytes: + dest[0] = mp_obj_new_int_from_uint(self->info.total_free_bytes); + break; + case MP_QSTR_total_allocated_bytes: + dest[0] = + mp_obj_new_int_from_uint(self->info.total_allocated_bytes); + break; + case MP_QSTR_largest_free_block: + dest[0] = mp_obj_new_int_from_uint(self->info.largest_free_block); + break; } } -MP_DEFINE_CONST_OBJ_TYPE( - heap_kind_stats_type, - MP_QSTR_heap_kind_stats, - MP_TYPE_FLAG_NONE, - print, heap_kind_stats_print, - attr, heap_kind_stats_attr -); +MP_DEFINE_CONST_OBJ_TYPE(heap_kind_stats_type, MP_QSTR_heap_kind_stats, + MP_TYPE_FLAG_NONE, print, heap_kind_stats_print, attr, + heap_kind_stats_attr); STATIC mp_obj_t heap_kind_stats_from_caps(qstr kind, uint32_t caps) { heap_kind_stats_obj_t *stats = m_new_obj(heap_kind_stats_obj_t); @@ -228,7 +250,8 @@ typedef struct _heap_stats_obj_t { const mp_obj_type_t heap_stats_type; -STATIC void heap_stats_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void heap_stats_print(const mp_print_t *print, mp_obj_t self_in, + mp_print_kind_t kind) { (void)kind; mp_printf(print, "HeapStats(general=[...],dma=[...])"); } @@ -239,21 +262,21 @@ STATIC void heap_stats_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { return; } switch (attr) { - case MP_QSTR_general: dest[0] = self->general; break; - case MP_QSTR_dma: dest[0] = self->dma; break; + case MP_QSTR_general: + dest[0] = self->general; + break; + case MP_QSTR_dma: + dest[0] = self->dma; + break; } } -MP_DEFINE_CONST_OBJ_TYPE( - heap_stats_type, - MP_QSTR_heap_stats, - MP_TYPE_FLAG_NONE, - print, heap_stats_print, - attr, heap_stats_attr -); +MP_DEFINE_CONST_OBJ_TYPE(heap_stats_type, MP_QSTR_heap_stats, MP_TYPE_FLAG_NONE, + print, heap_stats_print, attr, heap_stats_attr); STATIC mp_obj_t mp_heap_stats(void) { - mp_obj_t general = heap_kind_stats_from_caps(MP_QSTR_general, MALLOC_CAP_DEFAULT); + mp_obj_t general = + heap_kind_stats_from_caps(MP_QSTR_general, MALLOC_CAP_DEFAULT); mp_obj_t dma = heap_kind_stats_from_caps(MP_QSTR_general, MALLOC_CAP_DMA); heap_stats_obj_t *stats = m_new_obj(heap_stats_obj_t); @@ -267,22 +290,23 @@ STATIC mp_obj_t mp_heap_stats(void) { STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_heap_stats_obj, mp_heap_stats); STATIC const mp_rom_map_elem_t globals_table[] = { - { MP_ROM_QSTR(MP_QSTR_scheduler_snapshot), MP_ROM_PTR(&mp_scheduler_snapshot_obj) }, - { MP_ROM_QSTR(MP_QSTR_heap_stats), MP_ROM_PTR(&mp_heap_stats_obj)}, - - { MP_ROM_QSTR(MP_QSTR_RUNNING), MP_ROM_INT(eRunning) }, - { MP_ROM_QSTR(MP_QSTR_READY), MP_ROM_INT(eReady) }, - { MP_ROM_QSTR(MP_QSTR_BLOCKED), MP_ROM_INT(eBlocked) }, - { MP_ROM_QSTR(MP_QSTR_SUSPENDED), MP_ROM_INT(eSuspended) }, - { MP_ROM_QSTR(MP_QSTR_DELETED), MP_ROM_INT(eDeleted) }, - { MP_ROM_QSTR(MP_QSTR_INVALID), MP_ROM_INT(eInvalid) }, + {MP_ROM_QSTR(MP_QSTR_scheduler_snapshot), + MP_ROM_PTR(&mp_scheduler_snapshot_obj)}, + {MP_ROM_QSTR(MP_QSTR_heap_stats), MP_ROM_PTR(&mp_heap_stats_obj)}, + + {MP_ROM_QSTR(MP_QSTR_RUNNING), MP_ROM_INT(eRunning)}, + {MP_ROM_QSTR(MP_QSTR_READY), MP_ROM_INT(eReady)}, + {MP_ROM_QSTR(MP_QSTR_BLOCKED), MP_ROM_INT(eBlocked)}, + {MP_ROM_QSTR(MP_QSTR_SUSPENDED), MP_ROM_INT(eSuspended)}, + {MP_ROM_QSTR(MP_QSTR_DELETED), MP_ROM_INT(eDeleted)}, + {MP_ROM_QSTR(MP_QSTR_INVALID), MP_ROM_INT(eInvalid)}, }; STATIC MP_DEFINE_CONST_DICT(globals, globals_table); const mp_obj_module_t mp_module_kernel_user_cmodule = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&globals, + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t *)&globals, }; MP_REGISTER_MODULE(MP_QSTR_kernel, mp_module_kernel_user_cmodule); \ No newline at end of file diff --git a/usermodule/mp_leds.c b/usermodule/mp_leds.c index ad4e2008b7..127f9cbb7d 100644 --- a/usermodule/mp_leds.c +++ b/usermodule/mp_leds.c @@ -2,13 +2,12 @@ #include <stdio.h> #include <string.h> -#include "py/runtime.h" -#include "py/mphal.h" -#include "mphalport.h" -#include "modmachine.h" #include "extmod/virtpin.h" #include "machine_rtc.h" +#include "modmachine.h" +#include "mphalport.h" #include "py/builtin.h" +#include "py/mphal.h" #include "py/runtime.h" #include "st3m_leds.h" @@ -17,26 +16,31 @@ STATIC mp_obj_t mp_leds_set_brightness(mp_obj_t b) { st3m_leds_set_brightness(mp_obj_get_int(b)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_leds_set_brightness_obj, mp_leds_set_brightness); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_leds_set_brightness_obj, + mp_leds_set_brightness); STATIC mp_obj_t mp_leds_get_brightness() { return mp_obj_new_int(st3m_leds_get_brightness()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_get_brightness_obj, mp_leds_get_brightness); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_get_brightness_obj, + mp_leds_get_brightness); STATIC mp_obj_t mp_leds_set_auto_update(mp_obj_t on) { st3m_leds_set_auto_update(mp_obj_get_int(on)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_leds_set_auto_update_obj, mp_leds_set_auto_update); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_leds_set_auto_update_obj, + mp_leds_set_auto_update); STATIC mp_obj_t mp_leds_get_auto_update() { return mp_obj_new_int(st3m_leds_get_auto_update()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_get_auto_update_obj, mp_leds_get_auto_update); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_get_auto_update_obj, + mp_leds_get_auto_update); STATIC mp_obj_t mp_leds_set_gamma(mp_obj_t r, mp_obj_t g, mp_obj_t b) { - st3m_leds_set_gamma(mp_obj_get_float(r), mp_obj_get_float(g), mp_obj_get_float(b)); + st3m_leds_set_gamma(mp_obj_get_float(r), mp_obj_get_float(g), + mp_obj_get_float(b)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mp_leds_set_gamma_obj, mp_leds_set_gamma); @@ -45,46 +49,50 @@ STATIC mp_obj_t mp_leds_set_slew_rate(mp_obj_t b) { st3m_leds_set_slew_rate(mp_obj_get_int(b)); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_leds_set_slew_rate_obj, mp_leds_set_slew_rate); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_leds_set_slew_rate_obj, + mp_leds_set_slew_rate); STATIC mp_obj_t mp_leds_get_slew_rate() { return mp_obj_new_int(st3m_leds_get_slew_rate()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_get_slew_rate_obj, mp_leds_get_slew_rate); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_get_slew_rate_obj, + mp_leds_get_slew_rate); STATIC mp_obj_t mp_led_set_rgb(size_t n_args, const mp_obj_t *args) { - uint8_t index = mp_obj_get_int(args[0]); - uint8_t red = mp_obj_get_int(args[1]); - uint8_t green = mp_obj_get_int(args[2]); - uint8_t blue = mp_obj_get_int(args[3]); + uint8_t index = mp_obj_get_int(args[0]); + uint8_t red = mp_obj_get_int(args[1]); + uint8_t green = mp_obj_get_int(args[2]); + uint8_t blue = mp_obj_get_int(args[3]); st3m_leds_set_single_rgb(index, red, green, blue); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_led_set_rgb_obj, 4, 4, mp_led_set_rgb); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_led_set_rgb_obj, 4, 4, + mp_led_set_rgb); STATIC mp_obj_t mp_led_set_hsv(size_t n_args, const mp_obj_t *args) { - uint8_t index = mp_obj_get_int(args[0]); - float hue = mp_obj_get_float(args[1]); - float sat = mp_obj_get_float(args[2]); - float val = mp_obj_get_float(args[3]); + uint8_t index = mp_obj_get_int(args[0]); + float hue = mp_obj_get_float(args[1]); + float sat = mp_obj_get_float(args[2]); + float val = mp_obj_get_float(args[3]); st3m_leds_set_single_hsv(index, hue, sat, val); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_led_set_hsv_obj,4,4, mp_led_set_hsv); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_led_set_hsv_obj, 4, 4, + mp_led_set_hsv); STATIC mp_obj_t mp_led_set_all_rgb(mp_obj_t r, mp_obj_t g, mp_obj_t b) { - uint8_t red = mp_obj_get_int(r); - uint8_t green = mp_obj_get_int(g); - uint8_t blue = mp_obj_get_int(b); + uint8_t red = mp_obj_get_int(r); + uint8_t green = mp_obj_get_int(g); + uint8_t blue = mp_obj_get_int(b); st3m_leds_set_all_rgb(red, green, blue); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mp_led_set_all_rgb_obj, mp_led_set_all_rgb); STATIC mp_obj_t mp_led_set_all_hsv(mp_obj_t h, mp_obj_t s, mp_obj_t v) { - float hue = mp_obj_get_float(h); - float sat = mp_obj_get_float(s); - float val = mp_obj_get_float(v); + float hue = mp_obj_get_float(h); + float sat = mp_obj_get_float(s); + float val = mp_obj_get_float(v); st3m_leds_set_all_hsv(hue, sat, val); return mp_const_none; } @@ -97,27 +105,33 @@ STATIC mp_obj_t mp_leds_update() { STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_update_obj, mp_leds_update); STATIC const mp_rom_map_elem_t mp_module_leds_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_audio) }, - { MP_ROM_QSTR(MP_QSTR_set_rgb), MP_ROM_PTR(&mp_led_set_rgb_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_hsv), MP_ROM_PTR(&mp_led_set_hsv_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_all_rgb), MP_ROM_PTR(&mp_led_set_all_rgb_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_all_hsv), MP_ROM_PTR(&mp_led_set_all_hsv_obj) }, - { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mp_leds_update_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_brightness), MP_ROM_PTR(&mp_leds_get_brightness_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_brightness), MP_ROM_PTR(&mp_leds_set_brightness_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_auto_update), MP_ROM_PTR(&mp_leds_get_auto_update_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_auto_update), MP_ROM_PTR(&mp_leds_set_auto_update_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_gamma), MP_ROM_PTR(&mp_leds_set_gamma_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_slew_rate), MP_ROM_PTR(&mp_leds_get_slew_rate_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_slew_rate), MP_ROM_PTR(&mp_leds_set_slew_rate_obj) }, + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_audio)}, + {MP_ROM_QSTR(MP_QSTR_set_rgb), MP_ROM_PTR(&mp_led_set_rgb_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_hsv), MP_ROM_PTR(&mp_led_set_hsv_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_all_rgb), MP_ROM_PTR(&mp_led_set_all_rgb_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_all_hsv), MP_ROM_PTR(&mp_led_set_all_hsv_obj)}, + {MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&mp_leds_update_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_brightness), + MP_ROM_PTR(&mp_leds_get_brightness_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_brightness), + MP_ROM_PTR(&mp_leds_set_brightness_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_auto_update), + MP_ROM_PTR(&mp_leds_get_auto_update_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_auto_update), + MP_ROM_PTR(&mp_leds_set_auto_update_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_gamma), MP_ROM_PTR(&mp_leds_set_gamma_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_slew_rate), + MP_ROM_PTR(&mp_leds_get_slew_rate_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_slew_rate), + MP_ROM_PTR(&mp_leds_set_slew_rate_obj)}, }; -STATIC MP_DEFINE_CONST_DICT(mp_module_leds_globals, mp_module_leds_globals_table); +STATIC MP_DEFINE_CONST_DICT(mp_module_leds_globals, + mp_module_leds_globals_table); const mp_obj_module_t mp_module_leds = { - .base = { &mp_type_module }, + .base = {&mp_type_module}, .globals = (mp_obj_dict_t *)&mp_module_leds_globals, }; MP_REGISTER_MODULE(MP_QSTR_leds, mp_module_leds); - diff --git a/usermodule/mp_uctx.c b/usermodule/mp_uctx.c index 2b9d1bed27..0b3e318991 100644 --- a/usermodule/mp_uctx.c +++ b/usermodule/mp_uctx.c @@ -1,5 +1,5 @@ #ifndef __clang__ -#pragma GCC optimize ("O3") +#pragma GCC optimize("O3") #endif #include <stdlib.h> @@ -8,23 +8,22 @@ #include "py/objarray.h" #include "py/runtime.h" -#include "ctx_config.h" #include "ctx.h" +#include "ctx_config.h" typedef struct _mp_ctx_obj_t { - mp_obj_base_t base; - Ctx *ctx; - mp_obj_t user_data; + mp_obj_base_t base; + Ctx *ctx; + mp_obj_t user_data; } mp_ctx_obj_t; void gc_collect(void); #ifdef EMSCRIPTEN extern int _mp_quit; -void mp_idle (int ms); +void mp_idle(int ms); #else -void mp_idle (int ms) -{ - if (ms == 0) gc_collect(); +void mp_idle(int ms) { + if (ms == 0) gc_collect(); } #endif @@ -32,196 +31,158 @@ void gc_collect(void); /* since a lot of the ctx API has similar function signatures, we use macros to * avoid repeating the marshalling of arguments */ -#define MP_CTX_COMMON_FUN_0(name) \ - static mp_obj_t mp_ctx_##name(mp_obj_t self_in) \ - { \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); \ - ctx_##name(self->ctx); \ - return self_in; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_1(mp_ctx_##name##_obj, mp_ctx_##name); - -#define MP_CTX_COMMON_FUN_1F(name) \ - static mp_obj_t mp_ctx_##name(mp_obj_t self_in, mp_obj_t arg1) \ - { \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); \ - ctx_##name(self->ctx, (float)mp_obj_get_float(arg1)); \ - return self_in; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_##name##_obj, mp_ctx_##name); - -#define MP_CTX_COMMON_FUN_1I(name) \ - static mp_obj_t mp_ctx_##name(mp_obj_t self_in, mp_obj_t arg1) \ - { \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); \ - ctx_##name(self->ctx, mp_obj_get_int(arg1)); \ - return self_in; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_##name##_obj, mp_ctx_##name); - -#define MP_CTX_COMMON_FUN_2F(name) \ - static mp_obj_t mp_ctx_##name( \ - mp_obj_t self_in, mp_obj_t arg1, mp_obj_t arg2) \ - { \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); \ - ctx_##name( \ - self->ctx, \ - (float)mp_obj_get_float(arg1), \ - (float)mp_obj_get_float(arg2)); \ - return self_in; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_3(mp_ctx_##name##_obj, mp_ctx_##name); - -#define MP_CTX_COMMON_FUN_3F(name) \ - static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) \ - { \ - assert(n_args == 4); \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ - ctx_##name( \ - self->ctx, \ - (float)mp_obj_get_float(args[1]), \ - (float)mp_obj_get_float(args[2]), \ - (float)mp_obj_get_float(args[3])); \ - return args[0]; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( \ - mp_ctx_##name##_obj, 4, 4, mp_ctx_##name); - -#define MP_CTX_COMMON_FUN_4F(name) \ - static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) \ - { \ - assert(n_args == 5); \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ - ctx_##name( \ - self->ctx, \ - (float)mp_obj_get_float(args[1]), \ - (float)mp_obj_get_float(args[2]), \ - (float)mp_obj_get_float(args[3]), \ - (float)mp_obj_get_float(args[4])); \ - return args[0]; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( \ - mp_ctx_##name##_obj, 5, 5, mp_ctx_##name); - -#define MP_CTX_COMMON_FUN_5F(name) \ - static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) \ - { \ - assert(n_args == 6); \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ - ctx_##name( \ - self->ctx, \ - (float)mp_obj_get_float(args[1]), \ - (float)mp_obj_get_float(args[2]), \ - (float)mp_obj_get_float(args[3]), \ - (float)mp_obj_get_float(args[4]), \ - (float)mp_obj_get_float(args[5])); \ - return args[0]; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( \ - mp_ctx_##name##_obj, 6, 6, mp_ctx_##name); - -#define MP_CTX_COMMON_FUN_6F(name) \ - static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) \ - { \ - assert(n_args == 7); \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ - ctx_##name( \ - self->ctx, \ - (float)mp_obj_get_float(args[1]), \ - (float)mp_obj_get_float(args[2]), \ - (float)mp_obj_get_float(args[3]), \ - (float)mp_obj_get_float(args[4]), \ - (float)mp_obj_get_float(args[5]), \ - (int)mp_obj_get_float(args[6])); \ - return self; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( \ - mp_ctx_##name##_obj, 7, 7, mp_ctx_##name); - - -#define MP_CTX_COMMON_FUN_7F(name) \ - static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) \ - { \ - assert(n_args == 8); \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ - ctx_##name( \ - self->ctx, \ - (float)mp_obj_get_float(args[1]), \ - (float)mp_obj_get_float(args[2]), \ - (float)mp_obj_get_float(args[3]), \ - (float)mp_obj_get_float(args[4]), \ - (float)mp_obj_get_float(args[5]), \ - (float)mp_obj_get_float(args[6]), \ - (float)mp_obj_get_float(args[7])); \ - return self; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( \ - mp_ctx_##name##_obj, 8, 8, mp_ctx_##name); - - -#define MP_CTX_COMMON_FUN_9F(name) \ - static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) \ - { \ - assert(n_args == 10); \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ - ctx_##name( \ - self->ctx, \ - (float)mp_obj_get_float(args[1]), \ - (float)mp_obj_get_float(args[2]), \ - (float)mp_obj_get_float(args[3]), \ - (float)mp_obj_get_float(args[4]), \ - (float)mp_obj_get_float(args[5]), \ - (float)mp_obj_get_float(args[6]), \ - (float)mp_obj_get_float(args[7]), \ - (float)mp_obj_get_float(args[8]), \ - (float)mp_obj_get_float(args[9])); \ - return self; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( \ - mp_ctx_##name##_obj, 10, 10, mp_ctx_##name); - -#define MP_CTX_TEXT_FUNB(name) \ - static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) \ - { \ - assert(n_args == 4); \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ - ctx_##name( \ - self->ctx, \ - mp_obj_str_get_str(args[1]), \ - (float)mp_obj_get_float(args[2]), \ - (float)mp_obj_get_float(args[3])); \ - return args[0]; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( \ - mp_ctx_##name##_obj, 4, 4, mp_ctx_##name); - -#define MP_CTX_TEXT_FUN(name) \ - static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) \ - { \ - assert(n_args == 2); \ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ - ctx_##name( \ - self->ctx, \ - mp_obj_str_get_str(args[1])); \ - return args[0]; \ - } \ - MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( \ - mp_ctx_##name##_obj, 2, 2, mp_ctx_##name); +#define MP_CTX_COMMON_FUN_0(name) \ + static mp_obj_t mp_ctx_##name(mp_obj_t self_in) { \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); \ + ctx_##name(self->ctx); \ + return self_in; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_1(mp_ctx_##name##_obj, mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_1F(name) \ + static mp_obj_t mp_ctx_##name(mp_obj_t self_in, mp_obj_t arg1) { \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); \ + ctx_##name(self->ctx, (float)mp_obj_get_float(arg1)); \ + return self_in; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_##name##_obj, mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_1I(name) \ + static mp_obj_t mp_ctx_##name(mp_obj_t self_in, mp_obj_t arg1) { \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); \ + ctx_##name(self->ctx, mp_obj_get_int(arg1)); \ + return self_in; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_##name##_obj, mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_2F(name) \ + static mp_obj_t mp_ctx_##name(mp_obj_t self_in, mp_obj_t arg1, \ + mp_obj_t arg2) { \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); \ + ctx_##name(self->ctx, (float)mp_obj_get_float(arg1), \ + (float)mp_obj_get_float(arg2)); \ + return self_in; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_3(mp_ctx_##name##_obj, mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_3F(name) \ + static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) { \ + assert(n_args == 4); \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ + ctx_##name(self->ctx, (float)mp_obj_get_float(args[1]), \ + (float)mp_obj_get_float(args[2]), \ + (float)mp_obj_get_float(args[3])); \ + return args[0]; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_##name##_obj, 4, 4, \ + mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_4F(name) \ + static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) { \ + assert(n_args == 5); \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ + ctx_##name(self->ctx, (float)mp_obj_get_float(args[1]), \ + (float)mp_obj_get_float(args[2]), \ + (float)mp_obj_get_float(args[3]), \ + (float)mp_obj_get_float(args[4])); \ + return args[0]; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_##name##_obj, 5, 5, \ + mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_5F(name) \ + static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) { \ + assert(n_args == 6); \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ + ctx_##name(self->ctx, (float)mp_obj_get_float(args[1]), \ + (float)mp_obj_get_float(args[2]), \ + (float)mp_obj_get_float(args[3]), \ + (float)mp_obj_get_float(args[4]), \ + (float)mp_obj_get_float(args[5])); \ + return args[0]; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_##name##_obj, 6, 6, \ + mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_6F(name) \ + static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) { \ + assert(n_args == 7); \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ + ctx_##name(self->ctx, (float)mp_obj_get_float(args[1]), \ + (float)mp_obj_get_float(args[2]), \ + (float)mp_obj_get_float(args[3]), \ + (float)mp_obj_get_float(args[4]), \ + (float)mp_obj_get_float(args[5]), \ + (int)mp_obj_get_float(args[6])); \ + return self; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_##name##_obj, 7, 7, \ + mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_7F(name) \ + static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) { \ + assert(n_args == 8); \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ + ctx_##name(self->ctx, (float)mp_obj_get_float(args[1]), \ + (float)mp_obj_get_float(args[2]), \ + (float)mp_obj_get_float(args[3]), \ + (float)mp_obj_get_float(args[4]), \ + (float)mp_obj_get_float(args[5]), \ + (float)mp_obj_get_float(args[6]), \ + (float)mp_obj_get_float(args[7])); \ + return self; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_##name##_obj, 8, 8, \ + mp_ctx_##name); + +#define MP_CTX_COMMON_FUN_9F(name) \ + static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) { \ + assert(n_args == 10); \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ + ctx_##name(self->ctx, (float)mp_obj_get_float(args[1]), \ + (float)mp_obj_get_float(args[2]), \ + (float)mp_obj_get_float(args[3]), \ + (float)mp_obj_get_float(args[4]), \ + (float)mp_obj_get_float(args[5]), \ + (float)mp_obj_get_float(args[6]), \ + (float)mp_obj_get_float(args[7]), \ + (float)mp_obj_get_float(args[8]), \ + (float)mp_obj_get_float(args[9])); \ + return self; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_##name##_obj, 10, 10, \ + mp_ctx_##name); + +#define MP_CTX_TEXT_FUNB(name) \ + static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) { \ + assert(n_args == 4); \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ + ctx_##name(self->ctx, mp_obj_str_get_str(args[1]), \ + (float)mp_obj_get_float(args[2]), \ + (float)mp_obj_get_float(args[3])); \ + return args[0]; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_##name##_obj, 4, 4, \ + mp_ctx_##name); + +#define MP_CTX_TEXT_FUN(name) \ + static mp_obj_t mp_ctx_##name(size_t n_args, const mp_obj_t *args) { \ + assert(n_args == 2); \ + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); \ + ctx_##name(self->ctx, mp_obj_str_get_str(args[1])); \ + return args[0]; \ + } \ + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_##name##_obj, 2, 2, \ + mp_ctx_##name); #define MP_CTX_ATTR(name) \ - { MP_ROM_QSTR(MP_QSTR_##name), MP_ROM_INT(0) } -#define MP_CTX_INT_CONSTANT_UNPREFIXED(ident) \ - { \ - MP_ROM_QSTR(MP_QSTR_##ident), MP_ROM_INT((int)CTX_##ident) \ - } -#define MP_CTX_INT_CONSTANT(prefix, ident) \ - { \ - MP_ROM_QSTR(MP_QSTR_##ident), MP_ROM_INT((int)CTX_##prefix##_##ident)\ - } -#define MP_CTX_METHOD(name) \ - { \ - MP_ROM_QSTR(MP_QSTR_##name), MP_ROM_PTR(&mp_ctx_##name##_obj) \ - } + { MP_ROM_QSTR(MP_QSTR_##name), MP_ROM_INT(0) } +#define MP_CTX_INT_CONSTANT_UNPREFIXED(ident) \ + { MP_ROM_QSTR(MP_QSTR_##ident), MP_ROM_INT((int)CTX_##ident) } +#define MP_CTX_INT_CONSTANT(prefix, ident) \ + { MP_ROM_QSTR(MP_QSTR_##ident), MP_ROM_INT((int)CTX_##prefix##_##ident) } +#define MP_CTX_METHOD(name) \ + { MP_ROM_QSTR(MP_QSTR_##name), MP_ROM_PTR(&mp_ctx_##name##_obj) } /* CTX API functions {{{ */ @@ -272,96 +233,89 @@ MP_CTX_COMMON_FUN_6F(radial_gradient); MP_CTX_COMMON_FUN_3F(logo); -static mp_obj_t mp_ctx_key_down(size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - ctx_key_down (self->ctx, - mp_obj_get_int (args[1]), // keyval - mp_obj_str_get_str(args[2]), // string - mp_obj_get_int(args[3]));// time - return args[0]; +static mp_obj_t mp_ctx_key_down(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + ctx_key_down(self->ctx, + mp_obj_get_int(args[1]), // keyval + mp_obj_str_get_str(args[2]), // string + mp_obj_get_int(args[3])); // time + return args[0]; } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_key_down_obj, 4, 4, mp_ctx_key_down); -static mp_obj_t mp_ctx_incoming_message (size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - ctx_incoming_message (self->ctx, - mp_obj_str_get_str(args[1]), // string - mp_obj_get_int(args[2]));// time - return args[0]; +static mp_obj_t mp_ctx_incoming_message(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + ctx_incoming_message(self->ctx, + mp_obj_str_get_str(args[1]), // string + mp_obj_get_int(args[2])); // time + return args[0]; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_incoming_message_obj, 3, 3, mp_ctx_incoming_message); - -static mp_obj_t mp_ctx_key_up(size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - ctx_key_up (self->ctx, - mp_obj_get_int (args[1]), // keyval - mp_obj_str_get_str(args[2]), // string - mp_obj_get_int(args[3])); // time - return args[0]; +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_incoming_message_obj, 3, 3, + mp_ctx_incoming_message); + +static mp_obj_t mp_ctx_key_up(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + ctx_key_up(self->ctx, + mp_obj_get_int(args[1]), // keyval + mp_obj_str_get_str(args[2]), // string + mp_obj_get_int(args[3])); // time + return args[0]; } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_key_up_obj, 4, 4, mp_ctx_key_up); -static mp_obj_t mp_ctx_key_press(size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - ctx_key_press (self->ctx, - mp_obj_get_int (args[1]), // keyval - mp_obj_str_get_str(args[2]), // string - mp_obj_get_int(args[3])); // time - return args[0]; +static mp_obj_t mp_ctx_key_press(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + ctx_key_press(self->ctx, + mp_obj_get_int(args[1]), // keyval + mp_obj_str_get_str(args[2]), // string + mp_obj_get_int(args[3])); // time + return args[0]; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_key_press_obj, 4, 4, mp_ctx_key_press); - -static mp_obj_t mp_ctx_scrolled(size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - ctx_scrolled (self->ctx, - (float)mp_obj_get_float(args[1]), // x - (float)mp_obj_get_float(args[2]), // y - mp_obj_get_int(args[3]), // scroll-direction - mp_obj_get_int(args[4])); // time - return args[0]; +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_key_press_obj, 4, 4, + mp_ctx_key_press); + +static mp_obj_t mp_ctx_scrolled(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + ctx_scrolled(self->ctx, + (float)mp_obj_get_float(args[1]), // x + (float)mp_obj_get_float(args[2]), // y + mp_obj_get_int(args[3]), // scroll-direction + mp_obj_get_int(args[4])); // time + return args[0]; } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_scrolled_obj, 5, 5, mp_ctx_scrolled); -static mp_obj_t mp_ctx_pointer_press(size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - ctx_pointer_press (self->ctx, - (float)mp_obj_get_float(args[1]), // x - (float)mp_obj_get_float(args[2]), // y - mp_obj_get_int(args[3]), // device-no - mp_obj_get_int(args[4])); // time - return args[0]; +static mp_obj_t mp_ctx_pointer_press(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + ctx_pointer_press(self->ctx, + (float)mp_obj_get_float(args[1]), // x + (float)mp_obj_get_float(args[2]), // y + mp_obj_get_int(args[3]), // device-no + mp_obj_get_int(args[4])); // time + return args[0]; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_pointer_press_obj, 5, 5, mp_ctx_pointer_press); - -static mp_obj_t mp_ctx_pointer_motion(size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - ctx_pointer_motion(self->ctx, - (float)mp_obj_get_float(args[1]), - (float)mp_obj_get_float(args[2]), - mp_obj_get_int(args[3]), - mp_obj_get_int(args[4])); - return args[0]; +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_pointer_press_obj, 5, 5, + mp_ctx_pointer_press); + +static mp_obj_t mp_ctx_pointer_motion(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + ctx_pointer_motion(self->ctx, (float)mp_obj_get_float(args[1]), + (float)mp_obj_get_float(args[2]), + mp_obj_get_int(args[3]), mp_obj_get_int(args[4])); + return args[0]; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_pointer_motion_obj, 5, 5, mp_ctx_pointer_motion); - -static mp_obj_t mp_ctx_pointer_release(size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - ctx_pointer_release(self->ctx, - (float)mp_obj_get_float(args[1]), - (float)mp_obj_get_float(args[2]), - mp_obj_get_int(args[3]), - mp_obj_get_int(args[4])); - return args[0]; +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_pointer_motion_obj, 5, 5, + mp_ctx_pointer_motion); + +static mp_obj_t mp_ctx_pointer_release(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + ctx_pointer_release(self->ctx, (float)mp_obj_get_float(args[1]), + (float)mp_obj_get_float(args[2]), + mp_obj_get_int(args[3]), mp_obj_get_int(args[4])); + return args[0]; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_pointer_release_obj, 5, 5, mp_ctx_pointer_release); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_pointer_release_obj, 5, 5, + mp_ctx_pointer_release); #if 0 static mp_obj_t mp_ctx_pointer_drop(size_t n_args, const mp_obj_t *args) @@ -378,68 +332,59 @@ static mp_obj_t mp_ctx_pointer_drop(size_t n_args, const mp_obj_t *args) MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_pointer_drop_obj, 6, 6, mp_ctx_pointer_drop); #endif -static mp_obj_t mp_ctx_line_dash(mp_obj_t self_in, mp_obj_t dashes_in) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); +static mp_obj_t mp_ctx_line_dash(mp_obj_t self_in, mp_obj_t dashes_in) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); - size_t count = mp_obj_get_int(mp_obj_len(dashes_in)); - float *dashes = m_malloc(sizeof(float) * count); - for (size_t i = 0; i < count; i++) { - dashes[i] = (float)mp_obj_get_float(mp_obj_subscr( - dashes_in, mp_obj_new_int(i), MP_OBJ_SENTINEL) - ); - } + size_t count = mp_obj_get_int(mp_obj_len(dashes_in)); + float *dashes = m_malloc(sizeof(float) * count); + for (size_t i = 0; i < count; i++) { + dashes[i] = (float)mp_obj_get_float( + mp_obj_subscr(dashes_in, mp_obj_new_int(i), MP_OBJ_SENTINEL)); + } - ctx_line_dash(self->ctx, dashes, count); + ctx_line_dash(self->ctx, dashes, count); #if MICROPY_MALLOC_USES_ALLOCATED_SIZE - m_free(dashes, sizeof(float)*count); + m_free(dashes, sizeof(float) * count); #else - m_free(dashes); + m_free(dashes); #endif - return self_in; + return self_in; } MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_line_dash_obj, mp_ctx_line_dash); - -static mp_obj_t mp_ctx_in_fill(mp_obj_t self_in, mp_obj_t arg1, mp_obj_t arg2) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_bool (ctx_in_fill (self->ctx, - (float)mp_obj_get_float(arg1), +static mp_obj_t mp_ctx_in_fill(mp_obj_t self_in, mp_obj_t arg1, mp_obj_t arg2) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(ctx_in_fill(self->ctx, (float)mp_obj_get_float(arg1), (float)mp_obj_get_float(arg2))); } MP_DEFINE_CONST_FUN_OBJ_3(mp_ctx_in_fill_obj, mp_ctx_in_fill); -static mp_obj_t mp_ctx_texture (size_t n_args, const mp_obj_t *args) -{ - mp_buffer_info_t buffer_info; - assert(n_args == 7); - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); +static mp_obj_t mp_ctx_texture(size_t n_args, const mp_obj_t *args) { + mp_buffer_info_t buffer_info; + assert(n_args == 7); + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - if (!mp_get_buffer(args[1], &buffer_info, MP_BUFFER_READ)) - { - mp_raise_TypeError("not a buffer"); - } - int format = mp_obj_get_int (args[2]); - int width = mp_obj_get_int (args[3]); - int height = mp_obj_get_int (args[4]); - int stride = mp_obj_get_int (args[5]); - //static int eid_no = 0; - //char ieid[10]; // this is a workaround for the rasterizer - // not keeping the cache validity expected - // as we drive it - // it also means we cannot properly use - // eid based APIs - //sprintf (ieid, "%i", eid_no++); - ctx_define_texture (self->ctx, - NULL, width, height, stride, format, - buffer_info.buf, NULL); - return args[0]; + if (!mp_get_buffer(args[1], &buffer_info, MP_BUFFER_READ)) { + mp_raise_TypeError("not a buffer"); + } + int format = mp_obj_get_int(args[2]); + int width = mp_obj_get_int(args[3]); + int height = mp_obj_get_int(args[4]); + int stride = mp_obj_get_int(args[5]); + // static int eid_no = 0; + // char ieid[10]; // this is a workaround for the rasterizer + // not keeping the cache validity expected + // as we drive it + // it also means we cannot properly use + // eid based APIs + // sprintf (ieid, "%i", eid_no++); + ctx_define_texture(self->ctx, NULL, width, height, stride, format, + buffer_info.buf, NULL); + return args[0]; } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_texture_obj, 6, 6, mp_ctx_texture); - #if 0 static mp_obj_t mp_ctx_font(mp_obj_t self_in, mp_obj_t font_in) { @@ -451,375 +396,358 @@ static mp_obj_t mp_ctx_font(mp_obj_t self_in, mp_obj_t font_in) MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_font_obj, mp_ctx_font); #endif -static mp_obj_t mp_ctx_get_font_name (mp_obj_t self_in, mp_obj_t no_in) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); - int no = mp_obj_get_int (no_in); - const char *name = ctx_get_font_name (self->ctx, no); - if (name) - return mp_obj_new_str(name, strlen(name)); - return mp_const_none; +static mp_obj_t mp_ctx_get_font_name(mp_obj_t self_in, mp_obj_t no_in) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); + int no = mp_obj_get_int(no_in); + const char *name = ctx_get_font_name(self->ctx, no); + if (name) return mp_obj_new_str(name, strlen(name)); + return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_get_font_name_obj, mp_ctx_get_font_name); - -static mp_obj_t mp_ctx_text_width(mp_obj_t self_in, mp_obj_t string_in) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); - const char *string = mp_obj_str_get_str(string_in); - return mp_obj_new_float(ctx_text_width(self->ctx, string)); +static mp_obj_t mp_ctx_text_width(mp_obj_t self_in, mp_obj_t string_in) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); + const char *string = mp_obj_str_get_str(string_in); + return mp_obj_new_float(ctx_text_width(self->ctx, string)); } MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_text_width_obj, mp_ctx_text_width); -static mp_obj_t mp_ctx_add_stop(size_t n_args, const mp_obj_t *args) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); - mp_obj_t color_in = args[2]; - - float pos = (float)mp_obj_get_float(args[1]); - - float alpha_f = 1.0f; - if (n_args == 4) { - alpha_f = (float)mp_obj_get_float(args[3]); - } - if (alpha_f < 0.0f || alpha_f > 1.0f) { - mp_raise_ValueError("alpha must be between 0.0 or 1.0"); - } - - mp_obj_t red_in, green_in, blue_in; - if (mp_obj_get_int(mp_obj_len(color_in)) < 3) { - mp_raise_ValueError("color must have 3 elements"); - } - red_in = mp_obj_subscr(color_in, mp_obj_new_int(0), MP_OBJ_SENTINEL); - green_in = mp_obj_subscr(color_in, mp_obj_new_int(1), MP_OBJ_SENTINEL); - blue_in = mp_obj_subscr(color_in, mp_obj_new_int(2), MP_OBJ_SENTINEL); - - /* - * The color can be either floats between 0 and 1 or integers between 0 - * and 255. Make this decision based on the first element we find. - */ - if (mp_obj_is_type(red_in, &mp_type_float)) { - float red, green, blue; - red = (float)mp_obj_get_float(red_in); - green = (float)mp_obj_get_float(green_in); - blue = (float)mp_obj_get_float(blue_in); - - ctx_gradient_add_stop( - self->ctx, pos, red, green, blue, alpha_f - ); - } else { - uint8_t red, green, blue, alpha; - red = mp_obj_get_int(red_in); - green = mp_obj_get_int(green_in); - blue = mp_obj_get_int(blue_in); - - alpha = (int)(alpha_f * 255.0f); - ctx_gradient_add_stop_u8( - self->ctx, pos, red, green, blue, alpha - ); - } - - return args[0]; +static mp_obj_t mp_ctx_add_stop(size_t n_args, const mp_obj_t *args) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(args[0]); + mp_obj_t color_in = args[2]; + + float pos = (float)mp_obj_get_float(args[1]); + + float alpha_f = 1.0f; + if (n_args == 4) { + alpha_f = (float)mp_obj_get_float(args[3]); + } + if (alpha_f < 0.0f || alpha_f > 1.0f) { + mp_raise_ValueError("alpha must be between 0.0 or 1.0"); + } + + mp_obj_t red_in, green_in, blue_in; + if (mp_obj_get_int(mp_obj_len(color_in)) < 3) { + mp_raise_ValueError("color must have 3 elements"); + } + red_in = mp_obj_subscr(color_in, mp_obj_new_int(0), MP_OBJ_SENTINEL); + green_in = mp_obj_subscr(color_in, mp_obj_new_int(1), MP_OBJ_SENTINEL); + blue_in = mp_obj_subscr(color_in, mp_obj_new_int(2), MP_OBJ_SENTINEL); + + /* + * The color can be either floats between 0 and 1 or integers between 0 + * and 255. Make this decision based on the first element we find. + */ + if (mp_obj_is_type(red_in, &mp_type_float)) { + float red, green, blue; + red = (float)mp_obj_get_float(red_in); + green = (float)mp_obj_get_float(green_in); + blue = (float)mp_obj_get_float(blue_in); + + ctx_gradient_add_stop(self->ctx, pos, red, green, blue, alpha_f); + } else { + uint8_t red, green, blue, alpha; + red = mp_obj_get_int(red_in); + green = mp_obj_get_int(green_in); + blue = mp_obj_get_int(blue_in); + + alpha = (int)(alpha_f * 255.0f); + ctx_gradient_add_stop_u8(self->ctx, pos, red, green, blue, alpha); + } + + return args[0]; } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_ctx_add_stop_obj, 3, 4, mp_ctx_add_stop); #ifdef EMSCRIPTEN -#include <sys/types.h> -#include <sys/stat.h> #include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> #endif - STATIC void generic_method_lookup(mp_obj_t obj, qstr attr, mp_obj_t *dest) { const mp_obj_type_t *type = mp_obj_get_type(obj); if (MP_OBJ_TYPE_HAS_SLOT(type, locals_dict)) { - // generic method lookup - // this is a lookup in the object (ie not class or type) - //assert(type->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now - //mp_map_t *locals_map = &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map; - mp_map_elem_t *elem = mp_map_lookup(&MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); - if (elem != NULL) { - mp_convert_member_lookup(obj, type, elem->value, dest); - } + // generic method lookup + // this is a lookup in the object (ie not class or type) + // assert(type->locals_dict->base.type == &mp_type_dict); // MicroPython + // restriction, for now mp_map_t *locals_map = + // &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map; + mp_map_elem_t *elem = + mp_map_lookup(&MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map, + MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); + if (elem != NULL) { + mp_convert_member_lookup(obj, type, elem->value, dest); + } } } extern const mp_obj_type_t mp_ctx_type; #if CTX_TINYVG -static mp_obj_t mp_ctx_tinyvg_get_size (mp_obj_t self_in, mp_obj_t buffer_in) -{ - mp_buffer_info_t buffer_info; - if (!mp_get_buffer(buffer_in, &buffer_info, MP_BUFFER_READ)) - { - mp_raise_TypeError("not a buffer"); - } - int width = 0, height = 0; - ctx_tinyvg_get_size (buffer_info.buf, - buffer_info.len, &width, &height); - mp_obj_t mp_w = MP_OBJ_NEW_SMALL_INT(width); - mp_obj_t mp_h = MP_OBJ_NEW_SMALL_INT(height); - mp_obj_t tup[] = { mp_w, mp_h }; - return mp_obj_new_tuple(2, tup); +static mp_obj_t mp_ctx_tinyvg_get_size(mp_obj_t self_in, mp_obj_t buffer_in) { + mp_buffer_info_t buffer_info; + if (!mp_get_buffer(buffer_in, &buffer_info, MP_BUFFER_READ)) { + mp_raise_TypeError("not a buffer"); + } + int width = 0, height = 0; + ctx_tinyvg_get_size(buffer_info.buf, buffer_info.len, &width, &height); + mp_obj_t mp_w = MP_OBJ_NEW_SMALL_INT(width); + mp_obj_t mp_h = MP_OBJ_NEW_SMALL_INT(height); + mp_obj_t tup[] = {mp_w, mp_h}; + return mp_obj_new_tuple(2, tup); } MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_tinyvg_get_size_obj, mp_ctx_tinyvg_get_size); -static mp_obj_t mp_ctx_tinyvg_draw (mp_obj_t self_in, mp_obj_t buffer_in) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_buffer_info_t buffer_info; - - if (!mp_get_buffer(buffer_in, &buffer_info, MP_BUFFER_READ)) - { - mp_raise_TypeError("not a buffer"); - } - ctx_tinyvg_draw (self->ctx, - buffer_info.buf, - buffer_info.len, - 0); - return self_in; +static mp_obj_t mp_ctx_tinyvg_draw(mp_obj_t self_in, mp_obj_t buffer_in) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_buffer_info_t buffer_info; + + if (!mp_get_buffer(buffer_in, &buffer_info, MP_BUFFER_READ)) { + mp_raise_TypeError("not a buffer"); + } + ctx_tinyvg_draw(self->ctx, buffer_info.buf, buffer_info.len, 0); + return self_in; } MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_tinyvg_draw_obj, mp_ctx_tinyvg_draw); #endif /* CTX API functions }}} */ -static void mp_ctx_set_pixels (Ctx *ctx, void *user_data, - int x_in, int y_in, int width_in, int height_in, void *buf_in, - int buf_size) -{ - mp_obj_t args[5] = {mp_obj_new_int (x_in), - mp_obj_new_int (y_in), - mp_obj_new_int (width_in), - mp_obj_new_int (height_in), - mp_obj_new_memoryview(BYTEARRAY_TYPECODE, - buf_size, buf_in)}; - mp_call_function_n_kw (user_data, 5, 0, args); +static void mp_ctx_set_pixels(Ctx *ctx, void *user_data, int x_in, int y_in, + int width_in, int height_in, void *buf_in, + int buf_size) { + mp_obj_t args[5] = { + mp_obj_new_int(x_in), mp_obj_new_int(y_in), mp_obj_new_int(width_in), + mp_obj_new_int(height_in), + mp_obj_new_memoryview(BYTEARRAY_TYPECODE, buf_size, buf_in)}; + mp_call_function_n_kw(user_data, 5, 0, args); } -static int mp_ctx_update_fb (Ctx *ctx, void *user_data) -{ - mp_obj_t ret = mp_call_function_0(user_data); - if (mp_obj_is_true (ret)) return 1; - return 0; +static int mp_ctx_update_fb(Ctx *ctx, void *user_data) { + mp_obj_t ret = mp_call_function_0(user_data); + if (mp_obj_is_true(ret)) return 1; + return 0; } mp_obj_t mp_ctx_from_ctx(Ctx *ctx) { - mp_ctx_obj_t *o = m_new_obj(mp_ctx_obj_t); - o->base.type = &mp_ctx_type; - o->ctx = ctx; - return MP_OBJ_FROM_PTR(o); + mp_ctx_obj_t *o = m_new_obj(mp_ctx_obj_t); + o->base.type = &mp_ctx_type; + o->ctx = ctx; + return MP_OBJ_FROM_PTR(o); } -static mp_obj_t mp_ctx_make_new( - const mp_obj_type_t *type, - size_t n_args, - size_t n_kw, - const mp_obj_t *all_args -) { - mp_ctx_obj_t *o = m_new_obj(mp_ctx_obj_t); - o->base.type = type; - enum { ARG_width, ARG_height, ARG_stride, ARG_format, ARG_buffer, ARG_memory_budget, ARG_flags, - ARG_set_pixels, ARG_update, ARG_userdata }; +static mp_obj_t mp_ctx_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *all_args) { + mp_ctx_obj_t *o = m_new_obj(mp_ctx_obj_t); + o->base.type = type; + enum { + ARG_width, + ARG_height, + ARG_stride, + ARG_format, + ARG_buffer, + ARG_memory_budget, + ARG_flags, + ARG_set_pixels, + ARG_update, + ARG_userdata + }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_width, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_height, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_stride, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_format, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_buffer, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_memory_budget, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 24 * 1024} }, - { MP_QSTR_flags, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, - { MP_QSTR_set_pixels, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_update, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_userdata, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + {MP_QSTR_width, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0}}, + {MP_QSTR_height, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0}}, + {MP_QSTR_stride, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0}}, + {MP_QSTR_format, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0}}, + {MP_QSTR_buffer, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_memory_budget, + MP_ARG_KW_ONLY | MP_ARG_INT, + {.u_int = 24 * 1024}}, + {MP_QSTR_flags, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0}}, + {MP_QSTR_set_pixels, + MP_ARG_KW_ONLY | MP_ARG_OBJ, + {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_update, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, + {MP_QSTR_userdata, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}}, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, + MP_ARRAY_SIZE(allowed_args), allowed_args, args); int format = args[ARG_format].u_int; int width = args[ARG_width].u_int; int height = args[ARG_height].u_int; int stride = args[ARG_stride].u_int; int memory_budget = args[ARG_memory_budget].u_int; int flags = args[ARG_flags].u_int; - if (args[ARG_set_pixels].u_obj != MP_OBJ_NULL) - { + if (args[ARG_set_pixels].u_obj != MP_OBJ_NULL) { mp_obj_t set_pixels_in = args[ARG_set_pixels].u_obj; mp_obj_t update_fb_in = args[ARG_update].u_obj; - if (update_fb_in != mp_const_none && - !mp_obj_is_callable(update_fb_in)) - mp_raise_ValueError(MP_ERROR_TEXT("invalid update_fb handler")); + if (update_fb_in != mp_const_none && !mp_obj_is_callable(update_fb_in)) + mp_raise_ValueError(MP_ERROR_TEXT("invalid update_fb handler")); if (set_pixels_in != mp_const_none && - !mp_obj_is_callable(set_pixels_in)) - mp_raise_ValueError(MP_ERROR_TEXT("invalid set_pixels handler")); - - o->ctx = ctx_new_cb (width, height, format, - mp_ctx_set_pixels, set_pixels_in, - update_fb_in != mp_const_none ? mp_ctx_update_fb : NULL, update_fb_in, - memory_budget, - NULL, - flags); - return MP_OBJ_FROM_PTR(o); + !mp_obj_is_callable(set_pixels_in)) + mp_raise_ValueError(MP_ERROR_TEXT("invalid set_pixels handler")); + + o->ctx = + ctx_new_cb(width, height, format, mp_ctx_set_pixels, set_pixels_in, + update_fb_in != mp_const_none ? mp_ctx_update_fb : NULL, + update_fb_in, memory_budget, NULL, flags); + return MP_OBJ_FROM_PTR(o); } - if (args[ARG_buffer].u_obj != MP_OBJ_NULL) - { + if (args[ARG_buffer].u_obj != MP_OBJ_NULL) { mp_buffer_info_t buffer_info; - if (!mp_get_buffer(args[ARG_buffer].u_obj, &buffer_info, MP_BUFFER_READ)) - { - mp_raise_TypeError("not a buffer"); + if (!mp_get_buffer(args[ARG_buffer].u_obj, &buffer_info, + MP_BUFFER_READ)) { + mp_raise_TypeError("not a buffer"); } - o->ctx = ctx_new_for_framebuffer (buffer_info.buf, - width, height, stride, format); - return MP_OBJ_FROM_PTR(o); + o->ctx = ctx_new_for_framebuffer(buffer_info.buf, width, height, stride, + format); + return MP_OBJ_FROM_PTR(o); } #ifdef EMSCRIPTEN - o->ctx = ctx_wasm_get_context(memory_budget); + o->ctx = ctx_wasm_get_context(memory_budget); #else - o->ctx = ctx_new (width, height, NULL); + o->ctx = ctx_new(width, height, NULL); #endif return MP_OBJ_FROM_PTR(o); } - -STATIC mp_obj_t -mp_ctx_attr_op (mp_obj_t self_in, qstr attr, mp_obj_t set_val) -{ - mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (set_val == MP_OBJ_NULL) { - switch (attr) - { - case MP_QSTR_font: - { - const char *font = ctx_get_font (self->ctx); - return mp_obj_new_str(font, strlen(font)); +STATIC mp_obj_t mp_ctx_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set_val) { + mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (set_val == MP_OBJ_NULL) { + switch (attr) { + case MP_QSTR_font: { + const char *font = ctx_get_font(self->ctx); + return mp_obj_new_str(font, strlen(font)); } - case MP_QSTR_image_smoothing: - return mp_obj_new_int(ctx_get_image_smoothing (self->ctx)); - case MP_QSTR_fill_rule: - return mp_obj_new_int(ctx_get_fill_rule (self->ctx)); + case MP_QSTR_image_smoothing: + return mp_obj_new_int(ctx_get_image_smoothing(self->ctx)); + case MP_QSTR_fill_rule: + return mp_obj_new_int(ctx_get_fill_rule(self->ctx)); #if CTX_BLENDING_AND_COMPOSITING - case MP_QSTR_blend_mode: - return mp_obj_new_int(ctx_get_blend_mode (self->ctx)); - case MP_QSTR_compositing_mode: - return mp_obj_new_int(ctx_get_compositing_mode (self->ctx)); + case MP_QSTR_blend_mode: + return mp_obj_new_int(ctx_get_blend_mode(self->ctx)); + case MP_QSTR_compositing_mode: + return mp_obj_new_int(ctx_get_compositing_mode(self->ctx)); #endif - case MP_QSTR_flags: - return mp_obj_new_int(ctx_cb_get_flags (self->ctx)); - - case MP_QSTR_line_cap: - return mp_obj_new_int(ctx_get_line_cap (self->ctx)); - case MP_QSTR_line_join: - return mp_obj_new_int(ctx_get_line_join (self->ctx)); - case MP_QSTR_text_align: - return mp_obj_new_int(ctx_get_text_align (self->ctx)); - case MP_QSTR_text_baseline: - return mp_obj_new_int(ctx_get_text_baseline (self->ctx)); - case MP_QSTR_font_size: - return mp_obj_new_float(ctx_get_font_size (self->ctx)); - case MP_QSTR_line_width: - return mp_obj_new_float(ctx_get_line_width (self->ctx)); - case MP_QSTR_line_dash_offset: - return mp_obj_new_float(ctx_get_line_dash_offset (self->ctx)); - case MP_QSTR_line_height: - return mp_obj_new_float(ctx_get_line_height (self->ctx)); - case MP_QSTR_wrap_left: - return mp_obj_new_float(ctx_get_wrap_left (self->ctx)); - case MP_QSTR_wrap_right: - return mp_obj_new_float(ctx_get_wrap_right (self->ctx)); - case MP_QSTR_miter_limit: - return mp_obj_new_float(ctx_get_miter_limit (self->ctx)); - case MP_QSTR_global_alpha: - return mp_obj_new_float(ctx_get_global_alpha (self->ctx)); - case MP_QSTR_width: - return mp_obj_new_int(ctx_width (self->ctx)); - case MP_QSTR_height: - return mp_obj_new_int(ctx_height (self->ctx)); - case MP_QSTR_x: - return mp_obj_new_float(ctx_x (self->ctx)); - case MP_QSTR_y: - return mp_obj_new_float(ctx_y (self->ctx)); - } - } - else - { - switch (attr) - { - case MP_QSTR_font: - ctx_font (self->ctx, mp_obj_str_get_str (set_val)); break; - case MP_QSTR_image_smoothing: - ctx_image_smoothing (self->ctx, mp_obj_get_int (set_val)); break; - case MP_QSTR_fill_rule: - ctx_fill_rule (self->ctx, mp_obj_get_int (set_val)); break; - case MP_QSTR_line_cap: - ctx_line_cap (self->ctx, mp_obj_get_int (set_val)); break; - case MP_QSTR_flags: - ctx_cb_set_flags (self->ctx, mp_obj_get_int (set_val)); break; - case MP_QSTR_line_join: - ctx_line_join (self->ctx, mp_obj_get_int (set_val)); break; - case MP_QSTR_text_align: - ctx_text_align (self->ctx, mp_obj_get_int (set_val)); break; + case MP_QSTR_flags: + return mp_obj_new_int(ctx_cb_get_flags(self->ctx)); + + case MP_QSTR_line_cap: + return mp_obj_new_int(ctx_get_line_cap(self->ctx)); + case MP_QSTR_line_join: + return mp_obj_new_int(ctx_get_line_join(self->ctx)); + case MP_QSTR_text_align: + return mp_obj_new_int(ctx_get_text_align(self->ctx)); + case MP_QSTR_text_baseline: + return mp_obj_new_int(ctx_get_text_baseline(self->ctx)); + case MP_QSTR_font_size: + return mp_obj_new_float(ctx_get_font_size(self->ctx)); + case MP_QSTR_line_width: + return mp_obj_new_float(ctx_get_line_width(self->ctx)); + case MP_QSTR_line_dash_offset: + return mp_obj_new_float(ctx_get_line_dash_offset(self->ctx)); + case MP_QSTR_line_height: + return mp_obj_new_float(ctx_get_line_height(self->ctx)); + case MP_QSTR_wrap_left: + return mp_obj_new_float(ctx_get_wrap_left(self->ctx)); + case MP_QSTR_wrap_right: + return mp_obj_new_float(ctx_get_wrap_right(self->ctx)); + case MP_QSTR_miter_limit: + return mp_obj_new_float(ctx_get_miter_limit(self->ctx)); + case MP_QSTR_global_alpha: + return mp_obj_new_float(ctx_get_global_alpha(self->ctx)); + case MP_QSTR_width: + return mp_obj_new_int(ctx_width(self->ctx)); + case MP_QSTR_height: + return mp_obj_new_int(ctx_height(self->ctx)); + case MP_QSTR_x: + return mp_obj_new_float(ctx_x(self->ctx)); + case MP_QSTR_y: + return mp_obj_new_float(ctx_y(self->ctx)); + } + } else { + switch (attr) { + case MP_QSTR_font: + ctx_font(self->ctx, mp_obj_str_get_str(set_val)); + break; + case MP_QSTR_image_smoothing: + ctx_image_smoothing(self->ctx, mp_obj_get_int(set_val)); + break; + case MP_QSTR_fill_rule: + ctx_fill_rule(self->ctx, mp_obj_get_int(set_val)); + break; + case MP_QSTR_line_cap: + ctx_line_cap(self->ctx, mp_obj_get_int(set_val)); + break; + case MP_QSTR_flags: + ctx_cb_set_flags(self->ctx, mp_obj_get_int(set_val)); + break; + case MP_QSTR_line_join: + ctx_line_join(self->ctx, mp_obj_get_int(set_val)); + break; + case MP_QSTR_text_align: + ctx_text_align(self->ctx, mp_obj_get_int(set_val)); + break; #if CTX_BLENDING_AND_COMPOSITING - case MP_QSTR_blend_mode: - ctx_blend_mode (self->ctx, mp_obj_get_int (set_val)); break; - case MP_QSTR_compositing_mode: - ctx_compositing_mode (self->ctx, mp_obj_get_int (set_val)); break; + case MP_QSTR_blend_mode: + ctx_blend_mode(self->ctx, mp_obj_get_int(set_val)); + break; + case MP_QSTR_compositing_mode: + ctx_compositing_mode(self->ctx, mp_obj_get_int(set_val)); + break; #endif - case MP_QSTR_text_baseline: - ctx_text_baseline (self->ctx, mp_obj_get_int (set_val)); break; - case MP_QSTR_line_width: - ctx_line_width (self->ctx, (float)mp_obj_get_float (set_val)); break; - case MP_QSTR_line_height: - ctx_line_height (self->ctx, (float)mp_obj_get_float (set_val)); break; - case MP_QSTR_wrap_left: - ctx_wrap_left (self->ctx, (float)mp_obj_get_float (set_val)); break; - case MP_QSTR_wrap_right: - ctx_wrap_right (self->ctx, (float)mp_obj_get_float (set_val)); break; - - case MP_QSTR_line_dash_offset: - ctx_line_dash_offset (self->ctx, (float)mp_obj_get_float (set_val)); break; - case MP_QSTR_miter_limit: - ctx_miter_limit (self->ctx, (float)mp_obj_get_float (set_val)); break; - case MP_QSTR_global_alpha: - ctx_global_alpha (self->ctx, (float)mp_obj_get_float (set_val)); break; - case MP_QSTR_font_size: - ctx_font_size (self->ctx, (float)mp_obj_get_float (set_val)); break; + case MP_QSTR_text_baseline: + ctx_text_baseline(self->ctx, mp_obj_get_int(set_val)); + break; + case MP_QSTR_line_width: + ctx_line_width(self->ctx, (float)mp_obj_get_float(set_val)); + break; + case MP_QSTR_line_height: + ctx_line_height(self->ctx, (float)mp_obj_get_float(set_val)); + break; + case MP_QSTR_wrap_left: + ctx_wrap_left(self->ctx, (float)mp_obj_get_float(set_val)); + break; + case MP_QSTR_wrap_right: + ctx_wrap_right(self->ctx, (float)mp_obj_get_float(set_val)); + break; + + case MP_QSTR_line_dash_offset: + ctx_line_dash_offset(self->ctx, + (float)mp_obj_get_float(set_val)); + break; + case MP_QSTR_miter_limit: + ctx_miter_limit(self->ctx, (float)mp_obj_get_float(set_val)); + break; + case MP_QSTR_global_alpha: + ctx_global_alpha(self->ctx, (float)mp_obj_get_float(set_val)); + break; + case MP_QSTR_font_size: + ctx_font_size(self->ctx, (float)mp_obj_get_float(set_val)); + break; + } + return set_val; } - return set_val; - } - return self_in; + return self_in; } - STATIC void mp_ctx_attr(mp_obj_t obj, qstr attr, mp_obj_t *dest) { - - if(attr == MP_QSTR_width - ||attr == MP_QSTR_height - ||attr == MP_QSTR_font + if (attr == MP_QSTR_width || attr == MP_QSTR_height || attr == MP_QSTR_font #if CTX_BLENDING_AND_COMPOSITING - ||attr == MP_QSTR_blend_mode - ||attr == MP_QSTR_compositing_mode + || attr == MP_QSTR_blend_mode || attr == MP_QSTR_compositing_mode #endif - ||attr == MP_QSTR_flags - ||attr == MP_QSTR_line_cap - ||attr == MP_QSTR_line_join - ||attr == MP_QSTR_text_align - ||attr == MP_QSTR_fill_rule - ||attr == MP_QSTR_image_smoothing - ||attr == MP_QSTR_text_baseline - ||attr == MP_QSTR_line_width - ||attr == MP_QSTR_line_dash_offset - ||attr == MP_QSTR_line_height - ||attr == MP_QSTR_wrap_left - ||attr == MP_QSTR_wrap_right - ||attr == MP_QSTR_miter_limit - ||attr == MP_QSTR_global_alpha - ||attr == MP_QSTR_font_size - ||attr == MP_QSTR_font - ||attr == MP_QSTR_x - ||attr == MP_QSTR_y) - { + || attr == MP_QSTR_flags || attr == MP_QSTR_line_cap || + attr == MP_QSTR_line_join || attr == MP_QSTR_text_align || + attr == MP_QSTR_fill_rule || attr == MP_QSTR_image_smoothing || + attr == MP_QSTR_text_baseline || attr == MP_QSTR_line_width || + attr == MP_QSTR_line_dash_offset || attr == MP_QSTR_line_height || + attr == MP_QSTR_wrap_left || attr == MP_QSTR_wrap_right || + attr == MP_QSTR_miter_limit || attr == MP_QSTR_global_alpha || + attr == MP_QSTR_font_size || attr == MP_QSTR_font || + attr == MP_QSTR_x || attr == MP_QSTR_y) { if (dest[0] == MP_OBJ_NULL) { // load attribute mp_obj_t val = mp_ctx_attr_op(obj, attr, MP_OBJ_NULL); @@ -827,10 +755,9 @@ STATIC void mp_ctx_attr(mp_obj_t obj, qstr attr, mp_obj_t *dest) { } else { // delete/store attribute if (mp_ctx_attr_op(obj, attr, dest[1]) != MP_OBJ_NULL) - dest[0] = MP_OBJ_NULL; // indicate success + dest[0] = MP_OBJ_NULL; // indicate success } - } - else { + } else { // A method call generic_method_lookup(obj, attr, dest); } @@ -838,253 +765,247 @@ STATIC void mp_ctx_attr(mp_obj_t obj, qstr attr, mp_obj_t *dest) { /* CTX class/type */ - static const mp_rom_map_elem_t mp_ctx_locals_dict_table[] = { - MP_CTX_METHOD(line_to), - MP_CTX_METHOD(move_to), - MP_CTX_METHOD(curve_to), - MP_CTX_METHOD(quad_to), - MP_CTX_METHOD(rel_line_to), - MP_CTX_METHOD(rel_move_to), - MP_CTX_METHOD(rel_curve_to), - MP_CTX_METHOD(rel_quad_to), - MP_CTX_METHOD(rectangle), - MP_CTX_METHOD(arc), - MP_CTX_METHOD(arc_to), - MP_CTX_METHOD(rel_arc_to), - MP_CTX_METHOD(round_rectangle), - MP_CTX_METHOD(begin_path), - MP_CTX_METHOD(close_path), - MP_CTX_METHOD(in_fill), - MP_CTX_METHOD(fill), - MP_CTX_METHOD(stroke), - MP_CTX_METHOD(paint), - MP_CTX_METHOD(clip), - MP_CTX_METHOD(text), - MP_CTX_METHOD(text_width), - MP_CTX_METHOD(rotate), - MP_CTX_METHOD(scale), - MP_CTX_METHOD(translate), - MP_CTX_METHOD(apply_transform), - MP_CTX_METHOD(start_group), - MP_CTX_METHOD(end_group), - MP_CTX_METHOD(preserve), - MP_CTX_METHOD(linear_gradient), - MP_CTX_METHOD(radial_gradient), - MP_CTX_METHOD(add_stop), - MP_CTX_METHOD(line_dash), - MP_CTX_METHOD(texture), - MP_CTX_METHOD(save), - MP_CTX_METHOD(restore), - MP_CTX_METHOD(start_frame), - MP_CTX_METHOD(end_frame), - MP_CTX_METHOD(get_font_name), - - MP_CTX_METHOD(gray), - MP_CTX_METHOD(rgb), - MP_CTX_METHOD(rgba), + MP_CTX_METHOD(line_to), + MP_CTX_METHOD(move_to), + MP_CTX_METHOD(curve_to), + MP_CTX_METHOD(quad_to), + MP_CTX_METHOD(rel_line_to), + MP_CTX_METHOD(rel_move_to), + MP_CTX_METHOD(rel_curve_to), + MP_CTX_METHOD(rel_quad_to), + MP_CTX_METHOD(rectangle), + MP_CTX_METHOD(arc), + MP_CTX_METHOD(arc_to), + MP_CTX_METHOD(rel_arc_to), + MP_CTX_METHOD(round_rectangle), + MP_CTX_METHOD(begin_path), + MP_CTX_METHOD(close_path), + MP_CTX_METHOD(in_fill), + MP_CTX_METHOD(fill), + MP_CTX_METHOD(stroke), + MP_CTX_METHOD(paint), + MP_CTX_METHOD(clip), + MP_CTX_METHOD(text), + MP_CTX_METHOD(text_width), + MP_CTX_METHOD(rotate), + MP_CTX_METHOD(scale), + MP_CTX_METHOD(translate), + MP_CTX_METHOD(apply_transform), + MP_CTX_METHOD(start_group), + MP_CTX_METHOD(end_group), + MP_CTX_METHOD(preserve), + MP_CTX_METHOD(linear_gradient), + MP_CTX_METHOD(radial_gradient), + MP_CTX_METHOD(add_stop), + MP_CTX_METHOD(line_dash), + MP_CTX_METHOD(texture), + MP_CTX_METHOD(save), + MP_CTX_METHOD(restore), + MP_CTX_METHOD(start_frame), + MP_CTX_METHOD(end_frame), + MP_CTX_METHOD(get_font_name), + + MP_CTX_METHOD(gray), + MP_CTX_METHOD(rgb), + MP_CTX_METHOD(rgba), #if CTX_PARSER - MP_CTX_METHOD(parse), + MP_CTX_METHOD(parse), #endif #if 1 #if CTX_TINYVG - MP_CTX_METHOD(tinyvg_draw), - MP_CTX_METHOD(tinyvg_get_size), + MP_CTX_METHOD(tinyvg_draw), + MP_CTX_METHOD(tinyvg_get_size), #endif #endif - MP_CTX_METHOD(logo), - - // Instance attributes - MP_CTX_ATTR(x), - MP_CTX_ATTR(y), - MP_CTX_ATTR(width), - MP_CTX_ATTR(height), - MP_CTX_ATTR(font), - MP_CTX_ATTR(image_smoothing), + MP_CTX_METHOD(logo), + + // Instance attributes + MP_CTX_ATTR(x), + MP_CTX_ATTR(y), + MP_CTX_ATTR(width), + MP_CTX_ATTR(height), + MP_CTX_ATTR(font), + MP_CTX_ATTR(image_smoothing), #if CTX_BLENDING_AND_COMPOSITING - MP_CTX_ATTR(compositing_mode), - MP_CTX_ATTR(blend_mode), + MP_CTX_ATTR(compositing_mode), + MP_CTX_ATTR(blend_mode), #endif - MP_CTX_ATTR(flags), - MP_CTX_ATTR(line_cap), - MP_CTX_ATTR(line_join), - MP_CTX_ATTR(text_align), - MP_CTX_ATTR(fill_rule), - MP_CTX_ATTR(text_baseline), - MP_CTX_ATTR(line_width), - MP_CTX_ATTR(line_dash_offset), - MP_CTX_ATTR(line_height), - MP_CTX_ATTR(wrap_left), - MP_CTX_ATTR(wrap_right), - MP_CTX_ATTR(miter_limit), - MP_CTX_ATTR(global_alpha), - MP_CTX_ATTR(font_size), - - MP_CTX_INT_CONSTANT(FLAG,LOWFI), - MP_CTX_INT_CONSTANT(FLAG,GRAY2), - MP_CTX_INT_CONSTANT(FLAG,GRAY4), - MP_CTX_INT_CONSTANT(FLAG,GRAY8), - MP_CTX_INT_CONSTANT(FLAG,RGB332), - MP_CTX_INT_CONSTANT(FLAG,HASH_CACHE), - //MP_CTX_INT_CONSTANT(FLAG,DAMAGE_CONTROL), - MP_CTX_INT_CONSTANT(FLAG,KEEP_DATA), - MP_CTX_INT_CONSTANT(FLAG,INTRA_UPDATE), - MP_CTX_INT_CONSTANT(FLAG,STAY_LOW), + MP_CTX_ATTR(flags), + MP_CTX_ATTR(line_cap), + MP_CTX_ATTR(line_join), + MP_CTX_ATTR(text_align), + MP_CTX_ATTR(fill_rule), + MP_CTX_ATTR(text_baseline), + MP_CTX_ATTR(line_width), + MP_CTX_ATTR(line_dash_offset), + MP_CTX_ATTR(line_height), + MP_CTX_ATTR(wrap_left), + MP_CTX_ATTR(wrap_right), + MP_CTX_ATTR(miter_limit), + MP_CTX_ATTR(global_alpha), + MP_CTX_ATTR(font_size), + + MP_CTX_INT_CONSTANT(FLAG, LOWFI), + MP_CTX_INT_CONSTANT(FLAG, GRAY2), + MP_CTX_INT_CONSTANT(FLAG, GRAY4), + MP_CTX_INT_CONSTANT(FLAG, GRAY8), + MP_CTX_INT_CONSTANT(FLAG, RGB332), + MP_CTX_INT_CONSTANT(FLAG, HASH_CACHE), + // MP_CTX_INT_CONSTANT(FLAG,DAMAGE_CONTROL), + MP_CTX_INT_CONSTANT(FLAG, KEEP_DATA), + MP_CTX_INT_CONSTANT(FLAG, INTRA_UPDATE), + MP_CTX_INT_CONSTANT(FLAG, STAY_LOW), #if CTX_ENABLE_CBRLE - MP_CTX_INT_CONSTANT(FLAG,CBRLE), + MP_CTX_INT_CONSTANT(FLAG, CBRLE), #endif - MP_CTX_INT_CONSTANT(FILL_RULE,WINDING), - MP_CTX_INT_CONSTANT(FILL_RULE,EVEN_ODD), - MP_CTX_INT_CONSTANT(JOIN,BEVEL), - MP_CTX_INT_CONSTANT(JOIN,ROUND), - MP_CTX_INT_CONSTANT(JOIN,MITER), - MP_CTX_INT_CONSTANT(CAP,NONE), - MP_CTX_INT_CONSTANT(CAP,ROUND), - MP_CTX_INT_CONSTANT(CAP,SQUARE), + MP_CTX_INT_CONSTANT(FILL_RULE, WINDING), + MP_CTX_INT_CONSTANT(FILL_RULE, EVEN_ODD), + MP_CTX_INT_CONSTANT(JOIN, BEVEL), + MP_CTX_INT_CONSTANT(JOIN, ROUND), + MP_CTX_INT_CONSTANT(JOIN, MITER), + MP_CTX_INT_CONSTANT(CAP, NONE), + MP_CTX_INT_CONSTANT(CAP, ROUND), + MP_CTX_INT_CONSTANT(CAP, SQUARE), #if CTX_BLENDING_AND_COMPOSITING - MP_CTX_INT_CONSTANT(COMPOSITE,SOURCE_OVER), - MP_CTX_INT_CONSTANT(COMPOSITE,COPY), - MP_CTX_INT_CONSTANT(COMPOSITE,SOURCE_IN), - MP_CTX_INT_CONSTANT(COMPOSITE,SOURCE_OUT), - MP_CTX_INT_CONSTANT(COMPOSITE,SOURCE_ATOP), - MP_CTX_INT_CONSTANT(COMPOSITE,CLEAR), - MP_CTX_INT_CONSTANT(COMPOSITE,DESTINATION_OVER), - MP_CTX_INT_CONSTANT(COMPOSITE,DESTINATION), - MP_CTX_INT_CONSTANT(COMPOSITE,DESTINATION_IN), - MP_CTX_INT_CONSTANT(COMPOSITE,DESTINATION_OUT), - MP_CTX_INT_CONSTANT(COMPOSITE,DESTINATION_ATOP), - MP_CTX_INT_CONSTANT(COMPOSITE,XOR), - MP_CTX_INT_CONSTANT(BLEND,NORMAL), - MP_CTX_INT_CONSTANT(BLEND,MULTIPLY), - MP_CTX_INT_CONSTANT(BLEND,SCREEN), - MP_CTX_INT_CONSTANT(BLEND,OVERLAY), - MP_CTX_INT_CONSTANT(BLEND,DARKEN), - MP_CTX_INT_CONSTANT(BLEND,LIGHTEN), - MP_CTX_INT_CONSTANT(BLEND,COLOR_DODGE), - MP_CTX_INT_CONSTANT(BLEND,COLOR_BURN), - MP_CTX_INT_CONSTANT(BLEND,HARD_LIGHT), - MP_CTX_INT_CONSTANT(BLEND,SOFT_LIGHT), - MP_CTX_INT_CONSTANT(BLEND,DIFFERENCE), - MP_CTX_INT_CONSTANT(BLEND,EXCLUSION), - MP_CTX_INT_CONSTANT(BLEND,HUE), - MP_CTX_INT_CONSTANT(BLEND,SATURATION), - MP_CTX_INT_CONSTANT(BLEND,COLOR), - MP_CTX_INT_CONSTANT(BLEND,LUMINOSITY), - MP_CTX_INT_CONSTANT(BLEND,DIVIDE), - MP_CTX_INT_CONSTANT(BLEND,ADDITION), - MP_CTX_INT_CONSTANT(BLEND,SUBTRACT), + MP_CTX_INT_CONSTANT(COMPOSITE, SOURCE_OVER), + MP_CTX_INT_CONSTANT(COMPOSITE, COPY), + MP_CTX_INT_CONSTANT(COMPOSITE, SOURCE_IN), + MP_CTX_INT_CONSTANT(COMPOSITE, SOURCE_OUT), + MP_CTX_INT_CONSTANT(COMPOSITE, SOURCE_ATOP), + MP_CTX_INT_CONSTANT(COMPOSITE, CLEAR), + MP_CTX_INT_CONSTANT(COMPOSITE, DESTINATION_OVER), + MP_CTX_INT_CONSTANT(COMPOSITE, DESTINATION), + MP_CTX_INT_CONSTANT(COMPOSITE, DESTINATION_IN), + MP_CTX_INT_CONSTANT(COMPOSITE, DESTINATION_OUT), + MP_CTX_INT_CONSTANT(COMPOSITE, DESTINATION_ATOP), + MP_CTX_INT_CONSTANT(COMPOSITE, XOR), + MP_CTX_INT_CONSTANT(BLEND, NORMAL), + MP_CTX_INT_CONSTANT(BLEND, MULTIPLY), + MP_CTX_INT_CONSTANT(BLEND, SCREEN), + MP_CTX_INT_CONSTANT(BLEND, OVERLAY), + MP_CTX_INT_CONSTANT(BLEND, DARKEN), + MP_CTX_INT_CONSTANT(BLEND, LIGHTEN), + MP_CTX_INT_CONSTANT(BLEND, COLOR_DODGE), + MP_CTX_INT_CONSTANT(BLEND, COLOR_BURN), + MP_CTX_INT_CONSTANT(BLEND, HARD_LIGHT), + MP_CTX_INT_CONSTANT(BLEND, SOFT_LIGHT), + MP_CTX_INT_CONSTANT(BLEND, DIFFERENCE), + MP_CTX_INT_CONSTANT(BLEND, EXCLUSION), + MP_CTX_INT_CONSTANT(BLEND, HUE), + MP_CTX_INT_CONSTANT(BLEND, SATURATION), + MP_CTX_INT_CONSTANT(BLEND, COLOR), + MP_CTX_INT_CONSTANT(BLEND, LUMINOSITY), + MP_CTX_INT_CONSTANT(BLEND, DIVIDE), + MP_CTX_INT_CONSTANT(BLEND, ADDITION), + MP_CTX_INT_CONSTANT(BLEND, SUBTRACT), #endif - MP_CTX_INT_CONSTANT(TEXT_BASELINE,ALPHABETIC), - MP_CTX_INT_CONSTANT(TEXT_BASELINE,TOP), - MP_CTX_INT_CONSTANT(TEXT_BASELINE,HANGING), - MP_CTX_INT_CONSTANT(TEXT_BASELINE,MIDDLE), - MP_CTX_INT_CONSTANT(TEXT_BASELINE,IDEOGRAPHIC), - MP_CTX_INT_CONSTANT(TEXT_BASELINE,BOTTOM), - MP_CTX_INT_CONSTANT(TEXT_ALIGN,START), - MP_CTX_INT_CONSTANT(TEXT_ALIGN,END), - MP_CTX_INT_CONSTANT(TEXT_ALIGN,CENTER), - MP_CTX_INT_CONSTANT(TEXT_ALIGN,LEFT), - MP_CTX_INT_CONSTANT(TEXT_ALIGN,RIGHT), - - MP_CTX_INT_CONSTANT(FORMAT,GRAY8), - MP_CTX_INT_CONSTANT(FORMAT,GRAYA8), - MP_CTX_INT_CONSTANT(FORMAT,RGB8), - MP_CTX_INT_CONSTANT(FORMAT,RGBA8), - MP_CTX_INT_CONSTANT(FORMAT,BGRA8), - MP_CTX_INT_CONSTANT(FORMAT,RGB565), - MP_CTX_INT_CONSTANT(FORMAT,RGB565_BYTESWAPPED), - MP_CTX_INT_CONSTANT(FORMAT,RGB332), - //MP_CTX_INT_CONSTANT(FORMAT,RGBAF), - //MP_CTX_INT_CONSTANT(FORMAT,GRAYF), - //MP_CTX_INT_CONSTANT(FORMAT,GRAYAF), - MP_CTX_INT_CONSTANT(FORMAT,GRAY1), - MP_CTX_INT_CONSTANT(FORMAT,GRAY2), - MP_CTX_INT_CONSTANT(FORMAT,GRAY4), - MP_CTX_INT_CONSTANT(FORMAT,YUV420), - - MP_CTX_INT_CONSTANT_UNPREFIXED(PRESS), - MP_CTX_INT_CONSTANT_UNPREFIXED(MOTION), - MP_CTX_INT_CONSTANT_UNPREFIXED(RELEASE), - MP_CTX_INT_CONSTANT_UNPREFIXED(ENTER), - MP_CTX_INT_CONSTANT_UNPREFIXED(LEAVE), - MP_CTX_INT_CONSTANT_UNPREFIXED(TAP), - MP_CTX_INT_CONSTANT_UNPREFIXED(TAP_AND_HOLD), - MP_CTX_INT_CONSTANT_UNPREFIXED(DRAG_PRESS), - MP_CTX_INT_CONSTANT_UNPREFIXED(DRAG_MOTION), - MP_CTX_INT_CONSTANT_UNPREFIXED(DRAG_RELEASE), - MP_CTX_INT_CONSTANT_UNPREFIXED(KEY_PRESS), - MP_CTX_INT_CONSTANT_UNPREFIXED(KEY_DOWN), - MP_CTX_INT_CONSTANT_UNPREFIXED(KEY_UP), - MP_CTX_INT_CONSTANT_UNPREFIXED(SCROLL), - MP_CTX_INT_CONSTANT_UNPREFIXED(MESSAGE), - MP_CTX_INT_CONSTANT_UNPREFIXED(DROP), - MP_CTX_INT_CONSTANT_UNPREFIXED(SET_CURSOR), + MP_CTX_INT_CONSTANT(TEXT_BASELINE, ALPHABETIC), + MP_CTX_INT_CONSTANT(TEXT_BASELINE, TOP), + MP_CTX_INT_CONSTANT(TEXT_BASELINE, HANGING), + MP_CTX_INT_CONSTANT(TEXT_BASELINE, MIDDLE), + MP_CTX_INT_CONSTANT(TEXT_BASELINE, IDEOGRAPHIC), + MP_CTX_INT_CONSTANT(TEXT_BASELINE, BOTTOM), + MP_CTX_INT_CONSTANT(TEXT_ALIGN, START), + MP_CTX_INT_CONSTANT(TEXT_ALIGN, END), + MP_CTX_INT_CONSTANT(TEXT_ALIGN, CENTER), + MP_CTX_INT_CONSTANT(TEXT_ALIGN, LEFT), + MP_CTX_INT_CONSTANT(TEXT_ALIGN, RIGHT), + + MP_CTX_INT_CONSTANT(FORMAT, GRAY8), + MP_CTX_INT_CONSTANT(FORMAT, GRAYA8), + MP_CTX_INT_CONSTANT(FORMAT, RGB8), + MP_CTX_INT_CONSTANT(FORMAT, RGBA8), + MP_CTX_INT_CONSTANT(FORMAT, BGRA8), + MP_CTX_INT_CONSTANT(FORMAT, RGB565), + MP_CTX_INT_CONSTANT(FORMAT, RGB565_BYTESWAPPED), + MP_CTX_INT_CONSTANT(FORMAT, RGB332), + // MP_CTX_INT_CONSTANT(FORMAT,RGBAF), + // MP_CTX_INT_CONSTANT(FORMAT,GRAYF), + // MP_CTX_INT_CONSTANT(FORMAT,GRAYAF), + MP_CTX_INT_CONSTANT(FORMAT, GRAY1), + MP_CTX_INT_CONSTANT(FORMAT, GRAY2), + MP_CTX_INT_CONSTANT(FORMAT, GRAY4), + MP_CTX_INT_CONSTANT(FORMAT, YUV420), + + MP_CTX_INT_CONSTANT_UNPREFIXED(PRESS), + MP_CTX_INT_CONSTANT_UNPREFIXED(MOTION), + MP_CTX_INT_CONSTANT_UNPREFIXED(RELEASE), + MP_CTX_INT_CONSTANT_UNPREFIXED(ENTER), + MP_CTX_INT_CONSTANT_UNPREFIXED(LEAVE), + MP_CTX_INT_CONSTANT_UNPREFIXED(TAP), + MP_CTX_INT_CONSTANT_UNPREFIXED(TAP_AND_HOLD), + MP_CTX_INT_CONSTANT_UNPREFIXED(DRAG_PRESS), + MP_CTX_INT_CONSTANT_UNPREFIXED(DRAG_MOTION), + MP_CTX_INT_CONSTANT_UNPREFIXED(DRAG_RELEASE), + MP_CTX_INT_CONSTANT_UNPREFIXED(KEY_PRESS), + MP_CTX_INT_CONSTANT_UNPREFIXED(KEY_DOWN), + MP_CTX_INT_CONSTANT_UNPREFIXED(KEY_UP), + MP_CTX_INT_CONSTANT_UNPREFIXED(SCROLL), + MP_CTX_INT_CONSTANT_UNPREFIXED(MESSAGE), + MP_CTX_INT_CONSTANT_UNPREFIXED(DROP), + MP_CTX_INT_CONSTANT_UNPREFIXED(SET_CURSOR), }; static MP_DEFINE_CONST_DICT(mp_ctx_locals_dict, mp_ctx_locals_dict_table); -MP_DEFINE_CONST_OBJ_TYPE( - mp_ctx_type, - MP_QSTR_ctx_type, - MP_TYPE_FLAG_NONE, - //print, array_print, - make_new, mp_ctx_make_new, - attr, mp_ctx_attr, - locals_dict, &mp_ctx_locals_dict -); +MP_DEFINE_CONST_OBJ_TYPE(mp_ctx_type, MP_QSTR_ctx_type, MP_TYPE_FLAG_NONE, + // print, array_print, + make_new, mp_ctx_make_new, attr, mp_ctx_attr, + locals_dict, &mp_ctx_locals_dict); /* const mp_obj_type_t mp_ctx_type = { - .base = { &mp_type_type }, - .name = MP_QSTR_Context, - .make_new = mp_ctx_make_new, - .locals_dict = (mp_obj_t)&mp_ctx_locals_dict, + .base = { &mp_type_type }, + .name = MP_QSTR_Context, + .make_new = mp_ctx_make_new, + .locals_dict = (mp_obj_t)&mp_ctx_locals_dict, .attr = mp_ctx_attr }; */ /* The globals table for this module */ static const mp_rom_map_elem_t mp_ctx_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ctx_module) }, - { MP_ROM_QSTR(MP_QSTR_Context), MP_ROM_PTR(&mp_ctx_type) }, - - MP_CTX_INT_CONSTANT(FORMAT,GRAY8), - MP_CTX_INT_CONSTANT(FORMAT,GRAYA8), - MP_CTX_INT_CONSTANT(FORMAT,RGB8), - MP_CTX_INT_CONSTANT(FORMAT,RGBA8), - MP_CTX_INT_CONSTANT(FORMAT,BGRA8), - MP_CTX_INT_CONSTANT(FORMAT,RGB565), - MP_CTX_INT_CONSTANT(FORMAT,RGB565_BYTESWAPPED), - MP_CTX_INT_CONSTANT(FORMAT,RGB332), - //MP_CTX_INT_CONSTANT(FORMAT,RGBAF), - //MP_CTX_INT_CONSTANT(FORMAT,GRAYF), - //MP_CTX_INT_CONSTANT(FORMAT,GRAYAF), - MP_CTX_INT_CONSTANT(FORMAT,GRAY1), - MP_CTX_INT_CONSTANT(FORMAT,GRAY2), - MP_CTX_INT_CONSTANT(FORMAT,GRAY4), - MP_CTX_INT_CONSTANT(FORMAT,YUV420), - - MP_CTX_INT_CONSTANT(FLAG,LOWFI), - MP_CTX_INT_CONSTANT(FLAG,GRAY2), - MP_CTX_INT_CONSTANT(FLAG,GRAY4), - MP_CTX_INT_CONSTANT(FLAG,GRAY8), - MP_CTX_INT_CONSTANT(FLAG,RGB332), - MP_CTX_INT_CONSTANT(FLAG,HASH_CACHE), -// MP_CTX_INT_CONSTANT(FLAG,DAMAGE_CONTROL), - MP_CTX_INT_CONSTANT(FLAG,KEEP_DATA), - MP_CTX_INT_CONSTANT(FLAG,INTRA_UPDATE), - MP_CTX_INT_CONSTANT(FLAG,STAY_LOW), + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ctx_module)}, + {MP_ROM_QSTR(MP_QSTR_Context), MP_ROM_PTR(&mp_ctx_type)}, + + MP_CTX_INT_CONSTANT(FORMAT, GRAY8), + MP_CTX_INT_CONSTANT(FORMAT, GRAYA8), + MP_CTX_INT_CONSTANT(FORMAT, RGB8), + MP_CTX_INT_CONSTANT(FORMAT, RGBA8), + MP_CTX_INT_CONSTANT(FORMAT, BGRA8), + MP_CTX_INT_CONSTANT(FORMAT, RGB565), + MP_CTX_INT_CONSTANT(FORMAT, RGB565_BYTESWAPPED), + MP_CTX_INT_CONSTANT(FORMAT, RGB332), + // MP_CTX_INT_CONSTANT(FORMAT,RGBAF), + // MP_CTX_INT_CONSTANT(FORMAT,GRAYF), + // MP_CTX_INT_CONSTANT(FORMAT,GRAYAF), + MP_CTX_INT_CONSTANT(FORMAT, GRAY1), + MP_CTX_INT_CONSTANT(FORMAT, GRAY2), + MP_CTX_INT_CONSTANT(FORMAT, GRAY4), + MP_CTX_INT_CONSTANT(FORMAT, YUV420), + + MP_CTX_INT_CONSTANT(FLAG, LOWFI), + MP_CTX_INT_CONSTANT(FLAG, GRAY2), + MP_CTX_INT_CONSTANT(FLAG, GRAY4), + MP_CTX_INT_CONSTANT(FLAG, GRAY8), + MP_CTX_INT_CONSTANT(FLAG, RGB332), + MP_CTX_INT_CONSTANT(FLAG, HASH_CACHE), + // MP_CTX_INT_CONSTANT(FLAG,DAMAGE_CONTROL), + MP_CTX_INT_CONSTANT(FLAG, KEEP_DATA), + MP_CTX_INT_CONSTANT(FLAG, INTRA_UPDATE), + MP_CTX_INT_CONSTANT(FLAG, STAY_LOW), #if CTX_ENABLE_CBRLE - MP_CTX_INT_CONSTANT(FLAG,CBRLE), + MP_CTX_INT_CONSTANT(FLAG, CBRLE), #endif }; static MP_DEFINE_CONST_DICT(mp_ctx_module_globals, mp_ctx_module_globals_table); const mp_obj_module_t mp_module_ctx = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t *)&mp_ctx_module_globals, + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t *)&mp_ctx_module_globals, }; /* This is a special macro that will make MicroPython aware of this module */ -- GitLab