forked from OpenBCI/OpenBCI_Cyton_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenBCI_32bit_Library.h
390 lines (357 loc) · 13.4 KB
/
OpenBCI_32bit_Library.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
insert header here
*/
#ifndef _____OpenBCI_32bit__
#define _____OpenBCI_32bit__
#include <DSPI.h>
#include <Arduino.h>
#include <OpenBCI_Wifi_Master.h>
#include <OpenBCI_Wifi_Master_Definitions.h>
#include "OpenBCI_32bit_Library_Definitions.h"
void __USER_ISR ADS_DRDY_Service(void);
class OpenBCI_32bit_Library {
public:
// ENUMS
typedef enum ACCEL_MODE {
ACCEL_MODE_ON,
ACCEL_MODE_OFF
};
typedef enum TIME_SYNC_MODE {
TIME_SYNC_MODE_ON,
TIME_SYNC_MODE_OFF
};
typedef enum MARKER_MODE {
MARKER_MODE_ON,
MARKER_MODE_OFF
};
typedef enum DEBUG_MODE {
DEBUG_MODE_ON,
DEBUG_MODE_OFF
};
typedef enum BOARD_MODE {
BOARD_MODE_DEFAULT,
BOARD_MODE_DEBUG,
BOARD_MODE_ANALOG,
BOARD_MODE_DIGITAL,
BOARD_MODE_MARKER,
BOARD_MODE_BLE,
BOARD_MODE_END_OF_MODES // This must be the last entry-insert any new board modes above this line
};
typedef enum MULTI_CHAR_COMMAND {
MULTI_CHAR_CMD_NONE,
MULTI_CHAR_CMD_PROCESSING_INCOMING_SETTINGS_CHANNEL,
MULTI_CHAR_CMD_PROCESSING_INCOMING_SETTINGS_LEADOFF,
MULTI_CHAR_CMD_SERIAL_PASSTHROUGH,
MULTI_CHAR_CMD_SETTINGS_BOARD_MODE,
MULTI_CHAR_CMD_SETTINGS_SAMPLE_RATE,
MULTI_CHAR_CMD_INSERT_MARKER
};
typedef enum PACKET_TYPE {
PACKET_TYPE_ACCEL,
PACKET_TYPE_RAW_AUX,
PACKET_TYPE_USER_DEFINED,
PACKET_TYPE_ACCEL_TIME_SET,
PACKET_TYPE_ACCEL_TIME_SYNC,
PACKET_TYPE_RAW_AUX_TIME_SET,
PACKET_TYPE_RAW_AUX_TIME_SYNC
};
typedef enum SAMPLE_RATE {
SAMPLE_RATE_16000,
SAMPLE_RATE_8000,
SAMPLE_RATE_4000,
SAMPLE_RATE_2000,
SAMPLE_RATE_1000,
SAMPLE_RATE_500,
SAMPLE_RATE_250
};
// STRUCTS
typedef struct {
uint32_t baudRate;
boolean rx;
boolean tx;
} SerialInfo;
typedef struct {
boolean rx;
boolean tx;
} SpiInfo;
typedef struct {
uint8_t sampleNumber;
uint8_t data[BLE_TOTAL_DATA_BYTES];
boolean ready;
boolean flushing;
uint8_t bytesFlushed;
uint8_t bytesLoaded;
} BLE;
// Start up functions
OpenBCI_32bit_Library();
boolean accelHasNewData(void);
void accelUpdateAxisData(void);
void accelWriteAxisDataSerial(void);
void activateAllChannelsToTestCondition(byte testInputCode, byte amplitudeCode, byte freqCode);
void activateChannel(byte); // enable the selected channel
void ADS_writeChannelData(void);
void ADS_writeChannelDataAvgDaisy(void);
void ADS_writeChannelDataNoAvgDaisy(void);
void attachDaisy(void);
void begin(void);
void beginDebug(void);
void beginDebug(uint32_t);
void beginPinsAnalog(void);
void beginPinsDebug(void);
void beginPinsDefault(void);
void beginPinsDigital(void);
void beginSerial0(void);
void beginSerial0(uint32_t);
void beginSerial1(void);
void beginSerial1(uint32_t);
void boardReset(void);
void changeChannelLeadOffDetect();
void changeChannelLeadOffDetect(byte N);
void channelSettingsArraySetForAll(void);
void channelSettingsArraySetForChannel(byte N);
void channelSettingsSetForChannel(byte, byte, byte, byte, byte, byte, byte);
boolean checkMultiCharCmdTimer(void);
void csLow(int);
void csHigh(int);
void configureInternalTestSignal(byte,byte);
void configureLeadOffDetection(byte, byte);
void deactivateChannel(byte); // disable given channel 1-8(16)
void disable_accel(void); // stop data acquisition and go into low power mode
void enable_accel(byte); // start acceleromoeter with default settings
void endMultiCharCmdTimer(void);
void endSerial0(void);
void endSerial1(void);
const char* getBoardMode(void);
char getChannelCommandForAsciiChar(char);
char getCharSerial0(void);
char getCharSerial1(void);
char getConstrainedChannelNumber(byte);
byte getDefaultChannelSettingForSetting(byte);
char getDefaultChannelSettingForSettingAscii(byte);
char getGainForAsciiChar(char);
uint8_t * getGains(void);
char getMultiCharCommand(void);
char getNumberForAsciiChar(char);
const char* getSampleRate(void);
char getTargetSSForConstrainedChannelNumber(byte);
char getYesOrNoForAsciiChar(char);
boolean hasDataSerial0(void);
boolean hasDataSerial1(void);
boolean isADSDataAvailable(void);
boolean isProcessingMultibyteMsg(void);
void leadOffConfigureSignalForAll(byte, byte);
void leadOffConfigureSignalForTargetSS(byte, byte, byte);
void leadOffSetForAllChannels(void);
void leadOffSetForChannel(byte, byte, byte);
void ledFlash(int);
void loop(void);
void printAll(char);
void printAll(const char *);
void printlnAll(void);
void printlnAll(const char *);
void printlnSerial(void);
void printlnSerial(char);
void printlnSerial(int);
void printlnSerial(int, int);
void printlnSerial(const char *);
void printSerial(int);
void printSerial(char);
void printSerial(int, int);
void printSerial(const char *);
boolean processChar(char);
boolean processCharWifi(char);
void processIncomingBoardMode(char);
void processIncomingSampleRate(char);
void processInsertMarker(char);
void processIncomingChannelSettings(char);
void processIncomingLeadOffSettings(char);
void reportDefaultChannelSettings(void);
void removeDaisy(void);
void resetADS(int); // reset all the ADS1299's settings
void resetChannelSettingsArrayToDefault(byte channelSettingsArray[][OPENBCI_NUMBER_OF_CHANNEL_SETTINGS]);
void resetLeadOffArrayToDefault(byte leadOffArray[][OPENBCI_NUMBER_OF_LEAD_OFF_SETTINGS]);
void startADS(void);
void stopADS(void);
void sendChannelData(void);
void sendChannelData(PACKET_TYPE);
void setBoardMode(uint8_t);
void setChannelsToDefault(void);
void setCurPacketType(void);
void setSampleRate(uint8_t newSampleRateCode);
void sendEOT(void);
void setSerialInfo(SerialInfo, boolean, boolean, uint32_t);
boolean smellDaisy(void);
void startMultiCharCmdTimer(char);
void streamSafeChannelDeactivate(byte);
void streamSafeChannelActivate(byte);
void streamSafeSetSampleRate(SAMPLE_RATE);
void streamSafeChannelSettingsForChannel(byte, byte, byte, byte, byte, byte, byte);
void streamSafeSetAllChannelsToDefault(void);
void streamSafeReportAllChannelDefaults(void);
void streamSafeLeadOffSetForChannel(byte, byte, byte);
void streamSafeTimeSendSyncSetPacket(void);
void streamStart(void);
void streamStop(void);
void tryMultiAbort(void);
void updateBoardData(void);
void updateBoardData(boolean);
void updateChannelData(void); // retrieve data from ADS
void updateDaisyData(void);
void updateDaisyData(boolean);
void useAccel(boolean);
void useTimeStamp(boolean);
void write(uint8_t);
void writeAuxDataSerial(void);
void writeChannelSettings(void);
void writeChannelSettings(byte);
void writeSerial(uint8_t);
void writeSpi(uint8_t);
void writeTimeCurrent(void);
void writeTimeCurrentSerial(uint32_t newTime);
void writeZeroAux(void);
void zeroAuxData(void);
// Variables
boolean boardUseSRB1; // used to keep track of if we are using SRB1
boolean daisyPresent;
boolean daisyUseSRB1;
boolean streaming;
boolean useInBias[OPENBCI_NUMBER_OF_CHANNELS_DAISY]; // used to remember if we were included in Bias before channel power down
boolean useSRB2[OPENBCI_NUMBER_OF_CHANNELS_DAISY];
boolean verbosity; // turn on/off Serial verbosity
byte boardChannelDataRaw[OPENBCI_NUMBER_BYTES_PER_ADS_SAMPLE]; // array to hold raw channel data
byte channelSettings[OPENBCI_NUMBER_OF_CHANNELS_DAISY][OPENBCI_NUMBER_OF_CHANNEL_SETTINGS]; // array to hold current channel settings
byte daisyChannelDataRaw[OPENBCI_NUMBER_BYTES_PER_ADS_SAMPLE];
byte defaultChannelSettings[OPENBCI_NUMBER_OF_CHANNEL_SETTINGS]; // default channel settings
byte lastBoardDataRaw[OPENBCI_NUMBER_BYTES_PER_ADS_SAMPLE];
byte lastDaisyDataRaw[OPENBCI_NUMBER_BYTES_PER_ADS_SAMPLE];
byte leadOffSettings[OPENBCI_NUMBER_OF_CHANNELS_DAISY][OPENBCI_NUMBER_OF_LEAD_OFF_SETTINGS]; // used to control on/off of impedance measure for P and N side of each channel
byte meanBoardDataRaw[OPENBCI_NUMBER_BYTES_PER_ADS_SAMPLE];
byte meanDaisyDataRaw[OPENBCI_NUMBER_BYTES_PER_ADS_SAMPLE];
byte sampleCounter;
byte sampleCounterBLE;
int ringBufBLEHead;
int ringBufBLETail;
int boardChannelDataInt[OPENBCI_NUMBER_CHANNELS_PER_ADS_SAMPLE]; // array used when reading channel data as ints
int daisyChannelDataInt[OPENBCI_NUMBER_CHANNELS_PER_ADS_SAMPLE]; // array used when reading channel data as ints
int lastBoardChannelDataInt[OPENBCI_NUMBER_CHANNELS_PER_ADS_SAMPLE];
int lastDaisyChannelDataInt[OPENBCI_NUMBER_CHANNELS_PER_ADS_SAMPLE];
int meanBoardChannelDataInt[OPENBCI_NUMBER_CHANNELS_PER_ADS_SAMPLE];
int meanDaisyChannelDataInt[OPENBCI_NUMBER_CHANNELS_PER_ADS_SAMPLE];
int numChannels;
short auxData[3]; // This is user faceing
short axisData[3];
unsigned long lastSampleTime;
volatile boolean channelDataAvailable;
// ENUMS
ACCEL_MODE curAccelMode;
BOARD_MODE curBoardMode;
DEBUG_MODE curDebugMode;
PACKET_TYPE curPacketType;
SAMPLE_RATE curSampleRate;
TIME_SYNC_MODE curTimeSyncMode;
// Stucts
BLE bufferBLE[BLE_RING_BUFFER_SIZE];
// STRUCTS
SerialInfo iSerial0;
SerialInfo iSerial1;
// Class Objects
DSPI0 spi; // use DSPI library
// #ifdef __OpenBCI_Wifi_Master__
void accelWriteAxisDataWifi(void);
void ADS_writeChannelDataWifi(boolean daisy);
void writeAuxDataWifi(void);
void writeTimeCurrentWifi(uint32_t newTime);
// #endif
private:
byte ADS_getDeviceID(int);
void boardBeginADSInterrupt(void);
boolean boardBegin(void);
boolean boardBeginDebug(void);
boolean boardBeginDebug(int);
void changeInputType(byte);
int getX(void);
int getY(void);
int getZ(void);
void bufferBLEReset(void);
void bufferBLEReset(BLE *);
void initialize(void);
void initialize_accel(byte); // initialize
void initialize_ads(void);
void initializeSerialInfo(SerialInfo);
void initializeVariables(void);
void initializeSpiInfo(SpiInfo);
byte LIS3DH_getDeviceID(void);
byte LIS3DH_read(byte); // read a register on LIS3DH
int LIS3DH_read16(byte); // read two bytes, used to get axis data
void LIS3DH_write(byte,byte); // write a register on LIS3DH
boolean LIS3DH_DataReady(void); // check LIS3DH_DRDY pin
boolean LIS3DH_DataAvailable(void); // check LIS3DH STATUS_REG2
void LIS3DH_readAllRegs(void);
void LIS3DH_writeAxisDataSerial(void);
void LIS3DH_writeAxisDataForAxisSerial(uint8_t);
void LIS3DH_updateAxisData(void);
void LIS3DH_zeroAxisData(void);
void printADSregisters(int);
void printAllRegisters(void);
void printFailure();
void printHex(byte);
void printlnHex(byte);
void printRegisterName(byte);
void printSuccess();
void RDATA(int); // read data one-shot
void RDATAC(int); // go into read data continuous mode
void RESET(int); // set all register values to default
byte RREG(byte,int); // read one ADS register
void RREGS(byte,byte,int); // read multiple ADS registers
void SDATAC(int); // get out of read data continuous mode
void sendChannelDataSerial(PACKET_TYPE);
void sendChannelDataSerialBLE(PACKET_TYPE packetType);
void sendTimeWithAccelSerial(void);
void sendTimeWithRawAuxSerial(void);
void STANDBY(int); // go into low power mode
void START(int); // start data acquisition
void STOP(int); // stop data acquisition
void WAKEUP(int); // get out of low power mode
void WREG(byte,byte,int); // write one ADS register
void WREGS(byte,byte,int); // write multiple ADS registers
byte xfer(byte); // SPI Transfer function
// Variables
boolean commandFromSPI;
boolean firstDataPacket;
boolean isMultiCharCmd; // A multi char command is in progress
boolean isRunning;
boolean settingBoardMode;
boolean settingSampleRate;
boolean newMarkerReceived; // flag to indicate a new marker has been received
byte regData[24]; // array is used to mirror register data
char buffer[1];
char markerValue;
char multiCharCommand; // The type of command
char currentChannelSetting;
char optionalArgBuffer5[5];
char optionalArgBuffer6[6];
char optionalArgBuffer7[7];
int boardStat; // used to hold the status register
int daisyStat;
int DRDYpinValue;
int lastDRDYpinValue;
int numberOfIncomingSettingsProcessedChannel;
int numberOfIncomingSettingsProcessedLeadOff;
int numberOfIncomingSettingsProcessedBoardType;
uint8_t optionalArgCounter;
unsigned long multiCharCmdTimeout; // the timeout in millis of the current multi char command
unsigned long timeOfLastRead;
unsigned long timeOfMultiByteMsgStart;
#ifdef __OpenBCI_Wifi_Master__
// functions
void LIS3DH_writeAxisDataWifi(void);
void LIS3DH_writeAxisDataForAxisWifi(uint8_t);
void sendChannelDataWifi(PACKET_TYPE, boolean);
void sendRawAuxWifi(void);
void sendTimeWithAccelWifi(void);
void sendTimeWithRawAuxWifi(void);
#endif
};
// This let's us call into the class from within the library if necessary
extern OpenBCI_32bit_Library board;
#endif