Skip to content

Commit

Permalink
Added UTC time to live mode output
Browse files Browse the repository at this point in the history
  • Loading branch information
atbrask committed Sep 28, 2015
1 parent 6e2aa6d commit 97ba886
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 49 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ History
-------
1.0 (2015-03-24) Uploaded initial version to GitHub
1.1 (2015-03-26) Added support for downloading recorded data
1.2 (2015-09-28) Added UTC time to live mode output

Syntax
------
Expand Down
54 changes: 29 additions & 25 deletions cms50dplus.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env python
import sys, serial, argparse, csv, time
import sys, serial, argparse, csv, datetime

class LiveDataPoint(object):
def __init__(self, data):
def __init__(self, time, data):
if [d & 0x80 != 0 for d in data] != [True, False, False, False, False]:
raise ValueError("Invalid data packet.")

self.time = time

# 1st byte
self.signalStrength = data[0] & 0x0f
self.fingerOut = bool(data[0] & 0x10)
Expand Down Expand Up @@ -61,37 +63,39 @@ def getBytes(self):

def __repr__(self):
hexBytes = ['0x{0:02X}'.format(byte) for byte in self.getBytes()]
return "LiveDataPoint([{0}])".format(', '.join(hexBytes))
return "LiveDataPoint({0}, [{1}])".format(self.time.__repr__(), ', '.join(hexBytes))

def __str__(self):
return ", ".join(["Signal Strength = {0}",
"Finger Out = {1}",
"Dropping SpO2 = {2}",
"Beep = {3}",
"Pulse waveform = {4}",
"Bar Graph = {5}",
"Probe Error = {6}",
"Searching = {7}",
"Pulse Rate = {8} bpm",
"SpO2 = {9}%"]).format(self.signalStrength,
self.fingerOut,
self.droppingSpO2,
self.beep,
self.pulseWaveform,
self.barGraph,
self.probeError,
self.searching,
self.pulseRate,
self.bloodSpO2)
return ", ".join(["Time = {0}",
"Signal Strength = {1}",
"Finger Out = {2}",
"Dropping SpO2 = {3}",
"Beep = {4}",
"Pulse waveform = {5}",
"Bar Graph = {6}",
"Probe Error = {7}",
"Searching = {8}",
"Pulse Rate = {9} bpm",
"SpO2 = {10}%"]).format(self.time,
self.signalStrength,
self.fingerOut,
self.droppingSpO2,
self.beep,
self.pulseWaveform,
self.barGraph,
self.probeError,
self.searching,
self.pulseRate,
self.bloodSpO2)

@staticmethod
def getCsvColumns():
return ["PulseRate", "SpO2", "PulseWaveform", "BarGraph",
return ["Time", "PulseRate", "SpO2", "PulseWaveform", "BarGraph",
"SignalStrength", "Beep", "FingerOut", "Searching",
"DroppingSpO2", "ProbeError"]

def getCsvData(self):
return [self.pulseRate, self.bloodSpO2, self.pulseWaveform,
return [self.time, self.pulseRate, self.bloodSpO2, self.pulseWaveform,
self.barGraph, self.signalStrength, self.beep,
self.fingerOut, self.searching, self.droppingSpO2,
self.probeError]
Expand Down Expand Up @@ -199,7 +203,7 @@ def getLiveData(self):

if byte & 0x80:
if idx == 5 and packet[0] & 0x80:
yield LiveDataPoint(packet)
yield LiveDataPoint(datetime.datetime.utcnow(), packet)
packet = [0]*5
idx = 0

Expand Down
60 changes: 36 additions & 24 deletions unittests.py
Original file line number Diff line number Diff line change
@@ -1,105 +1,117 @@
#!/usr/bin/env python
import unittest
import unittest, datetime
from cms50dplus import *

class CMS50DplusTests(unittest.TestCase):
# LIVE DATA

def test_LiveData_init_length(self):
self.assertRaises(ValueError, LiveDataPoint, [0x80,0,0,0])
LiveDataPoint([0x80,0,0,0,0])
t = datetime.datetime.utcnow()
self.assertRaises(ValueError, LiveDataPoint, t, [0x80,0,0,0])
LiveDataPoint(t, [0x80,0,0,0,0])

def test_LiveData_init_syncbit(self):
LiveDataPoint([0x80,0,0,0,0])
self.assertRaises(ValueError, LiveDataPoint, [0,0,0,0,0])
self.assertRaises(ValueError, LiveDataPoint, [0,0x80,0,0,0])
self.assertRaises(ValueError, LiveDataPoint, [0,0,0x80,0,0])
self.assertRaises(ValueError, LiveDataPoint, [0,0,0,0x80,0])
self.assertRaises(ValueError, LiveDataPoint, [0,0,0,0,0x80])
t = datetime.datetime.utcnow()
LiveDataPoint(t, [0x80,0,0,0,0])
self.assertRaises(ValueError, LiveDataPoint, t, [0,0,0,0,0])
self.assertRaises(ValueError, LiveDataPoint, t, [0,0x80,0,0,0])
self.assertRaises(ValueError, LiveDataPoint, t, [0,0,0x80,0,0])
self.assertRaises(ValueError, LiveDataPoint, t, [0,0,0,0x80,0])
self.assertRaises(ValueError, LiveDataPoint, t, [0,0,0,0,0x80])

def test_LiveData_signalStrength(self):
t = datetime.datetime.utcnow()
for x in range(16):
y = LiveDataPoint([0x80 | x, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80 | x, 0, 0, 0, 0])
self.assertEquals(y.signalStrength, x)
self.assertEquals(y.getBytes(), [0x80 | x, 0, 0, 0, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_fingerOut(self):
t = datetime.datetime.utcnow()
# false
y = LiveDataPoint([0x80, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80, 0, 0, 0, 0])
self.assertFalse(y.fingerOut)
# true
y = LiveDataPoint([0x80 | 0x10, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80 | 0x10, 0, 0, 0, 0])
self.assertTrue(y.fingerOut)
self.assertEquals(y.getBytes(), [0x80 | 0x10, 0, 0, 0, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_droppingSpO2(self):
t = datetime.datetime.utcnow()
# false
y = LiveDataPoint([0x80, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80, 0, 0, 0, 0])
self.assertFalse(y.droppingSpO2)
# true
y = LiveDataPoint([0x80 | 0x20, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80 | 0x20, 0, 0, 0, 0])
self.assertTrue(y.droppingSpO2)
self.assertEquals(y.getBytes(), [0x80 | 0x20, 0, 0, 0, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_beep(self):
t = datetime.datetime.utcnow()
# false
y = LiveDataPoint([0x80, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80, 0, 0, 0, 0])
self.assertFalse(y.beep)
# true
y = LiveDataPoint([0x80 | 0x40, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80 | 0x40, 0, 0, 0, 0])
self.assertTrue(y.beep)
self.assertEquals(y.getBytes(), [0x80 | 0x40, 0, 0, 0, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_pulseWaveform(self):
t = datetime.datetime.utcnow()
for x in range(128):
y = LiveDataPoint([0x80, x, 0, 0, 0])
y = LiveDataPoint(t, [0x80, x, 0, 0, 0])
self.assertEquals(y.pulseWaveform, x)
self.assertEquals(y.getBytes(), [0x80, x, 0, 0, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_barGraph(self):
t = datetime.datetime.utcnow()
for x in range(16):
y = LiveDataPoint([0x80, 0, x, 0, 0])
y = LiveDataPoint(t, [0x80, 0, x, 0, 0])
self.assertEquals(y.barGraph, x)
self.assertEquals(y.getBytes(), [0x80, 0, x, 0, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_probeError(self):
t = datetime.datetime.utcnow()
# false
y = LiveDataPoint([0x80, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80, 0, 0, 0, 0])
self.assertFalse(y.probeError)
# true
y = LiveDataPoint([0x80, 0, 0x10, 0, 0])
y = LiveDataPoint(t, [0x80, 0, 0x10, 0, 0])
self.assertTrue(y.probeError)
self.assertEquals(y.getBytes(), [0x80, 0, 0x10, 0, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_searching(self):
t = datetime.datetime.utcnow()
# false
y = LiveDataPoint([0x80, 0, 0, 0, 0])
y = LiveDataPoint(t, [0x80, 0, 0, 0, 0])
self.assertFalse(y.searching)
# true
y = LiveDataPoint([0x80, 0, 0x20, 0, 0])
y = LiveDataPoint(t, [0x80, 0, 0x20, 0, 0])
self.assertTrue(y.searching)
self.assertEquals(y.getBytes(), [0x80, 0, 0x20, 0, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_pulseRate(self):
t = datetime.datetime.utcnow()
for x in range(256):
highbit = (x & 0x80) >> 1
lowbits = x & 0x7f
y = LiveDataPoint([0x80, 0, highbit, lowbits, 0])
y = LiveDataPoint(t, [0x80, 0, highbit, lowbits, 0])
self.assertEquals(y.pulseRate, x)
self.assertEquals(y.getBytes(), [0x80, 0, highbit, lowbits, 0])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())

def test_LiveData_bloodSpO2(self):
t = datetime.datetime.utcnow()
for x in range(128):
y = LiveDataPoint([0x80, 0, 0, 0, x])
y = LiveDataPoint(t, [0x80, 0, 0, 0, x])
self.assertEquals(y.bloodSpO2, x)
self.assertEquals(y.getBytes(), [0x80, 0, 0, 0, x])
self.assertEquals(y.__repr__(), eval(y.__repr__()).__repr__())
Expand Down

0 comments on commit 97ba886

Please sign in to comment.