Skip to content

Commit

Permalink
feat: new modal & stealth mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Otrebor671 committed Aug 9, 2024
1 parent d0d0ed8 commit 0eae788
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 31 deletions.
20 changes: 18 additions & 2 deletions firmware/components/buzzer/buzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ typedef struct {
uint8_t pin;
uint32_t freq;
uint32_t duty;
bool enabled;
} buzzer_t;

buzzer_t buzzer;
static buzzer_t buzzer;

void buzzer_enable() {
buzzer.enabled = true;
}
void buzzer_disable() {
buzzer.enabled = false;
}

void buzzer_begin(uint8_t pin) {
buzzer.pin = pin;
Expand Down Expand Up @@ -57,8 +65,10 @@ void buzzer_set_duty(uint32_t duty) {
}

void buzzer_play() {
if (!buzzer.enabled) {
return;
}
buzzer_configure();

// Set the duty cycle
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, buzzer.duty));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
Expand All @@ -73,13 +83,19 @@ void buzzer_play_for_task(void* duration) {
}

void buzzer_play_for(uint32_t duration) {
if (!buzzer.enabled) {
return;
}
uint32_t* duration_ptr = malloc(sizeof(uint32_t));
*duration_ptr = duration;
xTaskCreate(buzzer_play_for_task, "buzzer_play_for_task", 2048, duration_ptr,
5, NULL);
}

void buzzer_stop() {
if (!buzzer.enabled) {
return;
}
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));

Expand Down
3 changes: 3 additions & 0 deletions firmware/components/buzzer/include/buzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ void buzzer_play_for(uint32_t duration);
* @return void
*/
void buzzer_stop();

void buzzer_enable();
void buzzer_disable();
3 changes: 2 additions & 1 deletion firmware/components/leds/include/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

typedef enum { LED_LEFT, LED_RIGHT } leds_enum;

void leds_init();
void leds_begin();
void leds_deinit();
void leds_on();
void leds_off();
void leds_set_brightness(uint8_t led, uint8_t brightness);
Expand Down
50 changes: 32 additions & 18 deletions firmware/components/leds/leds.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "leds.h"

#include <string.h>

#include "driver/ledc.h"
#include "ledc_controller.h"

Expand All @@ -8,43 +11,54 @@
#define RIGHT_LED_CHANNEL LEDC_CHANNEL_1
#define LEDC_TIMER LEDC_TIMER_0

led_t left_led, right_led;
static led_t *left_led, *right_led;

void leds_begin() {
left_led = (led_t*) malloc(sizeof(led_t));
right_led = (led_t*) malloc(sizeof(led_t));
*left_led = led_controller_led_new(LEFT_LED_IO, LEFT_LED_CHANNEL);
*right_led = led_controller_led_new(RIGHT_LED_IO, RIGHT_LED_CHANNEL);
led_controller_led_init(left_led);
led_controller_led_init(right_led);
}

void leds_init() {
left_led = led_controller_led_new(LEFT_LED_IO, LEFT_LED_CHANNEL);
right_led = led_controller_led_new(RIGHT_LED_IO, RIGHT_LED_CHANNEL);
led_controller_led_init(&left_led);
led_controller_led_init(&right_led);
void leds_deinit() {
led_controller_led_deinit(left_led);
led_controller_led_deinit(right_led);
free(left_led);
free(right_led);
left_led = NULL;
right_led = NULL;
}

void leds_on() {
led_controller_led_on(&left_led);
led_controller_led_on(&right_led);
led_controller_led_on(left_led);
led_controller_led_on(right_led);
}

void leds_off() {
led_controller_led_off(&left_led);
led_controller_led_off(&right_led);
led_controller_led_off(left_led);
led_controller_led_off(right_led);
}

void leds_set_brightness(uint8_t led, uint8_t brightness) {
led_controller_set_duty(led == LED_LEFT ? &left_led : &right_led, brightness);
led_controller_set_duty(led == LED_LEFT ? left_led : right_led, brightness);
}

void led_left_on() {
led_controller_led_on(&left_led);
led_controller_led_on(left_led);
}

void led_left_off() {
led_controller_led_off(&left_led);
led_controller_led_off(left_led);
}

void led_right_on() {
led_controller_led_on(&right_led);
led_controller_led_on(right_led);
}

void led_right_off() {
led_controller_led_off(&right_led);
led_controller_led_off(right_led);
}

void led_start_blink(uint8_t led,
Expand All @@ -53,14 +67,14 @@ void led_start_blink(uint8_t led,
uint32_t time_on,
uint32_t time_off,
uint32_t time_out) {
led_controller_start_blink_effect(led == LED_LEFT ? &left_led : &right_led,
led_controller_start_blink_effect(led == LED_LEFT ? left_led : right_led,
duty, pulse_count, time_on, time_off,
time_out);
}
void led_start_breath(uint8_t led, uint16_t period_ms) {
led_controller_start_breath_effect(led == LED_LEFT ? &left_led : &right_led,
led_controller_start_breath_effect(led == LED_LEFT ? left_led : right_led,
period_ms);
}
void led_stop(uint8_t led) {
led_controller_stop_any_effect(led == LED_LEFT ? &left_led : &right_led);
led_controller_stop_any_effect(led == LED_LEFT ? left_led : right_led);
}
8 changes: 6 additions & 2 deletions firmware/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ void app_main(void) {
#endif
uint64_t start_time, end_time;
start_time = esp_timer_get_time();
preferences_begin();

bool stealth_mode = preferences_get_bool("stealth_mode", false);
if (!stealth_mode) {
buzzer_enable();
leds_begin();
}
buzzer_begin(BUZZER_PIN);
leds_init();
preferences_begin();
sd_card_begin();
flash_fs_begin(flash_fs_screens_handler);
keyboard_module_begin();
Expand Down
14 changes: 9 additions & 5 deletions firmware/main/modules/menu_screens/screens_modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ typedef enum {
MENU_SETTINGS_SD_CARD,
MENU_SETTINGS_SD_CARD_INFO,
MENU_SETTINGS_SD_CARD_FORMAT,
MENU_STEALTH_MODE,
/* Menu count */
MENU_COUNT, // Keep this at the end
} screen_module_menu_t;
Expand Down Expand Up @@ -168,6 +169,7 @@ const char* menu_list[] = {
"MENU_SETTINGS_SD_CARD",
"MENU_SETTINGS_SD_CARD_INFO",
"MENU_SETTINGS_SD_CARD_FORMAT",
"MENU_STEALTH_MODE",
};

/**
Expand All @@ -187,7 +189,8 @@ const int next_menu_table[][MAX_NUM_ITEMS] = {
MENU_GPS //, MENU_FILE_MANAGER
},
// MENU_SETTINGS
{MENU_SETTINGS_DISPLAY, MENU_SETTINGS_SYSTEM, MENU_FILE_MANAGER},
{MENU_SETTINGS_DISPLAY, MENU_SETTINGS_SYSTEM, MENU_FILE_MANAGER,
MENU_STEALTH_MODE},
// MENU_ABOUT
{MENU_ABOUT_VERSION, MENU_ABOUT_LICENSE, MENU_ABOUT_CREDITS,
MENU_ABOUT_LEGAL, MENU_ABOUT_UPDATE},
Expand Down Expand Up @@ -287,6 +290,8 @@ const int next_menu_table[][MAX_NUM_ITEMS] = {
{MENU_SETTINGS_SD_CARD_INFO},
// MENU_SETTINGS_SD_CARD_FORMAT
{MENU_SETTINGS_SD_CARD_INFO},
// MENU_STEALTH_MODE
{MENU_STEALTH_MODE},
};

/**
Expand Down Expand Up @@ -352,6 +357,7 @@ const int prev_menu_table[] = {
MENU_SETTINGS_SYSTEM, // MENU_SETTINGS_SD_CARD
MENU_SETTINGS_SD_CARD, // MENU_SETTINGS_SD_CARD_INFO
MENU_SETTINGS_SD_CARD, // MENU_SETTINGS_SD_CARD_FORMAT
MENU_SETTINGS, // MENU_STEALTH_MODE
};

/**
Expand All @@ -375,10 +381,7 @@ char* applications_items[] = {
};

char* settings_items[] = {
"Display",
"System",
"File Manager",
NULL,
"Display", "System", "File Manager", "Stealth Mode", NULL,
};

char* about_items[] = {
Expand Down Expand Up @@ -795,4 +798,5 @@ char** menu_items[] = {
sd_card_settings_items, // MENU_SETTINGS_SD_CARD
sd_card_info, // MENU_SETTINGS_SD_CARD_INFO
sd_card_format_array, // MENU_SETTINGS_SD_CARD_FORMAT
empty_items, // MENU_STEALTH_MODE
};
69 changes: 67 additions & 2 deletions firmware/main/modules/modals/modals_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
#include "modals_screens.h"

static modal_get_user_selection_t* modal_get_user_selection_ctx;
static modal_get_radio_selection_t* modal_get_radio_selection_ctx;

static char* yes_no_options[] = {"NO", "YES", NULL};

static void (*custom_list_options_cb)(modal_get_user_selection_t*) = NULL;
static void (*custom_list_radio_options_cb)(modal_get_radio_selection_t*) =
NULL;

static void list_options() {
if (custom_list_options_cb) {
Expand All @@ -17,6 +21,14 @@ static void list_options() {
modals_screens_default_list_options_cb(modal_get_user_selection_ctx);
}

static void list_radio_options() {
if (custom_list_radio_options_cb) {
custom_list_radio_options_cb(modal_get_radio_selection_ctx);
return;
}
modals_screens_default_list_radio_options_cb(modal_get_radio_selection_ctx);
}

static void get_user_selection_input_cb(uint8_t button_name,
uint8_t button_event) {
if (button_event != BUTTON_PRESS_DOWN) {
Expand Down Expand Up @@ -50,6 +62,40 @@ static void get_user_selection_input_cb(uint8_t button_name,
}
}

static void get_radio_selection_input_cb(uint8_t button_name,
uint8_t button_event) {
if (button_event != BUTTON_PRESS_DOWN) {
return;
}
switch (button_name) {
case BUTTON_LEFT:
modal_get_radio_selection_ctx->consumed = true;
break;
case BUTTON_RIGHT:
modal_get_radio_selection_ctx->current_option =
modal_get_radio_selection_ctx->selected_option;
list_radio_options();
break;
case BUTTON_UP:
modal_get_radio_selection_ctx->selected_option =
modal_get_radio_selection_ctx->selected_option == 0
? modal_get_radio_selection_ctx->options_count - 1
: modal_get_radio_selection_ctx->selected_option - 1;
list_radio_options();
break;
case BUTTON_DOWN:
modal_get_radio_selection_ctx->selected_option =
++modal_get_radio_selection_ctx->selected_option <
modal_get_radio_selection_ctx->options_count
? modal_get_radio_selection_ctx->selected_option
: 0;
list_radio_options();
break;
default:
break;
}
}

int count_items(char** items) {
int count = 0;
while (items[count] != NULL) {
Expand All @@ -64,7 +110,7 @@ int8_t modals_module_get_user_selection(char** options, char* banner) {
modal_get_user_selection_ctx->options = options;
modal_get_user_selection_ctx->options_count = count_items(options);
modal_get_user_selection_ctx->banner = banner;
menu_screens_set_app_state(true, get_user_selection_input_cb);
menu_screens_set_app_state(true, get_radio_selection_input_cb);
list_options();
while (!modal_get_user_selection_ctx->consumed)
;
Expand All @@ -79,7 +125,7 @@ int8_t modals_module_get_user_y_n_selection(char* banner) {
modal_get_user_selection_ctx->options_count = 2;
modal_get_user_selection_ctx->banner = banner;
custom_list_options_cb = modals_screens_list_y_n_options_cb;
menu_screens_set_app_state(true, get_user_selection_input_cb);
menu_screens_set_app_state(true, get_radio_selection_input_cb);
list_options();
while (!modal_get_user_selection_ctx->consumed)
;
Expand All @@ -89,6 +135,25 @@ int8_t modals_module_get_user_y_n_selection(char* banner) {
return selection;
}

uint8_t modals_module_get_radio_selection(char** options,
char* banner,
uint8_t current_option) {
modal_get_radio_selection_ctx = malloc(sizeof(modal_get_radio_selection_t));
memset(modal_get_radio_selection_ctx, 0, sizeof(modal_get_radio_selection_t));
modal_get_radio_selection_ctx->options = options;
modal_get_radio_selection_ctx->options_count = count_items(options);
modal_get_radio_selection_ctx->banner = banner;
modal_get_radio_selection_ctx->current_option = current_option;
modal_get_radio_selection_ctx->selected_option = current_option;
menu_screens_set_app_state(true, get_radio_selection_input_cb);
list_radio_options();
while (!modal_get_radio_selection_ctx->consumed)
;
uint8_t selection = modal_get_radio_selection_ctx->current_option;
free(modal_get_radio_selection_ctx);
return selection;
}

void modals_module_show_info(char* head,
char* body,
size_t time_ms,
Expand Down
12 changes: 12 additions & 0 deletions firmware/main/modules/modals/modals_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ typedef struct {
volatile bool consumed;
} modal_get_user_selection_t;

typedef struct {
uint8_t selected_option;
uint8_t options_count;
char** options;
char* banner;
uint8_t current_option;
volatile bool consumed;
} modal_get_radio_selection_t;

int8_t modals_module_get_user_selection(char** options, char* banner);
void set_get_user_selection_handler(void* cb);
int8_t modals_module_get_user_y_n_selection(char* banner);
uint8_t modals_module_get_radio_selection(char** options,
char* banner,
uint8_t current_option);
void modals_module_show_info(char* head,
char* body,
size_t time_ms,
Expand Down
Loading

0 comments on commit 0eae788

Please sign in to comment.