diff --git a/OpenBCI_32bit_Library.cpp b/OpenBCI_32bit_Library.cpp index 7a6d2ba..afd2f72 100644 --- a/OpenBCI_32bit_Library.cpp +++ b/OpenBCI_32bit_Library.cpp @@ -14,11 +14,12 @@ an OpenBCI 32bit board with an OpenBCI Daisy Module attached. /***************************************************/ // CONSTRUCTOR OpenBCI_32bit_Library::OpenBCI_32bit_Library() { - boardType = OPENBCI_BOARD_MODE_DEFAULT; + curBoardMode = OPENBCI_BOARD_MODE_DEFAULT; daisyPresent = false; sniffMode = false; useAccel = false; useAux = false; + channelDataAvailable = false; initializeVariables(); } @@ -27,6 +28,7 @@ OpenBCI_32bit_Library::OpenBCI_32bit_Library() { * @author: AJ Keller (@pushtheworldllc) */ void OpenBCI_32bit_Library::begin(void) { + curBoardMode = OPENBCI_BOARD_MODE_DEFAULT; // Bring the board up boardBegin(); } @@ -50,6 +52,7 @@ void OpenBCI_32bit_Library::begin(void) { void OpenBCI_32bit_Library::beginDebug(void) { // Bring the board up boolean started = boardBeginDebug(); + curBoardMode = OPENBCI_BOARD_MODE_DEBUG; sniffMode = true; @@ -68,29 +71,53 @@ void OpenBCI_32bit_Library::beginDebug(void) { * @author: AJ Keller (@pushtheworldllc) */ boolean OpenBCI_32bit_Library::beginSecondarySerial(void) { - // Bring the board up - return boardBeginDebug(OPENBCI_BAUD_RATE); + // Initalize the serial 1 port + Serial1.begin(OPENBCI_BAUD_RATE); + return true; } /** -* @description Called in every `loop()` and checks both `Serial0` and `Serial1` -* if `sniffMode` is `true`. +* @description Called in every `loop()` and checks `Serial0` * @returns {boolean} - `true` if there is data ready to be read */ -boolean OpenBCI_32bit_Library::isSerialAvailableForRead(void) { +boolean OpenBCI_32bit_Library::hasDataSerial0(void) { if (Serial0.available()) { return true; } else { - if (sniffMode && Serial1.available()) { - return true; - } else { - return false; - } + return false; } } /** +* @description Called in every `loop()` and checks `Serial0` +* @returns {boolean} - `true` if there is data ready to be read +*/ +boolean OpenBCI_32bit_Library::hasDataSerial1(void) { + if (Serial1.available()) { + return true; + } else { + return false; + } +} + +/** +* @description Called if `hasDataSerial0` is true, returns a char from `Serial0` +* @returns {char} - A char from the serial port +*/ +char OpenBCI_32bit_Library::getCharSerial0(void) { + return Serial0.read(); +} + +/** +* @description Called if `hasDataSerial1` is true, returns a char from `Serial1` +* @returns {char} - A char from the serial port +*/ +char OpenBCI_32bit_Library::getCharSerial1(void) { + return Serial1.read(); +} + +/** * @description If `isSerialAvailableForRead()` is `true` then this function is * called. Reads from `Serial0` first and foremost, which comes from the RFduino. * If `sniffMode` is true and `Serial0` didn't have any data, we will try to @@ -99,15 +126,15 @@ boolean OpenBCI_32bit_Library::isSerialAvailableForRead(void) { * many safe guards. * @returns {char} - The character from the serial port. */ -char OpenBCI_32bit_Library::readOneSerialChar(void) { - if (Serial0.available()) { - return Serial0.read(); - } else if (sniffMode && Serial1.available()) { - return Serial1.read(); - } else { - return 0x00; - } -} +// char OpenBCI_32bit_Library::readOneSerialChar(void) { +// if (Serial0.available()) { +// return Serial0.read(); +// } else if (sniffMode && Serial1.available()) { +// return Serial1.read(); +// } else { +// return 0x00; +// } +// } /** * @description Public function for sending data to the PC @@ -126,7 +153,7 @@ void OpenBCI_32bit_Library::writeSerial(char *data, int len) { * @return {boolean} - True if processing a message and false otherwise */ boolean OpenBCI_32bit_Library::isProcessingMultibyteMsg(void) { - return isProcessingIncomingSettingsChannel || isProcessingIncomingSettingsLeadOff || isProcessingIncomingPacketType; + return isProcessingIncomingSettingsChannel || isProcessingIncomingSettingsLeadOff || settingBoardMode; } /** @@ -146,8 +173,8 @@ boolean OpenBCI_32bit_Library::processChar(char character) { processIncomingChannelSettings(character); } else if (isProcessingIncomingSettingsLeadOff) { processIncomingLeadOffSettings(character); - } else if (isProcessingIncomingPacketType) { - setStreamPacketType(character); + } else if (settingBoardMode) { + processIncomingBoardMode(character); } } else { // Normal... switch (character){ @@ -361,9 +388,9 @@ boolean OpenBCI_32bit_Library::processChar(char character) { break; // PACKET SET TYPE - case OPENBCI_PACKET_TYPE_SET: + case OPENBCI_BOARD_MODE_SET: // Tell this function which case that is - isProcessingIncomingPacketType = true; + settingBoardMode = true; break; default: @@ -409,9 +436,11 @@ boolean OpenBCI_32bit_Library::boardBegin(void) { pinMode(OPENBCI_PIN_LED, OUTPUT); pinMode(OPENBCI_PIN_PGC, OUTPUT); - // chill for a bit - // TODO: See the result of removing this - // delay(1000); + // Startup for interrupt + setIntVector(_EXTERNAL_4_VECTOR, ADS_DRDY_Service); // connect interrupt to ISR + setIntPriority(_EXTERNAL_4_VECTOR, 4, 0); // set interrupt priority and sub priority + clearIntFlag(_EXTERNAL_4_IRQ); // these two need to be done together + setIntEnable(_EXTERNAL_4_IRQ); // clear any flags before enabing the irq // Do a soft reset boardReset(); @@ -431,9 +460,11 @@ boolean OpenBCI_32bit_Library::boardBeginDebug(void) { // Initalize the serial debug port Serial1.begin(OPENBCI_BAUD_RATE); - // TODO: See the result of removing this - // chill for a bit - // delay(1000); + // Startup for interrupt + setIntVector(_EXTERNAL_4_VECTOR, ADS_DRDY_Service); // connect interrupt to ISR + setIntPriority(_EXTERNAL_4_VECTOR, 4, 0); // set interrupt priority and sub priority + clearIntFlag(_EXTERNAL_4_IRQ); // these two need to be done together + setIntEnable(_EXTERNAL_4_IRQ); // clear any flags before enabing the irq // Do a soft reset boardReset(); @@ -516,85 +547,21 @@ void OpenBCI_32bit_Library::activateAllChannelsToTestCondition(byte testInputCod * remaining 4 bytes. * @param `character` - {char} - The character you want to process... */ -void OpenBCI_32bit_Library::processIncomingLeadOffSettings(char character) { - - if (character == OPENBCI_CHANNEL_IMPEDANCE_LATCH && numberOfIncomingSettingsProcessedLeadOff < OPENBCI_NUMBER_OF_BYTES_SETTINGS_LEAD_OFF - 1) { - // We failed somehow and should just abort - // reset numberOfIncomingSettingsProcessedLeadOff - numberOfIncomingSettingsProcessedLeadOff = 0; - - // put flag back down - isProcessingIncomingSettingsLeadOff = false; +void OpenBCI_32bit_Library::processIncomingBoardMode(char c) { + if (isValidBoardType(c)) { + curBoardMode = c; if (!streaming) { - Serial0.print("Lead off failure: too few chars"); sendEOT(); + Serial0.print("Success: Board type set"); + sendEOT(); } - - return; - } - switch (numberOfIncomingSettingsProcessedLeadOff) { - case 1: // channel number - currentChannelSetting = getChannelCommandForAsciiChar(character); - break; - case 2: // pchannel setting - leadOffSettings[currentChannelSetting][PCHAN] = getNumberForAsciiChar(character); - break; - case 3: // nchannel setting - leadOffSettings[currentChannelSetting][NCHAN] = getNumberForAsciiChar(character); - break; - case 4: // 'Z' latch - if (character != OPENBCI_CHANNEL_IMPEDANCE_LATCH) { - if (!streaming) { - Serial0.print("Err: 5th char not "); - Serial0.println(OPENBCI_CHANNEL_IMPEDANCE_LATCH); - sendEOT(); - } - // We failed somehow and should just abort - // reset numberOfIncomingSettingsProcessedLeadOff - numberOfIncomingSettingsProcessedLeadOff = 0; - - // put flag back down - isProcessingIncomingSettingsLeadOff = false; - - } - break; - default: // should have exited - if (!streaming) { - Serial0.print("Err: too many chars "); - sendEOT(); - } - // We failed somehow and should just abort - // reset numberOfIncomingSettingsProcessedLeadOff - numberOfIncomingSettingsProcessedLeadOff = 0; - - // put flag back down - isProcessingIncomingSettingsLeadOff = false; - return; - } - - // increment the number of bytes processed - numberOfIncomingSettingsProcessedLeadOff++; - - if (numberOfIncomingSettingsProcessedLeadOff == (OPENBCI_NUMBER_OF_BYTES_SETTINGS_LEAD_OFF)) { - // We are done processing lead off settings... - + } else { if (!streaming) { - Serial0.print("Lead off set for "); Serial0.println(currentChannelSetting + 1); sendEOT(); - } - - if (sniffMode && Serial1) { - Serial1.print("Lead off set for "); Serial1.println(currentChannelSetting + 1); + Serial0.print("Failure: invalid board mode"); + sendEOT(); } - - // Set lead off settings - streamSafeLeadOffSetForChannel(currentChannelSetting + 1,leadOffSettings[currentChannelSetting][PCHAN],leadOffSettings[currentChannelSetting][NCHAN]); - - // reset numberOfIncomingSettingsProcessedLeadOff - numberOfIncomingSettingsProcessedLeadOff = 0; - - // put flag back down - isProcessingIncomingSettingsLeadOff = false; } + settingBoardMode = false; } /** @@ -692,6 +659,108 @@ void OpenBCI_32bit_Library::processIncomingChannelSettings(char character) { } } +/** + * @description When a 'z' is found on the serial port, we jump to this function + * where we continue to read from the serial port and read the + * remaining 4 bytes. + * @param `character` - {char} - The character you want to process... + */ +void OpenBCI_32bit_Library::processIncomingLeadOffSettings(char character) { + + if (character == OPENBCI_CHANNEL_IMPEDANCE_LATCH && numberOfIncomingSettingsProcessedLeadOff < OPENBCI_NUMBER_OF_BYTES_SETTINGS_LEAD_OFF - 1) { + // We failed somehow and should just abort + // reset numberOfIncomingSettingsProcessedLeadOff + numberOfIncomingSettingsProcessedLeadOff = 0; + + // put flag back down + isProcessingIncomingSettingsLeadOff = false; + + if (!streaming) { + Serial0.print("Lead off failure: too few chars"); sendEOT(); + } + + return; + } + switch (numberOfIncomingSettingsProcessedLeadOff) { + case 1: // channel number + currentChannelSetting = getChannelCommandForAsciiChar(character); + break; + case 2: // pchannel setting + leadOffSettings[currentChannelSetting][PCHAN] = getNumberForAsciiChar(character); + break; + case 3: // nchannel setting + leadOffSettings[currentChannelSetting][NCHAN] = getNumberForAsciiChar(character); + break; + case 4: // 'Z' latch + if (character != OPENBCI_CHANNEL_IMPEDANCE_LATCH) { + if (!streaming) { + Serial0.print("Err: 5th char not "); + Serial0.println(OPENBCI_CHANNEL_IMPEDANCE_LATCH); + sendEOT(); + } + // We failed somehow and should just abort + // reset numberOfIncomingSettingsProcessedLeadOff + numberOfIncomingSettingsProcessedLeadOff = 0; + + // put flag back down + isProcessingIncomingSettingsLeadOff = false; + + } + break; + default: // should have exited + if (!streaming) { + Serial0.print("Err: too many chars "); + sendEOT(); + } + // We failed somehow and should just abort + // reset numberOfIncomingSettingsProcessedLeadOff + numberOfIncomingSettingsProcessedLeadOff = 0; + + // put flag back down + isProcessingIncomingSettingsLeadOff = false; + return; + } + + // increment the number of bytes processed + numberOfIncomingSettingsProcessedLeadOff++; + + if (numberOfIncomingSettingsProcessedLeadOff == (OPENBCI_NUMBER_OF_BYTES_SETTINGS_LEAD_OFF)) { + // We are done processing lead off settings... + + if (!streaming) { + Serial0.print("Lead off set for "); Serial0.println(currentChannelSetting + 1); sendEOT(); + } + + if (sniffMode && Serial1) { + Serial1.print("Lead off set for "); Serial1.println(currentChannelSetting + 1); + } + + // Set lead off settings + streamSafeLeadOffSetForChannel(currentChannelSetting + 1,leadOffSettings[currentChannelSetting][PCHAN],leadOffSettings[currentChannelSetting][NCHAN]); + + // reset numberOfIncomingSettingsProcessedLeadOff + numberOfIncomingSettingsProcessedLeadOff = 0; + + // put flag back down + isProcessingIncomingSettingsLeadOff = false; + } +} + + +boolean OpenBCI_32bit_Library::isValidBoardType(char c) { + switch (c) { + case OPENBCI_BOARD_MODE_DEFAULT: + case OPENBCI_BOARD_MODE_DEBUG: + case OPENBCI_BOARD_MODE_WIFI: + case OPENBCI_BOARD_MODE_INPUT_ANALOG: + case OPENBCI_BOARD_MODE_INPUT_DIGITAL: + return true; + default: + return false; + } +} + + // <<<<<<<<<<<<<<<<<<<<<<<<< BOARD WIDE FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> void OpenBCI_32bit_Library::initialize(){ @@ -707,16 +776,23 @@ void OpenBCI_32bit_Library::initialize(){ initializeVariables(); } +// void __USER_ISR ADS_DRDY_Service() { +void __USER_ISR ADS_DRDY_Service() { + clearIntFlag(_EXTERNAL_4_IRQ); // clear the irq, or else it will continually interrupt! + if(bitRead(PORTA,0) == 0){ + board.channelDataAvailable = true; + } +} + void OpenBCI_32bit_Library::initializeVariables(void) { streaming = false; sendTimeSyncUpPacket = false; timeSynced = false; isProcessingIncomingSettingsChannel = false; isProcessingIncomingSettingsLeadOff = false; - isProcessingIncomingPacketType = false; + settingBoardMode = false; numberOfIncomingSettingsProcessedChannel = 0; numberOfIncomingSettingsProcessedLeadOff = 0; - numberOfIncomingBytesProcessedTime = 0; currentChannelSetting = 0; timeOffset = 0; timeSetCharArrived = 0; @@ -2036,6 +2112,8 @@ boolean OpenBCI_32bit_Library::isADSDataAvailable(void) { // CALLED WHEN DRDY PIN IS ASSERTED. NEW ADS DATA AVAILABLE! void OpenBCI_32bit_Library::updateChannelData(){ + // this needs to be reset, or else it will constantly flag us + channelDataAvailable = false; updateBoardData(); if(daisyPresent) {updateDaisyData();} } @@ -2778,21 +2856,4 @@ void OpenBCI_32bit_Library::resetLeadOffArrayToDefault(byte leadOffArray[][OPENB } } -/** - * @description Used to change the current packet type - * @param {char} - The type of stream packet to send - */ -void OpenBCI_32bit_Library::setStreamPacketType(char newPacketType) { - switch (newPacketType) { - case OPENBCI_PACKET_TYPE_TIME_SYNCED: - streamPacketType = (char)OPENBCI_PACKET_TYPE_TIME_SYNCED; - break; - case OPENBCI_PACKET_TYPE_V3: - default: - streamPacketType = (char)OPENBCI_PACKET_TYPE_V3; - break; - } - isProcessingIncomingPacketType = false; -} - OpenBCI_32bit_Library board; diff --git a/OpenBCI_32bit_Library.h b/OpenBCI_32bit_Library.h index 57ef4ee..454fd16 100644 --- a/OpenBCI_32bit_Library.h +++ b/OpenBCI_32bit_Library.h @@ -10,27 +10,30 @@ #include #include "OpenBCI_32bit_Library_Definitions.h" - +void __USER_ISR ADS_DRDY_Service(void); class OpenBCI_32bit_Library { public: // Start up functions OpenBCI_32bit_Library(); - void begin(void); - void beginDebug(void); + boolean accelHasNewData(void); + void accelUpdateAxisData(void); + void accelWriteAxisData(void); + void begin(void); + void beginDebug(void); boolean beginSecondarySerial(void); - char readOneSerialChar(void); + char getCharSerial0(void); + char getCharSerial1(void); + boolean hasDataSerial0(void); + boolean hasDataSerial1(void); - void writeSerial(char *data, int len); + + void writeSerial(char *data, int len); boolean isADSDataAvailable(void); - boolean isSerialAvailableForRead(void); - boolean accelHasNewData(void); - void accelUpdateAxisData(void); - void accelWriteAxisData(void); void writeTimeCurrent(void); void writeZeroAux(void); @@ -47,8 +50,9 @@ class OpenBCI_32bit_Library { void leadOffSetForChannel(byte channelNumber, byte pInput, byte nInput); boolean processChar(char character); - void processIncomingLeadOffSettings(char character); + void processIncomingBoardMode(char character); void processIncomingChannelSettings(char character); + void processIncomingLeadOffSettings(char character); void resetChannelSettingsArrayToDefault(byte channelSettingsArray[][OPENBCI_NUMBER_OF_CHANNEL_SETTINGS]); void resetLeadOffArrayToDefault(byte leadOffArray[][OPENBCI_NUMBER_OF_LEAD_OFF_SETTINGS]); @@ -58,7 +62,6 @@ class OpenBCI_32bit_Library { void sendChannelDataWithTimeAndAccel(void); void sendChannelDataWithTimeAndRawAux(void); - void setStreamPacketType(char newPacketType); void streamSafeChannelDeactivate(byte channelNumber); void streamSafeChannelActivate(byte channelNumber); void streamSafeChannelSettingsForChannel(byte channelNumber, byte powerDown, byte gain, byte inputType, byte bias, byte srb2, byte srb1); @@ -86,17 +89,18 @@ class OpenBCI_32bit_Library { boolean sendTimeSyncUpPacket; boolean isProcessingIncomingSettingsChannel; boolean isProcessingIncomingSettingsLeadOff; - // boolean isProcessingIncomingTime; - boolean isProcessingIncomingPacketType; + boolean settingBoardMode; + volatile boolean channelDataAvailable; + boolean isProcessingMultibyteMsg(void); + boolean isValidBoardType(char c); + + uint8_t curBoardMode; - int boardType; int numberOfIncomingSettingsProcessedChannel; int numberOfIncomingSettingsProcessedLeadOff; - int numberOfIncomingBytesProcessedTime; char streamPacketType; char currentChannelSetting; - // HardwareSerial *_serial; // Getters char getChannelCommandForAsciiChar(char asciiChar); diff --git a/OpenBCI_32bit_Library_Definitions.h b/OpenBCI_32bit_Library_Definitions.h index 08a8fb6..d0fc3e4 100644 --- a/OpenBCI_32bit_Library_Definitions.h +++ b/OpenBCI_32bit_Library_Definitions.h @@ -317,15 +317,20 @@ #define OPENBCI_STREAM_STOP 's' /** Miscellaneous */ -#define OPENBCI_MISC_QUERY_REGISTER_SETTINGS '?' -#define OPENBCI_MISC_SOFT_RESET 'v' +#define OPENBCI_MISC_QUERY_REGISTER_SETTINGS '?' +#define OPENBCI_MISC_SOFT_RESET 'v' /** 16 Channel Commands */ #define OPENBCI_CHANNEL_MAX_NUMBER_8 'c' #define OPENBCI_CHANNEL_MAX_NUMBER_16 'C' /** Set Packet Type */ -#define OPENBCI_PACKET_TYPE_SET '/' +#define OPENBCI_BOARD_MODE_SET '/' +#define OPENBCI_BOARD_MODE_DEFAULT '0' +#define OPENBCI_BOARD_MODE_DEBUG '1' +#define OPENBCI_BOARD_MODE_WIFI '2' +#define OPENBCI_BOARD_MODE_INPUT_ANALOG '3' +#define OPENBCI_BOARD_MODE_INPUT_DIGITAL '4' /** Sync Clocks */ #define OPENBCI_TIME_SET '<' @@ -336,6 +341,7 @@ #define OPENBCI_NUMBER_OF_CHANNELS_DEFAULT 8 /** Helpful numbers */ +#define OPENBCI_NUMBER_OF_BOARD_SETTINGS 1 #define OPENBCI_NUMBER_OF_CHANNEL_SETTINGS 6 #define OPENBCI_NUMBER_OF_LEAD_OFF_SETTINGS 2 @@ -368,9 +374,4 @@ #define OPENBCI_FIRMWARE_VERSION_V1 1 #define OPENBCI_FIRMWARE_VERSION_V2 1 -#define OPENBCI_BOARD_MODE_DEFAULT 0x00 -#define OPENBCI_BOARD_MODE_SERIAL_EXTERN 0x01 -#define OPENBCI_BOARD_MODE_INPUT_ANALOG 0x02 -#define OPENBCI_BOARD_MODE_INPUT_DIGITAL 0x03 - #endif diff --git a/changelog.md b/changelog.md index 0b5b489..31eaa22 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,15 @@ -# 2.0.0 +# v2.0.0-rc.6 + +### New Features + +* Add `hasDataSerial0`, `hasDataSerial1`, `getCharSerial0`, and `getCharSerial1` ### Breaking Changes +* Removed `isSerialAvailableForRead` + +# 2.0.0 + ### Bug Fixes * Sending `d` did not terminate with an EOT (`$$$`) diff --git a/examples/BasicBoard/BasicBoard.ino b/examples/BasicBoard/BasicBoard.ino index 493055c..94ebeb6 100644 --- a/examples/BasicBoard/BasicBoard.ino +++ b/examples/BasicBoard/BasicBoard.ino @@ -10,23 +10,21 @@ void setup() { void loop() { if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) {} + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); - // Read from the ADS(s) and store data into - board.updateChannelData(); - - if (board.timeSynced) { + if (board.timeSynced) { board.sendChannelDataWithTimeAndRawAux(); - } else { + } else { // Send standard packet with channel data board.sendChannelDataWithRawAux(); + } } } - // Check the serial port for new data - if (board.isSerialAvailableForRead()) { + if (board.hasDataSerial0()) { // Read one char and process it - board.processChar(board.readOneSerialChar()); + board.processChar(board.getCharSerial0()); } } diff --git a/examples/BasicBoardDebug/BasicBoardDebug.ino b/examples/BasicBoardDebug/BasicBoardDebug.ino index 609ec27..d5886ec 100644 --- a/examples/BasicBoardDebug/BasicBoardDebug.ino +++ b/examples/BasicBoardDebug/BasicBoardDebug.ino @@ -10,23 +10,26 @@ void setup() { void loop() { if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) {} + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); - // Read from the ADS(s) and store data into - board.updateChannelData(); - - if (board.timeSynced) { + if (board.timeSynced) { board.sendChannelDataWithTimeAndRawAux(); - } else { + } else { // Send standard packet with channel data board.sendChannelDataWithRawAux(); + } } } // Check the serial port for new data - if (board.isSerialAvailableForRead()) { + if (board.hasDataSerial0()) { + // Read one char and process it + board.processChar(board.getCharSerial0()); + } + if (board.hasDataSerial1()) { // Read one char and process it - board.processChar(board.readOneSerialChar()); + board.processChar(board.getCharSerial1()); } } diff --git a/examples/BoardWith2ButtonExternalTriggers/BoardWith2ButtonExternalTriggers.ino b/examples/BoardWith2ButtonExternalTriggers/BoardWith2ButtonExternalTriggers.ino index f5cc38d..8774352 100644 --- a/examples/BoardWith2ButtonExternalTriggers/BoardWith2ButtonExternalTriggers.ino +++ b/examples/BoardWith2ButtonExternalTriggers/BoardWith2ButtonExternalTriggers.ino @@ -41,51 +41,48 @@ void setup() { void loop() { if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) { - // Could do tiny maintiance tasks here - } - - // Read from the ADS(s) and store data into - board.updateChannelData(); + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); - leftButtonValue = digitalRead(leftButton); // feel the left button - if (leftButtonValue != lastLeftButtonValue){ // if it's changed, + // Read the left button value + leftButtonValue = digitalRead(leftButton); + if (leftButtonValue != lastLeftButtonValue) { // if it's changed, if (leftButtonValue == HIGH){ // if it's gone from LOW to HIGH - // 0x6220 converts to PI in GUI - board.auxData[0] = 0x6220; - addAuxToSD = true; // add Aux Data to the SD card if it's there + // 0x6220 converts to PI in GUI + board.auxData[0] = 0x6220; + addAuxToSD = true; // add Aux Data to the SD card if it's there } lastLeftButtonValue = leftButtonValue; // keep track of the changes - } + } - rightButtonValue = digitalRead(rightButton); // feel the right button - if (rightButtonValue != lastRightButtonValue){ // if it's changed, + // Read the right button value + rightButtonValue = digitalRead(rightButton); + if (rightButtonValue != lastRightButtonValue){ // if it's changed, if (rightButtonValue == HIGH){ // if it's gone from LOW to HIGH - // 0x6220 converts to PI in GUI - board.auxData[1] = 0x6220; - addAuxToSD = true; // add Aux Data to the SD card if it's there + // 0x6220 converts to PI in GUI + board.auxData[1] = 0x6220; + addAuxToSD = true; // add Aux Data to the SD card if it's there } lastRightButtonValue = rightButtonValue; // keep track of the changes - } + } - // Verify the SD file is open - if(SDfileOpen) { + // Verify the SD file is open + if(SDfileOpen) { // Write to the SD card, writes aux data writeDataToSDcard(board.sampleCounter); - } - - - // Send standard packet with channel data and accel data - // includes aux data because we set told the board to add it - board.sendChannelData(); + } + // Send standard packet with channel data and accel data + // includes aux data because we set told the board to add it + board.sendChannelData(); + } } // Check the serial port for new data - if (board.isSerialAvailableForRead()) { + if (board.hasDataSerial0()) { // Read one char from the serial port - char newChar = board.readOneSerialChar(); + char newChar = board.getCharSerial0(); // Send to the sd library for processing sdProcessChar(newChar); diff --git a/examples/BoardWithAccel/BoardWithAccel.ino b/examples/BoardWithAccel/BoardWithAccel.ino index 304fccd..5631df3 100644 --- a/examples/BoardWithAccel/BoardWithAccel.ino +++ b/examples/BoardWithAccel/BoardWithAccel.ino @@ -15,30 +15,28 @@ void loop() { // The main dependency of this single threaded microcontroller is to // stream data from the ADS. if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) {} + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); - // Read from the ADS(s) and store data into - board.updateChannelData(); - - // Check to see if accel has new data - if(board.accelHasNewData()){ + // Check to see if accel has new data + if(board.accelHasNewData()){ // Get new accel data board.accelUpdateAxisData(); - } + } - // Send standard packet with channel data - if (board.timeSynced) { + // Send standard packet with channel data + if (board.timeSynced) { board.sendChannelDataWithTimeAndAccel(); - } else { + } else { board.sendChannelDataWithAccel(); + } } - } // Check the serial port for new data - if (board.isSerialAvailableForRead()) { + if (board.hasDataSerial0()) { // Read one char and process it - board.processChar(board.readOneSerialChar()); + board.processChar(board.getCharSerial0()); } } diff --git a/examples/BoardWithAnalogSensor/BoardWithAnalogSensor.ino b/examples/BoardWithAnalogSensor/BoardWithAnalogSensor.ino index a72444d..bb13e97 100644 --- a/examples/BoardWithAnalogSensor/BoardWithAnalogSensor.ino +++ b/examples/BoardWithAnalogSensor/BoardWithAnalogSensor.ino @@ -12,32 +12,31 @@ void setup() { void loop() { - // The main dependency of this single threaded microcontroller is to - // stream data from the ADS. - if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) {} - - // Read from the ADS(s) and store data into - board.updateChannelData(); - - // Read from the analog sensor and store auxiliary position 0 - // take a reading from the ADC. Result range from 0 to 1023 - board.auxData[0] = analogRead(A7); - - // Send standard packet with channel data and accel data - // includes aux data because we set `useAux` in setup() - if (board.timeSynced) { - board.sendChannelDataWithTimeAndRawAux(); - } else { - // Send standard packet with channel data - board.sendChannelDataWithRawAux(); - } + // The main dependency of this single threaded microcontroller is to + // stream data from the ADS. + if (board.streaming) { + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); + + // Read from the analog sensor and store auxiliary position 0 + // take a reading from the ADC. Result range from 0 to 1023 + board.auxData[0] = analogRead(A7); + + // Send standard packet with channel data and accel data + // includes aux data because we set `useAux` in setup() + if (board.timeSynced) { + board.sendChannelDataWithTimeAndRawAux(); + } else { + // Send standard packet with channel data + board.sendChannelDataWithRawAux(); + } } + } - // Check the serial port for new data - if (board.isSerialAvailableForRead()) { + // Check the serial port for new data + if (board.hasDataSerial0()) { // Read one char and process it - board.processChar(board.readOneSerialChar()); - } + board.processChar(board.getCharSerial0()); + } } diff --git a/examples/BoardWithDigitalRead/BoardWithDigitalRead.ino b/examples/BoardWithDigitalRead/BoardWithDigitalRead.ino index aaf9e4c..3f783c0 100644 --- a/examples/BoardWithDigitalRead/BoardWithDigitalRead.ino +++ b/examples/BoardWithDigitalRead/BoardWithDigitalRead.ino @@ -18,10 +18,8 @@ void loop() { // The main dependency of this single threaded microcontroller is to // stream data from the ADS. if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) {} - - // Read from the ADS(s) and store data into + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false board.updateChannelData(); // Read from the analog sensor and store auxiliary position 0 @@ -36,11 +34,12 @@ void loop() { // Send standard packet with channel data board.sendChannelDataWithRawAux(); } + } } // Check the serial port for new data - if (board.isSerialAvailableForRead()) { - // Read one char and process it - board.processChar(board.readOneSerialChar()); + if (board.hasDataSerial0()) { + // Read one char and process it + board.processChar(board.getCharSerial0()); } } diff --git a/examples/BoardWithPulseSensor/BoardWithPulseSensor.ino b/examples/BoardWithPulseSensor/BoardWithPulseSensor.ino index 155cd2e..60c025b 100644 --- a/examples/BoardWithPulseSensor/BoardWithPulseSensor.ino +++ b/examples/BoardWithPulseSensor/BoardWithPulseSensor.ino @@ -1,6 +1,6 @@ /* * Built by Joel Murphy from code made by Push The World LLC based on code made for OpenBCI by Joel, Conor, Luke, and Leif. - * + * */ @@ -50,23 +50,21 @@ void loop() { // The main dependency of this single threaded microcontroller is to // stream data from the ADS. if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) {} + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); - // Read from the ADS(s) and store data into - board.updateChannelData(); + // Read from the PulseSensor and store auxilary position 0 + // take a reading from the ADC. Result range from 0 to 1023 + getPulse(); - // Read from the PulseSensor and store auxilary position 0 - // take a reading from the ADC. Result range from 0 to 1023 - getPulse(); - - // Send standard packet with channel data and accel data - // includes aux data because we set told the board to add it - board.sendChannelData(); + // Send standard packet with channel data and accel data + // includes aux data because we set told the board to add it + board.sendChannelData(); + } } - // Check the serial port for new data - if (board.isSerialAvailableForRead()) { + if (board.hasDataSerial0()) { // Read one char and process it board.processChar(board.readOneSerialChar()); } diff --git a/examples/BoardWithSD/BoardWithSD.ino b/examples/BoardWithSD/BoardWithSD.ino index 6399114..89362d0 100644 --- a/examples/BoardWithSD/BoardWithSD.ino +++ b/examples/BoardWithSD/BoardWithSD.ino @@ -16,26 +16,25 @@ void setup() { void loop() { if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) {} + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); - // Read from the ADS(s) and store data into - board.updateChannelData(); - - // Verify the SD file is open - if(SDfileOpen) { + // Verify the SD file is open + if(SDfileOpen) { // Write to the SD card writeDataToSDcard(board.sampleCounter); - } + } - // Send standard packet with channel data - board.sendChannelData(); + // Send standard packet with channel data + board.sendChannelData(); + } } // Check the serial port for new data - if (board.isSerialAvailableForRead()) { + if (board.hasDataSerial0()) { // Read one char from the serial port - char newChar = board.readOneSerialChar(); + char newChar = board.getCharSerial0(); // Send to the sd library for processing sdProcessChar(newChar); diff --git a/examples/BoardWithWifi/BoardWithWifi.ino b/examples/BoardWithWifi/BoardWithWifi.ino new file mode 100644 index 0000000..becb09a --- /dev/null +++ b/examples/BoardWithWifi/BoardWithWifi.ino @@ -0,0 +1,56 @@ +#include +#include +#include + +// THIS IS NOT COMPLETE OR WORKING! + +void setup() { + // Bring up the OpenBCI Board + board.begin(); + board.beginSecondarySerial(); + +} + +void loop() { + + if (board.streaming) { + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); + + if (board.timeSynced) { + board.sendChannelDataWithTimeAndRawAux(); + } else { + // Send standard packet with channel data + board.sendChannelDataWithRawAux(); + } + } + } + + // Check the serial port for new data + if (Serial0.available()) { + char newChar = board.getCharSerial0(); + // Read one char and process it + board.processChar(newChar); + + if (newChar == 'h') { + Serial1.print("AT"); + } else if (newChar == 'k') { + Serial1.print("AT+RST"); + } else if (newChar == 'n') { + Serial1.print("AT+GMR"); + } else if (newChar == 'N') { + Serial1.print("AT+CWMODE=3"); + } else if (newChar == 'o') { + Serial1.print("AT+CWJAP="","""); + } else if (newChar == 'O') { + Serial1.print("AT+CWJAP?"); + } else if (newChar == 'V') { + Serial1.print("AT+CWLAP"); + } + } + + if (board.hasDataSerial1()) { + Serial0.write(Serial1.read()); + } +} diff --git a/examples/DefaultBoard/DefaultBoard.ino b/examples/DefaultBoard/DefaultBoard.ino index 6378d23..b419e64 100644 --- a/examples/DefaultBoard/DefaultBoard.ino +++ b/examples/DefaultBoard/DefaultBoard.ino @@ -19,47 +19,40 @@ void setup() { void loop() { if (board.streaming) { - // Wait for the ADS to signal it's ready with new data - while (board.waitForNewChannelData()) { - // Could do tiny maintiance tasks here - } - - // Read from the ADS(s) and store data into - board.updateChannelData(); + if (board.channelDataAvailable) { + // Read from the ADS(s), store data, set channelDataAvailable flag to false + board.updateChannelData(); - // Check to see if accel has new data - if(board.accelHasNewData()){ + // Check to see if accel has new data + if(board.accelHasNewData()) { // Get new accel data board.accelUpdateAxisData(); // Tell the SD_Card_Stuff.ino to add accel data in the next write to SD addAccelToSD = true; // Set false after writeDataToSDcard() - } + } - // Verify the SD file is open - if(SDfileOpen) { + // Verify the SD file is open + if(SDfileOpen) { // Write to the SD card, writes aux data writeDataToSDcard(board.sampleCounter); - } - - - if (board.timeSynced) { + } + if (board.timeSynced) { // Send time synced packet with channel data, current board time, and an accel reading - // X axis is sent on sampleCounter % 10 == 0 - // Y axis is sent on sampleCounter % 10 == 1 - // Z axis is sent on sampleCounter % 10 == 2 + // X axis is sent on sampleCounter % 10 == 7 + // Y axis is sent on sampleCounter % 10 == 8 + // Z axis is sent on sampleCounter % 10 == 9 board.sendChannelDataWithTimeAndAccel(); - } else { + } else { // Send standard packet with channel data board.sendChannelDataWithAccel(); + } } - } - - // Check the serial port for new data - if (board.isSerialAvailableForRead()) { - // Read one char from the serial port - char newChar = board.readOneSerialChar(); + // Check serial 0 for new data + if (board.hasDataSerial0()) { + // Read one char from the serial 0 port + char newChar = board.getCharSerial0(); // Send to the sd library for processing sdProcessChar(newChar); diff --git a/keywords.txt b/keywords.txt index 7231edb..5cfdae3 100755 --- a/keywords.txt +++ b/keywords.txt @@ -15,10 +15,12 @@ accelHasNewData KEYWORD2 accelUpdateAxisData KEYWORD2 begin KEYWORD2 beginDebug KEYWORD2 +getCharSerial0 KEYWORD2 +getCharSerial1 KEYWORD2 +hasDataSerial0 KEYWORD2 +hasDataSerial1 KEYWORD2 isADSDataAvailable KEYWORD2 -isSerialAvailableForRead KEYWORD2 processChar KEYWORD2 -readOneSerialChar KEYWORD2 sendChannelData KEYWORD2 sendChannelDataWithAccel KEYWORD2 sendChannelDataWithRawAux KEYWORD2 diff --git a/tests-ptw-assert/OpenBCI_32bit_Libraries_Test/OpenBCI_32bit_Libraries_Test.ino b/tests-ptw-assert/OpenBCI_32bit_Libraries_Test/OpenBCI_32bit_Libraries_Test.ino index 3352345..6ae1e5f 100644 --- a/tests-ptw-assert/OpenBCI_32bit_Libraries_Test/OpenBCI_32bit_Libraries_Test.ino +++ b/tests-ptw-assert/OpenBCI_32bit_Libraries_Test/OpenBCI_32bit_Libraries_Test.ino @@ -1,4 +1,4 @@ - #include +#include #include #include "OpenBCI_32bit.h" #include "PTW-Arduino-Assert.h" @@ -179,7 +179,7 @@ void testSendChannelData() { byte expectedNumberOfBytes = 34; byte acutalNumberOfBytes = board.sendChannelData(); - + Serial1.print("\n"); test.assertEqual(expectedNumberOfBytes, acutalNumberOfBytes, "Stream packet writes 34 bytes");