/* ===== pfod Command for Led ==== pfodApp msg {.} --> {,<+7>~Led Control`0~V4|A<+4>`0~Led is ~~Off\On~} */ // Using RFduino BLE board V2.3.1 // Using Arduino V1.6.8 IDE /* Code generated by pfodDesignerV2 V2.0.2060 * (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 // evaluates to no eeprom for RFduninoBLE #include int swap01(int); // method prototype for slider end swaps // =========== pfodBLESerial definitions class pfodBLESerial : 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(); static void addReceiveBytes(const uint8_t* bytes, size_t len); const static uint8_t pfodEOF[1]; const static char* pfodCloseConnection; volatile static bool connected; 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; volatile static uint8_t rxBuffer[BLE_RX_MAX_LENGTH]; size_t txIdx; uint8_t txBuffer[BLE_MAX_LENGTH]; }; 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("V5"); // 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 'Led is' const int cmd_A_pin = 3; // name the output pin for 'Led is' // the setup routine runs once on reset: void setup() { for (int i=3; i>0; i--) { // wait a few secs to see if we are being programmed delay(1000); } cmd_A_var = 0; //pinMode(cmd_A_pin, INPUT_PULLUP); pinMode(cmd_A_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_A_pin,cmd_A_var); // set output bleSerial.begin(); // start the BLE 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) { // user moved slider -- 'Led is' // in the main Menu of Led // 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 ('!' == cmd) { // CloseConnection command closeConnection(parser.getPfodAppStream()); } else { // unknown command parser.print(F("{}")); // always send back a pfod msg otherwise pfodApp will disconnect. } } // <<<<<<<<<<< Your other loop() code goes here } void closeConnection(Stream *io) { // add any special code here to force connection to be dropped } 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 ~~Off\\On~")); // Note the \\ inside the "'s to send \ ... 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("}")); // close pfod message // ============ end of menu =========== } //=========== RFduino BLE callback methods void RFduinoBLE_onConnect() { // clear parser with -1 in case partial message left, should not be one bleSerial.addReceiveBytes(bleSerial.pfodEOF, sizeof(bleSerial.pfodEOF)); bleSerial.connected = true; } void RFduinoBLE_onDisconnect() { // clear parser with -1 and insert {!} incase connection just lost bleSerial.addReceiveBytes(bleSerial.pfodEOF, sizeof(bleSerial.pfodEOF)); bleSerial.addReceiveBytes((const uint8_t*)bleSerial.pfodCloseConnection, sizeof(bleSerial.pfodCloseConnection)); bleSerial.connected = false; } void RFduinoBLE_onReceive(char *data, int len) { bleSerial.addReceiveBytes((const uint8_t*)data, len); } // ========== pfodBLESerial methods pfodBLESerial::pfodBLESerial() {}; bool pfodBLESerial::isConnected() { return (connected); } void pfodBLESerial::begin() { RFduinoBLE.begin();} void pfodBLESerial::close() {} void pfodBLESerial::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) { 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() { 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; } while (!RFduinoBLE.send((const char*)txBuffer, txIdx)) { delay(10); // wait a bit } txIdx = 0; } int pfodBLESerial::peek() { if (rxTail == rxHead) { return -1; } size_t nextIdx = (rxTail + 1) % sizeof(rxBuffer); uint8_t byte = rxBuffer[nextIdx]; return byte; } 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]; } } //======================= end pfodBLESerial methods int swap01(int in) { return (in==0)?1:0; } // ============= end generated code =========