/* ===== pfod Command for Menu_1 ==== pfodApp msg {.} --> {,<+7>~Led Control`0~V10|A<+4>`0~Led is ~~Low\High~|B<+4>~Voltage Plot} */ // Using Arduino Nano 33 IoT Board (V1.18.11) // Use ArduinoBLE library V2.0.0 // Use Arduino V1.8.13+ IDE /* Code generated by pfodDesignerV3 V3.0.3871 */ /* * (c)2014-2021 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. */ #include #include // download the libraries from http://www.forward.com.au/pfod/pfodParserLibraries/index.html // pfodParser.zip V3.51+ contains pfodParser, pfodSecurity, pfodDelay, pfodBLEBufferedSerial, pfodSMS and pfodRadio #include // un-comment DEBUG_BLE_SERIAL to debug connect/disconnect //#define DEBUG_BLE_SERIAL int swap01(int); // method prototype for slider end swaps float getPlotVarScaling(long varMax, long varMin, float displayMax, float displayMin); // =========== pfodBLESerial class definition ======= static const char* localName = "Nano 33 BLE"; // <<<<<< change this string to customize the adverised name of your board (max 31 chars) class pfodBLESerial : public Stream { public: static BLEService uartService; static BLEDescriptor uartNameDescriptor; static BLECharacteristic rxCharacteristic; static BLEDescriptor rxNameDescriptor; static BLECharacteristic txCharacteristic; static BLEDescriptor txNameDescriptor; pfodBLESerial(); bool begin(); void poll(); size_t write(uint8_t); size_t write(const uint8_t*, size_t); int read(); int available(); void flush(); int peek(); void close(); bool isConnected(); static void connectHandler(BLEDevice central); static void disconnectHandler(BLEDevice central); static void receiveHandler(BLEDevice central, BLECharacteristic rxCharacteristic); private: static const int BLE_MAX_LENGTH = 20; static const int BLE_RX_MAX_LENGTH = 256; static volatile size_t rxHead; static volatile size_t rxTail; static volatile uint8_t rxBuffer[BLE_RX_MAX_LENGTH]; static size_t txIdx; static uint8_t txBuffer[BLE_MAX_LENGTH]; static void addReceiveBytes(const uint8_t* bytes, size_t len); volatile static bool connected; }; // =========== end pfodBLESerial class definition ======= pfodParser parser("V11"); // create a parser to handle the pfod messages pfodBLESerial bleSerial; // create a BLE serial connection pfodBLEBufferedSerial bleBufferedSerial; // 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 'Led is' 0=Low 1=High const int cmd_A_pin = 13; // name the output pin for 'Led is' unsigned long plot_mSOffset = 0; // set by {@} response bool clearPlot = false; // set by the {@} response code // 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 sec, edit this to change the plot data interval // the setup routine runs once on reset: void setup() { #ifdef DEBUG_BLE_SERIAL Serial.begin(9600); for (int i = 10; i > 0; i--) { Serial.print(' '); Serial.print(i); delay(1000); } Serial.println(); #endif cmd_A_var = 0; pinMode(cmd_A_pin, OUTPUT); // output for 'Led is' is initially LOW, digitalWrite(cmd_A_pin,cmd_A_var); // set output // 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 local name and service UUID // begin initialization if (!bleSerial.begin()) { while (1) { #ifdef DEBUG_BLE_SERIAL Serial.println("Starting BLE failed!"); #endif delay(3000); // repeat every 3 sec } } parser.connect(bleBufferedSerial.connect(&bleSerial)); #ifdef DEBUG_BLE_SERIAL Serial.println("BLE Started"); #endif 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 clearPlot = true; // clear plot on reconnect as have new plot_mSOffset parser.print(F("{@`0}")); // return `0 as 'current' raw data milliseconds // now handle commands returned from button/sliders } else if('A'==cmd) { // user moved slider -- 'Led is' // in the main Menu of Menu_1 // set output based on slider 0=Low 1=High 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 sendMainMenuUpdate(); // always send back a pfod msg otherwise pfodApp will disconnect. } else if('B'==cmd) { // user pressed -- 'Voltage Plot' // in the main Menu of Menu_1 // return plotting msg. parser.print(F("{=Chart Label~mm:ss")); if (clearPlot) { clearPlot = false; parser.print(F("~C")); } parser.print(F("|time (mm:ss)|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) { // add any special code here to force connection to be dropped ((pfodBLESerial*)io)->close(); } void sendData() { if (plotDataTimer.justFinished()) { 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>~Led Control`0")); 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("~Led is ~~Low\\High~")); // Note the \\ inside the "'s to send \ ... parser.print(F("|B<+4>")); parser.print(F("~Voltage Plot")); 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(F("}")); // close pfod message // ============ end of menu =========== } // ========== pfodBLESerial =========== BLEService pfodBLESerial::uartService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); BLEDescriptor pfodBLESerial::uartNameDescriptor("2901", localName); BLECharacteristic pfodBLESerial::rxCharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWrite, BLE_MAX_LENGTH); BLEDescriptor pfodBLESerial::rxNameDescriptor("2901", "RX - (Write)"); BLECharacteristic pfodBLESerial::txCharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLEIndicate | BLERead | BLENotify, BLE_MAX_LENGTH); BLEDescriptor pfodBLESerial::txNameDescriptor("2901", "TX - (Indicate|Notify)"); volatile size_t pfodBLESerial::rxHead; volatile size_t pfodBLESerial::rxTail; volatile uint8_t pfodBLESerial::rxBuffer[BLE_RX_MAX_LENGTH]; size_t pfodBLESerial::txIdx; uint8_t pfodBLESerial::txBuffer[BLE_MAX_LENGTH]; volatile bool pfodBLESerial::connected; pfodBLESerial::pfodBLESerial() { connected = false; } void pfodBLESerial::connectHandler(BLEDevice central) { (void)(central); // may not be used, just suppress warning #ifdef DEBUG_BLE_SERIAL // print the central's MAC address: Serial.print("Connected to central: "); Serial.println(central.address()); #endif pfodBLESerial::connected = true; } void pfodBLESerial::disconnectHandler(BLEDevice central) { (void)(central); // may not be used, just suppress warning #ifdef DEBUG_BLE_SERIAL // print the central's MAC address: Serial.print("Disconnected from central: "); Serial.println(central.address()); #endif pfodBLESerial::connected = false; } void pfodBLESerial::receiveHandler(BLEDevice central, BLECharacteristic rxCharacteristic) { (void)(central); // may not be used, just suppress warning size_t len = rxCharacteristic.valueLength(); const unsigned char *data = rxCharacteristic.value(); #ifdef DEBUG_BLE_SERIAL Serial.print("'"); Serial.write(data, len); Serial.print("'"); Serial.println(); #endif pfodBLESerial::addReceiveBytes((const uint8_t*)data, len); if (strnstr((const char*)data, "{!}", len)) { central.disconnect(); // need to call this from this method!! } } bool pfodBLESerial::begin() { // begin initialization if (!BLE.begin()) { return false; } BLE.setConnectionInterval(80, 160); // 160 => 200mS max connection interval to match default pfodBLEBufferedSerial setting // set advertised local name and service UUID: BLE.setLocalName(localName); BLE.setAdvertisedService(uartService); rxCharacteristic.addDescriptor(rxNameDescriptor); rxCharacteristic.addDescriptor(uartNameDescriptor); txCharacteristic.addDescriptor(txNameDescriptor); txCharacteristic.addDescriptor(uartNameDescriptor); // add the characteristic to the service uartService.addCharacteristic(rxCharacteristic); uartService.addCharacteristic(txCharacteristic); // add service BLE.addService(uartService); // assign event handlers for connected, disconnected to peripheral BLE.setEventHandler(BLEConnected, connectHandler); BLE.setEventHandler(BLEDisconnected, disconnectHandler); // assign event handlers for characteristic rxCharacteristic.setEventHandler(BLEWritten, receiveHandler); // start advertising BLE.advertise(); return true; } bool pfodBLESerial::isConnected() { return (connected && txCharacteristic.subscribed()); } void pfodBLESerial::close() { #ifdef DEBUG_BLE_SERIAL Serial.println(F("close() called")); #endif } int pfodBLESerial::read() { if (rxTail == rxHead) { return -1; } // note increment rxHead befor writing so need to increment rxTail befor reading rxTail = (rxTail + 1) % sizeof(rxBuffer); uint8_t b = rxBuffer[rxTail]; return b; } void pfodBLESerial::addReceiveBytes(const uint8_t* bytes, size_t len) { // note increment rxHead befor writing so need to increment rxTail befor reading for (size_t i = 0; i < len; i++) { rxHead = (rxHead + 1) % sizeof(rxBuffer); rxBuffer[rxHead] = bytes[i]; } } void pfodBLESerial::poll() { BLE.poll(); } // called as part of parser.parse() so will poll() each loop() int pfodBLESerial::available() { poll(); flush(); // send any pending data now. This happens at the top of each loop() int rtn = ((rxHead + sizeof(rxBuffer)) - rxTail ) % sizeof(rxBuffer); return rtn; } void pfodBLESerial::flush() { if (txIdx == 0) { return; } txCharacteristic.setValue(txBuffer, txIdx); txIdx = 0; poll(); } int pfodBLESerial::peek() { poll(); if (rxTail == rxHead) { return -1; } size_t nextIdx = (rxTail + 1) % sizeof(rxBuffer); uint8_t byte = rxBuffer[nextIdx]; return byte; } size_t pfodBLESerial::write(const uint8_t* bytes, size_t len) { for (size_t i = 0; i < len; i++) { write(bytes[i]); } return len; // just assume it is all written } size_t pfodBLESerial::write(uint8_t b) { poll(); if (!isConnected()) { return 1; } txBuffer[txIdx++] = b; if ((txIdx == sizeof(txBuffer)) || (b == ((uint8_t)'\n')) || (b == ((uint8_t)'}')) ) { flush(); // send this buffer if full or end of msg or rawdata newline } return 1; } int swap01(int in) { return (in==0)?1:0; } // ============= end generated code =========