diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..1647830 Binary files /dev/null and b/.DS_Store differ diff --git a/PythonDriver/.DS_Store b/PythonDriver/.DS_Store new file mode 100644 index 0000000..3bce582 Binary files /dev/null and b/PythonDriver/.DS_Store differ diff --git a/PythonDriver/.vscode/settings.json b/PythonDriver/.vscode/settings.json new file mode 100644 index 0000000..9ee86e7 --- /dev/null +++ b/PythonDriver/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[python]": { + "editor.defaultFormatter": "ms-python.autopep8" + }, + "python.formatting.provider": "none" +} \ No newline at end of file diff --git a/PythonDriver/TestBufferAscii.py b/PythonDriver/TestBufferAscii.py new file mode 100644 index 0000000..ab86e6f --- /dev/null +++ b/PythonDriver/TestBufferAscii.py @@ -0,0 +1,8 @@ +import sys +import glob + +from module.ChannelBuffersSerial import * +if __name__ == '__main__': + + + pass diff --git a/PythonDriver/TestBufferAsciiTime.py b/PythonDriver/TestBufferAsciiTime.py new file mode 100644 index 0000000..9191c3f --- /dev/null +++ b/PythonDriver/TestBufferAsciiTime.py @@ -0,0 +1,6 @@ +import sys +import glob + +if __name__ == '__main__': + + pass diff --git a/PythonDriver/TestBufferBinary16bits.py b/PythonDriver/TestBufferBinary16bits.py new file mode 100644 index 0000000..9191c3f --- /dev/null +++ b/PythonDriver/TestBufferBinary16bits.py @@ -0,0 +1,6 @@ +import sys +import glob + +if __name__ == '__main__': + + pass diff --git a/PythonDriver/TestBufferBinary16bitsTime.py b/PythonDriver/TestBufferBinary16bitsTime.py new file mode 100644 index 0000000..9191c3f --- /dev/null +++ b/PythonDriver/TestBufferBinary16bitsTime.py @@ -0,0 +1,6 @@ +import sys +import glob + +if __name__ == '__main__': + + pass diff --git a/PythonDriver/TestBufferBinary8bits.py b/PythonDriver/TestBufferBinary8bits.py new file mode 100644 index 0000000..9191c3f --- /dev/null +++ b/PythonDriver/TestBufferBinary8bits.py @@ -0,0 +1,6 @@ +import sys +import glob + +if __name__ == '__main__': + + pass diff --git a/PythonDriver/TestBufferBinary8bitsTime.py b/PythonDriver/TestBufferBinary8bitsTime.py new file mode 100644 index 0000000..9191c3f --- /dev/null +++ b/PythonDriver/TestBufferBinary8bitsTime.py @@ -0,0 +1,6 @@ +import sys +import glob + +if __name__ == '__main__': + + pass diff --git a/PythonDriver/module/.DS_Store b/PythonDriver/module/.DS_Store new file mode 100644 index 0000000..9683c02 Binary files /dev/null and b/PythonDriver/module/.DS_Store differ diff --git a/PythonDriver/module/ChannelBuffersSerial.py b/PythonDriver/module/ChannelBuffersSerial.py new file mode 100644 index 0000000..4274472 --- /dev/null +++ b/PythonDriver/module/ChannelBuffersSerial.py @@ -0,0 +1,157 @@ + +import sys +import glob +import serial +import serial.tools.list_ports +from multipledispatch import dispatch + + +class SerialPort(serial.Serial): + + global ser # global variable to store the serial object + + def __init__(self): + super(SerialPort, self).__init__() + + def listSerialPorts(self): + """ + Lists the serial port names available on the system + """ + # Checking the computer operational system: + if sys.platform.startswith('win'): + ports = ['COM%s' % (i + 1) for i in range(256)] + elif sys.platform.startswith('linux'): + # this excludes your current terminal "/dev/tty" + ports = glob.glob('/dev/tty[A-Za-z]*') + elif sys.platform.startswith('cygwin'): # for windows/cygwin + ports = glob.glob('/dev/tty.*') + elif sys.platform.startswith('darwin'): + ports = glob.glob('/dev/cu.*') + else: + raise EnvironmentError('Unsupported platform') + + # Definning variables: + portsFound = [] + for port in ports: + try: + s = serial.Serial(port) + s.close() + portsFound.append(port) + except (OSError, serial.SerialException): + pass + + return portsFound + + def findDevices(self, device_serial_name="Arduino"): + """ + Look through all the available serial connections, the serial objects with "device_serial_name" + inside + """ + comPorts = [] + + portsFound = self.listSerialPorts() + connectionAvailable = len(portsFound) + + if (device_serial_name != "All"): + for curr_device in range(connectionAvailable): + devicePort = str(portsFound[curr_device]) + + if device_serial_name in devicePort: + comPorts.append((devicePort.split(' '))[0]) + else: + for curr_device in range(connectionAvailable): + devicePort = str(portsFound[curr_device]) + + comPorts.append((devicePort.split(' '))[0]) + + return comPorts + + def connect(self, serialPort, baudrate=9600, timeout=10, xonxoff=False): + if serialPort is not None: + try: + self.ser = serial.Serial( + port=serialPort, baudrate=baudrate, timeout=timeout, xonxoff=xonxoff) + print("Connected to :") + print(serialPort) + except serial.SerialException as var: + print("Connection issued!\n Exception detail:"+var) + + def disconnect(self): + self.ser.close() + + def isComStablished(self, command, readStr="ACK", encoding='ascii', errors='strict'): + command += str('\n') + serialString = val = SerialPort.ser.write( + command.encode(encoding, errors)) + + # inCommingData=self.ser.read_until(b'}') + inCommingData = SerialPort.ser.readline() + + if (inCommingData == readStr): + return True + + else: + return False + + +class ReadChannel(SerialPort): + + def __init__(self, channelNumber): + super(SerialPort, self).__init__() + self.channelNo = channelNumber + + # Declaring and implementing all the available functions for the channelBuffer + + def Ascii(self, sepCar='\t', decode='ascii'): + readData = self.ser.readline().decode(decode).split(sepCar) + readData.remove("\r\n") + + return readData + + def AsciiTimeStamp(self, sepCar='\t'): + + readData = self.Ascii(sepCar) + + return readData + + def RawData16(self): + + pass + + def RawData8(self): + + pass + + def RawDataTimeStamp16(self): + + pass + + def RawDataTimeStamp8(self): + + pass + + +if __name__ == '__main__': + + # uart = SerialPort() + uart = ReadChannel(4) + ports = uart.listSerialPorts() + + devices = uart.findDevices("usbmodem") + # devices = uart.findDevices("Arduino") + # print("Devices available : ", devices) + # print("Comports available : ", ports) + dat = devices[0] + print("dat : "+str(type(dat))) + + uart.connect(dat) + + uart.connect(devices[0]) + + for i in range(10): + # dataRead = uart.Ascii() + dataRead = uart.AsciiTimeStamp() + + print(dataRead) + + uart.disconnect() diff --git a/StreamBufferedChannel.ino b/StreamBufferedChannel.ino new file mode 100644 index 0000000..d303941 --- /dev/null +++ b/StreamBufferedChannel.ino @@ -0,0 +1,29 @@ +#include + +#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); + +} diff --git a/StreamChannel/.DS_Store b/StreamChannel/.DS_Store new file mode 100644 index 0000000..72d6c8a Binary files /dev/null and b/StreamChannel/.DS_Store differ diff --git a/StreamChannel/StreamChannel.cpp b/StreamChannel/StreamChannel.cpp new file mode 100644 index 0000000..286cf48 --- /dev/null +++ b/StreamChannel/StreamChannel.cpp @@ -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; iappendChannelData(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; +} diff --git a/StreamChannel/StreamChannel.hpp b/StreamChannel/StreamChannel.hpp new file mode 100644 index 0000000..d9bc8eb --- /dev/null +++ b/StreamChannel/StreamChannel.hpp @@ -0,0 +1,37 @@ +#ifndef _STREAMCHANNEL_HPP_ +#define _STREAMCHANNEL_HPP_ + +#include +#include + +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*/ diff --git a/examples/.DS_Store b/examples/.DS_Store new file mode 100644 index 0000000..0f17413 Binary files /dev/null and b/examples/.DS_Store differ diff --git a/examples/TestBufferAscii/TestBufferAscii.ino b/examples/TestBufferAscii/TestBufferAscii.ino new file mode 100644 index 0000000..7843fda --- /dev/null +++ b/examples/TestBufferAscii/TestBufferAscii.ino @@ -0,0 +1,17 @@ +#include + +#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"); +} diff --git a/examples/TestBufferAsciiTime/TestBufferAsciiTime.ino b/examples/TestBufferAsciiTime/TestBufferAsciiTime.ino new file mode 100644 index 0000000..9ba554d --- /dev/null +++ b/examples/TestBufferAsciiTime/TestBufferAsciiTime.ino @@ -0,0 +1,33 @@ +#include + +#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; + + + +} + + diff --git a/examples/TestBufferBinary16bits/TestBufferBinary16bits.ino b/examples/TestBufferBinary16bits/TestBufferBinary16bits.ino new file mode 100644 index 0000000..bee74bf --- /dev/null +++ b/examples/TestBufferBinary16bits/TestBufferBinary16bits.ino @@ -0,0 +1,19 @@ +#include + +#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); + + +} diff --git a/examples/TestBufferBinary16bitsTime/TestBufferBinary16bitsTime.ino b/examples/TestBufferBinary16bitsTime/TestBufferBinary16bitsTime.ino new file mode 100644 index 0000000..bf9f060 --- /dev/null +++ b/examples/TestBufferBinary16bitsTime/TestBufferBinary16bitsTime.ino @@ -0,0 +1,30 @@ +#include + +#define SIZE 3 +uint16_t DATA[SIZE]={200, 128,150}; +unsigned long previousTime; + +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; + +} diff --git a/examples/TestBufferBinary8bits/TestBufferBinary8bits.ino b/examples/TestBufferBinary8bits/TestBufferBinary8bits.ino new file mode 100644 index 0000000..df6a93b --- /dev/null +++ b/examples/TestBufferBinary8bits/TestBufferBinary8bits.ino @@ -0,0 +1,19 @@ +#include + +#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); + +} diff --git a/examples/TestBufferBinary8bitsTime/TestBufferBinary8bitsTime.ino b/examples/TestBufferBinary8bitsTime/TestBufferBinary8bitsTime.ino new file mode 100644 index 0000000..50ed8e3 --- /dev/null +++ b/examples/TestBufferBinary8bitsTime/TestBufferBinary8bitsTime.ino @@ -0,0 +1,20 @@ +#include + +#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); + + +}