Changeset - 9ae5559ab974
[Not reviewed]
default
0 6 0
matthewreed - 6 years ago 2018-12-30 21:46:42

Added current setpoint number to heating screens, and a few fixes for floating point numbers and such.
6 files changed with 39 insertions and 12 deletions:
0 comments (0 inline, 0 general)
inc/states.h
Show inline comments
 
#ifndef STATES_H
 
#define STATES_H
 

	
 
#include "stm32f3xx_hal.h"
 
#include "config.h"
 

	
 
typedef struct {
 
    float temp;
 
    uint8_t state_resume;
 
    uint8_t state;
 
    int32_t setpoint;
 
    float setpoint;
 
    uint8_t pid_enabled;
 
    uint8_t error_code;
 
    uint8_t setpoint_index;
 
} therm_status_t;
 

	
 
typedef union
 
{
 
     struct {
 
        uint32_t boottobrew;
 
        uint32_t temp_units;
 
        uint32_t windup_guard;
 
        uint32_t k_p;
 
        uint32_t k_i;
 
        uint32_t k_d;
 
        int32_t temp_offset; //TODO: convert to float
 
        float temp_offset;
 
        uint32_t ignore_error;
 
        int32_t setpoints[MAX_SETPOINTS];
 
        float setpoints[MAX_SETPOINTS];
 
        uint32_t control_mode;
 
        uint32_t sensor_type;
 
        uint32_t plant_type;
 
        uint32_t hysteresis;
 
        uint32_t setpoint_count;
 
        uint32_t setpoint_aux_select_enable;
 
    } val;
 

	
 
    uint16_t data[128];
 
} therm_settings_t;
 

	
 
enum tempunits {
 
    TEMP_UNITS_CELSIUS = 0,
 
    TEMP_UNITS_FAHRENHEIT,
 
};
 

	
 
enum state {
 
    STATE_IDLE = 0,
 

	
 
	STATE_SETSENSORTYPE,
 
    STATE_SETMODE,
 
    STATE_SETPLANTTYPE,
 
    STATE_SETSETPOINTCOUNT,
 
    STATE_SETSETPOINTAUXSELECTENABLE,
inc/system/gpio.h
Show inline comments
 
@@ -26,29 +26,30 @@
 
#define SW_C_Pin GPIO_PIN_7
 
#define SW_C_GPIO_Port GPIOA
 
#define SW_DOWN SW_C_GPIO_Port, SW_C_Pin
 
 
#define SW_BTN_Pin GPIO_PIN_0
 
#define SW_BTN_GPIO_Port GPIOB
 
#define SW_BTN SW_BTN_GPIO_Port , SW_BTN_Pin
 
 
#define LED_PIN GPIO_PIN_6
 
#define LED_GPIO_Port GPIOB
 
#define LED LED_GPIO_Port, LED_PIN
 
 
#define AUX_INPUT_Pin GPIO_PIN_10
 
#define AUX_INPUT_GPIO_Port GPIOA
 
#define AUX_INPUT AUX_INPUT_GPIO_Port , AUX_INPUT_Pin
 
 
#define AUX_RETURN_Pin GPIO_PIN_9
 
#define AUX_RETURN_GPIO_Port GPIOA
 
#define AUX_RETURN AUX_RETURN_GPIO_Port , AUX_RETURN_Pin
 
 
 
void user_input(uint16_t* to_modify);
 
void user_input_min_max(uint16_t* to_modify, uint16_t min, uint16_t max);
 
void user_input_signed(int32_t* to_modify);
 
void user_input_float(float* to_modify);
 
 
void gpio_init(void);
 
void gpio_led_blueblink(uint8_t num_blinks);
 
 
#endif
src/display.c
Show inline comments
 
@@ -542,159 +542,167 @@ void display_process(void)
 
            else if(!HAL_GPIO_ReadPin(SW_UP)) {
 
                set->val.temp_units = TEMP_UNITS_FAHRENHEIT;
 
            }
 
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
                set->val.temp_units = TEMP_UNITS_CELSIUS;
 
            }
 

	
 
            // Event Handler
 
            // N/A
 

	
 
        } break;
 

	
 

	
 
        case STATE_SETTEMPOFFSET:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set temp offset ]
 
            // [ g = 12         ]
 
            ssd1306_drawstring("Temp Cal Offset", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            ssd1306_drawchar(updown(), 1, 52);
 

	
 
            char tempstr[12];
 
            snprintf(tempstr, 12, "O=%ld", set->val.temp_offset);
 
            snprintf(tempstr, 12, "O=%.1f  ", set->val.temp_offset);
 
            ssd1306_drawstring(tempstr, 1, 60);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                flash_savesettings();
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input_signed(&set->val.temp_offset);
 
                user_input_float(&set->val.temp_offset);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 

	
 
        } break;
 

	
 

	
 
        case STATE_PREHEAT:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating brew ]
 
            // [ 30 => 120 C             ]
 
            if(set->val.plant_type == PLANT_HEATER)
 
                ssd1306_drawstring("Preheating...", 0, 0);
 
                ssd1306_drawstring("Preheat...", 0, 0);
 
            else
 
                ssd1306_drawstring("Precooling...", 0, 0);
 
                ssd1306_drawstring("Precool...", 0, 0);
 

	
 
            char tempstr[4];
 
            snprintf(tempstr, 4, "#%d  ", (status->setpoint_index + 1));
 
            ssd1306_drawstring(tempstr, 0, 65);
 

	
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 

	
 
            status->pid_enabled = 1;
 
            status->setpoint = set->val.setpoints[status->setpoint_index];
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
                flash_savesettings();
 
            }
 
            else if (!(set->val.setpoint_aux_select_enable) && SW_LEFT_PRESSED)
 
            {
 
                if (status->setpoint_index > 0)
 
                {
 
                    status->setpoint_index--;
 
                }
 
            }
 
            else if (!(set->val.setpoint_aux_select_enable) && SW_RIGHT_PRESSED)
 
            {
 
                if (status->setpoint_index < (set->val.setpoint_count - 1))
 
                {
 
                    status->setpoint_index++;
 
                }
 
            }
 
            else {
 
                user_input_signed(&set->val.setpoints[status->setpoint_index]);
 
                user_input_float(&set->val.setpoints[status->setpoint_index]);
 
            }
 

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN;
 
            }
 

	
 
            if(set->val.setpoint_aux_select_enable && aux_input)
 
            {
 
                status->setpoint_index = 1;
 
            }
 
            else if(set->val.setpoint_aux_select_enable && !aux_input)
 
            {
 
                status->setpoint_index = 0;
 
            }
 

	
 
        } break;
 

	
 
        case STATE_MAINTAIN:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to brew ]
 
            // [ 30 => 120 C           ]
 

	
 
            if(set->val.plant_type == PLANT_HEATER)
 
                ssd1306_drawstring("Preheated!", 0, 0);
 
            else
 
                ssd1306_drawstring("Precooled!", 0, 0);
 

	
 
            char tempstr[4];
 
            snprintf(tempstr, 4, "#%d  ", (status->setpoint_index + 1));
 
            ssd1306_drawstring(tempstr, 0, 65);
 

	
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
            status->setpoint = set->val.setpoints[status->setpoint_index];
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
                flash_savesettings();
 
            }
 
            else if (!(set->val.setpoint_aux_select_enable) && SW_LEFT_PRESSED)
 
            {
 
                if (status->setpoint_index > 0)
 
                {
 
                    status->setpoint_index--;
 
                }
 
            }
 
            else if (!(set->val.setpoint_aux_select_enable) && SW_RIGHT_PRESSED)
 
            {
 
                if (status->setpoint_index < (set->val.setpoint_count - 1))
 
                {
 
                    status->setpoint_index++;
 
                }
 
            }
 
            else {
 
                user_input_signed(&set->val.setpoints[status->setpoint_index]);
 
                user_input_float(&set->val.setpoints[status->setpoint_index]);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 

	
 
        } break;
 

	
 
        // Thermocouple error
 
        case STATE_TC_ERROR:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to steam ]
 
            // [ 30 => 120 C            ]
 
            ssd1306_drawstring("Error:              ", 0, 0);
 

	
 
            char tempstr[6];
 
            itoa(status->error_code, tempstr, 10);
 
            ssd1306_drawstring(tempstr, 0, 57);
 

	
 
            //TODO: add RTD error codes
 

	
 
            if(status->error_code == 1)
 
                ssd1306_drawstring("    TC Open Circuit", 1, 0);
 
            else if(status->error_code == 4)
 
@@ -821,40 +829,40 @@ void display_process(void)
 
    sw_up_last = sw_up;
 
    sw_down_last = sw_down;
 
    sw_left_last = sw_left;
 
    sw_right_last = sw_right;
 
}
 

	
 

	
 
static float temp_last = 43002.0;
 
static float setpoint_last = 10023.0;
 

	
 
// Draw current setpoint on display
 
static void draw_setpoint(therm_status_t* status) {
 
    // FIXME: need to do this when switching modes too
 
    if(status->temp != temp_last || trigger_drawsetpoint) {
 
        char tempstr[8];
 
        snprintf(tempstr, 8, "%4.1f", status->temp);
 
        ssd1306_drawstringbig(tempstr, 3, 0);
 
    }
 

	
 
    if(trigger_drawsetpoint) 
 
        ssd1306_drawstringbig(">", 3, 74);
 

	
 
    if(status->setpoint != setpoint_last || trigger_drawsetpoint) {
 
        char tempstr[4];
 
        snprintf(tempstr, 4, "%ld     ", status->setpoint);
 
        snprintf(tempstr, 4, "%3.0f     ", status->setpoint);
 
        ssd1306_drawstringbig(tempstr, 3, 90);
 
    }
 

	
 
    trigger_drawsetpoint = 0;
 
    setpoint_last = status->setpoint;
 
    temp_last = status->temp;
 
}
 

	
 
void display_startup_screen() {
 
    ssd1306_clearscreen();
 
    ssd1306_drawstring("therm v0.4", 1, 40);
 
    ssd1306_drawstring("protofusion.org/therm", 3, 0);
 
}
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
src/main.c
Show inline comments
 
@@ -16,63 +16,62 @@
 
#include "error.h"
 
#include "flash.h"
 
#include "ssd1306/ssd1306.h"
 
#include "pwmout.h"
 
 
 
int main(void)
 
{
 
    sysclock_init();
 
    hal_init();
 
    gpio_init();
 
    flash_init();
 
 
    ssd1306_init();
 
 
    // Startup screen
 
    display_startup_screen();
 
    HAL_Delay(2000);
 
    ssd1306_clearscreen();
 
    //ssd1306_drawlogo();
 
 
    // Default status
 
    runtime_status()->temp = 0.0;
 
    runtime_status()->state_resume = 0;
 
    runtime_status()->setpoint = 70;
 
    runtime_status()->setpoint = 70.0;
 
    runtime_status()->pid_enabled = 0;
 
    runtime_status()->setpoint_index = 0;
 
    if (flash_getsettings()->val.boottobrew)
 
    {
 
        runtime_status()->state = STATE_PREHEAT;
 
    }
 
    else
 
    {
 
        runtime_status()->state = STATE_IDLE;
 
    }
 
 
    pid_init();
 
    pwmout_init();
 
    flash_init();
 
    watchdog_init();
 
    tempsense_init();
 
 
    // Soft timers
 
    uint32_t last_pid = 0;
 
    uint32_t last_thermostat = 0;
 
    uint32_t last_1hz = 0;
 
    uint32_t last_5hz = 0;
 
 
    int16_t duty = 0;
 
 
    while (1)
 
    {
 
 
        if(HAL_GetTick() - last_1hz > 750)
 
        {
 
            display_1hz();
 
            last_1hz = HAL_GetTick();
 
        }
 
 
        if(HAL_GetTick() - last_5hz > 200)
 
        {
 
            tempsense_readtemp();
 
            runtime_status()->temp = tempsense_gettemp();
src/system/gpio.c
Show inline comments
 
@@ -88,24 +88,42 @@ void user_input_min_max(uint16_t* to_mod
 
            CHANGE_RESET;
 
            (*to_modify)--;
 
        }
 
    }
 
}
 
 
 
// Increment/decrement signed variable with up/down buttons
 
void user_input_signed(int32_t* to_modify)
 
{
 
    //fixme: need to cast to 16/32 bits correctly
 
    if(CHANGE_ELAPSED) {
 
        if(!HAL_GPIO_ReadPin(SW_UP) ) {
 
            CHANGE_RESET;
 
            if (*to_modify < 32768)
 
                (*to_modify)++;
 
        }
 
        else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
            CHANGE_RESET;
 
            if (*to_modify >= -32768)
 
                (*to_modify)--;
 
        }
 
    }
 
}
 
 
// Increment/decrement signed variable with up/down buttons
 
void user_input_float(float* to_modify)
 
{
 
    //fixme: need to cast to 16/32 bits correctly
 
    if(CHANGE_ELAPSED) {
 
        if(!HAL_GPIO_ReadPin(SW_UP) ) {
 
            CHANGE_RESET;
 
            if (*to_modify < 32768)
 
                (*to_modify)++;
 
        }
 
        else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
            CHANGE_RESET;
 
            if (*to_modify >= -32768)
 
                (*to_modify)--;
 
        }
 
    }
 
}
src/tempsense.c
Show inline comments
 
@@ -20,44 +20,45 @@ void tempsense_init(void)
 
    // we need to re-init...
 
}
 

	
 

	
 
// Request reading from configured temperature sensor
 
void tempsense_readtemp(void)
 
{
 

	
 
    switch(flash_getsettings()->val.sensor_type)
 
    {
 
        case SENSOR_TC_K:
 
        case SENSOR_TC_E:
 
        case SENSOR_TC_N:
 
        case SENSOR_TC_R:
 
        case SENSOR_TC_S:
 
        case SENSOR_TC_T:
 
        {
 
            // Read MAX31856
 
            max31856_process();
 
        } break;
 

	
 
        case SENSOR_NTC:
 
        {
 
            // Read analog value from internal ADC, linearize the reading, etc
 
            //TODO
 
        } break;
 

	
 
    }
 

	
 
    // either return latest reading from DMA loop (NTC, etc)
 
    // or initiate a blocking read and return it.
 
    // Need to gracefully handle the timeout...
 
}
 

	
 

	
 
// Get latest temperature in requested units
 
float tempsense_gettemp(void)
 
{
 
    float temp_converted = max31856_latest_temp();
 

	
 
    if(flash_getsettings()->val.temp_units == TEMP_UNITS_FAHRENHEIT)
 
        temp_converted = temp_converted * 1.8f + 32.0f;
 

	
 
    return temp_converted;
 
}
0 comments (0 inline, 0 general)