
Code for Browonout Generator box
main.cpp
- Committer:
- el16ba
- Date:
- 2020-02-14
- Revision:
- 4:44760dafcab7
- Parent:
- 3:16478dd3d1f3
- Child:
- 5:a8297e5f4de1
File content as of revision 4:44760dafcab7:
#include "mbed.h" #include <string> #include <stdio.h> #include <stdlib.h> #include <FileHandle.h> #include <vector> #include <iostream> #include <cstdio> #include <cstdlib> 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; Serial pc(USBTX, USBRX); // for PC debug LocalFileSystem fs("local"); #define NUMCONFIGPARAM 3 bool flg_start = 0; bool flg_END = false; long double configData[NUMCONFIGPARAM]; string strData[NUMCONFIGPARAM]; //function prototypes void init(); bool is_num_char(char c); bool print_config(); void get_config_data(); void power(char pwr = 0); void cycle_wait(long double tOn = configData[0], long double tOff = configData[2]); void cycle_loop(long double tOn = configData[0], long double tOff = configData[2]); void serial_mode(); //returns if the given character is a number or decimal point character bool is_num_char(char c) { bool isChar = false; if((c >= '0' and c <= '9') or c == '.') { isChar = true; } return isChar; } //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 flg_error; pc.printf("Opening file 'config.txt'... "); FILE* fp = fopen("/local/config.txt", "r"); pc.printf("%s\n\r", (!fp ? "Fail :(" : "OK")); pc.printf("\r\n==========\r\n"); if (!fp) { error("error: %s (%d)\n", strerror(errno), -errno); flg_error = 1; } while (!feof(fp)) { int c = fgetc(fp); pc.printf("%c", c); } fclose(fp); pc.printf("\r\n==========\r\n"); return flg_error; } //Reads reads and converts data from config file //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() { 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)) { //loop till end of file c = fgetc(fp); isNumChar = is_num_char(c); if(isNumChar) { //at number character? currentStr.push_back(c); isReading = true; } else if(!isNumChar and isReading) { //at end of number string? isReading = false; strData[strData_index] = currentStr; configData[strData_index] = strtod(currentStr.c_str(), &pEnd); strData_index++; currentStr = ""; } if (strData_index >= NUMCONFIGPARAM ) { //Read all nessesary data? break; } } fclose(fp); 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) { do21 = pwr; led1 = pwr; } void cycle_wait(long double tOn, long double tOff) { power(1); ThisThread::sleep_for(tOn); power(0); ThisThread::sleep_for(tOff); power(1); } void cycle_loop(long double tOn, long double tOff) { while(1) { cycle_wait(tOn,tOff); } } string read_serial_input() { bool flg_read = true; char c; string str = ""; while(flg_read) { c = pc.getc(); pc.putc(c); if(c == 8 or c == 127) { //If back space or delete remove last character str.pop_back(); } else if (c == '\n') { str.pop_back(); flg_read = false; break; // } else if(c < ' ') { //If none visible character, bar backspace or delete // flg_read = false; // break; } else { str.push_back(c); } } return str; } bool is_num_str(string str) { bool isNumC = false; bool output = true; for(int i = 0; i < str.length(); i++) { isNumC = is_num_char(str[i]); if(!isNumC) { output = false; } } return output; } long double get_num_input() { long double output; bool flg_gotStr = false; string str_v = ""; char* pEnd; while(!flg_gotStr) { pc.puts("value: "); str_v = read_serial_input(); if (is_num_str(str_v)) { output = strtod(str_v.c_str(), &pEnd); flg_gotStr = true; break; } else { pc.printf("entry contained non number characters please try again: \r\n"); } } return output; } void serial_mode() { pc.printf("Serial Mode \r\n"); bool flg_read = true; bool flg_read2 = true; char c; string str_c = ""; while(flg_read) { str_c = read_serial_input(); if (str_c == "on"){ power(1); pc.printf("on\r\n"); } else if(str_c == "off"){ power(0); pc.printf("off\r\n"); } else if(str_c == "cyclew") { cycle_wait(); pc.printf("cycled \r\n"); } else if(str_c == "loop") { pc.printf("Looping restart unit to end loop\r\n"); cycle_loop(); } else if(str_c == "newval") { pc.printf("enter your new on and off times(ms)\r\n"); pc.puts("Time on\r\n"); configData[0] = get_num_input(); pc.puts("Time off\r\n"); configData[2] = get_num_input(); pc.printf("Data: %Lf, %Lf, %Lf\r\n",configData[0],configData[1],configData[2]); } else { pc.printf("Unknown command, see README for commands \r\n"); } str_c = ""; } } int main() { init(); while(!flg_END) { switch((int)configData[1]) { case(1): //Cut the power to the machine once cycle_wait(); pc.printf("cycled \r\n"); break; 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; } } pc.printf("Program End \r\n"); return 0; }