constuint8_tchannelCountSize=1;// always fixed at 1 byte
constunsignedcharchannelCount[channelCountSize]={0x01};// number of active channels send to the plot, for now this is ADC. Should be 0x02 if Batt is also send.
...
...
@@ -38,7 +42,7 @@ byte sensorStatusBuffer[8];
// the task durations define the sample rate of the tasks:
constlongaquisitionTaskDuration=125;// µs per cycle //for aquisition of sensor data
longaquisitionTaskDuration=125;// µs per cycle //for aquisition of sensor data
constlongcommunicationTaskDuration=10000;// µs per cycle //of data transmission to client via Blueooth
constuint8_tjitterMax=10;// after this number of missed samples, the device is stopped.
intjitterCounter=0;// amount of times the sample momement was more than one sample time off.
staticcharreceivedChars[numChars];// an array to store the received data
staticbooleanrecvInProgress=false;
booleannewData=false;
staticbyteindex=0;
charcharactersRead;
elapsedMillisTime=0;
//TODO: cancel while loop if new data continues to arrive
while(Serial.available()>0&&newData==false&&elapsedMillisTime<maxReadTime)//read until no new data is available, or maxReadTime is reached
{
charactersRead=Serial.read();
if(recvInProgress==true)
{
if(charactersRead!=endMarker)
{
receivedChars[index]=charactersRead;
index++;
if(index>=numChars)
{
index=numChars-1;//TODO: comment FM: does not seem logical to do?
helpers::printToLog("Received more characters via serial than the max array length! "+String(receivedChars),verboseLevels::error);
}
}
else
{
receivedChars[index]='\0';// terminate the string
recvInProgress=false;
index=0;
newData=true;
}
}
elseif(charactersRead==startMarker)
{
recvInProgress=true;
}
}
if(newData==true)//print the parsed command for debug purposes.
{
helpers::printToLog("Received via Serial: "+String(receivedChars),verboseLevels::info);
}
returnreceivedChars;
}
uint32_tsampleCounter=0;
uint32_tsampleCounterOld=0;
uint32_tmillisOld=0;
uint8_tmillisWraps=0;// count the number of times millis() reached 2^32.
uint8_tdownSampleCounter=0;// count up each aquisition cycle
/*
this taks reads the sensor data at a fixed interval. Based upon the realtime example of library TaskScheduler (https://github.com/arkhipenko/TaskScheduler)
this task will be started and stopped based upon the <start> / <stop> commands received via Bluetooth.
elseif(strncmp(receivedChars,"SetSampleRate",13)==0)// command starting with "SetSampleRate", should always be followed by 4 integers, for example "SetSampleRate1000" for 1000 Hz
{
if(aquisitionTask.isEnabled())
{
helpers::printToLog("Received Set Sample Rate command, but aquisition is active!",verboseLevels::error);
}
else
{
charargument[5];// stores the value received from the host in ASCCI text
memcpy(argument,&receivedChars[13],4);// copy characters from full input command
helpers::printToLog("Received Set Sample Rate command is below "+String(minSampleRate)+" or above "+String(maxSampleRate),verboseLevels::error);
}
elseif(newSampleRate/downSampler<minSampleRate)
{
helpers::printToLog("Received Set Sample Rate command is too low in combination with configured downsampler: "+String(downSampler),verboseLevels::error);
}
else
{
aquisitionTaskDuration=1000000/newSampleRate;// µs per cycle //for aquisition of sensor data
elseif(strncmp(receivedChars,"SetDownSampleRate",17)==0)// command starting with "SetDownSampleRate", should always be followed by 4 integers, for example "SetDownSampleRate0016" for 16 times downsample
{
if(aquisitionTask.isEnabled())
{
helpers::printToLog("Received Set DownSample Rate command, but aquisition is active!",verboseLevels::error);
}
else
{
charargument[5];// stores the value received from the host in ASCCI text
memcpy(argument,&receivedChars[17],4);// copy characters from full input command
helpers::printToLog("Received Set Down Sample Rate command is too low in combination with configured sample rate: "+String(1000000/aquisitionTask.getInterval()),verboseLevels::error);
}
else
{
downSampler=newDownSampleRate;//set new down sample rate
helpers::printToLog(" try to send buffer B of size (bytes): "+String(btBufferB.getPointerPosition())+", data: "+String(btBufferB.buffer[10]),verboseLevels::extraBuffers);
if(Serial.availableForWrite())
{
Serial.write((uint8_t*)btBufferB.buffer,btBufferB.getPointerPosition());// write buffer until pointer position
Serial.write((uint8_t*)btBufferB.buffer,btBufferB.getPointerPosition());// write buffer until pointer position
}
btBufferB.clearBuffer();// clear buffer B & reset pointer