Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added New Options #102

Merged
merged 4 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 52 additions & 24 deletions Software/Chameleon/Device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

import serial
import serial.tools.list_ports
Expand All @@ -13,15 +13,20 @@ class Device:
COMMAND_DOWNLOAD = "DOWNLOAD"
COMMAND_SETTING = "SETTING"
COMMAND_UID = "UID"
COMMAND_GETUID = "GETUID"
COMMAND_IDENTIFY = "IDENTIFY"
COMMAND_DUMPMFU = "DUMP_MFU"
COMMAND_CONFIG = "CONFIG"
COMMAND_LOG_DOWNLOAD = "LOGDOWNLOAD"
COMMAND_LOG_CLEAR = "LOGCLEAR"
COMMAND_LOGMODE = "LOGMODE"
COMMAND_LBUTTON = "LBUTTON"
COMMAND_LBUTTONLONG = "LBUTTON_LONG"
COMMAND_RBUTTON = "RBUTTON"
COMMAND_RBUTTONLONG = "RBUTTON_LONG"
COMMAND_GREEN_LED = "LEDGREEN"
COMMAND_RED_LED = "LEDRED"

STATUS_CODE_OK = 100
STATUS_CODE_OK_WITH_TEXT = 101
STATUS_CODE_WAITING_FOR_XMODEM = 110
Expand Down Expand Up @@ -49,33 +54,32 @@ class Device:
SUGGEST_CHAR = "?"
SET_CHAR = "="
GET_CHAR = "?"

def __init__(self, verboseFunc = None):
self.verboseFunc = verboseFunc
self.serial = serial.Serial(None, 9600, timeout=5.0)
self.versionString = ""
self.supportedConfs = []

def verboseLog(self, text):
if (self.verboseFunc):
self.verboseFunc(text)

def listDevices():
devices = []

for port in serial.tools.list_ports.grep("({0:04x}:{1:04x})|({0:04X}:{1:04X})".format(Chameleon.USB_VID, Chameleon.USB_PID)):
devices.append(port[0])

return devices

def connect(self, comport):
return devices

def connect(self, comport):
self.serial.port = comport
try:
self.serial.open()
except:
pass

if (self.serial.isOpen()):
# Send escape key to force clearing the Chameleon's input buffer
self.serial.write(b"\x1B")
Expand All @@ -94,20 +98,20 @@ def connect(self, comport):
return False

result = self.getCmdSuggestions(self.COMMAND_CONFIG)

if (result['statusCode'] == self.STATUS_CODE_OK_WITH_TEXT):
self.supportedConfs = result['response'].split(",")
else:
return False
else:
return False

return True

def disconnect(self):
self.verboseLog("Closing serial port")
self.serial.close()

def isConnected(self):
return self.serial.isOpen()

Expand All @@ -116,7 +120,7 @@ def read(self, size=1024, timeout=0.01):
data = self.serial.read(size)
self.serial.timeout = 5.0
return data

def writeCmd(self, cmd):
# Execute command
cmdLine = cmd + self.LINE_ENDING
Expand All @@ -130,47 +134,50 @@ def writeCmd(self, cmd):
return None
else:
self.verboseLog("Executing <{}>: {}".format(cmd, status))

statusCode, statusText = status.split(":")
statusCode = int(statusCode)

result = {'statusCode': statusCode, 'statusText': statusText, 'response': None}

if (statusCode == self.STATUS_CODE_OK_WITH_TEXT):
result['response'] = self.readResponse()
elif (statusCode == self.STATUS_CODE_TRUE):
result['response'] = True
elif (statusCode == self.STATUS_CODE_FALSE):
result['response'] = False

return result

def readResponse(self):
# Read response to command, if any
response = self.serial.readline().decode('ascii').rstrip()
self.verboseLog("Response: {}".format(response))
return response

def execCmd(self, cmd, args=None):
if (args is None):
return self.writeCmd("{}".format(cmd))
else:
return self.writeCmd("{} {}".format(cmd, args))

def getSetCmd(self, cmd, arg=None):
# Determine if set or get mode
if (arg is None):
return self.writeCmd("{}{}".format(cmd, self.GET_CHAR))
else:
return self.writeCmd("{}{}{}".format(cmd, self.SET_CHAR, arg))


def returnCmd(self, cmd, arg=None):
return self.writeCmd("{}".format(cmd))

def getCmdSuggestions(self, cmd):
result = self.getSetCmd(cmd, self.SUGGEST_CHAR)
if (result['response'] is not None):
result['suggestions'] = result['response'].split(",")

return result

def cmdUploadDump(self, dataStream):
if (self.execCmd(self.COMMAND_UPLOAD)['statusCode'] == self.STATUS_CODE_WAITING_FOR_XMODEM):
# XMODEM started
Expand Down Expand Up @@ -201,7 +208,7 @@ def cmdClearLog(self):

def cmdLogMode(self, newLogMode):
return self.getSetCmd(self.COMMAND_LOGMODE, newLogMode)

def cmdVersion(self):
return self.getSetCmd(self.COMMAND_VERSION)

Expand All @@ -211,6 +218,15 @@ def cmdSetting(self, newSetting = None):
def cmdUID(self, newUID = None):
return self.getSetCmd(self.COMMAND_UID, newUID)

def cmdGetUID(self):
return self.returnCmd(self.COMMAND_GETUID)

def cmdIdentify(self):
return self.returnCmd(self.COMMAND_IDENTIFY)

def cmdDumpMFU(self):
return self.returnCmd(self.COMMAND_DUMPMFU)

def cmdConfig(self, newConfig = None):
if (newConfig == self.SUGGEST_CHAR):
return self.getCmdSuggestions(self.COMMAND_CONFIG)
Expand All @@ -223,18 +239,30 @@ def cmdLButton(self, newAction = None):
else:
return self.getSetCmd(self.COMMAND_LBUTTON, newAction)

def cmdLButtonLong(self, newAction = None):
if (newAction == self.SUGGEST_CHAR):
return self.getCmdSuggestions(self.COMMAND_LBUTTONLONG)
else:
return self.getSetCmd(self.COMMAND_LBUTTONLONG, newAction)

def cmdRButton(self, newAction = None):
if (newAction == self.SUGGEST_CHAR):
return self.getCmdSuggestions(self.COMMAND_RBUTTON)
else:
return self.getSetCmd(self.COMMAND_RBUTTON, newAction)

def cmdRButtonLong(self, newAction = None):
if (newAction == self.SUGGEST_CHAR):
return self.getCmdSuggestions(self.COMMAND_RBUTTONLONG)
else:
return self.getSetCmd(self.COMMAND_RBUTTONLONG, newAction)

def cmdGreenLED(self, newFunction = None):
if (newFunction == self.SUGGEST_CHAR):
return self.getCmdSuggestions(self.COMMAND_GREEN_LED)
else:
return self.getSetCmd(self.COMMAND_GREEN_LED, newFunction)

def cmdRedLED(self, newFunction = None):
if (newFunction == self.SUGGEST_CHAR):
return self.getCmdSuggestions(self.COMMAND_RED_LED)
Expand Down
Loading