Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
29 changes: 29 additions & 0 deletions StreamBufferedChannel.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <StreamChannel.hpp>

#define SIZE 3
uint16_t DATA[SIZE]={200, 128,150};


StreamBuffer Channel;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Channel.setMaxChannelNumber((uint8_t) SIZE);
}

void loop() {
// put your main code here, to run repeatedly:
Channel.appendChannelArray(DATA,SIZE);



Channel.transmitBufferAscii(Serial, "\t");
//Channel.transmitBufferBinaryRaw8(Serial);
//Channel.transmitBufferBinaryRaw16(Serial);
//Channel.sendBufferAsciiTimeStamp(Serial, 12,"\t");
//Channel.sendBufferBinaryTimeStamp(Serial, 12);

delay(100);

}
Binary file added StreamChannel/.DS_Store
Binary file not shown.
144 changes: 144 additions & 0 deletions StreamChannel/StreamChannel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include "StreamChannel.hpp"



bool StreamBuffer::isFull(){


return ((_currentIndex==_definedSize)? true:false);

}


StreamBuffer::StreamBuffer(){
_definedSize=0;
_currentIndex=0;
}


void StreamBuffer::setMaxChannelNumber(uint8_t channelNo){
_definedSize=channelNo;

_buffer=new uint16_t[_definedSize];
_lsbBuffer=new uint8_t[_definedSize];
_msbBuffer=new uint8_t[_definedSize];

for (uint8_t i=0; i<_definedSize; i++){
_buffer[i]=0;
_lsbBuffer[i]=0;
_msbBuffer[i]=0;

}


}


void StreamBuffer::appendChannelData(uint16_t data){
_currentIndex=_currentIndex%_definedSize; //set the _currentIndex to 0 when the buffer if full and this function is called to append a new data

*(_buffer+_currentIndex)=data;
_currentIndex++;


}

void StreamBuffer::Flush(){
_currentIndex=0;
for (uint8_t i=0; i<_definedSize; i++) _buffer[i]=0;

}


void StreamBuffer::appendChannelArray(uint16_t *dataSet, uint8_t arraySize){
//the buffer is reset
Flush();

if((arraySize<=_definedSize)&&(arraySize>0)){
//fill the buffer with this datSet
for (uint8_t i=0; i<arraySize;i++){
this->appendChannelData(dataSet[i]);
}

}else if (arraySize>_definedSize){
//drop other packets
for (uint8_t i=0; i<_definedSize;i++){
this->appendChannelData(dataSet[i]);
}


}else{
//dont do anything if the array size if less thant 0

}


}


void StreamBuffer::transmitBufferAscii(Stream &stream, String sepCar){

for (uint8_t i=0; i<_currentIndex; i++){

stream.print(_buffer[i]);
stream.print(sepCar);
}
stream.println();


}


void StreamBuffer::transmitBufferBinaryRaw8(Stream &stream){
for(uint8_t i=0; i<_currentIndex; i++){
_lsbBuffer[i]=(_buffer[i] & 0xFF);
_msbBuffer[i]=(_buffer[i] >> 8) & 0xFF;
}

String header="#"+ String(String(_currentIndex).length())+String(_currentIndex*sizeof(uint8_t));
//Send MSB AS FIRST PACKET
stream.print(header);
stream.write((uint8_t *) _msbBuffer,_currentIndex*sizeof(uint8_t)); // Send the upper byte first
stream.println();

//Send LSB AS SECOND PACKET
stream.print(header);
stream.write((uint8_t *) _lsbBuffer ,_currentIndex*sizeof(uint8_t)); // Send the lower byte
stream.println();

}


void StreamBuffer::transmitBufferBinaryRaw16(Stream &stream){

String header="#"+ String(String(_currentIndex).length())+String(_currentIndex*sizeof(uint8_t));
stream.print(header);
stream.write((char *) _buffer, (_currentIndex)*sizeof(uint16_t));


}


void StreamBuffer::sendBufferAsciiTimeStamp(Stream &stream, unsigned long timeStamp, String sepCar){
stream.print(timeStamp);
stream.print(sepCar);
this->transmitBufferAscii(stream,sepCar);

}


void StreamBuffer::sendBufferBinaryTimeStamp(Stream &stream, unsigned long timeStamp){
String header="#"+ String(String(_currentIndex).length())+String(_currentIndex*sizeof(uint8_t)+sizeof(unsigned long));

stream.print(header);
stream.write((char *) timeStamp, sizeof(unsigned long));
stream.write((char *) _buffer, (_currentIndex)*sizeof(uint16_t));

}


StreamBuffer::~StreamBuffer(){
delete[] _buffer;
delete[] _lsbBuffer;
delete[] _msbBuffer;
}
37 changes: 37 additions & 0 deletions StreamChannel/StreamChannel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef _STREAMCHANNEL_HPP_
#define _STREAMCHANNEL_HPP_

#include <Arduino.h>
#include <stdlib.h>

class StreamBuffer{

private:
//Declaring variables being used betwween the functions
uint16_t *_buffer;
uint8_t *_lsbBuffer;
uint8_t *_msbBuffer;
uint8_t _definedSize;
uint8_t _currentIndex;

bool isFull();

public:
//Declaring API to be implemented and used afterword
StreamBuffer();
void setMaxChannelNumber(uint8_t channelNo=3); //Allocate a buffer of 8 bit long
void appendChannelData(uint16_t); //insert the value from one channel to the BufferChannelQueue
void appendChannelArray(uint16_t *, uint8_t); //add an array with values from each channels
void transmitBufferAscii(Stream &stream, String sepCar=";");
void transmitBufferBinaryRaw8(Stream &stream);
void transmitBufferBinaryRaw16(Stream &stream);
void sendBufferAsciiTimeStamp(Stream &stream, unsigned long, String sepCar=";");
void sendBufferBinaryTimeStamp(Stream &stream, unsigned long);
void Flush();
~StreamBuffer();

};

//extern StreamBuffer StreamChannel;

#endif /*STREAMCHANNEL_H*/
Binary file added examples/.DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions examples/TestBufferAscii/TestBufferAscii.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <StreamChannel.hpp>

#define SIZE 3
uint16_t DATA[SIZE]={200, 128,150};


StreamBuffer Channel;

void setup() {
Serial.begin(9600);
Channel.setMaxChannelNumber((uint8_t) SIZE);
}

void loop() {
Channel.appendChannelArray(DATA,SIZE);
Channel.transmitBufferAscii(Serial, "\t");
}
33 changes: 33 additions & 0 deletions examples/TestBufferAsciiTime/TestBufferAsciiTime.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <StreamChannel.hpp>

#define SIZE 3
uint16_t DATA[SIZE]={200, 128,150};

unsigned long previousTime = 0;

StreamBuffer Channel;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Channel.setMaxChannelNumber((uint8_t) SIZE);
}

void loop() {
/* Updates frequently */
previousTime = millis();

Channel.appendChannelArray(DATA,SIZE);

unsigned long currentTime = millis();

Channel.sendBufferAsciiTimeStamp(Serial, (currentTime - previousTime),"\t");

/* Update the timing for the next time around */
previousTime = currentTime;



}


19 changes: 19 additions & 0 deletions examples/TestBufferBinary16bits/TestBufferBinary16bits.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <StreamChannel.hpp>

#define SIZE 3
uint16_t DATA[SIZE]={200, 128,150};


StreamBuffer Channel;

void setup() {
Serial.begin(9600);
Channel.setMaxChannelNumber((uint8_t) SIZE);
}

void loop() {
Channel.appendChannelArray(DATA,SIZE);
Channel.transmitBufferBinaryRaw16(Serial);


}
30 changes: 30 additions & 0 deletions examples/TestBufferBinary16bitsTime/TestBufferBinary16bitsTime.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <StreamChannel.hpp>

#define SIZE 3
uint16_t DATA[SIZE]={200, 128,150};


StreamBuffer Channel;

void setup() {
Serial.begin(9600);
Channel.setMaxChannelNumber((uint8_t) SIZE);
}

void loop() {
Channel.appendChannelArray(DATA,SIZE);

/* Updates frequently */
previousTime = millis();

Channel.appendChannelArray(DATA,SIZE);

unsigned long currentTime = millis();

Channel.sendBufferBinaryTimeStamp(Serial, (currentTime - previousTime));


/* Update the timing for the next time around */
previousTime = currentTime;

}
19 changes: 19 additions & 0 deletions examples/TestBufferBinary8bits/TestBufferBinary8bits.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <StreamChannel.hpp>

#define SIZE 3
uint16_t DATA[SIZE]={200, 128,150};


StreamBuffer Channel;

void setup() {
Serial.begin(9600);
Channel.setMaxChannelNumber((uint8_t) SIZE);
}

void loop() {
Channel.appendChannelArray(DATA,SIZE);

Channel.transmitBufferBinaryRaw8(Serial);

}
20 changes: 20 additions & 0 deletions examples/TestBufferBinary8bitsTime/TestBufferBinary8bitsTime.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <StreamChannel.hpp>

#define SIZE 3
uint16_t DATA[SIZE]={200, 128,150};


StreamBuffer Channel;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Channel.setMaxChannelNumber((uint8_t) SIZE);
}

void loop() {
// put your main code here, to run repeatedly:
Channel.appendChannelArray(DATA,SIZE);


}