From 440ef14ac19e2f58c072fc644d7be64cf0bb9b67 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sun, 31 May 2026 11:32:43 +0200 Subject: [PATCH] moved state to global --- fw/display.c | 6 +++--- fw/main.c | 30 ++++++++++++++---------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/fw/display.c b/fw/display.c index 653d37b..ade20eb 100644 --- a/fw/display.c +++ b/fw/display.c @@ -163,13 +163,13 @@ static uint8_t u8x8_byte_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *a u8g2_t* display_init(void) { -#if defined(SSD1306_128X32) && SSD1306_128X32 +#if SSD1306_128X32 u8g2_Setup_ssd1306_i2c_128x32_univision_f(&u8g2, U8G2_R0, u8x8_byte_i2c, u8x8_gpio_and_delay); -#elif defined(SSD1312_96X16) && SSD1312_96X16 +#elif SSD1312_96X16 // NOTE: display size is wrong, hardware is 96x16, but driver is configured for 120x28 u8g2_Setup_ssd1312_i2c_128x32_f(&u8g2, U8G2_R0, u8x8_byte_i2c, u8x8_gpio_and_delay); #else - static_assert(0, "unsupported display size"); + static_assert(0, "unsupported display"); #endif // TODO: log errors and return NULL on failure u8g2_InitDisplay(&u8g2); diff --git a/fw/main.c b/fw/main.c index da6039e..2815873 100644 --- a/fw/main.c +++ b/fw/main.c @@ -14,12 +14,20 @@ #include "coroutine.h" -// constants -u8g2_t *u8g2; -int16_t encoder = 0; // rotary encoder counter -uint32_t last_interrupt = 0; // last time the encoder interrupt was triggered -struct pd_profile_t pd_profile; -uint16_t vcc_mv = 3300; +/* ------------------------------ Global State ------------------------------ */ +u8g2_t *u8g2; // Display state +int16_t encoder; // Rotary encoder counter +struct pd_profile_t pd_profile; // USB Power Delivery profile +uint16_t vcc_mv = 3300; // Logic (and analog) voltage +uint16_t tip_mv; // Tip amplifier reading in mV +uint16_t tip_temp_c; // Converted tip temperature in degrees C +int16_t temp_c; // Board temperature in degrees C +uint16_t vbus_mv; // USB bus voltage in mV +uint16_t current_ma; // USB bus current to the heater in mA +uint16_t power; // Current power provided to the heater +uint16_t duty; // Current mosfet driver duty cycle (0-100%) +bool pwm = false; // PWM status (on-off) +bool enabled = false; // Power electronics enabled // convert the raw TPA191 adc reading to a current in milliamps @@ -449,10 +457,6 @@ __attribute__((noreturn)) int main(void) // No Power Delivery, maybe we are attached to a computer, so poll the input poll_input(); - static uint16_t vcc_mv = 3300; - static u16 tip_mv, tip_temp_c; - static s16 temp_c; - adc_injection_conversion(); temp_c = I16_FP_EMA_K4(temp_c, get_temp_c(adc_buffer[2])); @@ -498,18 +502,12 @@ __attribute__((noreturn)) int main(void) u8g2_ClearBuffer(u8g2); u8g2_SetFont(u8g2, u8g2_font_5x8_tr); - static bool pwm = false; // PWM status - static bool enabled = false; // Power electronics enabled static uint8_t count = 0; // Loop cycles with PWM on static s32 elapsed = 0; static bool prev_btn = true; bool btn = funDigitalRead(PIN_BTN); if (has_pd) { - static uint16_t vbus_mv, current_ma; - static int16_t temp_c, tip_temp_c; - static uint16_t power; - static uint16_t duty; vbus_mv = U16_FP_EMA_K4(vbus_mv, ((u32)adc_buffer[0]*vcc_mv*11)/4096); current_ma = U16_FP_EMA_K4(current_ma, get_current_ma(adc_buffer[1])); temp_c = I16_FP_EMA_K4(temp_c, get_temp_c(adc_buffer[2]));