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;
};
#define APP_MODE_MASK 0x0f
#define APP_FLAG_MACH 0x10
#define APP_MODE_MASK 0x0f
#define APP_FLAG_MACH 0x10
#define APP_FLAG_PITCH 0x20
#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)
{
const int end = start + len - 1;
for(int i = 0;i < len;++i) {
if (val || i == 0 || leading_zeros)
sevenseg_set_digit(end - i, val % 10);
else
sevenseg_set_digit(end - i, LED_BLANK);
val /= 10;
for (int i = 0; i < len; ++i) {
if (val || i == 0 || leading_zeros)
sevenseg_set_digit(end - i, val % 10);
else
sevenseg_set_digit(end - i, LED_BLANK);
val /= 10;
}
}
static void render_blank(int start, int len)
{
for(int i = 0;i < len;++i) {
sevenseg_set_digit(start+i, LED_BLANK);
for (int i = 0; i < len; ++i) {
sevenseg_set_digit(start + i, LED_BLANK);
}
}
}
static void update_hdg_display(void)
{
if (show_hdg) {
render_uint(0, 3, hdg, true);
render_uint(0, 3, hdg, true);
} else {
render_blank(0, 3);
render_blank(0, 3);
}
}
static void update_alt_display(void)
{
if (show_alt) {
render_uint(3, 5, alt, false);
render_uint(3, 5, alt, false);
} else {
render_blank(3, 5);
render_blank(3, 5);
}
}
static void update_vs_display(void)
{
if (pitch_mode) {
int val = vs;
unsigned int val_abs = val < 0 ? -val : val;
if (val < 0) {
sevenseg_set_digit(8, LED_MINUS);
} else {
sevenseg_set_digit(8, LED_BLANK);
}
render_uint(9, 2, val_abs, false);
sevenseg_set_digit(11, LED_DEGREE);
int val = vs;
unsigned int val_abs = val < 0 ? -val : val;
if (val < 0) {
sevenseg_set_digit(8, LED_MINUS);
} else {
sevenseg_set_digit(8, LED_BLANK);
}
render_uint(9, 2, val_abs, false);
sevenseg_set_digit(11, LED_DEGREE);
} else if (show_vs) {
int val = vs;
unsigned val_abs = val < 0 ? -val : val;
render_uint(8, 4, val_abs, false);
if (val >= 1000 || val <= -1000) {
sevenseg_mod_digit(8, val < 0 ? LED_DECIMAL : 0);
} else if (val < 0) {
sevenseg_set_digit(8, LED_MINUS);
}
int val = vs;
unsigned val_abs = val < 0 ? -val : val;
render_uint(8, 4, val_abs, false);
if (val >= 1000 || val <= -1000) {
sevenseg_mod_digit(8, val < 0 ? LED_DECIMAL : 0);
} else if (val < 0) {
sevenseg_set_digit(8, LED_MINUS);
}
} else {
render_blank(8, 4);
render_blank(8, 4);
}
}
@@ -131,12 +131,12 @@ static void update_as_display(void)
int val = as;
if (mach_mode) {
sevenseg_set_digit(12, 0 | LED_DECIMAL);
render_uint(13, 3, val, true);
render_uint(13, 3, val, true);
} else {
render_uint(12, 4, val, false);
render_uint(12, 4, val, false);
}
} 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
if (inc) {
val = ((val - step + 1) / step) * step;
if (val < 0)
val = ((val - step + 1) / step) * step;
else
val = (val / step) * step;
} else {
val = ((val + step - 1) / step) * step;
if (val < 0)
val = (val / step) * step;
else
val = ((val + step - 1) / step) * step;
}
if (inc) {
return val >= max - step + 1 ? (wrap ? min : max) : val + step;
return val >= max - step + 1 ? (wrap ? min : max) : val + step;
} 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)
{
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();
} 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();
} else if (rot_ch == 1) {
if (pitch_mode) {
vs = crement_val(!ccw, vs, -20, 20, 1, false);
} else {
vs = crement_val(!ccw, vs, -5000, 5000, 100, false);
}
if (pitch_mode) {
vs = crement_val(!ccw, vs, -20, 20, 1, false);
} else {
vs = crement_val(!ccw, vs, -5000, 5000, 100, false);
}
update_vs_display();
} else {
if (mach_mode) {
as = crement_val(!ccw, as, 0, 990, 10, false);
as = crement_val(!ccw, as, 0, 990, 10, false);
} 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();
}
@@ -263,19 +269,19 @@ static void app_update_report(struct app_report const *report)
hdg = report->update.hdg;
update_hdg_display();
send_heading_reports = true;
show_hdg = true;
show_hdg = true;
} else if (report->update.hdg == -2) {
show_hdg = false;
show_hdg = false;
}
if (report->update.alt != 0xffff) {
if (report->update.alt == 0xfffe) {
show_alt = false;
} else {
alt = report->update.alt;
update_alt_display();
send_alt_reports = true;
show_alt = true;
}
if (report->update.alt == 0xfffe) {
show_alt = false;
} else {
alt = report->update.alt;
update_alt_display();
send_alt_reports = true;
show_alt = true;
}
}
if (report->update.vs != -1) {
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:
app_init_report(&report);
break;
case APP_BOOTLOADER:
reset_usb_boot(0, 0);
break;
case APP_BOOTLOADER:
reset_usb_boot(0, 0);
break;
default:
break;
}