add brainduino test code

This commit is contained in:
Jeffrey Paul 2019-08-27 18:12:52 +02:00
parent 8b6cf834b2
commit 3537f9d506
4 changed files with 110 additions and 0 deletions

4
brainduino/Makefile Normal file
View File

@ -0,0 +1,4 @@
default: run
run:
pipenv run python3 main.py

13
brainduino/Pipfile Normal file
View File

@ -0,0 +1,13 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
pyserial = "*"
sanelogging = "*"
[requires]
python_version = "3.7"

43
brainduino/Pipfile.lock generated Normal file
View File

@ -0,0 +1,43 @@
{
"_meta": {
"hash": {
"sha256": "02979d2499db65e8d3d0573c25c28fd31d393240497d4f3ec8607dbba02ea1be"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.7"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"colorlog": {
"hashes": [
"sha256:3cf31b25cbc8f86ec01fef582ef3b840950dea414084ed19ab922c8b493f9b42",
"sha256:450f52ea2a2b6ebb308f034ea9a9b15cea51e65650593dca1da3eb792e4e4981"
],
"version": "==4.0.2"
},
"pyserial": {
"hashes": [
"sha256:6e2d401fdee0eab996cf734e67773a0143b932772ca8b42451440cfed942c627",
"sha256:e0770fadba80c31013896c7e6ef703f72e7834965954a78e71a3049488d4d7d8"
],
"index": "pypi",
"version": "==3.4"
},
"sanelogging": {
"hashes": [
"sha256:ae018de50f00ace45b34dbade66b511651cb1758a7c26e262841651b2dd6f2cf"
],
"index": "pypi",
"version": "==1.0.1"
}
},
"develop": {}
}

50
brainduino/main.py Normal file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env python3
import serial
from sanelogging import log
import binascii
class Brainduino(object):
def __init__(self):
self.inbuffer = bytes()
def add_bytes(self,bytes):
self.inbuffer = self.inbuffer + bytes
if self.buffer_contains_line():
self.read_and_process_line_from_buffer()
def buffer_contains_line(self):
return self.inbuffer.find(b'\r') != -1
def process_line(self,line):
line = line.decode("UTF-8")
if len(line) != 16:
# this line is incomplete, skip
return
log.info("got line: " + line)
p = line.strip().split("\t")
print(binascii.unhexlify(p[0] + p[1]))
print(binascii.unhexlify(p[2] + p[3]))
def read_and_process_line_from_buffer(self):
i = self.inbuffer.find(b'\r')
if i < 1:
log.error("should not happen")
return
line = self.inbuffer[0:i+1]
self.inbuffer = self.inbuffer[i+1:]
self.process_line(line)
def main():
e = Brainduino()
with serial.Serial(
'/dev/tty.Alpheratz-SPPDev',
115200,
timeout=1
) as ser:
while True:
e.add_bytes(ser.read(16))
if __name__ == "__main__":
main()