Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • 89-apps-should-be-able-to-specify-if-they-want-wifi-to-be-disabled-when-entering-them
  • 9Rmain
  • allow-reloading-sunmenu
  • always-have-a-wifi-instance
  • anon/gpndemo
  • anon/update-sim
  • anon/webflasher
  • app_text_viewer
  • audio_input
  • audio_io
  • blm_dev_chan
  • ch3/bl00mbox_docs
  • ci-1690580595
  • dev_p4
  • dev_p4-iggy
  • dev_p4-iggy-rebased
  • dx/dldldld
  • dx/fb-save-restore
  • dx/hint-hint
  • dx/jacksense-headset-mic-only
  • events
  • fil3s-limit-filesize
  • fil3s-media
  • fpletz/flake
  • gr33nhouse-improvements
  • history-rewrite
  • icon-flower
  • iggy/stemming
  • iggy/stemming_merge
  • led_fix_fix
  • main
  • main+schneider
  • media_has_video_has_audio
  • micropython_api
  • mixer2
  • moon2_demo_temp
  • moon2_migrate_apps
  • more-accurate-battery
  • pippin/ctx_sprite_sheet_support
  • pippin/display-python-errors-on-display
  • pippin/make_empty_drawlists_skip_render_and_blit
  • pippin/more-accurate-battery
  • pippin/tcp_redirect_hack
  • pippin/tune_ctx_config_update_from_upstream
  • pippin/uhm_flash_access_bust
  • pressable_bugfix
  • py_only_update_fps_overlay_when_changing
  • q3k/doom-poc
  • q3k/render-to-texture
  • rahix/flow3rseeds
  • raw_captouch_new
  • raw_captouch_old
  • release/1.0.0
  • release/1.1.0
  • release/1.1.1
  • release/1.2.0
  • release/1.3.0
  • release/1.4.0
  • restore_blit
  • return_of_melodic_demo
  • rev4_micropython
  • schneider/application-remove-name
  • schneider/bhi581
  • schneider/factory_test
  • schneider/recovery
  • scope_hack
  • sdkconfig-spiram-tinyusb
  • sec/auto-nick
  • sec/blinky
  • sector_size_512
  • shoegaze-fps
  • smaller_gradient_lut
  • store_delta_ms_and_ins_as_class_members
  • task_cleanup
  • uctx-wip
  • w1f1-in-sim
  • widgets_draw
  • wifi-json-error-handling
  • wip-docs
  • wip-tinyusb
  • v1.0.0
  • v1.0.0+rc1
  • v1.0.0+rc2
  • v1.0.0+rc3
  • v1.0.0+rc4
  • v1.0.0+rc5
  • v1.0.0+rc6
  • v1.1.0
  • v1.1.0+rc1
  • v1.1.1
  • v1.2.0
  • v1.2.0+rc1
  • v1.3.0
  • v1.4.0
94 results

Target

Select target project
  • flow3r/flow3r-firmware
  • Vespasian/flow3r-firmware
  • alxndr42/flow3r-firmware
  • pl/flow3r-firmware
  • Kari/flow3r-firmware
  • raimue/flow3r-firmware
  • grandchild/flow3r-firmware
  • mu5tach3/flow3r-firmware
  • Nervengift/flow3r-firmware
  • arachnist/flow3r-firmware
  • TheNewCivilian/flow3r-firmware
  • alibi/flow3r-firmware
  • manuel_v/flow3r-firmware
  • xeniter/flow3r-firmware
  • maxbachmann/flow3r-firmware
  • yGifoom/flow3r-firmware
  • istobic/flow3r-firmware
  • EiNSTeiN_/flow3r-firmware
  • gnudalf/flow3r-firmware
  • 999eagle/flow3r-firmware
  • toerb/flow3r-firmware
  • pandark/flow3r-firmware
  • teal/flow3r-firmware
  • x42/flow3r-firmware
  • alufers/flow3r-firmware
  • dos/flow3r-firmware
  • yrlf/flow3r-firmware
  • LuKaRo/flow3r-firmware
  • ThomasElRubio/flow3r-firmware
  • ai/flow3r-firmware
  • T_X/flow3r-firmware
  • highTower/flow3r-firmware
  • beanieboi/flow3r-firmware
  • Woazboat/flow3r-firmware
  • gooniesbro/flow3r-firmware
  • marvino/flow3r-firmware
  • kressnerd/flow3r-firmware
  • quazgar/flow3r-firmware
  • aoid/flow3r-firmware
  • jkj/flow3r-firmware
  • naomi/flow3r-firmware
41 results
Select Git revision
  • 9Rmain
  • anon/gpndemo
  • anon/update-sim
  • anon/webflasher
  • audio_input
  • audio_io
  • bl00mbox
  • bl00mbox_old
  • captouch-threshold
  • ch3/bl00mbox_docs
  • ci-1690580595
  • compressor
  • dev_p4
  • dev_p4-iggy
  • dev_p4-iggy-rebased
  • dos
  • dos-main-patch-50543
  • events
  • fm_fix
  • fm_fix2
  • fpletz/flake
  • history-rewrite
  • icon-flower
  • iggy/stemming
  • iggy/stemming_merge
  • json-error
  • main
  • main+schneider
  • media-buf
  • micropython_api
  • moon2_applications
  • moon2_demo_temp
  • moon2_gay_drums
  • passthrough
  • phhw
  • pippin/display-python-errors-on-display
  • pippin/make_empty_drawlists_skip_render_and_blit
  • pippin/media_framework
  • pippin/uhm_flash_access_bust
  • pressable_bugfix
  • q3k/doom-poc
  • rahix/big-flow3r
  • rahix/flow3rseeds
  • raw_captouch_new
  • raw_captouch_old
  • release/1.0.0
  • release/1.1.0
  • release/1.1.1
  • rev4_micropython
  • schneider/application-remove-name
  • schneider/bhi581
  • schneider/factory_test
  • schneider/recovery
  • scope
  • scope_hack
  • sdkconfig-spiram-tinyusb
  • sec/auto-nick
  • sec/blinky
  • simtest
  • slewtest
  • t
  • test
  • test2
  • uctx-wip
  • view-think
  • vm-pending
  • vsync
  • wave
  • wip-docs
  • wip-tinyusb
  • v1.0.0
  • v1.0.0+rc1
  • v1.0.0+rc2
  • v1.0.0+rc3
  • v1.0.0+rc4
  • v1.0.0+rc5
  • v1.0.0+rc6
  • v1.1.0
  • v1.1.0+rc1
  • v1.1.1
  • v1.2.0
  • v1.2.0+rc1
  • v1.3.0
83 results
Show changes
Showing
with 0 additions and 2712 deletions
#include "badge23/audio.h"
#include "badge23/synth.h"
#include "badge23/scope.h"
#include "badge23/lock.h"
#include "driver/i2s.h"
#include "driver/i2c.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#define TIMEOUT_MS 1000
#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
static void audio_player_task(void* arg);
#define DMA_BUFFER_SIZE 64
#define DMA_BUFFER_COUNT 2
#define I2S_PORT 0
// 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)
#define SILLY_LOW_VOLUME_DB (-10000.)
static bool headphones_connected = 0;
static bool headset_connected = 0;
static bool line_in_connected = 0;
static bool headphones_detection_override = 0;
static int32_t software_volume = 0;
// maybe struct these someday but eh it works
static float headphones_volume_dB = 0;
static bool headphones_mute = 0;
const static float headphones_maximum_volume_system_dB = 3;
static float headphones_maximum_volume_user_dB = headphones_maximum_volume_system_dB;
static float headphones_minimum_volume_user_dB = headphones_maximum_volume_system_dB - 70;
static float speaker_volume_dB = 0;
static bool speaker_mute = 0;
const static float speaker_maximum_volume_system_dB = 14;
static float speaker_maximum_volume_user_dB = speaker_maximum_volume_system_dB;
static float speaker_minimum_volume_user_dB = speaker_maximum_volume_system_dB - 60;
uint8_t audio_headset_is_connected(){ return headset_connected; }
uint8_t audio_headphones_are_connected(){ return headphones_connected || headphones_detection_override; }
float audio_headphones_get_volume_dB(){ return headphones_volume_dB; }
float audio_speaker_get_volume_dB(){ return speaker_volume_dB; }
float audio_headphones_get_minimum_volume_dB(){ return headphones_minimum_volume_user_dB; }
float audio_speaker_get_minimum_volume_dB(){ return speaker_minimum_volume_user_dB; }
float audio_headphones_get_maximum_volume_dB(){ return headphones_maximum_volume_user_dB; }
float audio_speaker_get_maximum_volume_dB(){ return speaker_maximum_volume_user_dB; }
uint8_t audio_headphones_get_mute(){ return headphones_mute ? 1 : 0; }
uint8_t audio_speaker_get_mute(){ return speaker_mute ? 1 : 0; }
#if defined(CONFIG_BADGE23_HW_GEN_P3) || defined(CONFIG_BADGE23_HW_GEN_P4) || defined(CONFIG_BADGE23_HW_GEN_P6)
static uint8_t max98091_i2c_read(const uint8_t reg)
{
const uint8_t tx[] = {reg};
uint8_t rx[1];
xSemaphoreTake(mutex_i2c, portMAX_DELAY);
esp_err_t ret = i2c_master_write_read_device(I2C_MASTER_NUM, 0x10, tx, sizeof(tx), rx, sizeof(rx), TIMEOUT_MS / portTICK_PERIOD_MS);
xSemaphoreGive(mutex_i2c);
return rx[0];
}
static esp_err_t max98091_i2c_write(const uint8_t reg, const uint8_t data)
{
const uint8_t tx[] = {reg, data};
xSemaphoreTake(mutex_i2c, portMAX_DELAY);
esp_err_t ret = i2c_master_write_to_device(I2C_MASTER_NUM, 0x10, tx, sizeof(tx), TIMEOUT_MS / portTICK_PERIOD_MS);
xSemaphoreGive(mutex_i2c);
return ret;
}
static esp_err_t max98091_i2c_write_readback(const uint8_t reg, const uint8_t data)
{
const uint8_t tx[] = {reg, data};
xSemaphoreTake(mutex_i2c, portMAX_DELAY);
esp_err_t ret = i2c_master_write_to_device(I2C_MASTER_NUM, 0x10, tx, sizeof(tx), TIMEOUT_MS / portTICK_PERIOD_MS);
xSemaphoreGive(mutex_i2c);
if(max98091_i2c_read(reg) != data) printf("readback of %04X to %02X write failed\n", data, reg);
return ret;
}
static void init_codec()
{
// Enable CODEC
vTaskDelay(10 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x00, 0x80)); // shutdown
vTaskDelay(10 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x45, 0)); // shutdown
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x1b, 1 << 4)); // pclk = mclk / 1
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x26, (1 << 7) | (1 << 6))); // music, dc filter in record
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x06, 1 << 2)); // Sets up DAI for left-justified slave mode operation.
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x07, 1 << 5)); // Sets up the DAC to speaker path
// Somehow this was needed to get an input signal to the ADC, even though
// all other registers should be taken care of later. Don't know why.
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x09, 1 << 6)); // Sets up the line in to adc path
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x25, (1 << 1) | (1 << 0))); // SDOUT, SDIN enabled
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x42, 1 << 0)); // bandgap bias
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x43, 1 << 0)); // high performane mode
// Table 51. Digital Audio Interface (DAI) Format Configuration Register
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x2E, 1)); // Left DAC -> Left Speaker
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x2F, 2)); // Right DAC -> Right Speaker
//max98091_i2c_write_readback(0x2E, (1<<2) | (1<<1)); // Line A -> Left Speaker
//max98091_i2c_write_readback(0x2F, (1<<3) | (1<<0)); // LIne B -> Right Speaker
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x29, 1)); // Left DAC -> Left HP
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x2A, 2)); // Right DAC -> Right HP
// Mic bias is off
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x3E, (1<<4) |(1<<3) | (1<<2) | (1<<1) | (1<<0))); // enable micbias, line input amps, ADCs
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x0D, (1<<3) | (1<<2))); // IN3 SE -> Line A, IN4 SE -> Line B
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x15, (1<<4) )); // line B -> left ADC
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x16, (1<<3) )); // line A -> right ADC
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x44, (1<<2) | (1<<1) | (1<<0) )); // 128x oversampling, dithering, high performance ADC
max98091_i2c_write_readback(0x13, (1<<4) | (1<<5) | (1<<1) | (1<<0) ); // enable digital mic
// Enable headset mic
#if 0
max98091_i2c_write_readback(0x13, 0);
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x0F, (0<<1) | (1<<0) )); // IN5/IN6 to MIC1
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x10, (1<<6) | (1<<4) | (1<<2) )); // 20 dB gain on MIC1
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x15, (1<<5) )); // MIC1 -> left ADC
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x16, (1<<5) )); // MIC1 -> right ADC
#endif
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x3F, (1<<1) | (1<<0))); // output enable: enable dacs
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x45, 1<<7)); // power up
//max98091_i2c_write_readback(0x31, 0x2c); // 0db, no mute
//max98091_i2c_write_readback(0x32, 0x2c); // 0db, no mute
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x3F, (1<<7) | (1<<6) | (1<<5) | (1<<4) | (1<<1) | (1<<0))); // enable outputs, dacs
//max98091_i2c_write_readback(0x27, (1<<4) | (1<<5)); // full playback gain
//max98091_i2c_write_readback(0x31, 0x3f); // +14 db speaker
//max98091_i2c_write_readback(0x32, 0x3f); // +14 db speaker
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x41, 0x0));
ESP_ERROR_CHECK(max98091_i2c_write_readback(0x3D, 1<<7)); // jack detect enable
printf("4 readbacks failing here is normal dw ^w^\n");
}
static void i2s_init(void){
init_codec();
vTaskDelay(100 / portTICK_PERIOD_MS); // dunno if necessary
static const i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_TX,
.sample_rate = SAMPLE_RATE,
.bits_per_sample = 16,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
//.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB,
//^...technically wrong but works...? in idf v5 it's msb but don't try that late at night
.intr_alloc_flags = 0, // default interrupt priority
.dma_buf_count = DMA_BUFFER_COUNT,
.dma_buf_len = DMA_BUFFER_SIZE,
.use_apll = false
};
static const i2s_pin_config_t pin_config = {
.bck_io_num = 10,
.mck_io_num = 18,
.ws_io_num = 11,
.data_out_num = 12,
.data_in_num = I2S_PIN_NO_CHANGE
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
i2s_set_pin(I2S_PORT, &pin_config);
}
// irregular register value -> vol mapping taken from codec datasheet
typedef struct {
uint8_t register_value;
float volume_dB;
} vol_map_t;
const uint8_t speaker_map_len = 40;
const vol_map_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}};
const uint8_t headphones_map_len = 32;
const vol_map_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}};
void _audio_headphones_set_volume_dB(float vol_dB, bool mute){
uint8_t map_index = headphones_map_len - 1;
for(; map_index; map_index--){
if(headphones_map[map_index].volume_dB >= vol_dB) break;
}
uint8_t reg = headphones_map[map_index].register_value;
reg = (mute ? (1 << 7) : 0) | reg;
max98091_i2c_write(0x2C, reg); //left chan
max98091_i2c_write(0x2D, reg); //right chan
// note: didn't check if chan physically mapped to l/r or flipped.
// do the fine steps in software
// note: synchronizing both hw and software volume changes is somewhat tricky
float hardware_volume_dB = headphones_map[map_index].volume_dB;
float software_volume_dB = vol_dB - hardware_volume_dB;
if(software_volume_dB > 0) software_volume_dB = 0;
//if(!software_volume_enabled) software_volume_dB = 0; // breaks p1, might add option once it is retired
software_volume = (int32_t) (32768 * exp(software_volume_dB * NAT_LOG_DB));
headphones_volume_dB = hardware_volume_dB + software_volume_dB;
}
void _audio_speaker_set_volume_dB(float vol_dB, bool mute){
uint8_t map_index = speaker_map_len - 1;
for(; map_index; map_index--){
if(speaker_map[map_index].volume_dB >= vol_dB) break;
}
uint8_t reg = speaker_map[map_index].register_value;
reg = (mute ? (1 << 7) : 0) | reg;
max98091_i2c_write(0x31, reg); //left chan
max98091_i2c_write(0x32, reg); //right chan
//note: didn't check if chan physically mapped to l/r or flipped.
// do the fine steps in software
// note: synchronizing both hw and software volume changes is somewhat tricky
float hardware_volume_dB = speaker_map[map_index].volume_dB;
float software_volume_dB = vol_dB - hardware_volume_dB;
if(software_volume_dB > 0) software_volume_dB = 0;
//if(!software_volume_enabled) software_volume_dB = 0; // breaks p1, might add option once it is retired
software_volume = (int32_t) (32768. * exp(software_volume_dB * NAT_LOG_DB));
speaker_volume_dB = hardware_volume_dB + software_volume_dB;
}
void audio_headphones_set_mute(uint8_t mute){
headphones_mute = mute;
audio_headphones_set_volume_dB(headphones_volume_dB);
}
void audio_speaker_set_mute(uint8_t mute){
speaker_mute = mute;
audio_speaker_set_volume_dB(speaker_volume_dB);
}
#elif defined(CONFIG_BADGE23_HW_GEN_P1)
#define MAX_VOLUME_DB 10
#define MIN_VOLUME_DB (-80)
int32_t software_volume_premute; // ugly but this is an old prototype that will be phased out soon
static void i2s_init(void){
static const i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_TX,
.sample_rate = SAMPLE_RATE,
.bits_per_sample = 16,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
//.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = 0, // default interrupt priority
.dma_buf_count = DMA_BUFFER_COUNT,
.dma_buf_len = DMA_BUFFER_SIZE,
.use_apll = false
};
static const i2s_pin_config_t pin_config = {
.bck_io_num = 13,
.mck_io_num = 11,
.ws_io_num = 12,
.data_out_num = 14,
.data_in_num = I2S_PIN_NO_CHANGE
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
i2s_set_pin(I2S_PORT, &pin_config);
}
void _audio_speaker_set_volume_dB(float vol_dB, bool mute){
int32_t buf = 32767 * exp(vol_dB * NAT_LOG_DB);
software_volume_premute = buf;
if(mute){
software_volume = 0;
} else {
software_volume = software_volume_premute;
}
speaker_volume_dB = vol_dB;
}
void _audio_headphones_set_volume_dB(float vol_dB, bool mute){
}
void audio_headphones_set_mute(uint8_t mute){
headphones_mute = 1;
};
void audio_speaker_set_mute(uint8_t mute){
speaker_mute = mute;
if(speaker_mute){
software_volume = 0;
} else {
software_volume = software_volume_premute;
}
}
#else
#error "audio not implemented for this badge generation"
#endif
float audio_speaker_set_volume_dB(float vol_dB){
bool mute = speaker_mute || audio_headphones_are_connected();
if(vol_dB > speaker_maximum_volume_user_dB) vol_dB = speaker_maximum_volume_user_dB;
if(vol_dB < speaker_minimum_volume_user_dB){
vol_dB = SILLY_LOW_VOLUME_DB; // fake mute
mute = 1;
}
_audio_speaker_set_volume_dB(vol_dB, mute);
return speaker_volume_dB;
}
float audio_headphones_set_volume_dB(float vol_dB){
bool mute = headphones_mute || (!audio_headphones_are_connected());
if(vol_dB > headphones_maximum_volume_user_dB) vol_dB = headphones_maximum_volume_user_dB;
if(vol_dB < headphones_minimum_volume_user_dB){
vol_dB = SILLY_LOW_VOLUME_DB; // fake mute
mute = 1;
}
_audio_headphones_set_volume_dB(vol_dB, mute);
return headphones_volume_dB;
}
void audio_headphones_detection_override(uint8_t enable){
headphones_detection_override = enable;
audio_headphones_set_volume_dB(headphones_volume_dB);
audio_speaker_set_volume_dB(speaker_volume_dB);
}
float audio_headphones_adjust_volume_dB(float vol_dB){
if(audio_headphones_get_volume_dB() < headphones_minimum_volume_user_dB){ //fake mute
if(vol_dB > 0){
return audio_headphones_set_volume_dB(headphones_minimum_volume_user_dB);
} else {
return audio_headphones_get_volume_dB();
}
} else {
return audio_headphones_set_volume_dB(headphones_volume_dB + vol_dB);
}
}
float audio_speaker_adjust_volume_dB(float vol_dB){
if(audio_speaker_get_volume_dB() < speaker_minimum_volume_user_dB){ //fake mute
if(vol_dB > 0){
return audio_speaker_set_volume_dB(speaker_minimum_volume_user_dB);
printf("hi");
} else {
return audio_speaker_get_volume_dB();
}
} else {
return audio_speaker_set_volume_dB(speaker_volume_dB + vol_dB);
}
}
float audio_adjust_volume_dB(float vol_dB){
if(audio_headphones_are_connected()){
return audio_headphones_adjust_volume_dB(vol_dB);
} else {
return audio_speaker_adjust_volume_dB(vol_dB);
}
}
float audio_set_volume_dB(float vol_dB){
if(audio_headphones_are_connected()){
return audio_headphones_set_volume_dB(vol_dB);
} else {
return audio_speaker_set_volume_dB(vol_dB);
}
}
float audio_get_volume_dB(){
if(audio_headphones_are_connected()){
return audio_headphones_get_volume_dB();
} else {
return audio_speaker_get_volume_dB();
}
}
void audio_set_mute(uint8_t mute){
if(audio_headphones_are_connected()){
audio_headphones_set_mute(mute);
} else {
audio_speaker_set_mute(mute);
}
}
uint8_t audio_get_mute(){
if(audio_headphones_are_connected()){
return audio_headphones_get_mute();
} else {
return audio_speaker_get_mute();
}
}
float audio_headphones_set_maximum_volume_dB(float vol_dB){
if(vol_dB > headphones_maximum_volume_system_dB) vol_dB = headphones_maximum_volume_system_dB;
if(vol_dB < headphones_minimum_volume_user_dB) vol_dB = headphones_minimum_volume_user_dB;
headphones_maximum_volume_user_dB = vol_dB;
return headphones_maximum_volume_user_dB;
}
float audio_headphones_set_minimum_volume_dB(float vol_dB){
if(vol_dB > headphones_maximum_volume_user_dB) vol_dB = headphones_maximum_volume_user_dB;
if((vol_dB + 1) < SILLY_LOW_VOLUME_DB) vol_dB = SILLY_LOW_VOLUME_DB + 1.;
headphones_minimum_volume_user_dB = vol_dB;
return headphones_minimum_volume_user_dB;
}
float audio_speaker_set_maximum_volume_dB(float vol_dB){
if(vol_dB > speaker_maximum_volume_system_dB) vol_dB = speaker_maximum_volume_system_dB;
if(vol_dB < speaker_minimum_volume_user_dB) vol_dB = speaker_minimum_volume_user_dB;
speaker_maximum_volume_user_dB = vol_dB;
return speaker_maximum_volume_user_dB;
}
float audio_speaker_set_minimum_volume_dB(float vol_dB){
if(vol_dB > speaker_maximum_volume_user_dB) vol_dB = speaker_maximum_volume_user_dB;
if((vol_dB + 1) < SILLY_LOW_VOLUME_DB) vol_dB = SILLY_LOW_VOLUME_DB + 1.;
speaker_minimum_volume_user_dB = vol_dB;
return speaker_minimum_volume_user_dB;
}
float audio_speaker_get_volume_relative(){
float ret = audio_speaker_get_volume_dB();
if(ret < speaker_minimum_volume_user_dB) return 0; // fake mute
float vol_range = speaker_maximum_volume_user_dB - speaker_minimum_volume_user_dB;
ret -= speaker_minimum_volume_user_dB; // shift to above zero
ret /= vol_range; // restrict to 0..1 range
return (ret*0.99) + 0.01; // shift to 0.01 to 0.99 range to distinguish from fake mute
}
float audio_headphones_get_volume_relative(){
float ret = audio_headphones_get_volume_dB();
if(ret < headphones_minimum_volume_user_dB) return 0; // fake mute
float vol_range = headphones_maximum_volume_user_dB - headphones_minimum_volume_user_dB;
ret -= headphones_minimum_volume_user_dB; // shift to above zero
ret /= vol_range; // restrict to 0..1 range
return (ret*0.99) + 0.01; // shift to 0.01 to 0.99 range to distinguish from fake mute
}
float audio_get_volume_relative(){
if(audio_headphones_are_connected()){
return audio_headphones_get_volume_relative();
} else {
return audio_speaker_get_volume_relative();
}
}
void audio_update_jacksense(){
#if defined(CONFIG_BADGE23_HW_GEN_P1)
line_in_connected = 0;
#elif defined(CONFIG_BADGE23_HW_GEN_P3) || defined(CONFIG_BADGE23_HW_GEN_P4)
line_in_connected = 1;
#elif defined(CONFIG_BADGE23_HW_GEN_P6)
line_in_connected = 0; //TODO: read port expander
#endif
#if defined(CONFIG_BADGE23_HW_GEN_P1)
headphones_connected = 0;
headset_connected = 0;
#elif defined(CONFIG_BADGE23_HW_GEN_P3) || defined(CONFIG_BADGE23_HW_GEN_P4) || defined(CONFIG_BADGE23_HW_GEN_P6)
static uint8_t jck_prev = 255; // unreachable value -> initial comparision always untrue
uint8_t jck = max98091_i2c_read(0x02);
if(jck == 6){
headphones_connected = 0;
headset_connected = 0;
} else if(jck == 0){
headphones_connected = 1;
headset_connected = 0;
} else if(jck == 2){
headphones_connected = 1;
headset_connected = 1;
}
if(jck != jck_prev){ // update volume to trigger mutes if needed
audio_speaker_set_volume_dB(speaker_volume_dB);
audio_headphones_set_volume_dB(headphones_volume_dB);
}
jck_prev = jck;
#endif
}
typedef struct _audio_source_t{
void * render_data;
float (* render_function)(void *);
uint16_t index;
struct _audio_source_t * next;
} audio_source_t;
static audio_source_t * _audio_sources = NULL;
uint16_t add_audio_source(void * render_data, void * render_function){
//construct audio source struct
audio_source_t * src = malloc(sizeof(audio_source_t));
if(src == NULL) return;
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){
_audio_sources = src;
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
} else {
index_source = index_source->next;
}
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->next = src;
break;
} else {
audio_source = audio_source->next;
}
}
return src->index;
}
void remove_audio_source(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){
_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
break;
}
start_gap = audio_source;
audio_source = audio_source->next;
}
}
uint16_t count_audio_sources(){
uint16_t ret = 0;
audio_source_t * audio_source = _audio_sources;
while(audio_source != NULL){
audio_source = audio_source->next;
ret++;
}
return ret;
}
static void _audio_init(void) {
// TODO: this assumes I2C is already initialized
init_scope(241);
i2s_init();
audio_update_jacksense();
//ESP_ERROR_CHECK(i2s_channel_enable(tx_chan));
TaskHandle_t handle;
xTaskCreate(&audio_player_task, "Audio player", 3000, NULL, configMAX_PRIORITIES - 1, &handle);
}
static void audio_player_task(void* arg) {
int16_t buffer[DMA_BUFFER_SIZE * 2];
memset(buffer, 0, sizeof(buffer));
while(true) {
for(int i = 0; i < (DMA_BUFFER_SIZE * 2); i += 2){
float sample = 0;
audio_source_t * audio_source = _audio_sources;
while(audio_source != NULL){
sample += (*(audio_source->render_function))(audio_source->render_data);
audio_source = audio_source->next;
}
write_to_scope((int16_t) (1600. * sample));
sample = software_volume * (sample/10);
if(sample > 32767) sample = 32767;
if(sample < -32767) sample = -32767;
buffer[i] = (int16_t) sample;
buffer[i+1] = buffer[i];
}
size_t count = 0;
i2s_write(I2S_PORT, buffer, sizeof(buffer), &count, 1000);
if (count != sizeof(buffer)) {
printf("i2s_write_bytes: count (%d) != length (%d)\n", count, sizeof(buffer));
abort();
}
}
}
void audio_init() { _audio_init(); }
//#include <stdio.h>
//#include <string.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include <stdint.h>
#include <freertos/FreeRTOS.h>
#include <freertos/atomic.h>
#include "badge23/lock.h"
#define PETAL_PAD_TIP 0
#define PETAL_PAD_CCW 1
#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 AFE_INCR_CAP 1000
#if defined(CONFIG_BADGE23_HW_GEN_P3) || defined(CONFIG_BADGE23_HW_GEN_P4) || defined(CONFIG_BADGE23_HW_GEN_P6)
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};
#define DEFAULT_THRES_TOP 8000
#define DEFAULT_THRES_BOT 12000
#elif defined(CONFIG_BADGE23_HW_GEN_P1)
static const uint8_t top_map[] = {2, 2, 2, 0, 0, 8, 8, 8, 6, 6, 4, 4};
static const uint8_t top_stages = 12;
static const uint8_t bot_map[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9};
static const uint8_t bot_stages = 10;
static const uint8_t top_segment_map[] = {1,2,0,1,2,1,2,0,1,2,1,2}; //idk
static const uint8_t bot_segment_map[] = {3,0,3,0,3,0,0,3,0,3}; //idk
static const uint8_t bot_stage_config[] = {0,1,2,3,4,5,6,7,8,9,10,11};
#define DEFAULT_THRES_TOP 2000
#define DEFAULT_THRES_BOT 12000
#define AD7147_ADDR_TOP 0b101101
#define AD7147_ADDR_BOT 0b101100
#else
#error "captouch not implemented for this badge generation"
#endif
#if defined(CONFIG_BADGE23_HW_GEN_P4)
#define AD7147_ADDR_TOP 0b101100
#define AD7147_ADDR_BOT 0b101101
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_BADGE23_HW_GEN_P6)
#define AD7147_ADDR_TOP 0b101100
#define AD7147_ADDR_BOT 0b101101
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_BADGE23_HW_GEN_P3)
#define AD7147_ADDR_TOP 0b101101
#define AD7147_ADDR_BOT 0b101100
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 I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
#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 PETAL_PRESSED_DEBOUNCE 2
static struct ad714x_chip *chip_top;
static struct ad714x_chip *chip_bot;
typedef struct{
uint8_t config_mask;
uint16_t amb_values[4]; //ordered according to PETAL_PAD_*
uint16_t cdc_values[4]; //ordered according to PETAL_PAD_*
uint16_t thres_values[4]; //ordered according to PETAL_PAD_*
uint8_t pressed;
} petal_t;
static petal_t petals[10];
struct ad714x_chip {
uint8_t addr;
uint8_t gpio;
int pos_afe_offsets[13];
int neg_afe_offsets[13];
int neg_afe_offset_swap;
int stages;
};
static struct ad714x_chip chip_top_rev5 = {.addr = AD7147_ADDR_TOP, .gpio = 15,
.pos_afe_offsets = {4, 2, 2, 2, 2, 3, 4, 2, 2, 2, 2, 0},
.stages=top_stages};
static struct ad714x_chip chip_bot_rev5 = {.addr = AD7147_ADDR_BOT, .gpio = 15,
.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)
{
const uint8_t tx[] = {reg >> 8, reg & 0xFF, data >> 8, data & 0xFF};
xSemaphoreTake(mutex_i2c, portMAX_DELAY);
ESP_LOGI(TAG, "AD7147 write reg %X-> %X", reg, data);
xSemaphoreGive(mutex_i2c);
return i2c_master_write_to_device(I2C_MASTER_NUM, 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)
{
const uint8_t tx[] = {reg >> 8, reg & 0xFF};
uint8_t rx[len * 2];
xSemaphoreTake(mutex_i2c, portMAX_DELAY);
esp_err_t ret = i2c_master_write_read_device(I2C_MASTER_NUM, chip->addr, tx, sizeof(tx), rx, sizeof(rx), TIMEOUT_MS / portTICK_PERIOD_MS);
xSemaphoreGive(mutex_i2c);
for(int i = 0; i < len; i++) {
data[i] = (rx[i * 2] << 8) | rx[i * 2 + 1];
}
return ret;
}
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;
};
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]);
ad714x_i2c_write(chip, bank2 + stage * 8, connection_6_0);
ad714x_i2c_write(chip, bank2 + stage * 8 + 1, connection_12_7);
ad714x_i2c_write(chip, bank2 + stage * 8 + 2, afe_offset);
ad714x_i2c_write(chip, bank2 + stage * 8 + 3, sensitivity);
}
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;
};
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);
}
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){
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;
} else {
stage_config.cinX_connection_setup[stage] = CIN_CDC_POS;
}
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){
int8_t sat = 0;
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;
afe += delta_afe;
if(afe >= 63) afe = 63;
if(afe <= -63)afe = -63;
if(afe>0){
chip->pos_afe_offsets[stage] = afe;
chip->neg_afe_offsets[stage] = 0;
} else {
chip->pos_afe_offsets[stage] = 0;
chip->neg_afe_offsets[stage] = -afe;
}
captouch_configure_stage(chip, stage);
return sat;
}
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++) {
captouch_configure_stage(chip, i);
}
}
static void captouch_init_petals(){
for(int i = 0; i < 10; i++){
for(int j = 0; j < 4; j++){
petals[i].amb_values[j] = 0;
petals[i].cdc_values[j] = 0;
if(i%2){
petals[i].thres_values[j] = DEFAULT_THRES_BOT;
} else {
petals[i].thres_values[j] = DEFAULT_THRES_TOP;
}
}
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 < 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;
uint8_t cf = petals[petal].config_mask;
if(cf == 0b1110){ //CCW, CW, BASE
int32_t left = petals[petal].cdc_values[PETAL_PAD_CCW];
left -= petals[petal].amb_values[PETAL_PAD_CCW];
int32_t right = petals[petal].cdc_values[PETAL_PAD_CW];
right -= petals[petal].amb_values[PETAL_PAD_CW];
int32_t base = petals[petal].cdc_values[PETAL_PAD_BASE];
base -= petals[petal].amb_values[PETAL_PAD_BASE];
return (left + right)/2 - base;
}
if(cf == 0b111){ //CCW, CW, TIP
int32_t left = petals[petal].cdc_values[PETAL_PAD_CCW];
left -= petals[petal].amb_values[PETAL_PAD_CCW];
int32_t right = petals[petal].cdc_values[PETAL_PAD_CW];
right -= petals[petal].amb_values[PETAL_PAD_CW];
int32_t tip = petals[petal].cdc_values[PETAL_PAD_TIP];
tip -= petals[petal].amb_values[PETAL_PAD_TIP];
return (-left - right)/2 + tip;
}
if(cf == 0b1001){ //TIP, BASE
int32_t tip = petals[petal].cdc_values[PETAL_PAD_TIP];
tip -= petals[petal].amb_values[PETAL_PAD_TIP];
int32_t base = petals[petal].cdc_values[PETAL_PAD_BASE];
base -= petals[petal].amb_values[PETAL_PAD_BASE];
return tip - base;
}
if(cf == 0b1){ //TIP
int32_t tip = petals[petal].cdc_values[PETAL_PAD_TIP];
tip -= petals[petal].amb_values[PETAL_PAD_TIP];
return tip;
}
return 0;
}
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)
int32_t left = petals[petal].cdc_values[PETAL_PAD_CCW];
left -= petals[petal].amb_values[PETAL_PAD_CCW];
int32_t right = petals[petal].cdc_values[PETAL_PAD_CW];
right -= petals[petal].amb_values[PETAL_PAD_CW];
return left - right;
}
return 0;
}
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_bot, (struct ad7147_device_config){.sequence_stage_num = 11,
.decimation = 1,
});
}
uint16_t read_captouch(){
uint16_t bin_petals = 0;
for(int i = 0; i < 10; i++) {
if(petals[i].pressed){
bin_petals |= (1<<i);
}
}
return bin_petals;
}
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
Atomic_Increment_u32(&calib_active);
}
}
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++){
bool pressed = 0;
bool prev = petals[i].pressed;
for(int j = 0; j < 4; j++){
if((petals[i].amb_values[j] +
petals[i].thres_values[j]) <
petals[i].cdc_values[j]){
pressed = 1;
}
}
if(pressed){
petals[i].pressed = PETAL_PRESSED_DEBOUNCE;
} else if(petals[i].pressed){
petals[i].pressed--;
}
if(petals[i].pressed && (!prev)){
// TODO: PETAL_PRESS_CALLBACK
}
if((!petals[i].pressed) && prev){
// TODO: PETAL_RELEASE_CALLBACK
}
}
}
void cdc_to_petal(bool bot, bool amb, uint16_t cdc_data[], uint8_t cdc_data_length){
if(!bot){
for(int i = 0; i < cdc_data_length; i++){
if(amb){
petals[top_map[i]].amb_values[top_segment_map[i]] = cdc_data[i];
} else {
petals[top_map[i]].cdc_values[top_segment_map[i]] = cdc_data[i];
}
}
} else {
for(int i = 0; i < cdc_data_length; i++){
if(amb){
petals[bot_map[i]].amb_values[bot_segment_map[i]] = cdc_data[i];
} else {
petals[bot_map[i]].cdc_values[bot_segment_map[i]] = cdc_data[i];
}
}
}
}
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].cdc_values[pad];
}
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].amb_values[pad];
}
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].amb_values[pad] < petals[petal].cdc_values[pad]){
return petals[petal].cdc_values[pad] - petals[petal].amb_values[pad];
}
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].thres_values[pad] = thres;
}
static int32_t calib_target = 6000;
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;
}
}
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;
}
}
}
}
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);
check_petals_pressed();
}
}
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) {
data = cdc_data[0];
ambient = cdc_ambient[0];
} else {
data = cdc_data[1];
ambient = cdc_ambient[1];
}
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)
{
captouch_print_debug_info_chip(chip_top);
captouch_print_debug_info_chip(chip_bot);
}
#include "badge23/display.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "../../usermodule/uctx/uctx/ctx.h"
#include "flow3r_bsp.h"
#include "st3m_gfx.h"
typedef struct {
Ctx *ctx;
st3m_framebuffer_desc_t *desc;
} ctx_target_t;
static void new_ctx_target(ctx_target_t *target, st3m_framebuffer_desc_t *desc) {
Ctx *ctx = ctx_new_for_framebuffer(
desc->buffer,
FLOW3R_BSP_DISPLAY_WIDTH,
FLOW3R_BSP_DISPLAY_HEIGHT,
FLOW3R_BSP_DISPLAY_WIDTH * 2,
CTX_FORMAT_RGB565_BYTESWAPPED
);
assert(ctx != NULL);
int32_t offset_x = FLOW3R_BSP_DISPLAY_WIDTH / 2;
int32_t offset_y = FLOW3R_BSP_DISPLAY_HEIGHT / 2;
// rotate by 180 deg and translate x and y by 120 px to have (0,0) at the center of the screen
ctx_apply_transform(ctx,-1,0,offset_x,0,-1,offset_y,0,0,1);
target->ctx = ctx;
target->desc = desc;
}
static ctx_target_t targets[ST3M_GFX_NBUFFERS];
static void display_loading_splash(ctx_target_t *target) {
ctx_rgb(target->ctx, 0.157, 0.129, 0.167);
ctx_rectangle(target->ctx, -120, -120, 240, 240);
ctx_fill(target->ctx);
ctx_move_to(target->ctx, 0, 0);
ctx_rgb(target->ctx, 0.9, 0.9, 0.9);
ctx_text_align(target->ctx, CTX_TEXT_ALIGN_CENTER);
ctx_text_baseline(target->ctx, CTX_TEXT_BASELINE_ALPHABETIC);
ctx_text(target->ctx, "Loading...");
}
static ctx_target_t *next_target(void) {
st3m_framebuffer_desc_t *desc = st3m_gfx_framebuffer_get(portMAX_DELAY);
if (targets[desc->num].ctx == NULL) {
new_ctx_target(&targets[desc->num], desc);
}
return &targets[desc->num];
}
static bool initialized = false;
static ctx_target_t *sync_target = NULL;
void display_init() {
// HACK: needed until we have new async gfx api.
assert(ST3M_GFX_NBUFFERS == 1);
st3m_gfx_init();
for (int i = 0; i < ST3M_GFX_NBUFFERS; i++) {
targets[i].ctx = NULL;
targets[i].desc = NULL;
}
ctx_target_t *tgt = next_target();
display_loading_splash(tgt);
st3m_gfx_framebuffer_queue(tgt->desc);
sync_target = next_target();
flow3r_bsp_display_set_backlight(100);
initialized = true;
}
void display_update(){
if (!initialized) {
return;
}
st3m_gfx_framebuffer_queue(sync_target->desc);
ctx_target_t *tgt = next_target();
assert(tgt == sync_target);
}
void display_set_backlight(uint8_t percent) {
if (!initialized) {
return;
}
flow3r_bsp_display_set_backlight(percent);
}
Ctx *display_global_ctx(void) {
if (!initialized) {
return NULL;
}
return sync_target->ctx;
}
\ No newline at end of file
#include "badge23/captouch.h"
#include "badge23/audio.h"
#include "badge23/leds.h"
#include "badge23/display.h"
#include "badge23/spio.h"
#include "badge23/lock.h"
#include "flow3r_bsp.h"
#include "esp_log.h"
#include "driver/i2c.h"
#include "driver/spi_master.h"
#include <freertos/timers.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
static const char *TAG = "espan";
#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#if defined(CONFIG_BADGE23_HW_GEN_P3) || defined(CONFIG_BADGE23_HW_GEN_P4) || defined(CONFIG_BADGE23_HW_GEN_P6)
#define CONFIG_I2C_MASTER_SDA 2
#define CONFIG_I2C_MASTER_SCL 1
#elif defined(CONFIG_BADGE23_HW_GEN_P1)
#define CONFIG_I2C_MASTER_SDA 10
#define CONFIG_I2C_MASTER_SCL 9
#else
#error "i2c not implemented for this badge generation"
#endif
static QueueHandle_t i2c_queue = NULL;
static QueueHandle_t slow_system_status_queue = NULL;
static uint8_t dummy_data;
static esp_err_t i2c_master_init(void)
{
int i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = CONFIG_I2C_MASTER_SDA,
.scl_io_num = CONFIG_I2C_MASTER_SCL,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
i2c_param_config(i2c_master_port, &conf);
return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}
#define CAPTOUCH_POLLING_PERIOD 10
#define SLOW_SYSTEM_STATUS_PERIOD 100
static uint8_t hw_init_done = 0;
void i2c_timer(TimerHandle_t data){
xQueueSend(i2c_queue, &dummy_data, 0);
}
void i2c_task(void * data){
while(1){
xQueueReceive(i2c_queue, &dummy_data, portMAX_DELAY);
captouch_read_cycle();
update_button_state();
}
}
void slow_system_status_timer(TimerHandle_t data){
xQueueSend(slow_system_status_queue, &dummy_data, 0);
}
void slow_system_status_task(void * data){
while(1){
xQueueReceive(slow_system_status_queue, &dummy_data, portMAX_DELAY);
//read out stuff like jack detection, battery status, usb connection etc.
audio_update_jacksense();
leds_update_hardware();
}
}
void locks_init(){
mutex_i2c = xSemaphoreCreateMutex();
mutex_LED = xSemaphoreCreateMutex();
}
void os_app_main(void)
{
// Initialize display first as that gives us a nice splash screen.
display_init();
locks_init();
ESP_LOGI(TAG, "Starting on %s...", flow3r_bsp_hw_name);
ESP_ERROR_CHECK(i2c_master_init());
ESP_LOGI(TAG, "I2C initialized successfully");
audio_init();
leds_init();
init_buttons();
captouch_init();
spio_badge_link_disable(255);
captouch_force_calibration();
i2c_queue = xQueueCreate(1,1);
TaskHandle_t i2c_task_handle;
xTaskCreatePinnedToCore(&i2c_task, "I2C task", 4096, NULL, configMAX_PRIORITIES-1, &i2c_task_handle, 0);
TimerHandle_t i2c_timer_handle = xTimerCreate("I2C timer", pdMS_TO_TICKS(CAPTOUCH_POLLING_PERIOD), pdTRUE, (void *) 0, *i2c_timer);
if( xTimerStart(i2c_timer_handle, 0 ) != pdPASS) ESP_LOGI(TAG, "I2C timer initialization failed");
slow_system_status_queue = xQueueCreate(1,1);
TaskHandle_t slow_system_status_task_handle;
xTaskCreatePinnedToCore(&slow_system_status_task, "slow system status task", 4096, NULL, configMAX_PRIORITIES-2, &slow_system_status_task_handle, 0);
TimerHandle_t slow_system_status_timer_handle = xTimerCreate("slow system status timer", pdMS_TO_TICKS(SLOW_SYSTEM_STATUS_PERIOD), pdTRUE, (void *) 0, *slow_system_status_timer);
if( xTimerStart(slow_system_status_timer_handle, 0 ) != pdPASS) ESP_LOGI(TAG, "I2C task initialization failed");
hw_init_done = 1;
}
uint8_t hardware_is_initialized(){
return hw_init_done;
}
#ifndef DEF_apa102LEDStrip
#define DEF_apa102LEDStrip
struct apa102LEDStrip
{
unsigned char *LEDs;
short int _frameLength;
short int _endFrameLength;
short int _numLEDs;
unsigned char _bytesPerLED;
short int _counter;
unsigned char _globalBrightness;
};
void initLEDs(struct apa102LEDStrip *ledObject, short int numLEDs, unsigned char bytesPerLED, unsigned char globalBrightness);
void setPixel(struct apa102LEDStrip *ledObject, short int pixelIndex, unsigned char *pixelColour);
void getPixel(struct apa102LEDStrip *ledObject, short int pixelIndex, unsigned char *pixelColour);
#endif
#pragma once
#include <stdint.h>
#define SAMPLE_RATE 16000
/* Initializes I2S bus, the audio task and required data structures.
* Expects an initialized I2C bus, will fail ungracefully otherwise (TODO).
* Requires the I2C lock.
*/
void audio_init();
uint16_t count_audio_sources();
uint16_t add_audio_source(void * render_data, void * render_function);
void remove_audio_source(uint16_t index);
/* Polls hardware to check if headphones, headset or line in are plugged
* into the 3.5mm jacks. If it detects a plug in the headphone jack, speakers
* are automatically muted. There is no override for this at this moment.
*
* Should be called periodically (100ms ish?) by a low priority task. Requires
* the I2C lock.
*/
void audio_update_jacksense(void);
/* Returns 1 if headphones with or without microphone were connected to the
* headphone jack at the last call of audio_update_jacksense.
*/
uint8_t audio_headphones_are_connected();
/* Returns 1 if headphones with microphone were connected to the headphone jack
* at the last call of audio_update_jacksense.
*/
uint8_t audio_headset_is_connected();
/* If a sleeve contact mic doesn't pull the detection pin low enough the
* codec's built in headphone detection might fail. Calling this function
* with 'enable = 1' overrides the detection and assumes there's headphones
* plugged in. Call with 'enable = 0' to revert to automatic detection.
*/
void audio_headphones_detection_override(uint8_t enable);
/* Attempts to set target volume for the headphone output/onboard speakers
* respectively, clamps/rounds if necessary and returns the actual volume.
* Absolute reference arbitrary.
* Does not unmute, use audio_{headphones_/speaker_/}set_mute as needed.
* Enters fake mute if requested volume is below the value set by
* audio_{headphones/speaker}_set_minimum_volume_user.
*
* Note: This function uses a hardware PGA for the coarse value and software
* for the fine value. These two methods are as of yet not synced so that there
* may be a transient volume "hiccup". "p1" badges only use software volume.
* The unspecified variant automatically chooses the adequate channel (**).
*/
float audio_headphones_set_volume_dB(float vol_dB);
float audio_speaker_set_volume_dB(float vol_dB);
float audio_set_volume_dB(float vol_dB);
/* Like the audio_{headphones_/speaker_/}set_volume family but changes relative
* to last volume value.
*/
float audio_headphones_adjust_volume_dB(float vol_dB);
float audio_speaker_adjust_volume_dB(float vol_dB);
float audio_adjust_volume_dB(float vol_dB);
/* Returns volume as set with audio_{headphones/speaker}_set_volume_dB.
* The unspecified variant automatically chooses the adequate channel (**).
*/
float audio_headphones_get_volume_dB();
float audio_speaker_get_volume_dB();
float audio_get_volume_dB();
/* Mutes (mute = 1) or unmutes (mute = 0) the specified channel.
* The unspecified variant automatically chooses the adequate channel (**).
*
* Note: Even if a channel is unmuted it might not play sound depending on
* the return value of audio_headphone_are_connected. There is no override for
* this (see HEADPHONE PORT POLICY below).
*/
void audio_headphones_set_mute(uint8_t mute);
void audio_speaker_set_mute(uint8_t mute);
void audio_set_mute(uint8_t mute);
/* Returns 1 if channel is muted, 0 if channel is unmuted.
* The unspecified variant automatically chooses the adequate channel (**).
*/
uint8_t audio_headphones_get_mute();
uint8_t audio_speaker_get_mute();
uint8_t audio_get_mute();
/* 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.
*/
float audio_headphones_set_minimum_volume_dB(float vol_dB);
float audio_headphones_set_maximum_volume_dB(float vol_dB);
float audio_speaker_set_minimum_volume_dB(float vol_dB);
float audio_speaker_set_maximum_volume_dB(float vol_dB);
/* Returns the minimum and maximum allowed volume levels for speakers and headphones
* respectively. Change with audio_{headphones/speaker}_set_{minimum/maximum}_volume_dB.
*/
float audio_headphones_get_minimum_volume_dB();
float audio_headphones_get_maximum_volume_dB();
float audio_speaker_get_minimum_volume_dB();
float audio_speaker_get_maximum_volume_dB();
/* Syntactic sugar for drawing UI: Returns channel volume in a 0..1 range,
* scaled into a 0.01..1 range according to the values set with
* audio_{headphones_/speaker_/}set_{maximum/minimum}_volume_ and 0 if in a
* fake mute condition.
*
* The unspecified variant automatically chooses the adequate channel (**).
*/
float audio_headphones_get_volume_relative();
float audio_speaker_get_volume_relative();
float audio_get_volume_relative();
/* (**) if audio_headphones_are_connected returns 1 the "headphone" variant
* is chosen, else the "speaker" variant is chosen.
*/
/*
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.
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):
1) Badge link TX cannot be enabled for any of the headphone jack pins without a
cable detected in the jack. This is to protect users from plugging in headphones
while badge link is active and receiving a short but potentially very loud burst
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*.
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.)
*/
#pragma once
#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.
*
* captouch data: full uint16_t range per stage, higher values indicate touch.
* pad index: defined in captouch.c
*
*
* IOU: the internals are still subject to major restructuring and are not
* documented as of yet. will do once the data structures actually make sense
* and are not duct tape upon duct tape.
*/
/* polls data from both captouch chips and processes it, either by updating
* the captouch data exposed to the user or running a calibration cycle.
*
* the captouch chips has updated their registers every 9.2ms, so the fn
* should be called every 10ms or so.
*
* this will be replaced someday by an interrupt event triggered system,
* but this would ideally already implement configuration switching to
* optimize latency by grouping pads and to expose the unused pad which
* is a nontrivial task for another day.
*/
void captouch_read_cycle(void);
/* the captouch chip can generate an "afe" offset in the analog domain before
* 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/
*
* default target: 6000, manufacturer recommendation: 32676
*/
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.
*/
void captouch_force_calibration();
/* indicates if a calibration cycle is currently active. the readings for
* captouch_read_cycle and captouch_get_petal_* during a calibration cycle.
*
* 1: calibration cycle active
* 0: calibration cycle inactive
*/
uint8_t captouch_calibration_active();
/* returns uint16_t which encodes each petal "touched" state as the bit
* corresponding to the petal index. "touched" is determined by checking if
* any of the pads belonging to the petal read a value higher than their
* calibration value plus their threshold (see captouch_set_petal_pad_threshold)
*/
uint16_t read_captouch(void);
/* sets threshold for a petal pad above which read_captouch considers a pad as
* touched.
*
* petal: petal index
* pad: pad index
* thres: threshold value
*/
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.
*
* petal: petal index
* pad: pad index
*/
uint16_t captouch_get_petal_pad_raw(uint8_t petal, uint8_t pad);
/* returns calibration reference for a petal pad.
*
* petal: petal index
* pad: pad index
*/
uint16_t captouch_get_petal_pad_calib_ref(uint8_t petal, uint8_t pad);
/* returns calibrated value from petal. clamps below 0.
*
* petal: petal index
* pad: pad index
*/
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
*/
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);
/* TODO: didn't look into what it does, never used it, just copied it from
* the hardware verification firmware along with the rest. didn't break it
* _intentionally_ but never tested it either.
*/
void captouch_print_debug_info(void);
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <stdint.h>
#include "esp_err.h"
#define IMAGE_W 240
#define IMAGE_H 240
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Decode the jpeg ``image.jpg`` embedded into the program file into pixel data.
*
* @param pixels A pointer to a pointer for an array of rows, which themselves are an array of pixels.
* Effectively, you can get the pixel data by doing ``decode_image(&myPixels); pixelval=myPixels[ypos][xpos];``
* @return - ESP_ERR_NOT_SUPPORTED if image is malformed or a progressive jpeg file
* - ESP_ERR_NO_MEM if out of memory
* - ESP_OK on succesful decode
*/
esp_err_t decode_image(uint16_t **pixels);
#ifdef __cplusplus
}
#endif
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "../../usermodule/uctx/uctx/ctx.h"
void display_init();
void display_set_backlight(uint8_t percent);
// Transitional functionality operating on global framebuffer and global ctx.
// Will get replaced with 'real' ctx rendering system.
void display_ctx_init();
void display_update();
Ctx *display_global_ctx(void);
#pragma once
#include <stdint.h>
void os_app_main(void);
uint8_t hardware_is_initialized(void);
#pragma once
#include <stdint.h>
#include <stdbool.h>
/* inits stuff ig
*/
void leds_init();
/* Set a single/all LEDs to change color with the next (auto) update. Index starts above
* USB-C port and increments ccw. There are 8 LEDs per top petal.
*/
void leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void leds_set_single_hsv(uint8_t index, float hue, float sat, float value);
void leds_set_all_rgb(uint8_t red, uint8_t green, uint8_t blue);
void leds_set_all_hsv(float hue, float sat, float value);
/* Set/get global LED brightness. Default 69.
*/
void leds_set_brightness(uint8_t brightness);
uint8_t 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.
*/
void leds_set_slew_rate(uint8_t slew_rate);
uint8_t leds_get_slew_rate();
/* Update LEDs.
*/
void leds_update();
/* Used internally for slew rate animation loop.
*/
void leds_update_hardware();
/* 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 leds_set_gamma(float red, float green, float blue);
/* leds_update is periodically called by the background task for slew rate animation when
* set to 1. This adds user changes immediately. Useful with low slew rates.
*/
void leds_set_auto_update(bool on);
bool leds_get_auto_update();
#pragma once
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
SemaphoreHandle_t mutex_i2c;
SemaphoreHandle_t mutex_LED;
#pragma once
#include <stdint.h>
typedef struct {
int16_t * buffer;
int16_t buffer_size;
int16_t write_head_position; // last written value
// atomic value used to prevent simultaneous reads/writes (not a mutex!).
volatile uint32_t is_being_read;
} scope_t;
void init_scope(uint16_t size);
void write_to_scope(int16_t value);
void begin_scope_read();
void end_scope_read();
void read_line_from_scope(uint16_t * line, int16_t point);
#pragma once
#include <stdint.h>
#define BUTTON_PRESSED_DOWN 2
#define BUTTON_PRESSED_LEFT -1
#define BUTTON_PRESSED_RIGHT 1
#define BUTTON_NOT_PRESSED 0
#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_INDEX_LINE_IN_TIP 4
#define BADGE_LINK_PIN_INDEX_LINE_IN_RING 5
#if defined(CONFIG_BADGE23_HW_GEN_P6)
#define BADGE_LINK_PIN_INDEX_LINE_OUT_TIP 7
#define BADGE_LINK_PIN_INDEX_LINE_OUT_RING 6
#else
#define BADGE_LINK_PIN_INDEX_LINE_OUT_TIP 6
#define BADGE_LINK_PIN_INDEX_LINE_OUT_RING 7
#endif
/* Initializes GPIO modes, prefills structs, etc. Call before using library.
*/
void init_buttons();
/* Gets data from I2C portexpanders and GPIOs. Requires I2C lock.
*/
void update_button_state();
/* 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
*/
void spio_menu_button_set_left(bool left);
/* Gets user menu button user preference as set with spio_menu_button_set_left.
*/
int8_t spio_menu_button_get_left();
/* Read the state of the menu/application button at the last call of update_button_state.
* Compare with BUTTON_(NOT)PRESSED* constants for -h.
*/
int8_t spio_menu_button_get();
int8_t spio_application_button_get();
/* Read the state of the left/right button at the last call of update_button_state.
* Compare with BUTTON_(NOT)PRESSED* constants for -h.
* This ignores user preference and should be used only with good reason.
*/
int8_t spio_left_button_get();
int8_t spio_right_button_get();
/* 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 spio_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 spio_badge_link_get_active after execution.
*/
uint8_t spio_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 spio_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.
*/
uint8_t spio_badge_link_enable(uint8_t pin_mask);
#pragma once
#include <stdint.h>
#include <stdio.h>
#define TRAD_OSC_DECAY_STEP 0.01
#define TRAD_OSC_ATTACK_STEP 0.01
#define TRAD_OSC_MIN_ATTACK_ENV 0.01
#define TRAD_OSC_ATTACK_POP_BLOCK 16
typedef struct {
//user variables
float freq; //in hertz, negative frequencies for linFM allowed
float bend;
float vol; //output volume
float env;
uint8_t env_phase; //0: off, 1: attack, 2: hold, 3: decay
uint8_t skip_hold;
float gate; //below what volume the oscillator stops and returns 0
uint16_t decay_steps; //after how many sample rate steps the volume reduces
//by factor TRAD_OSC_DECAY_STEP, set 0 for no decay
uint16_t attack_steps;
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
float counter; //state of central sawtooth oscillator, [-1..1] typ.
uint16_t env_counter;
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
uint16_t noise_reg;
} trad_osc_t;
//#define KS_BUFFER_SIZE (SAMPLE_RATE)/20
#define KS_BUFFER_SIZE 800
typedef struct {
//user variables
float freq; //frequency in hertz, negative frequencies are rectified,
//minimum freq determined by KS_BUFFER_SIZE
float feedback; //feedback value, will be compensated with frequency
//for equal decay across spectrum, [-1..1] without
//internal data storage, not for user access
float tape[KS_BUFFER_SIZE]; //the delay chain
float real_feedback; //compensated feedback value
} ks_osc_t; //karplus strong
float 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(trad_osc_t * osc, uint16_t attack);
void trad_osc_set_decay(trad_osc_t * osc, uint16_t decay);
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 render_audio(
instrument_descriptor_t instrument_descriptor,
instrument_t instance,
unsigned int sample_count,
float * output_vector
){
for(unsigned int i = 0; i < sample_count; i++){
instrument_descriptor->render_audio_sample(instance, sample_count, output_vector[2*i]);
}
}
void render_audio_adding(
instrument_descriptor_t instrument_descriptor,
instrument_t instance,
unsigned int sample_count,
float * output_vector,
float gain
){
if(gain <= 0.0000001) return;
float temp[2];
for(unsigned int i = 0; i < sample_count; i++){
instrument_descriptor->render_audio_sample(instance, sample_count, temp);
output_vector[2*i] += gain * buffer[0];
output_vector[2*i+1] += gain * buffer[1];
}
}
void append_instrument_descriptor(
instrument_descriptor_list_t * list,
void * construct_instrument_descriptor
){
lle_t * element = list;
while(element->next != NULL){
element = element->next;
}
element->next = malloc(sizeof(lle_t);
if(element->next == NULL) return;
element = element->next;
element->next = NULL;
element->content = construct_instrument_descriptor();
}
instrument_descriptor_list_t * list_builtin_instrument_descriptors(){
//really hope we can make this more elegant some day
instrument_descriptor_list_t * list = NULL;
//add your instrument here!
append_instrument_descriptor(list, &minimal_example_descriptor);
return list;
}
void instantiate_instrument(active_instrument_list_t instruments,
instrument_descriptor_t descriptor
){
descriptor->new_instrument
}
void mix_instruments_audio(active_instrument_list_t instruments,
unsigned int sample_count,
float * output_vector
){
active_instrument_t instrument = instruments;
while(instrument->next != NULL){
render_audio_adding(instrument->descriptor,
instrument->instrument,
sample_count,
output_vector,
instrument->gain)
instrument = instrument->next;
}
}
/* wip instrument api for badge23 (in large parts inspired by ladspa)
current status: none of this is hooked up or functional or compiles, just drafting things rn
some core concepts:
- several instruments can run at the same time (e.g., drum computer running in background),
with only one of them being in foreground and using buttons and display
- instruments are as "self contained" as possible, i.e. they access buttons and display with
minimal host involvement and pretty much just produce audio "on their own". aside from
scheduling the host mostly does some cap touch preprocessing and the audio output mixdown
- different timing requirements -> different "threads" for audio, buttons, display each
(leds: special case, see below)
open questions:
- led animations: instruments should be able to output led patterns. maybe keeping a dummy
led array in ram for each running instrument and allowing users to create and run "shaders"
would be a desirable functional mode; do we want this/does this need any extra api?
- for performance reasons: instruments are expected to behave themselves, i.e. not access hw
without permission or write to read-only locations, can we do better than that without
excessive overhead? and if we can, do we _want_ to? (devices that make electronic musical
instruments malfunction on purpose are not uncommon in general)
- badge link/cv data input/output: can probably be added to the descriptor easily? shouldn't
freeze api before tho
*/
//===========================================================================================
//some hardware dummy definitions, move somewhere else someday maybe
typedef struct {
int intensity; //touch strength, considered as no touch if below 0
int rad; //radial position
int az; //cw azimuthal position (only meaningful for top petals);
} petal_t;
typedef int button_t; //0: no press, -1: left, 1: right, 2: down
typedef struct { //read-only (shared between all instruments, unprotected)
petal_t petals[10]; //even: top, odd: bottom, 0 at usb-c jack, count ccw
button_t button; //can be either left or right, depending on host
//handedness settings. the other button is reserved for
//host use and is not exposed here.
} hardware_inputs_t;
//===========================================================================================
typedef void * instrument_t; //contains instrument instance data, not to be interpreted by host
typedef struct _instrument_descriptor_t {
unsigned int unique_id;
//null terminated instrument name
const char * name;
//allocates memory for new instance data and returns pointer to it (mandatory)
instrument_t (* new_instrument)(const struct _instrument_descriptor * descriptor,
unsigned int sample_rate,
hardware_inputs_t * hw);
//frees instance data memory (mandatory)
void (* delete_instrument) (instrument_t instance);
//renders a single stereo audio sample (optional, NULL if not supported)
void (* render_audio_sample) (instrument_t instance);
//handles petal/button/sensor input, ideally runs every 3-8ms (optional, NULL if not supported)
void (* process_user_input) (instrument_t instance);
//only runs when instrument is in foreground (optional, NULL if not supported)
void (* update_display) (instrument_t instance);
//(optional, NULL if not supported)
void (* update_leds) (instrument_t instance);
} instrument_descriptor_t;
// this function is called once per instrument type before use (i.e. around boot) and returns a
// filled out instrument descriptor struct to be used by the host for creating instrument instances
// returns null if allocation fails
const instrument_descriptor_t * contruct_instrument_descriptor();
//===========================================================================================
//host-side helper functions
void render_audio(
instrument_descriptor_t instrument_descriptor,
instrument_t instance,
unsigned int sample_count,
float * output_vector
);
void render_audio_adding(
instrument_descriptor_t instrument_descriptor,
instrument_t instance,
unsigned int sample_count,
float * output_vector,
float gain
);
typedef struct {
void * content;
lle_t * next;
} lle_t; //linked list element
typedef lle_t instrument_descriptor_list_t;
typedef struct {
instrument_t * instrument;
instrument_descriptor_t descriptor;
char is_foreground;
char is_rendering_leds;
float gain;
} active_instrument_t;
void append_instrument_descriptor(
instrument_descriptor_list_t * list,
void * construct_instrument_descriptor
);
instrument_descriptor_list_t * list_builtin_instrument_descriptors();
typedef lle_t active_instrument_list_t;
void mix_instruments_audio(active_instrument_list_t instruments,
unsigned int sample_count,
float * output_vector
);
//===========================================================================================
//simple instrument example implementation
//trad_osc is made up rn, didn't check how the existing implementation works
typedef struct{
unsigned int * sample_rate;
hardware_inputs_t * hw;
trad_osc_t[30] osc;
unsigned int sample_rate;
hardware_inputs_t * hw;
float last_freq; //frequency of last note played to write to display
} minimal_example_t;
void minimal_example_render_sample(instrument_handle inst, float * stereo_output){
float acc = 0;
for(int i = 0; i < 30; i++){
acc += trad_osc_run(&inst.osc[i], inst.sample_rate);
}
stereo_output[0] = acc; // both channels the same -> mono
stereo_output[1] = acc;
}
void minimal_example_process_user_input(instrument_handle inst){
static int petal_prev[10];
for(int i = 0; i < 10; i++){
if(inst->hw.petals[i].intensity > 0){ //if the pad is touched...
if(!petal_prev[i]){ //and it's a new touch...
if(button != 2){ //if button isn't pressed: play single note in different octaves
int j = i + (inst->hw.button + 1) * 10; //choose osc
trad_osc_start(&inst.osc[j]); //play a tone
inst->last_freq = inst.osc[j].freq;
} else { //for button center: all the octaves at once
trad_osc_start(&inst.osc[i]); //play a tone
trad_osc_start(&inst.osc[i+10]); //play a tone
trad_osc_start(&inst.osc[i+20]); //play a tone
inst->last_freq = inst.osc[i+10].freq;
}
}
petal_prev[i] = 1;
} else {
petal_prev[i] = 0;
}
}
}
void minimal_example_update_display(instrument_handle inst){
display_print("%f", inst->last_freq);
}
static float pad_to_freq(int pad){
int j = pad % 10;
if(j) j++; //skip min 2nd
if(j>8) j++; //skip maj 6th
j += (pad/10) * 12; //add octaves
float freq = 440 * pow(2., j/12.);
}
instrument_t new_minimal_example(
const struct _instrument_descriptor * descriptor,
unsigned int sample_rate,
hardware_inputs_t * hw)
{
instrument_t inst = malloc(sizeof(minimal_example_t));
if(inst == NULL) return NULL;
inst->sample_rate = sample_rate;
inst->hw = hw;
for(int i = 0; i < 30; i++){
inst->osc[i] = trad_osc_new(pad_to_freq(i), sample_rate);
//other osc parameters (wave, envelope, etc.) omitted for clarity
}
return inst;
}
void delete_minimal_example(instrument_t inst){
free(inst);
}
const instrument_descriptor_t * minimal_example_descriptor(){
instrument_descriptor_t * inst = malloc(sizeof(instrument_descriptor_t));
if(inst == NULL) return NULL;
inst->unique_id = 0;
inst->name = "simple instrument";
inst->render_audio_sample = &minimal_example_render_sample;
inst->new_instrument = &new_minimal_example;
inst->delete_instrument = &delete_minimal_example;
inst->update_display = NULL;
inst->process_user_input = minimal_example_user_input;
inst->update_leds = NULL;
}
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "esp_system.h"
#include "badge23/leds.h"
#include "badge23/lock.h"
static uint8_t leds_brightness = 69;;
static uint8_t leds_slew_rate = 255;
static bool leds_auto_update = 0;
static uint8_t gamma_red[256];
static uint8_t gamma_green[256];
static uint8_t gamma_blue[256];
#if defined(CONFIG_BADGE23_HW_GEN_P1)
#define LED_SPI_PORT
#elif defined(CONFIG_BADGE23_HW_GEN_P3) || defined(CONFIG_BADGE23_HW_GEN_P4) || defined(CONFIG_BADGE23_HW_GEN_P6)
#define LED_ASYNC_PORT
#else
#error "leds not implemented for this badge generation"
#endif
typedef struct leds_cfg {
int leaf;
size_t size;
size_t position;
size_t slot;
int timer;
} leds_cfg_t;
static leds_cfg_t active_leds[11];
struct RGB
{
unsigned char R;
unsigned char G;
unsigned char B;
};
struct RGB led_target[40] = {0,};
struct RGB led_target_buffer[40] = {0,};
struct RGB led_hardware_value[40] = {0,};
struct HSV
{
double H;
double S;
double V;
};
struct RGB HSVToRGB(struct HSV hsv) {
double r = 0, g = 0, b = 0;
if (hsv.S == 0)
{
r = hsv.V;
g = hsv.V;
b = hsv.V;
}
else
{
int i;
double f, p, q, t;
if (hsv.H == 360)
hsv.H = 0;
else
hsv.H = hsv.H / 60;
i = (int)trunc(hsv.H);
f = hsv.H - i;
p = hsv.V * (1.0 - hsv.S);
q = hsv.V * (1.0 - (hsv.S * f));
t = hsv.V * (1.0 - (hsv.S * (1.0 - f)));
switch (i)
{
case 0:
r = hsv.V;
g = t;
b = p;
break;
case 1:
r = q;
g = hsv.V;
b = p;
break;
case 2:
r = p;
g = hsv.V;
b = t;
break;
case 3:
r = p;
g = q;
b = hsv.V;
break;
case 4:
r = t;
g = p;
b = hsv.V;
break;
default:
r = hsv.V;
g = p;
b = q;
break;
}
}
struct RGB rgb;
rgb.R = r * 255;
rgb.G = g * 255;
rgb.B = b * 255;
return rgb;
}
#ifdef LED_SPI_PORT
#include "driver/spi_master.h"
#include "badge23/apa102LEDStrip.h"
#define totalPixels 40
#define bytesPerPixel 4
static struct apa102LEDStrip leds;
static spi_device_handle_t spi_led;
static spi_transaction_t spiTransObject;
static esp_err_t ret;
static spi_bus_config_t buscfg;
static spi_device_interface_config_t devcfg;
#define maxSPIFrameInBytes 8000
#define maxSPIFrequency 10000000
void renderLEDs()
{
spi_device_queue_trans(spi_led, &spiTransObject, portMAX_DELAY);
}
static int setupSPI()
{
//Set up the Bus Config struct
buscfg.miso_io_num=-1;
buscfg.mosi_io_num=18;
buscfg.sclk_io_num=8;
buscfg.quadwp_io_num=-1;
buscfg.quadhd_io_num=-1;
buscfg.max_transfer_sz=maxSPIFrameInBytes;
//Set up the SPI Device Configuration Struct
devcfg.clock_speed_hz=maxSPIFrequency;
devcfg.mode=0;
devcfg.spics_io_num=-1;
devcfg.queue_size=1;
//Initialize the SPI driver
ret=spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
ESP_ERROR_CHECK(ret);
//Add SPI port to bus
ret=spi_bus_add_device(SPI2_HOST, &devcfg, &spi_led);
ESP_ERROR_CHECK(ret);
return ret;
}
static void set_single_led(uint8_t index, uint8_t c[3]){
setPixel(&leds, index, c);
}
static void _leds_init() {
memset(active_leds, 0 , sizeof(active_leds));
setupSPI();
initLEDs(&leds, totalPixels, bytesPerPixel, 255);
//Set up SPI tx/rx storage Object
memset(&spiTransObject, 0, sizeof(spiTransObject));
spiTransObject.length = leds._frameLength*8;
spiTransObject.tx_buffer = leds.LEDs;
//TaskHandle_t handle;
//xTaskCreate(&leds_task, "LEDs player", 4096, NULL, configMAX_PRIORITIES - 2, &handle);
}
#endif
#ifdef LED_ASYNC_PORT
#include "../../espressif__led_strip/include/led_strip.h"
led_strip_t *led_strip_init(uint8_t channel, uint8_t gpio, uint16_t led_num);
#define LED_GPIO_NUM 14
#define LED_RMT_CHAN 0
led_strip_t * led_strip;
static void _leds_init(){
memset(active_leds, 0 , sizeof(active_leds));
led_strip = led_strip_init(LED_RMT_CHAN, LED_GPIO_NUM, 40);
}
void set_single_led(uint8_t index, uint8_t c[3]){
index = ((39-index) + 1 + 32)%40;
led_strip->set_pixel(led_strip, index, c[0], c[1], c[2]);
}
static void renderLEDs(){
led_strip->refresh(led_strip, 1000);
}
#endif
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) {
return new;
}
if(new < old - slew){
return old - slew;
} else if(new < old) {
return new;
}
return old;
}
static void leds_update_target(){
for(int i = 0; i < 40; i++){
led_target[i].R = led_target_buffer[i].R;
led_target[i].G = led_target_buffer[i].G;
led_target[i].B = led_target_buffer[i].B;
}
}
void leds_update_hardware(){
if(leds_auto_update) leds_update_target();
xSemaphoreTake(mutex_LED, portMAX_DELAY);
for(int i = 0; i < 40; i++){
uint8_t c[3];
c[0] = led_target[i].R * leds_brightness/255;
c[1] = led_target[i].G * leds_brightness/255;
c[2] = led_target[i].B * leds_brightness/255;
c[0] = gamma_red[c[0]];
c[1] = gamma_green[c[1]];
c[2] = gamma_blue[c[2]];
c[0] = led_get_slew(led_hardware_value[i].R, c[0], leds_slew_rate);
c[1] = led_get_slew(led_hardware_value[i].G, c[1], leds_slew_rate);
c[2] = led_get_slew(led_hardware_value[i].B, c[2], leds_slew_rate);
led_hardware_value[i].R = c[0];
led_hardware_value[i].G = c[1];
led_hardware_value[i].B = c[2];
int8_t index = i + 3 % 40;
set_single_led(index, c);
}
renderLEDs();
xSemaphoreGive(mutex_LED);
}
void leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, uint8_t blue){
led_target_buffer[index].R = red;
led_target_buffer[index].G = green;
led_target_buffer[index].B = blue;
}
void leds_set_single_hsv(uint8_t index, float hue, float sat, float val){
struct RGB rgb;
struct HSV hsv;
hsv.H = hue;
hsv.S = sat;
hsv.V = val;
rgb = HSVToRGB(hsv);
led_target_buffer[index].R = rgb.R;
led_target_buffer[index].G = rgb.G;
led_target_buffer[index].B = rgb.B;
}
void leds_set_all_rgb(uint8_t red, uint8_t green, uint8_t blue){
for(int i = 0; i<40; i++){
leds_set_single_rgb(i, red, green, blue);
}
}
void leds_set_all_hsv(float h, float s, float v){
for(int i = 0; i<40; i++){
leds_set_single_hsv(i, h, s, v);
}
}
void leds_update(){
leds_update_target();
leds_update_hardware();
}
void leds_init(){
for(uint16_t i = 0; i<256; i++){
gamma_red[i] = i;
gamma_green[i] = i;
gamma_blue[i] = i;
}
_leds_init();
}
void leds_set_brightness(uint8_t b){
leds_brightness = b;
}
uint8_t leds_get_brightness(){
return leds_brightness;
}
void leds_set_slew_rate(uint8_t s){
leds_slew_rate = s;
}
uint8_t leds_get_slew_rate(){
return leds_slew_rate;
}
void leds_set_auto_update(bool on){
leds_auto_update = on;
}
bool leds_get_auto_update(){
return leds_auto_update;
}
void leds_set_gamma(float red, float green, float blue){
for(uint16_t i = 0; i<256; i++){
if(i == 0){
gamma_red[i] = 0;
gamma_green[i] = 0;
gamma_blue[i] = 0;
}
float step = ((float) i) / 255.;
gamma_red[i] = (uint8_t) (254.*(pow(step, red))+1);
gamma_green[i] = (uint8_t) (254.*(pow(step, green))+1);
gamma_blue[i] = (uint8_t) (254.*(pow(step, blue))+1);
}
}
#include "badge23/scope.h"
#include "esp_log.h"
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/atomic.h>
scope_t * scope = NULL;
static const char* TAG = "scope";
void init_scope(uint16_t size){
if (scope != NULL) {
return;
}
scope_t * scp = malloc(sizeof(scope_t));
if(scp == NULL) {
ESP_LOGE(TAG, "scope allocation failed, out of memory");
return;
}
scp->buffer_size = size;
scp->buffer = malloc(sizeof(int16_t) * scp->buffer_size);
if(scp->buffer == NULL){
free(scp);
ESP_LOGE(TAG, "scope buffer allocation failed, out of memory");
return;
}
memset(scp->buffer, 0, sizeof(int16_t) * scp->buffer_size);
scope = scp;
ESP_LOGI(TAG, "initialized");
}
static inline bool scope_being_read(void) {
return Atomic_CompareAndSwap_u32(&scope->is_being_read, 0, 0) == ATOMIC_COMPARE_AND_SWAP_FAILURE;
}
void write_to_scope(int16_t value){
if(scope == NULL || scope_being_read()) {
return;
}
scope->write_head_position++;
if(scope->write_head_position >= scope->buffer_size) scope->write_head_position = 0;
scope->buffer[scope->write_head_position] = value;
}
void begin_scope_read(void) {
if (scope == NULL) {
return;
}
Atomic_Increment_u32(&scope->is_being_read);
}
void read_line_from_scope(uint16_t * line, int16_t point){
if (scope == NULL) {
return;
}
memset(line, 0, 480);
int16_t startpoint = scope->write_head_position - point;
while(startpoint < 0){
startpoint += scope->buffer_size;
}
int16_t stoppoint = startpoint - 1;
if(stoppoint < 0){
stoppoint += scope->buffer_size;
}
int16_t start = (scope->buffer[point]/32 + 120);
int16_t stop = (scope->buffer[point+1]/32 + 120);
if(start>240) start = 240;
if(start<0) start = 0;
if(stop>240) stop = 240;
if(stop<0) stop = 0;
if(start > stop){
int16_t inter = start;
start = stop;
stop = inter;
}
for(int16_t i = start; i <= stop; i++){
line[i] = 255;
}
}
void end_scope_read(void) {
if (scope == NULL) {
return;
}
Atomic_Decrement_u32(&scope->is_being_read);
}