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 {
inc/system/gpio.h
Show inline comments
 
@@ -38,17 +38,18 @@
 
#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
 
@@ -554,53 +554,57 @@ void display_process(void)
 

	
 
        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();
 
            }
 
@@ -610,25 +614,25 @@ void display_process(void)
 
                {
 
                    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)
 
@@ -640,49 +644,53 @@ void display_process(void)
 

	
 
        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 ]
 
@@ -833,25 +841,25 @@ static void draw_setpoint(therm_status_t
 
    // 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);
src/main.c
Show inline comments
 
@@ -28,39 +28,38 @@ int main(void)
 
 
    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)
src/system/gpio.c
Show inline comments
 
@@ -100,12 +100,30 @@ void user_input_signed(int32_t* to_modif
 
        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
 
@@ -32,24 +32,25 @@ void tempsense_readtemp(void)
 
        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)
0 comments (0 inline, 0 general)