/* ===== pfod Command for Menu_1 ==== pfodApp msg {.} --> {,<+7>~Feather52`0~V38|A~Date/Time plot of A0} */ // Using Adafruit nRF52 Feather BLE /* Code generated by pfodDesignerV3 V3.0.3602 */ /* * (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. */ // 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 #include #include int swap01(int); // method prototype for slider end swaps float getPlotVarScaling(long varMax, long varMin, float displayMax, float displayMin); pfodParser parser("V2"); // create a parser to handle the pfod messages pfodBLEBufferedSerial bleBufferedSerial; #include const char* deviceName = "pfod bluefruit52"; // 30 chars or less in name // BLE Service BLEDis bledis; BLEUart bleuart; unsigned long plot_mSOffset = 0; // set by {@} response // 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 pfodDelay plotDataTimer; // plot data timer unsigned long PLOT_DATA_INTERVAL = 1000;// mS == 1 secs, edit this to change the plot data interval // the setup routine runs once on reset: void setup() { // set up for Chart // set pinMode for plot pins // calculate the plot vars scaling here once to reduce computation plot_1_scaling = getPlotVarScaling(1023,plot_1_varMin,3.6,plot_1_varDisplayMin); parser.connect(bleBufferedSerial.connect(&bleuart)); Bluefruit.autoConnLed(true); // use Bluefruit.autoConnLed(false); to save power or to control BLUE Led manually via Pin 19 Bluefruit.begin(); // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 Bluefruit.setTxPower(4); Bluefruit.setName(deviceName); // 30 chars or less in name Bluefruit.setConnectCallback(connect_callback); Bluefruit.setDisconnectCallback(disconnect_callback); // Configure and Start Device Information Service bledis.setManufacturer("Adafruit Industries"); bledis.setModel("Bluefruit Feather52"); bledis.begin(); // Configure and Start BLE Uart Service bleuart.begin(); // Set up and start advertising startAdv(); plotDataTimer.start(PLOT_DATA_INTERVAL); // start plot timer // <<<<<<<<< Your extra setup code goes here } // the loop routine runs over and over again forever: void loop() { 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 } // handle {@} request } else if('@'==cmd) { // pfodApp requested 'current' time plot_mSOffset = millis(); // capture current millis as offset rawdata timestamps parser.print(F("{@`0}")); // return `0 as 'current' raw data milliseconds // now handle commands returned from button/sliders } else if('A'==cmd) { // user pressed -- 'Date/Time plot of A0' // in the main Menu of Menu_1 // return plotting msg. parser.print(F("{=A0 Volts~E HH:mm:ss|date|A0~~~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. } } sendData(); // <<<<<<<<<<< Your other loop() code goes here } void closeConnection(Stream *io) { // nothing special here } void startAdv(void) { // Advertising packet Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); Bluefruit.Advertising.addTxPower(); // Include bleuart 128-bit uuid Bluefruit.Advertising.addService(bleuart); Bluefruit.ScanResponse.addName(); Bluefruit.Advertising.restartOnDisconnect(true); Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds } void connect_callback(uint16_t conn_handle) { } void disconnect_callback(uint16_t conn_handle, uint8_t reason) { } void sendData() { if (plotDataTimer.isFinished()) { plotDataTimer.repeat(); // restart plot data timer, without drift // assign values to plot variables from your loop variables or read ADC inputs plot_1_var = analogRead(A0); // 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(millis()-plot_mSOffset);// time in milliseconds 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 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("<+7>~Feather52`0")); parser.sendVersion(); // send the menu version // send menu items parser.print(F("|A")); parser.print(F("~Date/Time plot of A0")); parser.print(F("}")); // close pfod message } void sendMainMenuUpdate() { parser.print(F("{;")); // start an Update Menu pfod message // send menu items parser.print(F("}")); // close pfod message // ============ end of menu =========== } int swap01(int in) { return (in==0)?1:0; } // ============= end generated code =========