rp2040_app: Fix rounding to step in crement

This commit is contained in:
2023-06-10 00:38:31 +02:00
parent 81b5ccfbe8
commit b512372b19

View File

@@ -21,8 +21,8 @@ __attribute__((packed)) struct button_report {
uint32_t buttons; uint32_t buttons;
}; };
#define APP_MODE_MASK 0x0f #define APP_MODE_MASK 0x0f
#define APP_FLAG_MACH 0x10 #define APP_FLAG_MACH 0x10
#define APP_FLAG_PITCH 0x20 #define APP_FLAG_PITCH 0x20
#define APP_UPDATE 0 #define APP_UPDATE 0
@@ -65,63 +65,63 @@ static int min_as = 0, max_as = 999, max_mach = 990;
static void render_uint(int start, int len, unsigned val, bool leading_zeros) static void render_uint(int start, int len, unsigned val, bool leading_zeros)
{ {
const int end = start + len - 1; const int end = start + len - 1;
for(int i = 0;i < len;++i) { for (int i = 0; i < len; ++i) {
if (val || i == 0 || leading_zeros) if (val || i == 0 || leading_zeros)
sevenseg_set_digit(end - i, val % 10); sevenseg_set_digit(end - i, val % 10);
else else
sevenseg_set_digit(end - i, LED_BLANK); sevenseg_set_digit(end - i, LED_BLANK);
val /= 10; val /= 10;
} }
} }
static void render_blank(int start, int len) static void render_blank(int start, int len)
{ {
for(int i = 0;i < len;++i) { for (int i = 0; i < len; ++i) {
sevenseg_set_digit(start+i, LED_BLANK); sevenseg_set_digit(start + i, LED_BLANK);
} }
} }
static void update_hdg_display(void) static void update_hdg_display(void)
{ {
if (show_hdg) { if (show_hdg) {
render_uint(0, 3, hdg, true); render_uint(0, 3, hdg, true);
} else { } else {
render_blank(0, 3); render_blank(0, 3);
} }
} }
static void update_alt_display(void) static void update_alt_display(void)
{ {
if (show_alt) { if (show_alt) {
render_uint(3, 5, alt, false); render_uint(3, 5, alt, false);
} else { } else {
render_blank(3, 5); render_blank(3, 5);
} }
} }
static void update_vs_display(void) static void update_vs_display(void)
{ {
if (pitch_mode) { if (pitch_mode) {
int val = vs; int val = vs;
unsigned int val_abs = val < 0 ? -val : val; unsigned int val_abs = val < 0 ? -val : val;
if (val < 0) { if (val < 0) {
sevenseg_set_digit(8, LED_MINUS); sevenseg_set_digit(8, LED_MINUS);
} else { } else {
sevenseg_set_digit(8, LED_BLANK); sevenseg_set_digit(8, LED_BLANK);
} }
render_uint(9, 2, val_abs, false); render_uint(9, 2, val_abs, false);
sevenseg_set_digit(11, LED_DEGREE); sevenseg_set_digit(11, LED_DEGREE);
} else if (show_vs) { } else if (show_vs) {
int val = vs; int val = vs;
unsigned val_abs = val < 0 ? -val : val; unsigned val_abs = val < 0 ? -val : val;
render_uint(8, 4, val_abs, false); render_uint(8, 4, val_abs, false);
if (val >= 1000 || val <= -1000) { if (val >= 1000 || val <= -1000) {
sevenseg_mod_digit(8, val < 0 ? LED_DECIMAL : 0); sevenseg_mod_digit(8, val < 0 ? LED_DECIMAL : 0);
} else if (val < 0) { } else if (val < 0) {
sevenseg_set_digit(8, LED_MINUS); sevenseg_set_digit(8, LED_MINUS);
} }
} else { } else {
render_blank(8, 4); render_blank(8, 4);
} }
} }
@@ -131,12 +131,12 @@ static void update_as_display(void)
int val = as; int val = as;
if (mach_mode) { if (mach_mode) {
sevenseg_set_digit(12, 0 | LED_DECIMAL); sevenseg_set_digit(12, 0 | LED_DECIMAL);
render_uint(13, 3, val, true); render_uint(13, 3, val, true);
} else { } else {
render_uint(12, 4, val, false); render_uint(12, 4, val, false);
} }
} else { } else {
render_blank(12, 4); render_blank(12, 4);
} }
} }
@@ -144,37 +144,43 @@ static int crement_val(bool inc, int val, int min, int max, int step, bool wrap)
{ {
// round to step in correct direction // round to step in correct direction
if (inc) { if (inc) {
val = ((val - step + 1) / step) * step; if (val < 0)
val = ((val - step + 1) / step) * step;
else
val = (val / step) * step;
} else { } else {
val = ((val + step - 1) / step) * step; if (val < 0)
val = (val / step) * step;
else
val = ((val + step - 1) / step) * step;
} }
if (inc) { if (inc) {
return val >= max - step + 1 ? (wrap ? min : max) : val + step; return val >= max - step + 1 ? (wrap ? min : max) : val + step;
} else { } else {
return val <= min + step ? (wrap ? max : min) : val - step; return val <= min + step ? (wrap ? max : min) : val - step;
} }
} }
void rotary_event(int rot_ch, bool ccw) void rotary_event(int rot_ch, bool ccw)
{ {
if (rot_ch == 3) { if (rot_ch == 3) {
hdg = crement_val(!ccw, hdg, 0, 359, 1, true); hdg = crement_val(!ccw, hdg, 0, 359, 1, true);
update_hdg_display(); update_hdg_display();
} else if (rot_ch == 2) { } else if (rot_ch == 2) {
alt = crement_val(!ccw, alt, 0, 50000, 100, false); alt = crement_val(!ccw, alt, 0, 50000, 100, false);
update_alt_display(); update_alt_display();
} else if (rot_ch == 1) { } else if (rot_ch == 1) {
if (pitch_mode) { if (pitch_mode) {
vs = crement_val(!ccw, vs, -20, 20, 1, false); vs = crement_val(!ccw, vs, -20, 20, 1, false);
} else { } else {
vs = crement_val(!ccw, vs, -5000, 5000, 100, false); vs = crement_val(!ccw, vs, -5000, 5000, 100, false);
} }
update_vs_display(); update_vs_display();
} else { } else {
if (mach_mode) { if (mach_mode) {
as = crement_val(!ccw, as, 0, 990, 10, false); as = crement_val(!ccw, as, 0, 990, 10, false);
} else { } else {
as = crement_val(!ccw, as, min_as, max_as, 1, false); as = crement_val(!ccw, as, min_as, max_as, 1, false);
} }
update_as_display(); update_as_display();
} }
@@ -263,19 +269,19 @@ static void app_update_report(struct app_report const *report)
hdg = report->update.hdg; hdg = report->update.hdg;
update_hdg_display(); update_hdg_display();
send_heading_reports = true; send_heading_reports = true;
show_hdg = true; show_hdg = true;
} else if (report->update.hdg == -2) { } else if (report->update.hdg == -2) {
show_hdg = false; show_hdg = false;
} }
if (report->update.alt != 0xffff) { if (report->update.alt != 0xffff) {
if (report->update.alt == 0xfffe) { if (report->update.alt == 0xfffe) {
show_alt = false; show_alt = false;
} else { } else {
alt = report->update.alt; alt = report->update.alt;
update_alt_display(); update_alt_display();
send_alt_reports = true; send_alt_reports = true;
show_alt = true; show_alt = true;
} }
} }
if (report->update.vs != -1) { if (report->update.vs != -1) {
if (report->update.vs == -2) { if (report->update.vs == -2) {
@@ -329,9 +335,9 @@ void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_
case APP_INIT: case APP_INIT:
app_init_report(&report); app_init_report(&report);
break; break;
case APP_BOOTLOADER: case APP_BOOTLOADER:
reset_usb_boot(0, 0); reset_usb_boot(0, 0);
break; break;
default: default:
break; break;
} }