Skip to content

Commit

Permalink
ISSUE #1,#2,#7: Added v2 protocol flag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ajfclark committed Mar 24, 2021
1 parent 375e204 commit 1b4f8e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
5 changes: 3 additions & 2 deletions ble/moonboard_BLE_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ def monitor_btmon(self):

def process_rx(self,ba):
new_problem_string= self.unstuffer.process_bytes(ba)
mini = self.unstuffer.mini
flags = self.unstuffer.flags

if new_problem_string is not None:
problem= decode_problem_string(new_problem_string, mini)
problem= decode_problem_string(new_problem_string, flags)
self.new_problem(json.dumps(problem))
self.unstuffer.flags = ''
start_adv(self.logger)

@dbus.service.signal(dbus_interface="com.moonboard",
Expand Down
33 changes: 19 additions & 14 deletions ble/moonboard_app_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@

X_GRID_NAMES = string.ascii_uppercase[0:11]

def position_trans(p,mini):
def position_trans(p,num_rows):
"""convert led number (strip number) to moonboard grid """
if mini==True:
num_rows = 12
else:
num_rows = 18
col= p//num_rows
row= (p%num_rows) +1
if col%2==1:
row=(num_rows+1)-row
return X_GRID_NAMES[col]+str(row)

def decode_problem_string(s, mini):
holds = {'START':[],'MOVES':[],'TOP':[]}
def decode_problem_string(s, flags):
holds = {'START':[],'MOVES':[],'TOP':[], 'FLAGS':[flags]}

if flags.find("M") != -1:
num_rows = 12
else:
num_rows = 18

for h in s.split(','):
t,p = h[0],position_trans(int(h[1:]), mini)
t,p = h[0],position_trans(int(h[1:]), num_rows)
if t=='S':
holds['START'].append(p)
if t=='P':
Expand All @@ -34,15 +36,14 @@ class UnstuffSequence():
"""
START = 'l#'
STOP= '#'
MINI= '~M*'

def __init__(self,logger=None):
if logger is None:
self.logger= logging
else:
self.logger=logger
self.s=''
self.mini=False
self.flags=''

def process_bytes(self, ba):
"""
Expand All @@ -53,7 +54,14 @@ def process_bytes(self, ba):
s = bytearray.fromhex(ba).decode()
self.logger.debug("incoming bytes:"+str(s))

if s[:2]==self.START:
if s[0] == '~' and s[-1] == '*':
# Flag processing
self.flags=s[1:-1]
if s.find("M") != -1:
self.logger.debug('MINI')
if s.find("D") != -1:
self.logger.debug('BothLights')
elif s[:2]==self.START:
self.logger.debug('START')
if self.s =='':
if s[-1]==self.STOP:
Expand All @@ -72,8 +80,5 @@ def process_bytes(self, ba):
else:
self.logger.debug('error: not started')
self.s= ''
elif s[:3]==self.MINI:
self.logger.debug('MINI')
self.mini = True
else:
self.s+=s

0 comments on commit 1b4f8e9

Please sign in to comment.