# HG changeset patch
# User matthewreed
# Date 2018-12-30 14:46:34
# Node ID 574fd84a2bf8d4e7e18ed25781c5d62f24f86170
# Parent  b61e1f4d75c5f7940869037bfc1bdde0206ec447
Added options for number of setpoints and aux input enable
diff --git a/.cproject b/.cproject
--- a/.cproject
+++ b/.cproject
@@ -26,7 +26,7 @@
 							
 							
 							
-							
+							
 							
 							
 							
@@ -44,6 +44,7 @@
 							
 							
 							
+							
 							
 							
 								
@@ -318,7 +319,7 @@
 				
 			
 			
-				
+				
 					
 						
 							
diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml
--- a/.settings/language.settings.xml
+++ b/.settings/language.settings.xml
@@ -5,7 +5,7 @@
 			
 			
 			
-			
+			
 				
 				
 			
@@ -16,7 +16,7 @@
 			
 			
 			
-			
+			
 				
 				
 			
diff --git a/inc/config.h b/inc/config.h
--- a/inc/config.h
+++ b/inc/config.h
@@ -23,6 +23,7 @@
 // Interval of PID calculations
 #define PID_PERIOD 120
 
+#define MAX_SETPOINTS 10
 
 //////////////////////////////////////////////////////
 // Other settings
@@ -36,17 +37,19 @@
 // Default Settings
 //////////////////////////////////////////////////////
 
+//TODO: add defaults for all settings
 #define DEFAULT_BOOT_TO_BREW 0
 #define DEFAULT_TEMP_UNITS TEMP_UNITS_FAHRENHEIT
-#define DEFAULT_WINDUP_GUARD 10
-#define DEFAULT_K_P 10
-#define DEFAULT_K_I 1
-#define DEFAULT_K_D 1
+#define DEFAULT_WINDUP_GUARD 300
+#define DEFAULT_K_P 100
+#define DEFAULT_K_I 2
+#define DEFAULT_K_D 0
 #define DEFAULT_TEMP_OFFSET 0
 #define DEFAULT_IGNORE_ERROR 0
-#define DEFAULT_SETPOINT_BREW 70
-#define DEFAULT_SETPOINT_STEAM 70
+#define DEFAULT_SETPOINT 70
 #define DEFAULT_HYSTERESIS 1
+#define DEFAULT_SETPOINT_COUNT 1
+#define DEFAULT_SETPOINT_AUX_SELECT_ENABLE 0
 
 
 //////////////////////////////////////////////////////
diff --git a/inc/display.h b/inc/display.h
--- a/inc/display.h
+++ b/inc/display.h
@@ -2,6 +2,11 @@
 #define DISPLAY_H
 
 #include "states.h"
+#include "gpio.h"
+#include "ssd1306/ssd1306.h"
+#include "system/stringhelpers.h"
+#include "flash.h"
+#include "stdlib.h"
 
 #ifdef MAX31865_RTD_SENSOR
 #include "max31865.h"
diff --git a/inc/pid.h b/inc/pid.h
--- a/inc/pid.h
+++ b/inc/pid.h
@@ -3,6 +3,8 @@
 
 #include "stm32f3xx_hal.h"
 #include "states.h"
+#include "flash.h"
+#include "display.h"
 
 typedef struct {
 	int16_t last_pid_temp;
diff --git a/inc/pwmout.h b/inc/pwmout.h
--- a/inc/pwmout.h
+++ b/inc/pwmout.h
@@ -1,6 +1,10 @@
 #ifndef PWMOUT_H
 #define PWMOUT_H
 
+#include "stm32f3xx_hal.h"
+#include "gpio.h"
+#include "flash.h"
+#include "error.h"
 
 #define SSR_PIN GPIO_PIN_7
 #define SSR_GPIO_Port GPIOB
diff --git a/inc/states.h b/inc/states.h
--- a/inc/states.h
+++ b/inc/states.h
@@ -8,9 +8,10 @@ typedef struct {
     float temp;
     uint8_t state_resume;
     uint8_t state;
-    float setpoint;
+    int32_t setpoint;
     uint8_t pid_enabled;
     uint8_t error_code;
+    uint8_t setpoint_index;
 } therm_status_t;
 
 typedef union
@@ -22,14 +23,15 @@ typedef union
         uint32_t k_p;
         uint32_t k_i;
         uint32_t k_d;
-        int32_t temp_offset;
+        int32_t temp_offset; //TODO: convert to float
         uint32_t ignore_error;
-        int32_t setpoint_brew;
-        int32_t setpoint_steam;
+        int32_t 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];
@@ -46,6 +48,8 @@ enum state {
 	STATE_SETSENSORTYPE,
     STATE_SETMODE,
     STATE_SETPLANTTYPE,
+    STATE_SETSETPOINTCOUNT,
+    STATE_SETSETPOINTAUXSELECTENABLE,
     STATE_SETHYSTERESIS,
     STATE_SETP,
     STATE_SETI,
@@ -73,6 +77,11 @@ enum plant_type {
     PLANT_COOLER,
 };
 
+enum aux_select {
+    AUX_DISABLE = 0,
+    AUX_ENABLE,
+};
+
 enum GOTO_MODE {
 	#ifdef BOOTLOADER_SHORTCUT
 	MODE_BOOTLOADER,
diff --git a/inc/system/gpio.h b/inc/system/gpio.h
--- a/inc/system/gpio.h
+++ b/inc/system/gpio.h
@@ -35,8 +35,17 @@
 #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 gpio_init(void);
diff --git a/inc/thermostat.h b/inc/thermostat.h
--- a/inc/thermostat.h
+++ b/inc/thermostat.h
@@ -2,6 +2,10 @@
 #define THERMOSTAT_H
 
 #include "stm32f3xx_hal.h"
+#include "flash.h"
+#include "gpio.h"
+#include "stdlib.h"
+#include "display.h"
 
 
 float thermostat_process(void);
diff --git a/lib/ssd1306/ssd1306.c b/lib/ssd1306/ssd1306.c
--- a/lib/ssd1306/ssd1306.c
+++ b/lib/ssd1306/ssd1306.c
@@ -326,9 +326,9 @@ void ssd1306_drawlogo()
 // Print a single character
 void ssd1306_drawchar(unsigned char ascii, unsigned char row, unsigned char xPos)
 {
-    const unsigned char *srcPointer = (char*)-1;
+    const unsigned char *srcPointer = (unsigned char*)-1;
 
-    srcPointer = &fontData[(ascii-32)][0];
+    srcPointer = (unsigned char*)&fontData[(ascii-32)][0];
 
     setStartPage(row);
     setStartColumn(xPos);
@@ -407,7 +407,7 @@ void ssd1306_drawcharbig(unsigned char a
 
 
 // Print a string to the display
-void ssd1306_drawstring(const unsigned char *dataPtr, unsigned char row, unsigned char xPos)
+void ssd1306_drawstring(const char *dataPtr, unsigned char row, unsigned char xPos)
 {
     unsigned char *srcPointer;
 
@@ -425,7 +425,7 @@ void ssd1306_drawstring(const unsigned c
 
 
 // Print a string to the display, big font
-void ssd1306_drawstringbig(const unsigned char *dataPtr, unsigned char row, unsigned char xPos)
+void ssd1306_drawstringbig(const char *dataPtr, unsigned char row, unsigned char xPos)
 {
     char *srcPointer;
 
diff --git a/lib/ssd1306/ssd1306.h b/lib/ssd1306/ssd1306.h
--- a/lib/ssd1306/ssd1306.h
+++ b/lib/ssd1306/ssd1306.h
@@ -36,8 +36,8 @@ SPI_HandleTypeDef* spi_get();
 void ssd1306_init(void);
 void ssd1306_drawchar(unsigned char ascii, unsigned char row, unsigned char xPos);
 void ssd1306_drawcharbig(unsigned char ascii, unsigned char row, unsigned char xPos);
-void ssd1306_drawstring(const unsigned char *dataPtr, unsigned char row, unsigned char xPos);
-void ssd1306_drawstringbig(const unsigned char *dataPtr, unsigned char row, unsigned char xPos);
+void ssd1306_drawstring(const char *dataPtr, unsigned char row, unsigned char xPos);
+void ssd1306_drawstringbig(const char *dataPtr, unsigned char row, unsigned char xPos);
 void ssd1306_drawlogo();
 void ssd1306_clearscreen();
 
diff --git a/src/display.c b/src/display.c
--- a/src/display.c
+++ b/src/display.c
@@ -3,10 +3,6 @@
 //
 
 #include "display.h"
-#include "gpio.h"
-#include "ssd1306/ssd1306.h"
-#include "system/stringhelpers.h"
-#include "flash.h"
 
 // Private function prototypes
 static void draw_setpoint(therm_status_t* status);
@@ -30,9 +26,8 @@ static uint8_t sw_right_last = 0;
 
 // States
 static uint8_t trigger_drawsetpoint = 1;
-static int16_t last_temp = 21245;
-static int16_t last_temp_frac = 21245;
-static int16_t last_state = 123;
+static float last_temp = 21245;
+static int16_t last_state = STATE_RESET;
 static uint8_t goto_mode = MODE_HEAT;
 static uint8_t reset_mode = RESET_REBOOT;
 
@@ -72,6 +67,8 @@ void display_process(void)
     uint8_t sw_left = !HAL_GPIO_ReadPin(SW_LEFT);
     uint8_t sw_right = !HAL_GPIO_ReadPin(SW_RIGHT);
 
+    uint8_t aux_input = !HAL_GPIO_ReadPin(AUX_INPUT);
+
     switch(status->state)
     {
         // Idle state
@@ -115,12 +112,12 @@ void display_process(void)
                     ssd1306_drawstring("\x83 reset    ", 1, 40);
                 } break;
 
-#ifdef BOOTLOADER_SHORTCUT
+                #ifdef BOOTLOADER_SHORTCUT
                 case MODE_BOOTLOADER:
                 {
                     ssd1306_drawstring("\x83 dfu      ", 1, 40);
                 }
-#endif
+                #endif
             }
 
             // Button handler
@@ -136,7 +133,7 @@ void display_process(void)
                         status->state = STATE_RESET;
                         reset_mode = RESET_REBOOT;
                         break;
-#ifdef BOOTLOADER_SHORTCUT
+                    #ifdef BOOTLOADER_SHORTCUT
                     case MODE_BOOTLOADER:
                         ssd1306_clearscreen();
                         ssd1306_drawstring("Bootloader Entered", 0, 0);
@@ -145,7 +142,7 @@ void display_process(void)
                         //                        bootloader_enter(); // Resets into bootloader
                         status->state = STATE_RESET; // Just in case
                         break;
-#endif
+                    #endif
                     default:
                         status->state = STATE_PREHEAT;
                 }
@@ -157,7 +154,6 @@ void display_process(void)
                 goto_mode--;
             }
 
-
             // Event Handler
             // N/A
 
@@ -246,10 +242,7 @@ void display_process(void)
 
             // Button handler
             if(SW_BTN_PRESSED) {
-                if(set->val.control_mode == MODE_PID)
-                    status->state = STATE_SETP;
-                else
-                    status->state = STATE_SETHYSTERESIS;
+                status->state = STATE_SETSETPOINTCOUNT;
             }
             else if (!HAL_GPIO_ReadPin(SW_UP)) {
                 set->val.plant_type = PLANT_COOLER;
@@ -263,6 +256,86 @@ void display_process(void)
         } break;
 
 
+        case STATE_SETSETPOINTCOUNT:
+        {
+            // Write text to OLED
+            // [ therm :: number of setpoints ]
+            // [ p = 12         ]
+            ssd1306_drawstring("Num Setpoints", 0, 40);
+            ssd1306_drawlogo();
+
+            ssd1306_drawchar(updown(), 1, 52);
+
+            char tempstr[12];
+            snprintf(tempstr, 12, "%ld ", set->val.setpoint_count);
+            ssd1306_drawstring(tempstr, 1, 60);
+            ssd1306_drawstring("Press to accept", 3, 40);
+
+            // Button handler
+            if(SW_BTN_PRESSED) {
+                if (set->val.setpoint_count > 1)
+                {
+                    status->state = STATE_SETSETPOINTAUXSELECTENABLE;
+                }
+                else
+                {
+                    if(set->val.control_mode == MODE_PID)
+                        status->state = STATE_SETP;
+                    else
+                        status->state = STATE_SETHYSTERESIS;
+                }
+            }
+            else {
+                user_input_min_max((uint16_t*)&set->val.setpoint_count, 1, MAX_SETPOINTS);
+
+                //update setpoint index to the new maximum
+                if (status->setpoint_index >= set->val.setpoint_count)
+                {
+                    status->setpoint_index = set->val.setpoint_count - 1;
+                }
+            }
+
+            // Event Handler
+            // N/A
+
+        } break;
+
+
+        case STATE_SETSETPOINTAUXSELECTENABLE:
+        {
+            // Write text to OLED
+            // [ therm :: set p ]
+            // [ p = 12         ]
+            ssd1306_drawstring("Aux Select", 0, 40);
+            ssd1306_drawlogo();
+
+            ssd1306_drawchar(updown(), 1, 52);
+
+            if(set->val.setpoint_aux_select_enable == AUX_ENABLE)
+                ssd1306_drawstring("Enable ", 1, 60);
+            else
+                ssd1306_drawstring("Disable", 1, 60);
+
+            ssd1306_drawstring("Press to accept", 3, 40);
+
+            // Button handler
+            if(SW_BTN_PRESSED) {
+                if(set->val.control_mode == MODE_PID)
+                    status->state = STATE_SETP;
+                else
+                    status->state = STATE_SETHYSTERESIS;
+            }
+            else if (!HAL_GPIO_ReadPin(SW_UP)) {
+                set->val.setpoint_aux_select_enable = AUX_ENABLE;
+            }
+            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
+                set->val.setpoint_aux_select_enable = AUX_DISABLE;
+            }
+
+            // Event Handler
+            // N/A
+
+        } break;
 
 
         case STATE_SETP:
@@ -276,7 +349,7 @@ void display_process(void)
             ssd1306_drawchar(updown(), 1, 52);
 
             char tempstr[12];
-            snprintf(tempstr, 12, "P=%d", set->val.k_p);
+            snprintf(tempstr, 12, "P=%ld", set->val.k_p);
             ssd1306_drawstring(tempstr, 1, 60);
             ssd1306_drawstring("Press to accept", 3, 40);
 
@@ -304,7 +377,7 @@ void display_process(void)
             ssd1306_drawchar(updown(), 1, 52);
 
             char tempstr[12];
-            snprintf(tempstr, 12, "I=%d", set->val.k_i);
+            snprintf(tempstr, 12, "I=%ld", set->val.k_i);
             ssd1306_drawstring(tempstr, 1, 60);
 
             ssd1306_drawstring("Press to accept", 3, 40);
@@ -333,7 +406,7 @@ void display_process(void)
             ssd1306_drawchar(updown(), 1, 52);
 
             char tempstr[12];
-            snprintf(tempstr, 12, "D=%d", set->val.k_d);
+            snprintf(tempstr, 12, "D=%ld", set->val.k_d);
             ssd1306_drawstring(tempstr, 1, 60);
 
             ssd1306_drawstring("Press to accept", 3, 40);
@@ -362,7 +435,7 @@ void display_process(void)
             ssd1306_drawchar(updown(), 1, 52);
 
             char tempstr[12];
-            snprintf(tempstr, 12, "H=%d", set->val.hysteresis);
+            snprintf(tempstr, 12, "H=%ld", set->val.hysteresis);
             ssd1306_drawstring(tempstr, 1, 60);
 
             ssd1306_drawstring("Press to accept", 3, 40);
@@ -392,7 +465,7 @@ void display_process(void)
             ssd1306_drawchar(updown(), 1, 52);
 
             char tempstr[12];
-            snprintf(tempstr, 12, "G=%d", set->val.windup_guard);
+            snprintf(tempstr, 12, "G=%ld", set->val.windup_guard);
             ssd1306_drawstring(tempstr, 1, 60);
 
             ssd1306_drawstring("Press to accept", 3, 40);
@@ -490,7 +563,7 @@ void display_process(void)
             ssd1306_drawchar(updown(), 1, 52);
 
             char tempstr[12];
-            snprintf(tempstr, 12, "O=%d", set->val.temp_offset);
+            snprintf(tempstr, 12, "O=%ld", set->val.temp_offset);
             ssd1306_drawstring(tempstr, 1, 60);
 
             ssd1306_drawstring("Press to accept", 3, 40);
@@ -524,15 +597,29 @@ void display_process(void)
             draw_setpoint(status);
 
             status->pid_enabled = 1;
-            status->setpoint = set->val.setpoint_brew;
+            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.setpoint_brew);
+                user_input_signed(&set->val.setpoints[status->setpoint_index]);
             }
 
             // Event Handler
@@ -540,6 +627,15 @@ void display_process(void)
                 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:
@@ -555,15 +651,29 @@ void display_process(void)
 
             draw_setpoint(status);
             status->pid_enabled = 1;
-            status->setpoint = set->val.setpoint_brew;
+            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.setpoint_brew);
+                user_input_signed(&set->val.setpoints[status->setpoint_index]);
             }
 
             // Event Handler
@@ -601,9 +711,9 @@ void display_process(void)
             // Button handler
             if(SW_BTN_PRESSED) {
                 status->state = STATE_IDLE;
-#ifdef MAX31865_RTD_SENSOR
+                #ifdef MAX31865_RTD_SENSOR
                 max31865_clear_errors(spi_get());
-#endif
+                #endif
             }
             else if(SW_RIGHT_PRESSED) {
                 set->val.ignore_error = 1;
@@ -732,7 +842,7 @@ static void draw_setpoint(therm_status_t
 
     if(status->setpoint != setpoint_last || trigger_drawsetpoint) {
         char tempstr[4];
-        snprintf(tempstr, 4, "%g     ", status->setpoint);
+        snprintf(tempstr, 4, "%ld     ", status->setpoint);
         ssd1306_drawstringbig(tempstr, 3, 90);
     }
 
diff --git a/src/main.c b/src/main.c
--- a/src/main.c
+++ b/src/main.c
@@ -1,7 +1,7 @@
 //
 // Therm Firmware
 // Copyright 2018 Ethan Zonca
-// Author(s): Ethan Zonca
+// Author(s): Ethan Zonca and Matthew Reed
 //
 
 #include "stm32f3xx_hal.h"
@@ -24,6 +24,7 @@ int main(void)
     sysclock_init();
     hal_init();
     gpio_init();
+    flash_init();
 
     ssd1306_init();
 
@@ -31,14 +32,22 @@ int main(void)
     display_startup_screen();
     HAL_Delay(2000);
     ssd1306_clearscreen();
-    ssd1306_drawlogo();
+    //ssd1306_drawlogo();
 
     // Default status
     runtime_status()->temp = 0.0;
     runtime_status()->state_resume = 0;
-    runtime_status()->state = STATE_IDLE;
     runtime_status()->setpoint = 70;
     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();
diff --git a/src/pid.c b/src/pid.c
--- a/src/pid.c
+++ b/src/pid.c
@@ -3,7 +3,6 @@
 //
 
 #include "pid.h"
-#include "flash.h"
 
 // Private variables
 static pid_state_t state;
@@ -42,6 +41,7 @@ float pid_process(void)
         ssr_output = power_percent; //(((uint32_t)SSR_PERIOD * (uint32_t)10 * (uint32_t)100) * power_percent) / (uint32_t)1000000;
 
         // put ssr output on display
+        //TODO: move to display
         ssd1306_drawstring("      ", 0, 90); //fixme: this is bad, but I can't get the old digits to clear otherwise
         char tempstr[8];
         snprintf(tempstr, 8, "%4.1f%%", ssr_output/10.0);
@@ -52,7 +52,8 @@ float pid_process(void)
         ssr_output = 0.0;
     }
 
-    return ssr_output; //ssr_output;
+    return ssr_output; //ssr_output;=== START OF INFORMATION SECTION ===
+
 }
 
 
diff --git a/src/pwmout.c b/src/pwmout.c
--- a/src/pwmout.c
+++ b/src/pwmout.c
@@ -2,11 +2,7 @@
 // PWM Out: generate PWM waveform to control factory
 //
 
-#include "stm32f3xx_hal.h"
 #include "pwmout.h"
-#include "gpio.h"
-#include "flash.h"
-#include "error.h"
 
 
 // Private variables
diff --git a/src/system/flash.c b/src/system/flash.c
--- a/src/system/flash.c
+++ b/src/system/flash.c
@@ -42,11 +42,11 @@ void flash_savesettings()
     uint16_t writectr;
     for(writectr = 0; writectr < 128; writectr++)// 128 bytes data
     {
-        HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom_emulation+writectr,settings.data[writectr]);
+        HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, (uint32_t)(eeprom_emulation+writectr), settings.data[writectr]);
     }
 
     // Write magic value to flash
-    HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom_emulation+FLASH_MAGIC_LOC,FLASH_MAGIC_VALUE);
+    HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, (uint32_t)(eeprom_emulation+FLASH_MAGIC_LOC),FLASH_MAGIC_VALUE);
 
     // Lock flash memory
     HAL_FLASH_Lock();
@@ -68,14 +68,15 @@ void flash_restoresettings(void)
         }
     }
     // No data in flash! Set defaults here
+    //TODO: Set all defaults
     else
     {
-        settings.val.k_p = 100;
-        settings.val.k_i = 2;
-        settings.val.k_d = 0;
-        settings.val.windup_guard = 300;
+        settings.val.k_p = DEFAULT_K_P;
+        settings.val.k_i = DEFAULT_K_I;
+        settings.val.k_d = DEFAULT_K_D;
+        settings.val.windup_guard = DEFAULT_WINDUP_GUARD;
         settings.val.sensor_type = 1;
-        //torestore.values.can_id = 22;
+        settings.val.setpoint_count = DEFAULT_SETPOINT_COUNT;
     }
 }
 
diff --git a/src/system/gpio.c b/src/system/gpio.c
--- a/src/system/gpio.c
+++ b/src/system/gpio.c
@@ -30,11 +30,15 @@ void gpio_init(void)
     HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
 
 
-    GPIO_InitStruct.Pin = SW_A_Pin|SW_B_Pin|SW_C_Pin|SW_D_Pin;
+    GPIO_InitStruct.Pin = SW_A_Pin|SW_B_Pin|SW_C_Pin|SW_D_Pin|AUX_INPUT_Pin;
     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     HAL_GPIO_Init(SW_C_GPIO_Port, &GPIO_InitStruct);
 
+    GPIO_InitStruct.Pin = AUX_RETURN_Pin;
+    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+    HAL_GPIO_Init(AUX_RETURN_GPIO_Port, &GPIO_InitStruct);
+
     GPIO_InitStruct.Pin = SW_BTN_Pin;
     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
@@ -50,6 +54,7 @@ void gpio_init(void)
 
     // Define startup State
     HAL_GPIO_WritePin(LED, 0);
+    HAL_GPIO_WritePin(AUX_RETURN, 0);
 
 }
 
@@ -69,6 +74,23 @@ void user_input(uint16_t* to_modify)
     }
 }
 
+// Increment/decrement unsigned variable with up/down buttons
+void user_input_min_max(uint16_t* to_modify, uint16_t min, uint16_t max)
+{
+    if(CHANGE_ELAPSED) {
+        if(!HAL_GPIO_ReadPin(SW_UP) && ((*to_modify) < max))
+        {
+            CHANGE_RESET;
+            (*to_modify)++;
+        }
+        else if(!HAL_GPIO_ReadPin(SW_DOWN) && ((*to_modify) > min))
+        {
+            CHANGE_RESET;
+            (*to_modify)--;
+        }
+    }
+}
+
 
 // Increment/decrement signed variable with up/down buttons
 void user_input_signed(int32_t* to_modify)
diff --git a/src/system/interrupts.c b/src/system/interrupts.c
--- a/src/system/interrupts.c
+++ b/src/system/interrupts.c
@@ -22,11 +22,13 @@ void EXTI9_5_IRQHandler(void)
     HAL_GPIO_EXTI_IRQHandler(SW_B_Pin);
     HAL_GPIO_EXTI_IRQHandler(SW_A_Pin);
     HAL_GPIO_EXTI_IRQHandler(SW_C_Pin);
+    HAL_GPIO_EXTI_IRQHandler(AUX_RETURN_Pin);
 }
 
 void EXTI15_10_IRQHandler(void)
 {
     HAL_GPIO_EXTI_IRQHandler(SW_BTN_Pin);
+    HAL_GPIO_EXTI_IRQHandler(AUX_INPUT_Pin);
 }
 
 uint32_t last_button_press = 0;
diff --git a/src/thermostat.c b/src/thermostat.c
--- a/src/thermostat.c
+++ b/src/thermostat.c
@@ -3,8 +3,6 @@
 //
 
 #include "thermostat.h"
-#include "flash.h"
-#include "gpio.h"
 
 // PID implementation