Aes encryption code

Dependencies:   Crypto USBDevice mbed

Fork of larada by MZJ

Revision:
1:ccb26dd9845a
Parent:
0:aaf22a04f350
Child:
2:35e620097381
--- a/main.cpp	Thu Feb 04 03:42:24 2016 +0000
+++ b/main.cpp	Mon Feb 22 12:47:47 2016 +0000
@@ -2,19 +2,20 @@
 #include "USBSerial.h"
 #include "IAP.h"
 
-#define SERIAL //comment for USB operation, uncomment for serial
-//#define DEBUG 1
+// #define SERIAL //comment for USB operation, uncomment for serial
 #define CALIBRATE 0
 #define FUNCTION_CHECK 0
 
+// debounce duration for ON/OFF switch
+#define ON_OFF_DEBOUNCE 3000
+
 // longest user command we will accept
 #define MAX_COMMAND_LEN 64
 
 // where to start reading stored values from the EEPROM
 #define BASE_ADDRESS 1
 
-// temperature control stuff
-
+// temperature control stuff //
 // fixed values
 #define CONTROL_INTERRUPT 1000 // every 1ms
 #define MAX_DUTY 100 // This is both the number of times the heater_control interrupt will run per control period and the max amount of interrupts the heater triac can be on.
@@ -23,17 +24,17 @@
 #define DEFAULT_FILTER 0.05 // first order digital filter parameter, should be [0.0 - 1]. less means filter more, a value of 1 should turn it off
 //#define INITIAL_DUTY 25 // 25% duty generally results in a few degrees under 57C output for the ambient temperatures I've been testing at
 #define DEFAULT_INITIAL_DUTY 45 
-#define DEFAULT_NOMINAL_DUTY 7 // 11% duty is a good set point for Beta init (testing at 20C ambient)
+#define DEFAULT_NOMINAL_DUTY 28 // 11% duty is a good set point for Beta init (testing at 20C ambient)
 //#define SETPOINT 435 // this results in roughly 57C at the tines on the Alphas (really 53)
-#define DEFAULT_SETPOINT 463 //445 -> 53.1-54.1//475 -> 58-59.5// 500 -> 62.5 // 505 -> 59 //475 -> 53C // 445 // 505 seems right for second unit //535-> first unit //527 // this results in roughly 57C at the tines on the Betas
+#define DEFAULT_SETPOINT 58 //seems like there is a ~4C temperature drop between the sensor and the outside of the tips 
 #define DEFAULT_TOLERANCE 2 // if we are within this many ADC counts of the setpoint don't make any changes to the duty cycle
 //#define DUTY_TOO_LARGE 40 // If the duty gets larger than this we have problems (Alphas)
 #define DEFAULT_DUTY_TOO_LARGE 70 // If the duty gets larger than this we have problems (Betas) -> 65 duty should result in roughly 62C tip temps
 //#define DUTY_TOO_LARGE 85 // For testing
-//#define DEFAULT_TEMP_LIMIT_LOWER 50 // If the temperature measures too low during operation we are either doing a bad job or have a faulty temperature sensor
-#define DEFAULT_TEMP_LIMIT_LOWER 0 // For testing
+#define DEFAULT_TEMP_LIMIT_LOWER 12 // If the temperature measures too low during operation we are either doing a bad job or have a faulty temperature sensor
+//#define DEFAULT_TEMP_LIMIT_LOWER 0 // For testing
 //#define TEMP_LIMIT_UPPER 485 // No burning allowed (Alphas)
-#define DEFAULT_TEMP_LIMIT_UPPER 625 // No burning allowed (Betas)
+#define DEFAULT_TEMP_LIMIT_UPPER 65 // No burning allowed (Betas)
 
 float filter = DEFAULT_FILTER;
 uint8_t initial_duty = DEFAULT_INITIAL_DUTY;
@@ -49,6 +50,7 @@
     WAIT_FOR_TIP,
     INITIAL_RAMP,
     ACTIVE,
+    DONE,
     ERROR,
 };
 
@@ -78,13 +80,15 @@
 DigitalInOut fuel_gage_4(P1_31);
 DigitalOut tip_light(P1_14);
 
+// Other pins
 DigitalIn tip_sensor(P0_14);
+DigitalIn on_off(P1_28);
 
 #define ON_TIME_S 2700L // The device is active for 45 minutes
 //#define ON_TIME_S 150L
 #define RED_LED_ON_TIME_S 300L // The red LED is active for the last 5 minutes
 //#define RED_LED_ON_TIME_S 30L
-#define TIP_FLASH_INTERVAL_S 20L // How often the tip will flash
+#define TIP_FLASH_INTERVAL_S 25L // How often the tip will flash
 //Serial pc(0x1f00, 0x2012, 0x0001, false);
 
 #ifdef SERIAL
@@ -99,7 +103,7 @@
 Ticker tick_interrupt;
 
 volatile Heater_State heater = OFF;
-volatile State state = WAIT_FOR_TIP;
+volatile State state = IDLE;
 volatile Error error_state = NONE;
 
 volatile uint8_t duty = DEFAULT_INITIAL_DUTY;
@@ -114,7 +118,7 @@
 void interpret(char parameter, int value);
 void spin_lights(bool first);
 
-//INTERRUPT - increment this every ms since the normal mbed timer returns an int and real code needs an unsigned type
+//INTERRUPT - increment this every ms since the normal mbed timer returns an int and we need an unsigned type
 void tick(void) {millis++;}
 
 uint32_t get_time(void){
@@ -312,6 +316,7 @@
     wait_ms(1);
     pc.printf("Tolerance is %u\r\n\r\n", tolerance);
     wait_ms(1);
+    pc.printf("Spot treatment time is %l\r\n\r\n", TIP_FLASH_INTERVAL_S);
 }
 
 //interpret a user command
@@ -391,7 +396,7 @@
 // run this to manually check the hardware works
 // probably best to disable the heater on the first try
 void functional_check(void){
-  all_off();  
+  all_off();
   if(connected)pc.printf("Tip light\r\n");
   tip_light = 1;
   wait_ms(1000);
@@ -540,8 +545,26 @@
   all_off();
 }
 
+void check_on_off(void){
+static uint32_t count = 0;
+
+    // debounce
+    if(on_off){
+      count++;
+      if(count > ON_OFF_DEBOUNCE){
+        all_off();
+        state = IDLE;
+      }
+    }
+    else count = 0; 
+}
+
+void do_idle(bool first){
+    if(!on_off) state = WAIT_FOR_TIP;
+}
+
 // tip neeeds to be present before we can start the cycle
-// later we will also abort the cycle if the tip is removed
+// later we should also abort the cycle if the tip is removed
 void do_wait_for_tip(bool first){
   unsigned long time = get_time();
   unsigned long time_increment = time % 1800;
@@ -596,6 +619,7 @@
   static uint32_t last_print = get_time();
   
   if(first){
+    print_active_settings();
     start_time = get_time();
     fan = 1;
     set_duty(initial_duty);
@@ -609,7 +633,7 @@
   }
   
   if(get_time() - last_print > 5000){
-    if(connected)pc.printf("Duty: %u, Temp: %f, Time: %.2fs\r\n", get_duty(), get_temperature(), (float)get_time()/1000);
+    if(connected)pc.printf("Duty: %u, Temp: %.2f, Time: %.2fs\r\n", get_duty(), get_temperature(), (float)get_time()/1000);
     last_print = get_time();
   } 
   
@@ -637,8 +661,10 @@
   // check if we're done
   if(current_time >= ON_TIME_S * 1000L){
     if(connected)pc.printf("Done!\r\n");
-    abort(false);
-    state = IDLE;
+    //abort(false);
+    set_duty(0);
+    fan = 0;
+    state = DONE;
   }
   
   //if(!tip->in_place())abort(false);
@@ -651,12 +677,35 @@
     if(temperature_copy < setpoint - tolerance) duty_copy++;
     set_duty(duty_copy);
     
-    if(connected)pc.printf("Duty: %u, Temp: %f, Time: %.2fs\r\n", get_duty(), get_temperature(), ((float)get_time() - (float)start_time)/1000);
+    if(connected)pc.printf("Duty: %u, Temp: %.2f, Time: %.2fs\r\n", get_duty(), get_temperature(), ((float)get_time() - (float)start_time)/1000);
+  }
+}
+
+void do_done(bool first){
+  static uint32_t start_time = 0;
+  
+  if(first){
+    start_time = get_time();
+    //control_interrupt.detach();
+    all_off();
+    if(connected)pc.printf("Done!\r\n");
+  }
+  
+  heater_pin = 0;
+  uint32_t current_time = get_time() - start_time;
+
+  if(current_time % 2000 > 1000){
+    empty_led.input();
+  }
+  else{
+    empty_led.output();
+    empty_led = 0;  
   }
 }
 
 void print_state(void){
     if(!connected)return;
+    printf("State:\t");
     switch(state){
         case IDLE:
           printf("IDLE\r\n");
@@ -670,6 +719,9 @@
         case ACTIVE:
           printf("ACTIVE\r\n");
           break;
+        case DONE:
+          printf("DONE\r\n");
+          break;
         case ERROR:
           printf("ERROR\r\n");
           break;
@@ -680,14 +732,16 @@
 int main(){
   static State last_state = IDLE;
   init();
-  //if(FUNCTION_CHECK) functional_check();
+  if(FUNCTION_CHECK) functional_check();
   if(CALIBRATE){
       calibrate(true);
       while(1)calibrate(false);
   }
   
   while(1){
-    getInput();    
+    getInput();
+    
+    check_on_off();
     
     bool state_change = false;
     if(state != last_state){
@@ -698,7 +752,7 @@
       
    switch(state){
       case IDLE:
-        // later put a check to exit state per on/off switch
+        do_idle(state_change);
         break;
       case WAIT_FOR_TIP:
         do_wait_for_tip(state_change);
@@ -711,6 +765,9 @@
         do_treatment_cycle(state_change);
         spin_lights(false);
         break;
+      case DONE:
+        do_done(state_change);
+        break;
       case ERROR:
         abort(true);
         break;
@@ -725,12 +782,12 @@
   if(first) start_time = get_time();
   uint32_t current_time = get_time() - start_time;
   
-  uint32_t tip_time = current_time % 23000;
-  if(tip_time < 20000) tip_light = 1;
+  // tip should be solid for TIP_FLASH_INTERVAL_S seconds, then flash for 3 seconds
+  uint32_t tip_time = current_time % (TIP_FLASH_INTERVAL_S * 1000 + 3000);
+  if(tip_time < (TIP_FLASH_INTERVAL_S * 1000)) tip_light = 1;
   else tip_light = (tip_time % 100 < 50)?1:0;
-  //if(current_time > 4600) current_time = 0;
   
-  // handle fule gage LEDs
+  // handle fuel gage LEDs
   // this should be more directly linked to the treatment stop condition
   uint32_t step = (ON_TIME_S - RED_LED_ON_TIME_S)/5;
   step *= 1000;
@@ -821,6 +878,6 @@
     temperature_copy = temperature;
     __enable_irq();
   
-    if(DEBUG && (tester_count % 30) > 24)pc.printf("\t%f\t%u\t%f\r\n", temperature_copy, temp_sense.read_u16(), temp_sense.read());
+    if(connected && (tester_count % 30) > 24)pc.printf("\t%f\t%u\t%f\r\n", temperature_copy, temp_sense.read_u16(), temp_sense.read());
   }
 }*/
\ No newline at end of file