/* ===== pfod Command for Menu_1 ==== pfodApp msg {.} --> {,<+5>~Low Power BLE`1000~V2|A<+4>`0~Flash LED~~\ is On~t|!B<+4>`775~A2 ~V`1023`0~3.3~0~|C<+5>~Plot of A2} */ // Using Low Power nRF52 hardware for RedBearLabs BLE Nano V2 // Using Arduino V1.8.2 IDE /* Code generated by pfodDesignerV3 V3.0.3441 */ /* * (c)2014-2018 Forward Computing and Control Pty. Ltd. * NSW Australia, www.forward.com.au * This code is not warranted to be fit for any purpose. You may only use it at your own risk. * This generated code may be freely used for both private and commercial use * provided this copyright is maintained. */ // This sketch needs the Low Power nRF52 installed and compile using Arduino V1.8.2 IDE // see https://www.forward.com.au/pfod/BLE/LowPower/index.html #include // download the pfodParser library V3.10+ from http://www.forward.com.au/pfod/pfodParserLibraries/index.html // pfodParse.h includes WStream and then defines Stream as WStream to match the Redbear BLE hardware files and to avoid conflict with mbed::Stream #include // download the libraries from http://www.forward.com.au/pfod/pfodParserLibraries/index.html // pfodParser.zip V3.31+ contains pfodParser, pfodSecurity, pfodDelay, pfodBLEBufferedSerial, pfodSMS and pfodRadio int swap01(int); // method prototype for slider end swaps float getPlotVarScaling(long varMax, long varMin, float displayMax, float displayMin); pfodParser parser("V3"); // create a parser to handle the pfod messages lp_BLESerial bleSerial; // create a BLE serial connection // give the board pins names, if you change the pin number here you will change the pin controlled int cmd_A_var; // name the variable for 'Flash LED' 0= 1= is On lp_timer cmd_A_timer; // the pulse timer unsigned long cmd_A_PULSE_LENGTH = 2000; // 2.0 secs const int cmd_A_pin = 13; // name the output pin for 'Flash LED' int cmd_B_var; // name the variable for 'A2' lp_timer cmd_B_adcTimer; // ADC timer unsigned long cmd_B_ADC_READ_INTERVAL = 1000;// 1sec, edit this to change adc read interval const int cmd_B_pin = 2; // name the pin for 'A2' // plotting data variables int plot_1_varMin = 0; int plot_1_var = plot_1_varMin; float plot_1_scaling; float plot_1_varDisplayMin = 0.0; // plot 2 is hidden // plot 3 is hidden lp_timer plotDataTimer; // plot data timer unsigned long PLOT_DATA_INTERVAL = 1000;// ms == 1 sec, edit this to change the plot data interval // the setup routine runs once on reset: void setup() { cmd_A_var = 0; pinMode(cmd_A_pin, OUTPUT); // output for 'Flash LED' is initially LOW, digitalWrite(cmd_A_pin,cmd_A_var); // set output cmd_B_var = 0; // calculate the plot vars scaling here once to reduce computation plot_1_scaling = getPlotVarScaling(1023,plot_1_varMin,3.3,plot_1_varDisplayMin); // set advertised name bleSerial.setName("pfod BLE device"); // <<<<<<<< set your device name here // begin initialization bleSerial.begin(); parser.connect(&bleSerial); cmd_B_adcTimer.startTimer(cmd_B_ADC_READ_INTERVAL,cmd_B_readADC); // start ADC timer plotDataTimer.startTimer(PLOT_DATA_INTERVAL, sendData); // start plot timer // <<<<<<<<< Your extra setup code goes here } // the loop routine runs over and over again forever: void loop() { sleep(); // wait here for a trigger // check ble serial when triggered by anything uint8_t cmd = parser.parse(); // parse incoming data from connection // parser returns non-zero when a pfod command is fully parsed if (cmd != 0) { // have parsed a complete msg { to } uint8_t* pfodFirstArg = parser.getFirstArg(); // may point to \0 if no arguments in this msg. pfod_MAYBE_UNUSED(pfodFirstArg); // may not be used, just suppress warning long pfodLongRtn; // used for parsing long return arguments, if any pfod_MAYBE_UNUSED(pfodLongRtn); // may not be used, just suppress warning if ('.' == cmd) { // pfodApp has connected and sent {.} , it is asking for the main menu if (!parser.isRefresh()) { sendMainMenu(); // send back the menu designed } else { sendMainMenuUpdate(); // menu is cached just send update } // now handle commands returned from button/sliders } else if('A'==cmd) { // user moved slider -- 'Flash LED' // in the main Menu of Menu_1 // set output based on slider 0= 1= is On parser.parseLong(pfodFirstArg,&pfodLongRtn); // parse first arg as a long cmd_A_var = (int)pfodLongRtn; // set variable digitalWrite(cmd_A_pin,cmd_A_var); // set output if(cmd_A_var == 1) { cmd_A_timer.startDelay(cmd_A_PULSE_LENGTH,cmd_A_checkPulse); // high pulse } else { cmd_A_timer.stop(); } sendMainMenuUpdate(); // always send back a pfod msg otherwise pfodApp will disconnect. // } else if('B'==cmd) { // this is a label. pfodApp NEVER sends this cmd -- 'A2' // // in the main Menu of Menu_1 } else if('C'==cmd) { // user pressed -- 'Plot of A2' // in the main Menu of Menu_1 // return plotting msg. parser.print(F("{=Plot of A2|time (secs)|Plot_1~3.3~0.0~Volts||}")); } else if ('!' == cmd) { // CloseConnection command closeConnection(parser.getPfodAppStream()); } else { // unknown command parser.print(F("{}")); // always send back a pfod msg otherwise pfodApp will disconnect. } } } void closeConnection(Stream *io) { // add any special code here to force connection to be dropped ((lp_BLESerial*)io)->close(); } void sendData() { // assign values to plot variables from your loop variables or read ADC inputs plot_1_var = analogRead(2); // read input to plot // plot_2_var plot Hidden so no data assigned here // plot_3_var plot Hidden so no data assigned here // send plot data in CSV format parser.print(((float)millis())/1000.0); // time in secs parser.print(','); parser.print(((float)(plot_1_var-plot_1_varMin)) * plot_1_scaling + plot_1_varDisplayMin); parser.print(','); // Plot 2 is hidden. No data sent. parser.print(','); // Plot 3 is hidden. No data sent. parser.println(); // end of CSV data record } float getPlotVarScaling(long varMax, long varMin, float displayMax, float displayMin) { long varRange = varMax - varMin; if (varRange == 0) { varRange = 1; } // prevent divide by zero return (displayMax - displayMin)/((float)varRange); } void cmd_A_checkPulse() { // pulse timer timed out cmd_A_var = 0; // return output to LOW digitalWrite(cmd_A_pin,cmd_A_var); // update output pin } void cmd_B_readADC() { cmd_B_var = analogRead(cmd_B_pin); // read ADC input } void sendMainMenu() { // !! Remember to change the parser version string // every time you edit this method parser.print(F("{,")); // start a Menu screen pfod message // send menu background, format, prompt, refresh and version parser.print(F("<+5>~Low Power BLE`1000")); parser.sendVersion(); // send the menu version // send menu items parser.print(F("|A<+4>")); parser.print('`'); parser.print(cmd_A_var); // output the current state 0 Low or 1 High parser.print(F("~Flash LED~~\\ is On~t")); // Note the \\ inside the "'s to send \ ... parser.print(F("|!B<+4>")); parser.print('`'); parser.print(cmd_B_var); // output the current ADC reading parser.print(F("~A2 ~V`1023`0~3.3~0~")); parser.print(F("|C<+5>")); parser.print(F("~Plot of A2")); parser.print(F("}")); // close pfod message } void sendMainMenuUpdate() { parser.print(F("{;")); // start an Update Menu pfod message // send menu items parser.print(F("|A")); parser.print('`'); parser.print(cmd_A_var); // output the current state 0 Low or 1 High parser.print(F("|B")); parser.print('`'); parser.print(cmd_B_var); // output the current ADC reading parser.print(F("}")); // close pfod message // ============ end of menu =========== } int swap01(int in) { return (in==0)?1:0; } // ============= end generated code =========