def handle_host_command(self, ss, es, rxtx, s):
if s.startswith('AT+JSEC'):
- pin = s[s.find('\r\n') - 4:len(s) - 2]
+ pin = s[-4:]
self.put(ss, es, self.out_ann,
[ANN_ASCII, ['Host set the Bluetooth PIN to ' + pin]])
elif s.startswith('AT+JSLN'):
- name = s[s.find(',') + 1:-2]
+ name = s[s.find(',') + 1:]
self.put(ss, es, self.out_ann,
[ANN_ASCII, ['Host set the Bluetooth name to ' + name]])
else:
self.cmd[rxtx] = ''
def handle_device_reply(self, ss, es, rxtx, s):
- if s == 'ROK\r\n':
+ if s == 'ROK':
self.put(ss, es, self.out_ann,
[ANN_ASCII, ['Device initialized correctly']])
- elif s == 'OK\r\n':
+ elif s == 'OK':
self.put(ss, es, self.out_ann,
[ANN_ASCII, ['Device acknowledged last command']])
elif s.startswith('ERR'):
return
# Handle host commands and device replies.
+ # We remove trailing \r\n from the strings before handling them.
if rxtx == RX:
- self.handle_device_reply(ss, es, rxtx, self.cmd[rxtx])
+ self.handle_device_reply(ss, es, rxtx, self.cmd[rxtx][:-2])
elif rxtx == TX:
- self.handle_host_command(ss, es, rxtx, self.cmd[rxtx])
+ self.handle_host_command(ss, es, rxtx, self.cmd[rxtx][:-2])
else:
raise Exception('Invalid rxtx value: %d' % rxtx)
if deasserted:
self.cs_was_deasserted_during_data_word = 1
+ ws = self.options['wordsize']
+
# Receive MOSI bit into our shift register.
if self.options['bitorder'] == MSB_FIRST:
- self.mosidata |= mosi << (self.options['wordsize'] - 1 - self.bitcount)
+ self.mosidata |= mosi << (ws - 1 - self.bitcount)
else:
self.mosidata |= mosi << self.bitcount
# Receive MISO bit into our shift register.
if self.options['bitorder'] == MSB_FIRST:
- self.misodata |= miso << (self.options['wordsize'] - 1 - self.bitcount)
+ self.misodata |= miso << (ws - 1 - self.bitcount)
else:
self.misodata |= miso << self.bitcount
self.bitcount += 1
- # Continue to receive if not a byte yet.
- if self.bitcount != self.options['wordsize']:
+ # Continue to receive if not enough bits were received, yet.
+ if self.bitcount != ws:
continue
self.put(self.start_sample, self.samplenum, self.out_proto,
# Get the next data bit in LSB-first or MSB-first fashion.
if self.options['bit_order'] == LSB_FIRST:
self.databyte[rxtx] >>= 1
- self.databyte[rxtx] |= (signal << (self.options['num_data_bits'] - 1))
+ self.databyte[rxtx] |= \
+ (signal << (self.options['num_data_bits'] - 1))
elif self.options['bit_order'] == MSB_FIRST:
self.databyte[rxtx] <<= 1
self.databyte[rxtx] |= (signal << 0)