Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ src/resource.c \
src/animation.c \
src/font.c \
src/power.c \
src/audio.c \


# ASM sources
Expand Down
25 changes: 25 additions & 0 deletions src/audio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "CH58x_common.h"

#include "leddrv.h"

// TODO: setup an interrupt instead of polling, now is just for POC
int16_t mic_adc()
{
ADC_ChannelCfg(11);
uint64_t sum = 0;
for (int i = 0; i < 64; i++) {
sum += ADC_ExcutSingleConver();
// mDelayuS(1);
}

// adc idle level is 2048. Normalizing this to 0
return sum / 64 - 2048;
}

void mic_init()
{
/* adc 11 - pa7 */
GPIOA_ModeCfg(GPIO_Pin_7, GPIO_ModeIN_Floating);
ADC_ExtSingleChSampInit(SampleFreq_3_2, ADC_PGA_0);
ADC_ChannelCfg(11);
}
31 changes: 31 additions & 0 deletions src/audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef __AUDIO_H__
#define __AUDIO_H__

#include <stdint.h>

const uint16_t amp_wav_lut[8] = {
0b00000000000,
0b00000100000,
0b00001110000,
0b00011111000,
0b00111111100,
0b01111111110,
0b11111111111,
0b11111111111,
};

const uint16_t amp_wav_lut_w1[8] = {
0b00000100000,
0b00001110000,
0b00011111000,
0b00111111100,
0b01111111110,
0b11111111111,
0b11111111111,
0b11111111111,
};

int16_t mic_adc();
void mic_init();

#endif /* __AUDIO_H__ */
31 changes: 30 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "CH58x_sys.h"
#include "CH58xBLE_LIB.h"

#include "audio.h"
#include "leddrv.h"
#include "button.h"
#include "bmlist.h"
Expand Down Expand Up @@ -30,6 +31,7 @@ enum MODES {
NORMAL,
DOWNLOAD,
POWER_OFF,
AUDIO,
MODES_COUNT,
};

Expand Down Expand Up @@ -60,6 +62,7 @@ static void change_brightness()

static void mode_setup_download();
static void mode_setup_normal();
static void mode_setup_audio_visualize();

__HIGH_CODE
static void change_mode()
Expand All @@ -69,6 +72,7 @@ static void change_mode()
NULL,
mode_setup_normal,
mode_setup_download,
mode_setup_audio_visualize,
poweroff
};

Expand Down Expand Up @@ -398,6 +402,29 @@ static void mode_setup_normal()
start_normal_animation();
}

static void audio_visualize_poll()
{
while (1) {
for (int i=0; i<LED_COLS; i++) {
// FIXME: how to adjust this 300 dynamically
fb[i] = amp_wav_lut[(abs(mic_adc()) / 300)];
}
if (mode =! AUDIO) {
break;
}
}
}

static void mode_setup_audio_visualize()
{
tmos_stop_task(common_taskid, ANI_NEXT_STEP);
tmos_stop_task(common_taskid, ANI_MARQUE);
tmos_stop_task(common_taskid, ANI_FLASH);
tmos_stop_task(common_taskid, BLE_NEXT_STEP);

audio_visualize_poll();
}

void handle_after_rx()
{
if (badge_cfg.reset_rx) {
Expand Down Expand Up @@ -431,7 +458,7 @@ int main()
btn_onLongPress(KEY1, change_brightness);

power_init();
disp_charging();
// disp_charging();
cfg_init();
xbm_t spl = {
.bits = &(badge_cfg.splash_bm_bits),
Expand All @@ -446,6 +473,8 @@ int main()
ble_setup();

spawn_tasks();

mic_init();

mode = NORMAL;
while (1) {
Expand Down
Loading