From da0910b506a61bcddccb5470238e131d7482cf39 Mon Sep 17 00:00:00 2001 From: Billiam Date: Wed, 1 Jul 2015 23:07:50 -0500 Subject: [PATCH] Adding status information, fix line endings --- .gitignore | 6 +- LICENSE.md | 48 ++++++------ build.sh | 22 +++--- config.yml | 6 +- gauge.py | 188 ++++++++++++++++++++++++----------------------- requirements.txt | 6 +- 6 files changed, 139 insertions(+), 137 deletions(-) diff --git a/.gitignore b/.gitignore index 20ae7f3..f60ce26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -build/* -dist/* -gauge.spec +build/* +dist/* +gauge.spec diff --git a/LICENSE.md b/LICENSE.md index 9727101..78781bc 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,25 +1,25 @@ -The MIT License (MIT) -===================== - -Copyright © `2015` `Colin Fein` - -Permission is hereby granted, free of charge, to any person -obtaining a copy of this software and associated documentation -files (the “Software”), to deal in the Software without -restriction, including without limitation the rights to use, -copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +The MIT License (MIT) +===================== + +Copyright © `2015` `Colin Fein` + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the “Software”), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/build.sh b/build.sh index 7a2aa8d..3e6a262 100644 --- a/build.sh +++ b/build.sh @@ -1,11 +1,11 @@ -#!/usr/bin/env bash -set -e - -cd `dirname $0` - -rm gauge.zip -rm -r dist/* -pyinstaller --onefile gauge.py -cp config.yml dist -cp LICENSE.md dist/LICENSE.txt -7z a -tzip dist/gauge.zip ./dist/* -mx0 +#!/usr/bin/env bash +set -e + +cd `dirname $0` + +rm gauge.zip +rm -r dist/* +pyinstaller --onefile gauge.py +cp config.yml dist +cp LICENSE.md dist/LICENSE.txt +7z a -tzip dist/gauge.zip ./dist/* -mx0 diff --git a/config.yml b/config.yml index 11216fc..b3f9e81 100644 --- a/config.yml +++ b/config.yml @@ -1,4 +1,4 @@ -arduino_port: COM3 -telemetry_server: - host: 127.0.0.1 +arduino_port: COM3 +telemetry_server: + host: 127.0.0.1 port: 20777 \ No newline at end of file diff --git a/gauge.py b/gauge.py index 3fddbf8..b70d17c 100644 --- a/gauge.py +++ b/gauge.py @@ -1,93 +1,95 @@ -import socket -import struct -import asyncore -import serial -import yaml -import os.path -import sys -class Sender: - def __init__(self, serial_name): - self.ser = serial.Serial( - serial_name, - 115200, - writeTimeout=0, - timeout=0, - parity=serial.PARITY_NONE, - stopbits=serial.STOPBITS_ONE - ) - - def send(self, data): - self.ser.write(struct.pack('>cHHchcB', 'R', data['rpm'], data['max_rpm'], 'S', data['speed'], 'G', data['gear'])) - -class Receiver(asyncore.dispatcher): - def __init__(self, address, sender): - asyncore.dispatcher.__init__(self) - self.sender = sender - - self.address = address - self.reconnect() - - def reconnect(self): - self.create_socket(socket.AF_INET, socket.SOCK_DGRAM) - self.bind(self.address) - - def writable(self): - return False - - def handle_accept(self): - print 'accepted' - - def handle_connect(self): - print 'connected!' - - def handle_expt(self): - print 'exception occurred!' - self.close() - - def readable(self): - return True - - def handle_close(self): - print 'closing connection' - self.close() - - def handle_read(self): - data = self.recv(512) - - if not data: - return - - self.parse(data) - - def parse(self, data): - # Unpack the data. - stats = struct.unpack('64f', data[0:256]) - - gear = stats[33] - rpm = stats[37] * 10 - max_rpm = stats[63] * 10 - - data = { - 'speed': int(stats[7] * 3.6 * 0.625), # mph (remove '* 0.625' for kph) - 'gear': int(stats[33]), - 'rpm': int(stats[37] * 10), - 'max_rpm': int(stats[63] * 10) - } - self.sender.send(data) - - -if __name__ == '__main__': - if getattr(sys, 'frozen', None): - approot = os.path.dirname(sys.executable) - else: - approot = os.path.dirname(os.path.realpath(__file_)) - - try: - config = yaml.load(file(approot + '/config.yml', 'r')) - except yaml.YAMLError, exc: - print "Error in configuration file:", exc - - arduino = Sender(config['arduino_port']) - server = (config['telemetry_server']['host'], config['telemetry_server']['port']) - game = Receiver(server, arduino) - asyncore.loop() +import socket +import struct +import asyncore +import serial +import yaml +import os.path +import sys +class Sender: + def __init__(self, serial_name): + try: + self.ser = serial.Serial( + serial_name, + 115200, + writeTimeout=0, + timeout=0, + parity=serial.PARITY_NONE, + stopbits=serial.STOPBITS_ONE + ) + except serial.serialutil.SerialException, exc: + sys.exit("Could not connect to serial port: %s" % serial_name) + + print "Connected to serial port: %s" % serial_name + + def send(self, data): + self.ser.write(struct.pack('>cHHchcB', 'R', data['rpm'], data['max_rpm'], 'S', data['speed'], 'G', data['gear'])) + +class Receiver(asyncore.dispatcher): + def __init__(self, address, sender): + asyncore.dispatcher.__init__(self) + self.sender = sender + + self.address = address + self.reconnect() + + def reconnect(self): + self.received_data = False + + self.create_socket(socket.AF_INET, socket.SOCK_DGRAM) + self.bind(self.address) + print "Waiting for data on %s:%s" % self.address + + def writable(self): + return False + + def handle_expt(self): + print 'exception occurred!' + self.close() + + def readable(self): + return True + + def handle_read(self): + data = self.recv(512) + + if not data: + return + + if not self.received_data: + self.received_data = True + print "Receiving data on %s:%s" % self.address + + self.parse(data) + + def parse(self, data): + # Unpack the data. + stats = struct.unpack('64f', data[0:256]) + + gear = stats[33] + rpm = stats[37] * 10 + max_rpm = stats[63] * 10 + + data = { + 'speed': int(stats[7] * 3.6 * 0.625), # mph (remove '* 0.625' for kph) + 'gear': int(stats[33]), + 'rpm': int(stats[37] * 10), + 'max_rpm': int(stats[63] * 10) + } + self.sender.send(data) + + +if __name__ == '__main__': + if getattr(sys, 'frozen', None): + approot = os.path.dirname(sys.executable) + else: + approot = os.path.dirname(os.path.realpath(__file__)) + + try: + config = yaml.load(file(approot + '/config.yml', 'r')) + except yaml.YAMLError, exc: + print "Error in configuration file:", exc + + arduino = Sender(config['arduino_port']) + server = (config['telemetry_server']['host'], config['telemetry_server']['port']) + game = Receiver(server, arduino) + asyncore.loop() diff --git a/requirements.txt b/requirements.txt index ba36715..adf1629 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -pyserial==2.7 -PyYAML==3.11 -pyinstaller==2.1 +pyserial==2.7 +PyYAML==3.11 +pyinstaller==2.1