Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoders minor fixes #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "backlight.h"
#include "events.h"
#include "gps.h"
#include "mfk.h"
#include "vol.h"

#define DISP_BUF_SIZE (128 * 1024)

Expand Down Expand Up @@ -87,6 +89,8 @@ int main(void) {
vol->right[VOL_SELECT] = KEY_VOL_RIGHT_SELECT;

params_init();
mfk_change_mode(0);
vol_change_mode(0);
styles_init();

lv_obj_t *main_obj = main_screen();
Expand Down
9 changes: 4 additions & 5 deletions src/main_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,6 @@ static void main_screen_keypad_cb(lv_event_t * e) {
case KEYPAD_GEN:
if (keypad->state == KEYPAD_RELEASE) {
apps_disable();
mfk_set_mode(MFK_MIN_LEVEL);
buttons_unload_page();
buttons_load_page(PAGE_VOL_1);
voice_say_text_fmt("General menu keys");
Expand Down Expand Up @@ -881,12 +880,12 @@ static void spectrum_key_cb(lv_event_t * e) {

case KEY_VOL_LEFT_SELECT:
case '{':
vol_press(-1);
vol_change_mode(-1);
break;

case KEY_VOL_RIGHT_SELECT:
case '}':
vol_press(+1);
vol_change_mode(+1);
break;

case KEYBOARD_F9:
Expand All @@ -903,7 +902,7 @@ static void spectrum_key_cb(lv_event_t * e) {
break;

case MFK_STATE_SELECT:
mfk_press(-1);
mfk_change_mode(-1);
break;
}
break;
Expand All @@ -915,7 +914,7 @@ static void spectrum_key_cb(lv_event_t * e) {
break;

case MFK_STATE_SELECT:
mfk_press(+1);
mfk_change_mode(+1);
break;
}
break;
Expand Down
26 changes: 3 additions & 23 deletions src/mfk.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,32 +574,12 @@ void mfk_update(int16_t diff, bool voice) {
}
}

void mfk_press(int16_t dir) {
while (true) {
if (dir > 0) {
if (mfk_mode == MFK_LAST-1) {
mfk_mode = 0;
} else {
mfk_mode++;
}
} else {
if (mfk_mode == 0) {
mfk_mode = MFK_LAST-1;
} else {
mfk_mode--;
}
}

uint64_t mask = (uint64_t) 1L << mfk_mode;

if (params.mfk_modes & mask) {
break;
}
}

void mfk_change_mode(int16_t dir) {
mfk_mode = loop_modes(dir, mfk_mode, params.mfk_modes, MFK_LAST-1);
mfk_update(0, true);
}

void mfk_set_mode(mfk_mode_t mode) {
mfk_mode = mode;
mfk_state = MFK_STATE_EDIT;
}
2 changes: 1 addition & 1 deletion src/mfk.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ typedef enum {
extern mfk_state_t mfk_state;

void mfk_update(int16_t diff, bool voice);
void mfk_press(int16_t dir);
void mfk_change_mode(int16_t dir);
void mfk_set_mode(mfk_mode_t mode);
34 changes: 34 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,37 @@ uint64_t from_bcd(const uint8_t bcd_data[], uint8_t len) {

return data;
}


int loop_modes(int16_t dir, int mode, uint64_t modes, int max_val) {
while (1) {
if (dir > 0) {
if (mode == max_val) {
mode = 0;
} else {
mode++;
}
} else {
if (dir < 0)
{
if (mode == 0) {
mode = max_val;
} else {
mode--;
}
}
}
if (modes & (1LL << mode)) {
break;
}
if (dir == 0)
{
if (mode == max_val) {
mode = 0;
} else {
mode++;
}
}
}
return mode;
}
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ void lpf(float *x, float current, float beta);

void to_bcd(uint8_t bcd_data[], uint64_t data, uint8_t len);
uint64_t from_bcd(const uint8_t bcd_data[], uint8_t len);
int loop_modes(int16_t dir, int mode, uint64_t modes, const int max_val);
25 changes: 4 additions & 21 deletions src/vol.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "main.h"
#include "params.h"
#include "voice.h"
#include "util.h"

static vol_mode_t vol_mode = VOL_VOL;

Expand Down Expand Up @@ -197,30 +198,12 @@ void vol_update(int16_t diff, bool voice) {
}
}

void vol_press(int16_t dir) {
while (true) {
if (dir > 0) {
if (vol_mode == VOL_LAST-1) {
vol_mode = 0;
} else {
vol_mode++;
}
} else {
if (vol_mode == 0) {
vol_mode = VOL_LAST-1;
} else {
vol_mode--;
}
}

if (params.vol_modes & (1 << vol_mode)) {
break;
}
}

void vol_change_mode(int16_t dir) {
vol_mode = loop_modes(dir, vol_mode, params.vol_modes, VOL_LAST-1);
vol_update(0, true);
}

void vol_set_mode(vol_mode_t mode) {
vol_mode = mode;
vol->mode = VOL_EDIT;
}
2 changes: 1 addition & 1 deletion src/vol.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ typedef enum {
} vol_mode_t;

void vol_update(int16_t diff, bool voice);
void vol_press(int16_t dir);
void vol_change_mode(int16_t dir);
void vol_set_mode(vol_mode_t mode);