/* ===== pfod Command for Menu_1 ==== pfodApp msg {.} --> {,<+4>~Arduino Data`1000~V65|!A`775~A0 ~V`1023`0~3.3~0~|!E<-6>~|!B<+4>`1~Door is ~~Closed\Open~t|!F<-6>~|C`0~PWM Setting ~%`255`0~100~0~|D<+5>`0~LED is ~~Off\On~t|G<+4>~Voltage Plot} */ // Using Arduino/Genunio 101 BLE Board (Intel Curie Boards V1.0.5) // Use Arduino V1.6.8 IDE /* Code generated by pfodDesignerV2 V2.0.2142 * (c)2014-2016 Forward Computing and Control Pty. Ltd. * NSW Australia, www.forward.com.au * This generated code may be freely used for both private and commercial use */ #include // download the pfodParser library V2.34+ from http://www.forward.com.au/pfod/pfodParserLibraries/index.html #include #include int swap01(int); // method prototype for slider end swaps float getPlotVarScaling(long varMax, long varMin, float displayMax, float displayMin); // =========== pfodBLESerial definitions const char* localName = "101 BLE"; // <<<<<< change this string to customize the adverised name of your board (max 8 chars) class pfodBLESerial : public BLEPeripheral, public Stream { public: pfodBLESerial(); void 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(); private: const static uint8_t pfodEOF[1]; const static char* pfodCloseConnection; 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; volatile static uint8_t rxBuffer[BLE_RX_MAX_LENGTH]; volatile static bool connected; size_t txIdx; uint8_t txBuffer[BLE_MAX_LENGTH]; static void connectHandler(BLECentral& central); static void disconnectHandler(BLECentral& central); static void receiveHandler(BLECentral& central, BLECharacteristic& rxCharacteristic); static void addReceiveBytes(const uint8_t* bytes, size_t len); BLEService uartService = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); BLEDescriptor uartNameDescriptor = BLEDescriptor("2901", localName); BLECharacteristic rxCharacteristic = BLECharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWrite, BLE_MAX_LENGTH); BLEDescriptor rxNameDescriptor = BLEDescriptor("2901", "RX - (Write)"); BLECharacteristic txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLEIndicate, BLE_MAX_LENGTH); BLEDescriptor txNameDescriptor = BLEDescriptor("2901", "TX - (Indicate)"); }; volatile size_t pfodBLESerial::rxHead = 0; volatile size_t pfodBLESerial::rxTail = 0; volatile uint8_t pfodBLESerial::rxBuffer[BLE_RX_MAX_LENGTH]; const uint8_t pfodBLESerial::pfodEOF[1] = {(uint8_t) - 1}; const char* pfodBLESerial::pfodCloseConnection = "{!}"; volatile bool pfodBLESerial::connected = false; // =========== end pfodBLESerial definitions pfodParser parser("V66"); // create a parser to handle the pfod messages pfodBLESerial 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 'A0' unsigned long cmd_A_adcStartTime=0; // ADC timer unsigned long cmd_A_ADC_READ_INTERVAL = 1000;// 1sec, edit this to change adc read interval const int cmd_A_pin = A0; // name the pin for 'A0' int cmd_B_var; // name the variable for 'Door is' const int cmd_B_pin = 4; // name the input pin for 'Door is' int cmd_C_var; // name the variable for 'PWM Setting' const int cmd_C_pin = 5; // name the output pin for 'PWM Setting' int cmd_D_var; // name the variable for 'LED is' unsigned long cmd_D_pulseStartTime=0; // the time when cmd_D pulse started bool cmd_D_pulseRunning = false; // true when cmd_D pulse running unsigned long cmd_D_PULSE_LENGTH = 10000.0; // 10.0 secs const int cmd_D_pin = 13; // name the output pin for 'LED is' int cmd_G_var; // name the variable for 'Voltage Plot' // plotting data variables long plot_1_varMin = 0; long plot_1_var = plot_1_varMin; float plot_1_scaling; float plot_1_varDisplayMin = 0.0; // plot 2 is hidden // plot 3 is hidden unsigned long plotDataTimer = 0; // 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_B_var = 0; pinMode(cmd_B_pin, INPUT_PULLUP); // edit this to just pinMode(..,INPUT); if you don't want the internal pullup enabled cmd_C_var = 0; pinMode(cmd_C_pin, OUTPUT); // output for 'PWM Setting' is initially LOW, analogWrite(cmd_C_pin,cmd_C_var); // set output cmd_D_var = 0; //pinMode(cmd_D_pin, INPUT_PULLUP); pinMode(cmd_D_pin, OUTPUT); // output for 'LED is' is initially LOW, //uncomment INPUT_PULLUP line above and set variable to 1, if you want it initially HIGH digitalWrite(cmd_D_pin,cmd_D_var); // set output cmd_G_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 local name and service UUID // begin initialization bleSerial.begin(); parser.connect(&bleSerial); // <<<<<<<<< Your extra setup code goes here } // the loop routine runs over and over again forever: void loop() { byte 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 } byte* pfodFirstArg = parser.getFirstArg(); // may point to \0 if no arguments in this msg. long pfodLongRtn; // used for parsing long return arguments, if any 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) { // this is a label. pfodApp NEVER sends this cmd -- 'A0' // // in the main Menu of Menu_1 // } else if('E'==cmd) { // this is a label. pfodApp NEVER sends this cmd -- '' // // in the main Menu of Menu_1 // } else if('B'==cmd) { // this is a label. pfodApp NEVER sends this cmd -- 'Door is' // // in the main Menu of Menu_1 // } else if('F'==cmd) { // this is a label. pfodApp NEVER sends this cmd -- '' // // in the main Menu of Menu_1 } else if('C'==cmd) { // user moved PWM slider -- 'PWM Setting' // in the main Menu of Menu_1 parser.parseLong(pfodFirstArg,&pfodLongRtn); // parse first arg as a long cmd_C_var = (int)pfodLongRtn; // set variable analogWrite(cmd_C_pin,cmd_C_var); // set PWM output sendMainMenuUpdate(); // always send back a pfod msg otherwise pfodApp will disconnect. } else if('D'==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_D_var = (int)pfodLongRtn; // set variable digitalWrite(cmd_D_pin,cmd_D_var); // set output if(cmd_D_var == 1) { cmd_D_pulseStartTime = millis(); // high pulse cmd_D_pulseRunning = true; } else { cmd_D_pulseRunning = false; } sendMainMenuUpdate(); // always send back a pfod msg otherwise pfodApp will disconnect. } else if('G'==cmd) { // user pressed -- 'Voltage Plot' // in the main Menu of Menu_1 // return plotting msg. parser.print(F("{=Voltage at A0|time (secs)|A0~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. } } cmd_A_readADC(); cmd_B_var = digitalRead(cmd_B_pin); // read digital input cmd_D_checkPulse(); sendPlotData(); // <<<<<<<<<<< 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 sendPlotData() { if ((millis() - plotDataTimer) > PLOT_DATA_INTERVAL) { plotDataTimer = millis(); // restart plot data timer // 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(((float)plotDataTimer)/10000.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_readADC() { if ((millis() - cmd_A_adcStartTime) > cmd_A_ADC_READ_INTERVAL) { cmd_A_adcStartTime = millis(); // restart timer cmd_A_var = analogRead(cmd_A_pin); // read ADC input } } void cmd_D_checkPulse() { if (cmd_D_pulseRunning && ((millis() - cmd_D_pulseStartTime) > cmd_D_PULSE_LENGTH)) { cmd_D_pulseRunning = false; // timer finished cmd_D_var = 0; // return output to LOW digitalWrite(cmd_D_pin,cmd_D_var); // update output pin } } 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("<+4>~Arduino Data`1000")); parser.sendVersion(); // send the menu version // send menu items parser.print(F("|!A")); parser.print('`'); parser.print(cmd_A_var); // output the current ADC reading parser.print(F("~A0 ~V`1023`0~3.3~0~")); parser.print(F("|!E<-6>")); parser.print(F("~")); parser.print(F("|!B<+4>")); parser.print('`'); parser.print(cmd_B_var); // output the current state of the input parser.print(F("~Door is ~~Closed\\Open~t")); // Note the \\ inside the "'s to send \ ... parser.print(F("|!F<-6>")); parser.print(F("~")); parser.print(F("|C")); parser.print('`'); parser.print(cmd_C_var); // output the current PWM setting parser.print(F("~PWM Setting ~%`255`0~100~0~")); parser.print(F("|D<+5>")); parser.print('`'); parser.print(cmd_D_var); // output the current state 0 Low or 1 High parser.print(F("~LED is ~~Off\\On~t")); // Note the \\ inside the "'s to send \ ... parser.print(F("|G<+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 ADC reading parser.print(F("|B")); parser.print('`'); parser.print(cmd_B_var); // output the current state of the input parser.print(F("|C")); parser.print('`'); parser.print(cmd_C_var); // output the current PWM setting parser.print(F("|D")); parser.print('`'); parser.print(cmd_D_var); // output the current state 0 Low or 1 High parser.print(F("}")); // close pfod message // ============ end of menu =========== } // ========== pfodBLESerial methods pfodBLESerial::pfodBLESerial() : BLEPeripheral() { setLocalName(localName); addAttribute(uartService); addAttribute(uartNameDescriptor); setAdvertisedServiceUuid(uartService.uuid()); addAttribute(rxCharacteristic); addAttribute(rxNameDescriptor); rxCharacteristic.setEventHandler(BLEWritten, pfodBLESerial::receiveHandler); setEventHandler(BLEConnected, pfodBLESerial::connectHandler); setEventHandler(BLEDisconnected, pfodBLESerial::disconnectHandler); addAttribute(txCharacteristic); addAttribute(txNameDescriptor); }; bool pfodBLESerial::isConnected() { return (connected && txCharacteristic.subscribed()); } void pfodBLESerial::begin() { BLEPeripheral::begin(); } void pfodBLESerial::close() { BLEPeripheral::disconnect(); } void pfodBLESerial::poll() { BLEPeripheral::poll(); } 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) { BLEPeripheral::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 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; } // called as part of parser.parse() so will poll() each loop() int pfodBLESerial::available() { BLEPeripheral::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; BLEPeripheral::poll(); } int pfodBLESerial::peek() { BLEPeripheral::poll(); if (rxTail == rxHead) { return -1; } size_t nextIdx = (rxTail + 1) % sizeof(rxBuffer); uint8_t byte = rxBuffer[nextIdx]; return byte; } void pfodBLESerial::connectHandler(BLECentral& central) { // clear parser with -1 incase partial message left // should not be one addReceiveBytes(pfodEOF, sizeof(pfodEOF)); connected = true; } void pfodBLESerial::disconnectHandler(BLECentral& central) { // parser.closeConnection(); // clear parser with -1 and insert {!} incase connection just lost addReceiveBytes(pfodEOF, sizeof(pfodEOF)); addReceiveBytes((const uint8_t*)pfodCloseConnection, sizeof(pfodCloseConnection)); connected = false; } 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::receiveHandler(BLECentral& central, BLECharacteristic& rxCharacteristic) { size_t len = rxCharacteristic.valueLength(); const unsigned char *data = rxCharacteristic.value(); addReceiveBytes((const uint8_t*)data, len); } //======================= end pfodBLESerial methods int swap01(int in) { return (in==0)?1:0; } // ============= end generated code =========