1. Adam Dutko
  2. ExerciseTracker

Commits

Adam Dutko  committed 934f2aa

Initial import. Basic version works.

  • Participants
  • Branches master

Comments (0)

Files changed (9)

File .gitignore

View file
  • Ignore whitespace
+log/*
+*.pyc

File BLOCKER

View file
  • Ignore whitespace
+1) Need a seamless way to detect ble devices other than the sudo 
+   hcitool lescan mess.

File db/Local.py

View file
  • Ignore whitespace
+import sqlite3
+
+class Local:
+    def __init__(self, name="db/et.db"):
+        self.db = name
+        self.connection = ""
+        self.cursor = ""
+
+    def init(self):
+        try:
+            with open(self.db):
+                pass
+        except IOError:
+            ## Create the database
+            self.connection = sqlite3.connect(self.db)
+            self.cursor = self.connection.cursor()
+            self.cursor.execute('''CREATE TABLE sensors
+                (id INT, created TEXT, blid TEXT)''')
+            self.connection.commit()
+            self.connection.close()
+
+    def open(self):
+        try:
+            self.connection = sqlite3.connect(self.db)
+            self.cursor = self.connection.cursor()
+        except IOError:
+            print "ERROR: Please create the database."
+            exit(-1)
+    
+    def close(self):
+        self.connection.close()
+
+    def get_cursor(self):
+        self.open()
+        return self.cursor()
+
+    def register(self,blid):
+        if not blid:
+            print "ERROR: Bluetooth Device ID required."
+            exit()
+        cursor = self.get_cursor()
+        from datetime import date
+        today = date.today().strftime("%Y-%m-%d")
+        cursor.execute("INSERT INTO sensors \
+            VALUES (NULL,?,?)",today,blid)
+        self.connection.commit()

File db/__init__.py

  • Ignore whitespace
Empty file added.

File db/et.db

  • Ignore whitespace
Binary file added.

File et.py

View file
  • Ignore whitespace
+import curses
+from db.Local import Local
+from sensors.SensorTag import SensorTag
+
+###########
+## SETUP ##
+###########
+screen = curses.initscr()
+display = True
+
+######################
+## HELPER FUNCTIONS ##
+######################
+def setup():
+    curses.noecho()
+    screen.keypad(1)
+    screen.border(0)
+
+def refresh():
+    screen.refresh()
+
+def display(message,y=2,x=2):
+    screen.addstr(y, x, message) 
+    refresh()
+
+def cleanup(handle):
+    curses.nocbreak()
+    screen.keypad(0)
+    curses.echo()
+    curses.endwin()
+    if handle:
+        handle.disable()
+
+def enable():
+    data = SensorTag()
+    data.connect()
+    data.enable()
+    home("Logging data. Press q to stop.")
+    return data
+
+
+def print_banner(message):
+    height, width = screen.getmaxyx()
+    center_width = width/2 - (len(message))/2
+    center_width = int(center_width)
+    if center_width < 0:
+        center_width = 0
+    display(message,0,center_width)
+
+def home(update_message=""):
+    screen.clear()
+    screen.border(0)
+    print_banner("Cleveland State University Exercise Tutor")
+    display("")
+    display("Please select from the following options: ")
+    display("e - Enable Sensor(s)",4,4)
+    display("",5,4)
+    display("a - Administrative Options",6,4)
+    display("q - Quit",7,4)
+    display("",8,4)
+    display("Selection: ",9,4)
+
+    if update_message != "":
+        display(update_message,13,4)
+
+    option = screen.getch()
+    handle = ""
+    if option == ord('e'): 
+        handle = enable() 
+    elif option == ord('a'):
+        admin()
+    elif option == ord('q'): 
+        cleanup(handle)
+        exit()
+
+def admin(update_message=""):
+    screen.clear()
+    screen.border(0)
+    print_banner("Administrative Menu")
+    display("")
+    display("Please select from the following options: ")
+    display("0 - Setup Database",4,4)
+    display("",5,4)
+    display("b - Previous Menu",6,4)
+    display("",7,4)
+    display("Selection: ",8,4)
+
+    if update_message != "":
+        display(update_message,13,4)
+    
+    option = screen.getch()
+    if option == ord('0'): 
+        try:
+            with open("db/et.db"):
+                admin("Database already exists. Press ENTER")
+        except IOError:
+            database = Local()
+            database.init()
+            admin("Database setup complete! Press ENTER")
+    elif option == ord('b'):
+        home()
+
+################
+## MAIN BLOCK ##
+################
+setup()
+while display:
+    home() 

File sensors/SENSORS

View file
  • Ignore whitespace
+BC:6A:29:AB:BC:5C

File sensors/SensorTag.py

View file
  • Ignore whitespace
+import os
+import pexpect
+import subprocess
+from time import time 
+from distutils.spawn import find_executable
+
+class SensorTag:
+    def __init__(self):
+        self.tags = dict()
+        self.hciconfig = find_executable("hciconfig")
+        if not self.hciconfig:
+            print "ERROR: hciconfig must be installed."
+            exit(-1)
+        self.hcitool = find_executable("hcitool")
+        if not self.hcitool:
+            print "ERROR: hcitool must be installed."
+            exit(-1)
+        self.gatttool = find_executable("gatttool")
+        if not self.gatttool:
+            print "ERROR: gatttool must be installed."
+            exit(-1)
+        self.SENSORS = "sensors/SENSORS"
+
+    def detect(self):
+        pass
+    
+    def connect(self):
+        try:
+            sensors = open(self.SENSORS, 'r')
+            connection = ""
+            timestamp = str(time())[0:-3]
+            log_file = os.getcwd() + '/log/' + timestamp + '.run' 
+            log = open(log_file,"w+")
+            log.write("STARTING RUN @" + timestamp)
+            subprocess.call([self.hciconfig,"hci0","reset"]) 
+            for sensor in sensors:
+                sensor = sensor.rstrip()
+                log_name = os.getcwd() + '/log/' + sensor + '.data'
+                connection = pexpect.spawn('/bin/bash -c "gatttool -b ' + \
+                    sensor + ' -I | tee ' + log_name + '"')
+                connection.logfile = log
+                connection.expect('\[LE\]>', timeout=600)
+                connection.sendline('connect')
+                connection.expect('Connection successful')
+                self.tags[sensor] = connection
+            sensors.close()
+        except IOError:
+            print "ERROR: Static Sensor Tag file does not exist."
+            exit(-1)
+    
+    def enable(self):
+        enable_accelerometer = '31 01'
+        stream_accelerometer = '2E 0100'
+        enable_magnetomer = '44 01'
+        stream_magnetomer = '41 0100'
+        enable_gyroscope = '5B 01'
+        stream_gyroscope = '58 0700'
+        for tag in self.tags.values():
+            tag.sendline('char-write-cmd ' + enable_accelerometer) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + stream_accelerometer) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + enable_magnetomer) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + stream_magnetomer) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + enable_gyroscope) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + stream_gyroscope) 
+            tag.expect('\r\n')
+
+    def disable(self):
+        disable_accelerometer = '31 00'
+        stream_accelerometer = '2E 0000'
+        disable_magnetomer = '44 00'
+        stream_magnetomer = '41 0000'
+        disable_gyroscope = '5B 00'
+        stream_gyroscope = '58 0000'
+        for tag in self.tags.values():
+            tag.sendline('char-write-cmd ' + disable_accelerometer) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + stream_accelerometer) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + disable_magnetomer) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + stream_magnetomer) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + disable_gyroscope) 
+            tag.expect('\r\n')
+            tag.sendline('char-write-cmd ' + stream_gyroscope) 
+            tag.expect('\r\n')
+            tag.__del__()

File sensors/__init__.py

  • Ignore whitespace
Empty file added.