Code for Browonout Generator box

Revision:
3:16478dd3d1f3
Parent:
2:018630ec52d1
Child:
4:44760dafcab7
--- a/main.cpp	Tue Feb 11 14:59:35 2020 +0000
+++ b/main.cpp	Thu Feb 13 18:08:20 2020 +0000
@@ -6,11 +6,11 @@
 #include <FileHandle.h>
 #include <vector>
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
-DigitalOut do21(p21);
+DigitalOut led1(LED1, 0);
+DigitalOut led2(LED2, 0);
+DigitalOut led3(LED3, 0);
+DigitalOut led4(LED4, 0);
+DigitalOut do21(p21, 0);
 InterruptIn di22(p22,PullDown); //removing di22 and its related calls and functions causes a major fault for unknown reasons so it has been left in dispite not being used.
 Ticker tickOn;
 Ticker tickOff;
@@ -25,8 +25,17 @@
 long double configData[NUMCONFIGPARAM];
 string strData[NUMCONFIGPARAM];
 
+//function prototypes
+void init();
+bool is_num_char(char c);
+bool print_config();
+void get_config_data();
+
+
+
+
 //returns if the given character is a number or decimal point character
-bool Is_Num_Char(char c)
+bool is_num_char(char c)
 {
     bool isChar = false;
     if((c >= '0' and c <= '9') or c == '.') {
@@ -37,226 +46,146 @@
 
 //From: https://os.mbed.com/questions/83075/Is-it-possible-to-read-a-small-text-file/
 //Reads config.txt from local drive
-bool Print_Config()
+bool print_config()
 {
     bool flg_error;
-    printf("Opening file 'config.txt'... ");
+    pc.printf("Opening file 'config.txt'... ");
     FILE*   fp = fopen("/local/config.txt", "r");
-    printf("%s\n\r", (!fp ? "Fail :(" : "OK"));
+    pc.printf("%s\n\r", (!fp ? "Fail :(" : "OK"));
     if (!fp) {
         error("error: %s (%d)\n", strerror(errno), -errno);
         flg_error = 1;
     }
     while (!feof(fp)) {
         int c = fgetc(fp);
-        printf("%c", c);
+        pc.printf("%c", c);
     }
 
     fclose(fp);
-    printf("\r\n===========\r\n");
+    pc.printf("\r\n===========\r\n");
     return flg_error;
 }
 
 //Reads reads and converts data from config file
-void Get_Config_data()
+//Once a timed function (e.g. wait(), sleep_for(), ticker, timer e.t.c) is called opening another File pointer will cause the LPC1768 to crash with a Hard Fault.
+void get_config_data()
 {
-//    printf("Get Config data\r\n");
-//    FILE*   fp = fopen("/local/config.txt", "r");
-//    char c;
-//    char stateData = 0; //which data it is reading on duration, off duration e.t.c.
-//    char stateRead = 0; //0 = parsing, 1 = reading data
-//    char* pEnd;
-//    string data;
-//    printf("test");
-//    while (!feof(fp)) {
-//        c = fgetc(fp);
-//        if (Is_Num_Char(c)) {
-//            printf("Read in %d\r\n",c);
-//            data.push_back(c);
-//            stateRead = 1;
-//            c = NULL;
-//        } else if (stateRead == 1) {
-//            //convert to double and add to Data
-//            printf("stateData = %d\r\n",stateData);
-//            strData[stateData] = data;
-//            configData[stateData] = strtod(data.c_str(), &pEnd);
-//            data.clear();
-//            stateData++;
-//            stateRead = 0;
-//        }
-//    }
-//
-//    fclose(fp);
-
-
-    printf("Get Config Data \r\n");
+    pc.printf("Get Config Data \r\n");
     char c; //Current character being read
     char strData_index = 0;
     char* pEnd;
     bool isReading = false;
     bool isNumChar;
     string currentStr;
+
     FILE*   fp = fopen("/local/config.txt", "r");
 
-    while (!feof(fp)) {
+    while (!feof(fp)) {                                                         //loop till end of file
+
         c = fgetc(fp);
-        isNumChar = Is_Num_Char(c);
-        if(isNumChar) {
+        isNumChar = is_num_char(c);
+        if(isNumChar) {                                                         //at number character?
             currentStr.push_back(c);
             isReading = true;
-//            printf("num: %c\r\n", c);
-        } else if(!isNumChar and isReading) {
+        } else if(!isNumChar and isReading) {                                   //at end of number string?
             isReading = false;
             strData[strData_index] = currentStr;
             configData[strData_index] = strtod(currentStr.c_str(), &pEnd);
-            
-//            printf("string added: %s, %i\r\n", currentStr.c_str(), strData_index);
-            
             strData_index++;
             currentStr = "";
-
         }
-        if (strData_index >= NUMCONFIGPARAM ) {
+        if (strData_index >= NUMCONFIGPARAM ) {                                 //Read all nessesary data?
             break;
         }
-        
+
+    }
+    fclose(fp);
+    ThisThread::sleep_for(5000);
+    pc.printf("Data: %Lf, %Lf, %Lf\r\n",configData[0],configData[1],configData[2]);
+}
+
+void init()
+{
+    pc.printf("Initializing \r\n");
+    do21 = 0;
+    led1 = 0;
+    pc.baud(9600);
+    print_config();
+    get_config_data();
+}
+
+void power(char pwr = 0)
+{
+    do21 = pwr;
+    led1 = pwr;
+}
+
+void cycle_wait(long double tOn = configData[0], long double tOff = configData[2])
+{
+    power(1);
+    ThisThread::sleep_for(tOn);
+    power(0);
+    ThisThread::sleep_for(tOff);
+    power(1);
+}
+
+void cycle_loop(long double tOn = configData[0], long double tOff = configData[2])
+{
+    while(1) {
+        cycle_wait(tOn,tOff);
     }
 }
 
-// Runs wait functions and divides by 10 to account for error in function
-// unused as calling function causes too much of a delay when below miliseconds
-void wait_fixed(float t)
-{
-    thread_sleep_for(t / 10);
-}
-
-
-//Flips output
-void run_Cycle()
-{
-    //printf("On \r\n");
-    do21 = 1;
-    led1 = 1;
-    thread_sleep_for(configData[0]);
-    //printf("Off \r\n");
-    do21 = 0;
-    led1 = 0;
-    thread_sleep_for(configData[2]);
-    do21 = 1;
-    led1 = 1;
-
-}
-
-//interrupt function to start cycling
-void start()
-{
-    flg_start = 1;
-}
-
-//init function
-void init()
-{
-    do21 = 0;
-    di22.rise(&start);
-    Print_Config();
-    Get_Config_data();
-}
-
-void cycle_loop()
-{
-    while(1) {
-        run_Cycle();
-    }
-}
-
-void switch_2()
-{
-    do21 = !do21;
-    led1 = !led1;
-}
-
-void start_tick(float timeOnSec, float timeOffSec)
-{
-    tickOn.attach(&switch_2, timeOnSec);
-    tickOff.attach(&switch_2, timeOnSec + timeOffSec);
-}
-
-
-// Allows reads the USB input to allow serial commms to control the box
 void serial_mode()
 {
+    pc.printf("Serial Mode \r\n");
+
     bool flg_read = true;
-    string str;
     char c;
-    pc.baud(9600);
-    pc.printf("Serial Mode\n\r");
-    while(flg_read) {
-        c = pc.getc();
-        pc.putc(c);
-        if (c == '\n') {
-            if (str == "on\r") {
-                do21 = 1;
-                led1 = 1;
-                pc.printf("power on\r\n");
+    string str = "";
 
-            } else if (str == "off\r") {
-                do21 = 0;
-                led1 = 0;
-                pc.printf("power off\r\n");
-
-            } else if (str == "cycle\r") {
-                run_Cycle();
-                pc.printf("cycled\r\n");
-
-            } else if(str == "cycle2\r") {
-                pc.printf("clycle loop, power cycle box to stop\r\n");
+    while(flg_read) {
+        if (c == '\n') {
+            if(str == "cyclew\r"){
+                cycle_wait();
+            }else if(str == "loop\r") {
                 cycle_loop();
-
-            } else if(str == "cycle3\r") {
-                //start_tick();
-
-
-            } else if( str == "exit\r") {
-                flg_read = false;
-            } else {
-                pc.printf("unknown command, see README for command list\r\n");
+            }else{
+                pc.printf("Unknown command, see README for commands \r\n");
             }
             str = "";
         } else {
             str.push_back(c);
         }
     }
-    pc.printf("exit serial mode \r\n");
+
 }
 
-
-
 int main()
 {
-    int Mode;
-    printf("STARTING\r\n");
     init();
-    printf("Strings: On: %s, repeat: %s, Off: %s \r\n", strData[0].c_str(),strData[1].c_str(), strData[2].c_str() );
-    printf("Data: %Lf, %Lf, %Lf\r\n",configData[0],configData[1],configData[2]);
-
     while(!flg_END) {
-        Mode = (int)configData[1];
-        if(Mode == 0) {
-            run_Cycle();
-        } else if(Mode == 1) {
-            cycle_loop();
-        } else if(Mode == 2) {
-            serial_mode();
-            printf("1\r\n");
-            Get_Config_data();
-            printf("2\r\n");
-            printf("Data: %Lf, %Lf, %Lf\r\n",configData[0],configData[1],configData[2]);
+        switch((int)configData[1]) {
+            case(1):                                                            //Cut the power to the machine once
+                cycle_wait();
+                pc.printf("cycled \r\n");
+                break;
 
-        } else {
-            printf("Invalid Mode");
+            case(2):                                                            //Cut the power to the machine in a loop
+                pc.printf("Begining Power cycle loop, to end loop press restart button \r\n");
+                cycle_loop();
+                break;
+
+            case(3):                                                            //Launch in serial command mode
+                serial_mode();
+                break;
         }
+
     }
 
-    printf("END \r\n");
+    pc.printf("Program End \r\n");
+
+
+
     return 0;
-}
+}
\ No newline at end of file