Esempio n. 1
0
class Terminal:
    """ """
    def __init__(self, settings):
        self.settings = settings
        self.uart = None

    def start(self):
        """Start Terminal on UART0 interface."""
        # Conditionally enable terminal on UART0. Default: False.
        # https://forum.pycom.io/topic/1224/disable-console-to-uart0-to-use-uart0-for-other-purposes
        uart0_enabled = self.settings.get('interfaces.uart0.terminal', False)
        if uart0_enabled:
            from machine import UART
            self.uart = UART(0, 115200)
            #self.uart = UART(0)
            os.dupterm(self.uart)
        else:
            self.shutdown()

    def stop(self):
        """Shut down."""
        log.info('Shutting down Terminal')
        self.shutdown()

    def shutdown(self):
        """Shut down Terminal and UART0 interface."""
        os.dupterm(None)
        self.deinit()

    def deinit(self):
        """Shut down UART0 interface."""
        if self.uart:
            log.info('Shutting down UART0')
            self.uart.deinit()
Esempio n. 2
0
class mhz19:
    def __init__(self, uart_no):
        self.uart_no = uart_no
        self.start()
        self.ppm = 0
        self.temp = 0
        self.co2status = 0

    def start(self):
        self.uart = UART(self.uart_no, 9600)
        self.uart.init(9600, bits=8, parity=None, stop=1, timeout=10)

    def stop(self):
        while self.uart.any():
            self.uart.read(1)
        self.uart.deinit()

    def get_data(self):
        self.uart.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79")
        time.sleep(0.1)
        s = self.uart.read(9)
        try:
            z = bytearray(s)
        except:
            return 0
        # Calculate crc
        crc = self.crc8(s)
        if crc != z[8]:
            # we should restart the uart comm here..
            self.stop()
            time.sleep(1)
            self.start()

            print(
                'CRC error calculated %d bytes= %d:%d:%d:%d:%d:%d:%d:%d crc= %dn'
                % (crc, z[0], z[1], z[2], z[3], z[4], z[5], z[6], z[7], z[8]))
            return 0
        else:
            self.ppm = ord(chr(s[2])) * 256 + ord(chr(s[3]))
            self.temp = ord(chr(s[4])) - 40
            self.co2status = ord(chr(s[5]))
            return 1

    def crc8(self, a):
        crc = 0x00
        count = 1
        b = bytearray(a)
        while count < 8:
            crc += b[count]
            count = count + 1
        # Truncate to 8 bit
        crc %= 256
        # Invert number with xor
        crc = ~crc & 0xFF
        crc += 1
        return crc
Esempio n. 3
0
class CORGI85():
    def __init__(self):
        try:
            fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX)
            fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX)
            self.uart = UART(UART.UART2,
                             115200,
                             8,
                             None,
                             1,
                             timeout=1000,
                             read_buf_len=4096)
            print("Init CORGI85")
        except:
            print("Unable to init UART")

    def deinit(self):
        self.uart.deinit()
        del self.uart

    def wifi_check(self):
        data = self.uart.read()
        self.uart.write("\rWIFI_CHECK,\r")
        time.sleep_ms(100)
        if self.uart.any() > 0:
            data = self.uart.read()
            return int(data[0])
        else:
            return 0

    def thingspeak_init(self):
        print(">>> thingspeak_init")
        self.uart.write("\rThingspeak,init\r")

    def thingspeak_account_setup(self, api_key, channel_id):
        print(">>> thingspeak_account_setup")
        self.uart.write("\rThingspeak,account_setup,")
        self.uart.write(str(api_key))
        self.uart.write(",")
        self.uart.write(str(channel_id))
        self.uart.write("\r")

    def thingspeak_write_field(self, field, value):
        print(">>> thingspeak_write_field, field : ", field, ", value : ",
              value)
        self.uart.write("\rThingspeak,write_field,")
        self.uart.write(str(field))
        self.uart.write(",")
        self.uart.write(str(value))
        self.uart.write("\r")
Esempio n. 4
0
 def writeCommand(self, command):
     output = command + '\r\n'
     uart = UART(1,
                 baudrate=9600,
                 pins=(Pin.exp_board.G9, Pin.exp_board.G8))
     uart.write(output)
     #wait max 3(s) for output from the sensor
     waitcounter = 0
     while (waitcounter < 5 and not uart.any()):
         time.sleep(0.5)
         waitcounter += 1
     response = uart.readall()
     uart.deinit()
     print(response)
     return (response)
Esempio n. 5
0
class DOUBLE_GPS(object):
    GGA_MESSAGE = b"$GPGGA,141623.523,2143.963,S,04111.493,W,1,12,1.0,0.0,M,0.0,M,,*65\n"
    RMC_MESSAGE = b"$GPRMC,141623.523,A,2143.963,S,04111.493,W,,,301019,000.0,W*7B\n"
    UPDATE_RATE_1S = 'PMTK220,1000'

    def __init__(self, TX, RX, uart):
        self.uart = UART(uart, baudrate=9600)
        self.uart.init(9600, bits=8, tx=TX, rx=RX)
        self.flag = False

    def make_data_available(self, NMEA_sentence):
        self.uart.write(NMEA_sentence)

    def received_command(self):
        command = self.uart.readline()
        if (command != None):
            command, received_check_sum = command.split(b'*')
            command = command.strip(b'$')
            received_check_sum = received_check_sum[0:2]
            generated_check_sum = self.generate_checksum(command)
            command = command.decode()
            if command == self.UPDATE_RATE_1S:
                self.continuous_mode()
                self.flag = True
            return command
        else:
            return None

    def generate_checksum(self, command):
        checksum = 0
        for char in command:
            checksum ^= char
        return checksum

    def continuous_mode(self):
        self.my_timer = Timer(1)
        self.my_timer.init(period=1000,
                           mode=self.my_timer.PERIODIC,
                           callback=self.my_callback)

    def my_callback(self, timer):
        self.make_data_available(self.RMC_MESSAGE)

    def deinit(self):
        self.uart.deinit()
        if hasattr(self, 'my_timer'):
            self.my_timer.deinit()
Esempio n. 6
0
class Serial:
    """ 使用ESP32的串口连接外部设备
    """
    def __init__(self,
                 id=2,
                 baudrate=2000000,
                 rx_pin=14,
                 tx_pin=12,
                 timeout=2000,
                 timeout_char=10):
        import select
        self.uart = UART(id,
                         baudrate,
                         rx=rx_pin,
                         tx=tx_pin,
                         timeout=timeout,
                         rxbuf=4096)
        self.poll = select.poll()
        self.poll.register(self.uart, select.POLLIN)

    def close(self):
        self.uart.deinit()

    def read(self, size=1):
        data = self.uart.read(size)
        if data == None:
            data = b""
        return data
        # data = b""
        # while len(data) < size:
        #     r = self.uart.read(size - len(data))
        #     if r:
        #         data += r
        # return data

    def write(self, data):
        return self.uart.write(data)

    def inWaiting(self):
        res = self.poll.poll(0)
        if res:
            return 1
        return 0
class SEN0219_SERIAL:

    # byte mhzCmdReadPPM[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
    # byte mhzResp[9];    // 9 bytes bytes response
    # byte mhzCmdCalibrateZero[9] = {0xFF,0x01,0x87,0x00,0x00,0x00,0x00,0x00,0x78};
    # byte mhzCmdABCEnable[9] = {0xFF,0x01,0x79,0xA0,0x00,0x00,0x00,0x00,0xE6};
    # byte mhzCmdABCDisable[9] = {0xFF,0x01,0x79,0x00,0x00,0x00,0x00,0x00,0x86};
    # byte mhzCmdReset[9] = {0xFF,0x01,0x8d,0x00,0x00,0x00,0x00,0x00,0x72};

    def __init__(self, TX, RX):
        self.uart = UART(1, 9600, bits=8, parity=None, stop=1, pins=(TX,RX))
        #self.uart.write(b'\xFF\x01\x8D\x00\x00\x00\x00\x00\x72') #Reset

    def deinit(self):
        self.uart.deinit()

    def SEN_Serial_ABCOn(self):
        self.uart.write(b'\xFF\x01\x79\xA0\x00\x00\x00\x00\xE6') #ABC On
        self.uart.wait_tx_done(1000)

    def SEN_Serial_ABCOff(self):
        self.uart.write(b'\xFF\x01\x79\x00\x00\x00\x00\x00\x86') #ABC Off
        self.uart.wait_tx_done(1000)

    def SEN_Serial_read(self):
        self.uart.write(b'\xFF\x01\x86\x00\x00\x00\x00\x00\x79') #get gas command
        self.uart.wait_tx_done(1000)
        Attempts=5
        while(Attempts>0):
            data=self._SerialRead()
            if(data!=False):
                return data
            Attempts-=1
            utime.sleep(1)
        return False

    def _SerialRead(self):
        print(str(self.uart.any()))
        if(self.uart.any()>=9):
            data=self.uart.read(9)
            return data
        else:
            return False
Esempio n. 8
0
def chk_GNSS():
    while True:
        u = UART(1, 9600)
        u.init(9600, bits=8, parity=None, stop=1)
        # UBX-NAV-STATUS hex poll command to check if position fix
        buf = b'\xB5\x62\x01\x03\x00\x00\x04\x0D'
        REGISTER_FORMAT = '>b'  # one byte for nav status
        u.write(buf)
        #sleep(1)
        while not u.any():
            if u.any():
                break
# read 24 bytes
        poll = u.read(24)
        # offset for nav fix byte = 6 + 5
        nav_fix = ustruct.unpack_from(REGISTER_FORMAT, poll, 11)[0]
        print('nav fix= ', (nav_fix & 0x01))
        # offset for nav status code byte = 6 + 4
        nav_stat = ustruct.unpack_from(REGISTER_FORMAT, poll, 10)[0]
        print('nav status code= ', nav_stat)
        # UBX-NAV-POSLLH hex poll command
        buf = b'\xB5\x62\x01\x02\x00\x00\x03\x0A'
        # send poll command
        u.write(buf)
        while not u.any():
            if u.any():
                break
# read UBX-NAV-POSLLH poll result
        poll = u.read(36)
        u.deinit()  # this closes the UART
        #print ('read nav data ',poll)
        REGISTER_FORMAT = '<i'  # little endian 4 bytes
        # offset for longitude status byte = 6 + 4
        lon = ustruct.unpack_from(REGISTER_FORMAT, poll, 10)[0]
        lat = ustruct.unpack_from(REGISTER_FORMAT, poll, 14)[0]
        if (nav_fix & 0x01) != 1:
            print('no GNSS signal', (nav_fix / 2))
        else:
            print('longitude= ', (lon / 1E7))
            print('latitude= ', (lat / 1E7))
        sleep(2)
Esempio n. 9
0
class MHZ19:
    def __init__(self, rx_pin: int, tx_pin: int):
        self.uart = UART(2, baudrate=9600, rx=rx_pin, tx=tx_pin, timeout=100)

    def close(self):
        try:
            self.uart.deinit()
        except:
            pass

    def _cmd(self, cmd: int, cmd_data: bytes, resp_len: int):
        req_bytes = bytearray([
            0xff, # Fixed start byte.
            1,    # Sensor num.
            cmd,
        ])
        req_bytes += cmd_data
        req_bytes += bytes([0] * (8 - len(req_bytes)))
        req_bytes += bytes([MHZ19._checksum(req_bytes[1:])])
        assert len(req_bytes) == 9

        bytes_written = self.uart.write(req_bytes)
        assert bytes_written is not None
        if resp_len == 0:
            return

        resp_bytes = self.uart.read(resp_len)
        if len(resp_bytes) != resp_len:
            raise Exception('mhz19: not enough bytes received: %d' % len(resp_bytes))
        if resp_bytes[-1] != MHZ19._checksum(resp_bytes[1:-1]):
            raise MHZ19ChecksumError()
        return resp_bytes[2:]

    def gas_concentration(self) -> int:
        resp = self._cmd(0x86, bytes(), 9)
        return resp[0]<<8 | resp[1]

    @staticmethod
    def _checksum(data: bytes) -> int:
        return ((0xff - sum(data) & 0xff) + 1) & 0xff
Esempio n. 10
0
class Positioning(object):
	def __init__(self, TX, RX, uart):
		self.uart = UART(uart, baudrate=9600)
		self.uart.init(9600,bits=8,tx=TX,rx=RX)
		self.gps = GPS(self.uart)

	def send_command(self, command):
		self.gps.send_command(command)

	def received_command(self):
		command = self.uart.readline()
		if(command != None):
			return command
		else:
			return None

	def get_latitude(self):
		self.gps.update()
		return self.gps.latitude

	def deinit(self):
		self.uart.deinit()
Esempio n. 11
0
def checkNetwork():
    fm.register(6, fm.fpioa.UART1_TX)
    fm.register(7, fm.fpioa.UART1_RX)
    serial = UART(UART.UART1,
                  115200,
                  8,
                  None,
                  1,
                  timeout=1000,
                  read_buf_len=4096)
    start = utime.ticks_ms()
    serial.write('at\r\n')
    while True:
        data = serial.read()
        if 'OK' in data:
            print(data)
            showInfo("esp32 respone is OK", True)
            break
        if utime.ticks_ms() > 3000:
            showInfo("esp32 no respone", False)
            break
    serial.deinit()
    del serial
Esempio n. 12
0
def takePhotoAndSaveToFile(filename=None, retry=True):
    global uart

    SDCard.mountSDCard()
    uart = UART(2, baudrate=BAUD, pins=(TX,RX), timeout_chars=5)
    uart.readall()
    setsize(VC0706_160x120)
    reset()
    uart.readall()
    if(not getversion()):
        print("Camera not found")
        return(0)

    if takephoto():
        if(filename == None):
            filename = ''.join(map(str,RTC().now()))+'.jpg';

        f = open('/sd/'+filename, 'wb');

        try:
            gc.collect()
            readbuffer(getbufferlength(), f)
            return filename
        except Exception as e:
            print(e)
            if(retry):
                try:
                    time.sleep(2)
                    f.close()
                    gc.collect()
                    return takePhotoAndSaveToFile(filename, False)
                except Exception as ee:
                    print(ee)
        finally:
            f.close()
            uart.deinit()
            gc.collect()
Esempio n. 13
0
def config_M8():
    u = UART(1, 9600)
    u.init(9600, bits=8, parity=None, stop=1)
    # UBX-CFG-PRT command to set UART1 output=0 (UBX output only) and 9600 baud
    buf1 = b'\xB5\x62\x06\x00\x14\x00\x01\x00\x00\x00\xC0\x08\x00\x00\x80\x25\x00\x00\x07\x00\x01\x00\x00\x00\x00\x00\x90\xA9'
    u.write(buf1)
    # catch ack
    while not u.any():
        if u.any():
            break
    poll = u.read()
    # write UBX-CFG-PRT poll command
    buf1 = b'\xB5\x62\x06\x00\x01\x00\x01\x08\x22'
    u.write(buf1)
    # catch poll response
    while not u.any():
        if u.any():
            break
    poll = u.read()
    u.deinit()  # this closes the UART
    REGISTER_FORMAT = '<i'  # little endian 4 bytes
    # offset for baud rate = 6 + 8
    baud = ustruct.unpack_from(REGISTER_FORMAT, poll, 14)[0]
    print('baud rate= ', baud)
Esempio n. 14
0
 def identify(self):
   found = []
   for one in self.UARTs:
       if (len(one) != 2) or (not type(one[0]) is str) or (not type(one[1]) is str):
           continue
       ser = UART(len(self.uart), baudrate=9600, pins=one, timeout_chars=20)
       if self.debug: print("Try UART pins Tx %s, Rx %s" % one)
       for i in range(0,3): # try 3 times to read known pattern
           line = []
           sleep(2)
           try: line = ser.readall()
           except:
               print("Read error")
               continue
           if (line == None) or (not len(line)):   # try to wake up
               if not 'dust' in found:
                 if self.debug: print("Try to wake up device")
                 if not i: ser.write(b'BM\xe1\x00\x01\x01q') # try activate PMS
                 elif i < 2: ser.write(b'\xAA\xB4\x06\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\x06\xAB') # second try activate SDS
               continue
           # if self.debug: print("Read: %s" % line)
           if line.count(b'BM') > 0: # start char 0x42,0x4D
               self.dust = 'PMSx003'; self.D_Tx = one[0]; self.D_Rx = one[1]
               found.append('dust')
           elif line.count(b'\xAA') and line.count(b'\xC0'): # start char 0xAA,0xC0 tail 0xAB
               self.dust = 'SDS011'; self.D_Tx = one[0]; self.D_Rx = one[1]
               found.append('dust')
           elif line.count(b'\r\n') or (line.count(b',') > 1):
               self.useGPS = 'UART'; self.G_Tx = one[0]; self.G_Rx = one[1]
               found.append('gps')
           else: continue
           if self.debug: print("UART: %s on Tx %s, Rx %s" % (found[-1], one[0],one[1]))
           break
       if i > 2: print("Unknown device found on Tx %s, Rx %s" % one)
       ser.readall(); ser.deinit(); del ser
   return found
Esempio n. 15
0
    sys.exit()

uart = UART(2, 115200)
print(uart)
uart.init(57600, 8, None, 1, pins=('P11', 'P12'))
uart.init(baudrate=9600, stop=2, parity=UART.EVEN, pins=('P11', 'P12'))
uart.init(baudrate=115200, parity=UART.ODD, stop=1, pins=('P11', 'P12'))
uart.read()
print (uart.read())
print (uart.readline())
buff = bytearray(1)
print (uart.readinto(buff, 1))
print (uart.read())
print (uart.any())
print (uart.write('a'))
uart.deinit()

uart = UART(2, 1000000, pins=('P12', 'P11'))
print(uart)
uart.read()
print(uart.write(b'123456') == 6)
print(uart.read() == b'123456')
uart.deinit()
uart = UART(2, 1000000, pins=('P11', 'P12'))
print(uart)
uart.read()
print(uart.write(b'123456') == 6)
print(uart.read() == b'123456')
uart.deinit()

uart = UART(2, 1000000, pins=('P11', 'P12'))
Esempio n. 16
0
 def getConf(self, atype, pins, pwr=None):
   #self.conf = { # PCB TTL defaults example
   #    'usb':  {'name':'usb',    'pins':('P1','P0','P20'),  'use':False, 'baud':None},
   #    'dust': {'name':'PMSx003','pins':('P11','P10','P9'), 'use':None,  'baud':9600},
   #    'gps':  {'name':'NEO-6',  'pins':('P4'.'P3','P19'),  'use':None,  'baud':9600},
   #    }
   pins = tuple(pins)
   try:
     if self.conf[atype]['pins'] == pins:
       if self.conf[atype]['name']: return self.conf[atype]
   except: pass
   data = [
     (b'\x42\x4D\xE1\x00\x01\x01\x71',9600),     # PMS
     (b'\x7E\x00\xD0\x01\x01\x2D\x7E',115200),     # SPS info
     (b'\x7E\x00\x00\x02\x01\x03\xF9\x7E',115200), # SPS start
     (b'\xAA\xB4\x06\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\x06\xAB',9600), # SDS
     ]
   for baudrate in [9600, 115200]:
     if self.debug: print("Try uart (baud=%d) pins: " % baudrate, pins)
     if not (0 <= self.index < 3): raise ValueError("UART index %d fail" % nr)
     prev = self.Power(pins, on=True)
     if not prev: sleep_ms(500)
     ser = UART(self.index, baudrate=baudrate, pins=pins[:2], timeout_chars=20)
     fnd = None
     if self.debug: print("getIdent type %s" % atype)
     for i in range(0,2*len(data)):
       if data[i%len(data)][1] != baudrate: continue
       if not ser.any():
         sleep_ms(5*500 if atype == 'dust' else 500)
       try:
         line = ser.read()
         if self.debug: print("Read: ", line)
       except Exception as e:
         if self.debug: print("TTL dev search %s" % e)
         continue
       if (atype == 'dust') and ((line == None) or (not len(line))):   # try to wake up
         activate = data[i % len(data)][0]
         if self.debug: print("%d: Try a wakeup, send: " % i, activate)
         ser.write(activate)
         sleep_ms(500)
         continue
       else:
         if not line: continue
         if line.count(b'u-blox'): fnd = 'NEO-6'
         elif line.count(b'$GPTXT') or line.count(b'$GNG') or line.count(b'$GPG'): fnd = 'GPS'
         elif line.count(b'\x42\x4D') or line.count(b'BM\x00\x1C'): fnd = 'PMSx003'
         elif line.count(b'\xAA') and line.count(b'\xC0'): fnd = 'SDS011'
         elif line.count(b'~\x00\xD0\x00'): fnd = 'SPS30'
         elif line.count(b'~\x00\x00') or line.count(b'\x00\xFF~'): fnd = 'SPS30'
         if fnd: break
     ser.read(); ser.deinit(); del ser; self.Power(pins,on=prev)
     use = True; Dexplicit = None; calibrate = None
     if atype == 'dust':
       try: from Config import useDust as use
       except: pass
       Dexplicit = False
       try: from Config import Dexplicit
       except: pass
       if not 'calibrate' in self.conf.keys():
         calibrate = None
         try: from Config import calibrate
         except: pass
         self.config['calibrate'] = calibrate
     elif atype.lower() == 'gps':
       try: from Config import useGPS as use
       except: pass
     if fnd:
       thisConf = { 'name': fnd, 'baud':  baudrate, 'pins': pins, 'use': use }
       if Dexplicit != None: thisConf['explicit'] = Dexplicit
       if calibrate != None: thisConf['calibrate'] = calibrate
       self.conf[atype] = thisConf; self.conf['updated'] = True
       self.allocated.append(pins)
       return self.conf[atype]
   if (not pins[2]) and pwr:
     for dlf in 'P19','P20': # try dflts: P1? in V2.1
       fnd = self.getConf(atype,(pins[0],pins[1],dlf),baudrate=baudrate)
       if fnd: return fnd
   return None
Esempio n. 17
0
class LPF2(object):
    def __init__(self,
                 uartChannel,
                 txPin,
                 rxPin,
                 modes=defaultModes,
                 sensorType=WeDo_Ultrasonic,
                 timer=3,
                 freq=5):
        self.txPin = txPin
        self.rxPin = rxPin
        print('Start!')
        debug(9, 'debug1')
        self.uart = UART(uartChannel, 2400)
        debug(9, 'debug2')
        self.uart.init(baudrate=2400, tx=txPin, rx=rxPin)
        debug(9, 'debug3')
        self.txTimer = timer
        self.modes = modes
        self.current_mode = 0
        self.sensorType = sensorType
        self.connected = False
        self.payload = [bytearray([])] * len(self.modes)
        self.freq = freq
        self.oldbuffer = bytes([])
        self.textBuffer = bytearray(b'                ')
        self.lastHeartbeat = None

# -------- Payload definition

    def load_payload(self,
                     dataType,
                     array,
                     mode=None):  # note it must be a power of 2 length
        if (mode == None):
            mode = self.current_mode
        if isinstance(array, list):
            bit = math.floor(math.log2(length[dataType] * len(array)))
            bit = 4 if bit > 4 else bit  # max 16 bytes total (4 floats)
            array = array[:math.floor(
                (2**bit) / length[dataType])]  # max array size is 16 bytes
            value = b''
            for element in array:
                value += struct.pack(dataFormat[dataType], element)
        elif isinstance(array, bytes):
            bit = int(math.log2(len(array)))
            value = array
        else:
            bit = int(math.log2(length[dataType]))
            value = struct.pack(dataFormat[dataType], array)
        payload = bytearray([CMD_Data | (bit << CMD_LLL_SHIFT) | mode]) + value
        payload = self.addChksm(payload)

        self.payload[mode] = payload

#----- comm stuff

    def hubCallback(self, timerInfo):
        heartbeatReceived = False
        if self.connected:
            char = readchar(self.uart)  # read in any heartbeat bytes
            while char >= 0:
                if char == 0:  # port has nto been setup yet
                    pass
                elif char == BYTE_NACK:  # regular heartbeat pulse
                    heartbeatReceived = True
                    self.lastHeartbeat = utime.ticks_ms()
                elif char == CMD_Select:  # reset the mode
                    mode = readchar(self.uart)
                    cksm = readchar(self.uart)
                    if cksm == 0xff ^ CMD_Select ^ mode:
                        self.current_mode = mode
                        # modeChangeAnnouncement = bytearray(b'\x46') + self.current_mode.to_bytes(1, 'lsb')
                        # modeChangeAnnouncement = self.addChksm(modeChangeAnnouncement)
                        # self.writeIt(modeChangeAnnouncement)
                        print("Mode change:", mode)
                elif char == 0x46:  # sending over a string
                    zero = readchar(self.uart)
                    b9 = readchar(self.uart)
                    ck = 0xff ^ zero ^ b9
                    if ((zero == 0) &
                        (b9 == 0xb9)):  # intro bytes for the string
                        char = readchar(self.uart)  # size and mode
                        size = 2**((char & 0b111000) >> 3)
                        mode = char & 0b111
                        ck = ck ^ char
                        for i in range(len(self.textBuffer)):
                            self.textBuffer[i] = ord(b' ')
                        for i in range(size):
                            self.textBuffer[i] = readchar(self.uart)
                            ck = ck ^ self.textBuffer[i]
                        print(self.textBuffer)
                        cksm = readchar(self.uart)
                        if cksm == ck:
                            pass
                elif char == 0x4C:  # no idea what it is - but it sends a 0x20
                    thing = readchar(self.uart)
                    cksm = readchar(self.uart)
                    if cksm == 0xff ^ 0x4C ^ thing:
                        pass
                else:
                    print(char)
                char = readchar(self.uart)

        if (heartbeatReceived):
            size = self.writeIt(
                self.payload[self.current_mode])  # send out the latest payload
            if not size:
                self.conected = False
        # for m in range(len(self.modes)):
        #     size = self.writeIt(self.payload[m])
        #     utime.sleep_ms(20)
        #     if not size:
        #          self.connected = False
        if (utime.ticks_diff(utime.ticks_ms(),
                             self.lastHeartbeat) > HEARTBEAT_TIMEOUT
            ):  # no heartbeat received for a while; we're dead
            self.connected = False

    def writeIt(self, array):
        debug(2, 'SENT:' + str(binascii.hexlify(array)))
        return self.uart.write(array)

    def waitFor(self, char, timeout=2):
        starttime = utime.time()
        currenttime = starttime
        status = False
        while (currenttime - starttime) < timeout:
            utime.sleep_ms(5)
            currenttime = utime.time()
            if self.uart.any() > 0:
                data = readchar(self.uart)
                if data == ord(char):
                    status = True
                    break
        return status

    def addChksm(self, array):
        chksm = 0
        for b in array:
            chksm ^= b
        chksm ^= 0xFF
        array.append(chksm)
        return array

# -----  Init and close

    def init(self):
        debug(9, 'debug4')
        # self.uart.deinit()
        debug(9, 'debug5')
        # self.tx = machine.Pin(self.txPin, machine.Pin.OUT)
        # self.rx = machine.Pin(self.rxPin, machine.Pin.IN)
        # self.tx.value(0)
        # utime.sleep_ms(500)
        # self.tx.value(1)
        debug(9, 'debug6')
        self.uart.init(baudrate=2400, bits=8, parity=None, stop=1)
        debug(9, 'debug7')
        self.writeIt(b'\x00')

    def close(self):
        self.uart.deinit()
        # self.sendTimer.callback(None)
        self.sendTimer.deinit()
        self.connected = False

# ---- settup definitions

    def setType(self, sensorType):
        return self.addChksm(bytearray([CMD_Type, sensorType]))

    def defineBaud(self, baud):
        rate = baud.to_bytes(4, 'little')
        return self.addChksm(bytearray([CMD_Baud]) + rate)

    def defineVers(self, hardware, software):
        hard = hardware.to_bytes(4, 'big')
        soft = software.to_bytes(4, 'big')
        return self.addChksm(bytearray([CMD_Vers]) + hard + soft)

    def padString(self, string, num, startNum):
        reply = bytearray([startNum])  # start with name
        reply += string
        exp = math.ceil(math.log2(
            len(string))) if len(string) > 0 else 0  # find the next power of 2
        size = 2**exp
        exp = exp << 3
        length = size - len(string)
        for i in range(length):
            reply += bytearray([0])
        return self.addChksm(bytearray([CMD_ModeInfo | exp | num]) + reply)

    def buildFunctMap(self, mode, num, Type):
        exp = 1 << CMD_LLL_SHIFT
        mapType = mode[0]
        mapOut = mode[1]
        return self.addChksm(
            bytearray([CMD_ModeInfo | exp | num, Type, mapType, mapOut]))

    def buildFormat(self, mode, num, Type):
        exp = 2 << CMD_LLL_SHIFT
        sampleSize = mode[0] & 0xFF
        dataType = mode[1] & 0xFF
        figures = mode[2] & 0xFF
        decimals = mode[3] & 0xFF
        return self.addChksm(
            bytearray([
                CMD_ModeInfo | exp | num, Type, sampleSize, dataType, figures,
                decimals
            ]))

    def buildRange(self, settings, num, rangeType):
        exp = 3 << CMD_LLL_SHIFT
        minVal = struct.pack('<f', settings[0])
        maxVal = struct.pack('<f', settings[1])
        return self.addChksm(
            bytearray([CMD_ModeInfo | exp | num, rangeType]) + minVal + maxVal)

    def defineModes(self, modes):
        length = (len(modes) - 1) & 0xFF
        views = 0
        for i in modes:
            if (i[7]):
                views = views + 1
        views = (views - 1) & 0xFF
        return self.addChksm(bytearray([CMD_Mode, length, views]))

    def setupMode(self, mode, num):
        self.writeIt(self.padString(mode[0], num, NAME))  # write name
        self.writeIt(self.buildRange(mode[2], num, RAW))  # write RAW range
        self.writeIt(self.buildRange(mode[3], num, Pct))  # write Percent range
        self.writeIt(self.buildRange(mode[4], num, SI))  # write SI range
        self.writeIt(self.padString(mode[5], num, SYM))  # write symbol
        self.writeIt(self.buildFunctMap(mode[6], num,
                                        FCT))  # write Function Map
        self.writeIt(self.buildFormat(mode[1], num, FMT))  # write format


# -----   Start everything up

    def initialize(self):
        self.connected = False
        self.sendTimer = Timer(
            self.txTimer)  #, freq = self.freq)  # default is 200 ms
        # self.sendTimer.init(mode=Timer.PERIODIC, freq=self.freq)
        self.init()
        debug(9, 'debug8')
        self.writeIt(
            self.setType(self.sensorType)
        )  # set type to 35 (WeDo Ultrasonic) 61 (Spike color), 62 (Spike ultrasonic)
        self.writeIt(self.defineModes(self.modes))  # tell how many modes
        self.writeIt(self.defineBaud(MAX_BAUD))
        self.writeIt(self.defineVers(2, 2))
        num = len(self.modes) - 1
        for mode in reversed(self.modes):
            self.setupMode(mode, num)
            num -= 1
            utime.sleep_ms(5)

        self.writeIt(b'\x04')  #ACK
        # Check for ACK reply
        self.connected = self.waitFor(b'\x04')
        print('Success' if self.connected else 'Failed')

        # Reset Serial to High Speed
        # pull pin low
        # self.uart.deinit()
        if self.connected:
            # tx = machine.Pin(self.txPin, machine.Pin.OUT)
            # tx.value(0)
            # utime.sleep_ms(10)

            #change baudrate
            self.uart.init(baudrate=MAX_BAUD, bits=8, parity=None, stop=1)
            for m in range(len(self.modes)):
                self.load_payload('uInt8', 0, mode=m)

            #start callback  - MAKE SURE YOU RESTART THE CHIP EVERY TIME (CMD D) to kill previous callbacks running
            # self.sendTimer.callback(self.hubCallback)
            self.sendTimer.init(mode=Timer.PERIODIC,
                                freq=self.freq,
                                callback=self.hubCallback)
        return
Esempio n. 18
0
sensor.run(1)
fm.register(board_info.PIN15, fm.fpioa.UART1_TX)
fm.register(board_info.PIN17, fm.fpioa.UART1_RX)
uart_A = UART(UART.UART1, 115200, 8, None, 1, timeout=1000, read_buf_len=4096)

classes = ["racoon"]
task = kpu.load(0x600000)
anchor = (0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282,
          3.52778, 9.77052, 9.16828)
a = kpu.init_yolo2(task, 0.3, 0.3, 5, anchor)
while (True):
    img = sensor.snapshot()
    #.rotation_corr(z_rotation=90.0)
    #a = img.pix_to_ai()
    code = kpu.run_yolo2(task, img)
    if code:
        for i in code:
            a = img.draw_rectangle(i.rect(), color=(0, 255, 0))
            a = img.draw_string(i.x(),
                                i.y(),
                                classes[i.classid()],
                                color=(255, 0, 0),
                                scale=3)
            uart_A.write(str(i.rect()))
        a = lcd.display(img)
    else:
        a = lcd.display(img)
a = kpu.deinit(task)
uart_A.deinit()
del uart_A
Esempio n. 19
0
class ArloRobot(object):

    # com packet sending
    def com(self, packet):
        for i in packet:
            self.uart.write(i)
            self.uart.write(" ")
        self.uart.write("\r")
        tinit = utime.ticks_ms()
        resp = ''
        while (utime.ticks_ms() - tinit) < 150:  #timeout of 1600us
            data = self.uart.read(1)
            if data is not None and data != b'\r':
                resp = resp + str(data)[2:][:-1]

        if resp is not None:
            resp = resp.split("xd6")[-1].split("xc3")[-1].split(" ")

            try:
                resp = [int(i) for i in resp]
            except:
                return None

            if len(resp) != 2:
                return resp[0]
            return resp
        return resp

    # set up/set down
    # serialid is defined as the ID of the serial bus from the
    # microcontroller, however tx and rx can be defined
    def __init__(self, serialid=2, tx=17, rx=16, baudrate=19200):
        self.tx = tx
        self.rx = rx
        self.baudrate = baudrate
        self.uart = UART(serialid, self.baudrate)
        self.uart.init(self.baudrate,
                       bits=8,
                       parity=None,
                       stop=1,
                       txbuf=0,
                       tx=self.tx,
                       rx=self.rx)
        self.com(["TXPIN", "CH2"])  #needed so that reading is possible
        self.com(["DEC"])
        self.com(["ECHO", "ON"])

    # end serial connection
    def end(self):
        self.uart.deinit()

#-------------------------- movements methods------------------------

# Turn command
# motor_movements corresponds to the amount of encode positions
# top_speed to the positions per second

    def turn(self, motor_movement, top_speed):
        self.com(["TURN", str(motor_movement), str(top_speed)])

    # arc turns the motors so that the platform moves along the arc of a circle
    # of a given radius with a speed and an angle
    def arc(self, radius, top_speed, angle):
        self.com(["ARC", str(radius), str(top_speed), str(angle)])

    # left/right -> -32767 to 32767
    # speed -> 1 to 32767
    def move(self, left, right, speed):
        self.com(["MOVE", str(left), str(right), str(speed)])

    # left/right -> -32767 to 32767
    def go_speed(self, left, right):
        self.com(["GOSPD", str(left), str(right)])

    # left/right -> -127 to 127
    def go(self, left, right):
        self.com(["GO", str(left), str(right)])

    def travel(self, distance, top_speed, angle):
        self.com(["TRVL", str(distance), str(top_speed), str(angle)])

#--------------------------- information methods -----------------------

    def read_left_counts(self):
        return self.com(["DIST"])[0]

    def read_right_counts(self):
        return self.com(["DIST"])[1]

    def read_left_speed(self):
        return self.com(["SPD"])[0]

    def read_right_speed(self):
        return self.com(["SPD"])[1]

    def read_head_angle(self):
        return self.com(["HEAD"])[0]

    def read_firmware_ver(self):
        return self.com(["VER"])

    def read_hardware_ver(self):
        return self.com(["HWVER"])

    def clear_counts(self):
        return self.com(["RST"])

# ---------------------------- communication modes -----------------------

    def write_pulse_mode(self):
        self.com(["PULSE"])

    def set_lf_mode(self, status):
        return self.com(["SETLF", str(status)])

    def set_hex_com(self):
        return self.com(['HEX'])

    def set_dec_com(self):
        return self.com(['DEC'])

    def set_echo_mode(self, status):
        return self.com(["ECHO", str(status)])

    def set_verbose_mode(self, status):
        return self.com(["VERB", str(status)])

    def set_rx_pin(self, pin):
        return self.com(["RXPIN", str(pin)])

    def set_tx_pin(self, pin):
        return self.com(["TXPIN", str(pin)])

    def set_baud_rate(self, baud):
        return self.com(["BAUD", str(baud)])

    def set_pwm_scale(self, scale):
        return self.com(["SCALE", str(scale)])

    def set_pace(self, pace):
        return self.com(["PACE", str(pace)])

    def set_hold(self, hold):
        return self.com(["HOLD", str(baud)])


#-------------------------- closed loop constants ----------------------

    def set_ki_limit(self, limit):
        return self.com(["KIP", str(limit)])

    def set_ki_decay(self, decay):
        return self.com(["KIT", str(decay)])

    def set_kimax(self, maxim):
        return self.com(["KIMAX", str(maxim)])

    def set_ki_constant(self, constant):
        return self.com(["KI", str(constant)])

    def set_kp_constant(self, constant):
        return self.com(["KP", str(constant)])

    def set_acc_rate(self, acc):
        return self.com(["ACC", str(acc)])

    def set_ramp_rate(self, rate):
        return self.com(["RAMP", str(rate)])

    def set_live_zone(self, limit):
        return self.com(["LZ", str(limit)])

    def set_dead_zone(self, limit):
        return self.com(["DZ", str(limit)])

    def set_ppr(self, ppr):
        return self.com(["PPR", str(ppr)])
Esempio n. 20
0
class Player(object):
    """JQ6500 mini MP3 module."""

    EQ_NORMAL = 0
    EQ_POP = 1
    EQ_ROCK = 2
    EQ_JAZZ = 3
    EQ_CLASSIC = 4
    EQ_BASS = 5

    SRC_SDCARD = 1
    SRC_BUILTIN = 4

    LOOP_ALL = 0  # Plays all the tracks and repeats.
    LOOP_FOLDER = 1  # Plays all the tracks in the same folder and repeats.
    LOOP_ONE = 2  # Plays the same track and repeats.
    LOOP_RAM = 3  # Unknown
    LOOP_ONE_STOP = 4  # Plays the track and stops.
    LOOP_NONE = 4

    STATUS_STOPPED = 0
    STATUS_PLAYING = 1
    STATUS_PAUSED = 2

    READ_DELAY = .1

    def __init__(self, port=2, volume=20):
        """
        Constructor for JQ6500.

            Args:
                port (int): UART port # (default: 2).
                volume(int) : Initial volume (default: 20, range 0-30).
        """
        self.uart = UART(port, 9600)
        self.uart.read()
        self.reset()
        self.set_volume(volume)

    def clean_up(self):
        """Clean up and release resources."""
        self.reset()
        if 'deinit' in dir(self.uart):
            self.uart.deinit()

    def play(self):
        """Play the current file."""
        self.write_bytes([0x0D])

    def play_pause(self):
        """Toggle play or pause for the current file."""
        status = self.get_status()
        if status == self.STATUS_PAUSED or status == self.STATUS_STOPPED:
            self.play()
        elif status == self.STATUS_PLAYING:
            self.pause()

    def restart(self):
        """Restart current playing or paused file from the beginning."""
        old_volume = self.get_volume()
        self.set_volume(0)
        self.next()
        self.pause()
        self.set_volume(old_volume)
        self.prev()

    def pause(self):
        """Pause the current file.  Use play() to resume."""
        self.write_bytes([0x0E])

    def next(self):
        """Play the next file."""
        self.write_bytes([0x01])

    def prev(self):
        """Play the previous file."""
        self.write_bytes([0x02])

    def next_folder(self):
        """Play the next folder."""
        self.write_bytes([0x0F, 0x01])

    def prev_folder(self):
        """Play the previous folder."""
        self.write_bytes([0x0F, 0x00])

    def play_by_index(self, file_index):
        """
        Play file by FAT table index.

        Args:
            file_index (int):  File FAT table index number.

        Notes:
            The index number has nothing to do with the filename.
            To sort SD Card FAT table, search for a FAT sorting utility.
        """
        self.write_bytes([0x03, (file_index >> 8) & 0xFF, file_index & 0xFF])

    def play_by_number(self, folder_number, file_number):
        """
        Play file by folder number and file number.

        Args:
            folder_number (int):  Folder name number.
            file_number (int):  Filename number.

        Notes:
            Only applies to SD Card.
            To use this function, folders must be named from 00 to 99,
            and files must be named from 000.mp3 to 999.mp3.
        """
        self.write_bytes([0x12, folder_number & 0xFF, file_number & 0xFF])

    def volume_up(self):
        """Increase volume by 1 (Volume range 0-30)."""
        self.write_bytes([0x04])

    def volume_down(self):
        """Decrease volume by 1 (Volume range 0-30)."""
        self.write_bytes([0x05])

    def set_volume(self, level):
        """
        Set volume to a specific level.

        Args:
            level (int):  Volume level (Volume range 0-30).
        """
        assert (0 <= level <= 30)
        self.write_bytes([0x06, level])

    def set_equalizer(self, mode):
        """
        Set equalizer to 1 of 6 preset modes.

        Args:
            mode (int): (EQ_NORMAL, EQ_POP, EQ_ROCK, EQ_JAZZ,
                        EQ_CLASSIC, EQ_BASS).
        """
        self.write_bytes([0x07, mode])

    def set_looping(self, mode):
        """
        Set looping mode.

        Args:
            mode (int): (LOOP_ALL , LOOP_FOLDER, LOOP_ONE, LOOP_RAM,
                         LOOP_ONE_STOP, LOOP_NONE).
        """
        self.write_bytes([0x11, mode])

    def set_source(self, source):
        """
        Set source location of MP3 files (on-board flash or SD card).

        Args:
            source (int): (SRC_SDCARD, SRC_BUILTIN).

        Notes:
            SD card requires JQ6500-28P model.
        """
        self.write_bytes([0x09, source])

    def sleep(self):
        """
        Put the device to sleep.

        Notes:
            Not recommended for use with SD cards.
        """
        self.write_bytes([0x0A])

    def reset(self):
        """
        Soft reset of the device.

        Notes:
            Method is not reliable (especially with SD cards).
            Power-cycling is preferable.
        """
        self.write_bytes([0x0C])
        sleep(.5)

    def get_status(self):
        """
        Get device status. (STATUS_PAUSED,STATUS_PLAYING, STATUS_STOPPED).

        Notes:
            Only returns playing or paused with built-in flash.
            Method is unreliable with SD cardsself.
        """
        self.write_bytes([0x42])
        sleep(self.READ_DELAY)
        status = self.uart.read()
        sleep(self.READ_DELAY)
        if status.isdigit():
            return int(status)
        else:
            return -1

    def get_volume(self):
        """Get current volume level (0-30)."""
        self.write_bytes([0x43])
        sleep(self.READ_DELAY)
        level = self.read_bytes()
        return level

    def get_equalizer(self):
        """
        Get current equalizer mode.

        (EQ_NORMAL, EQ_POP, EQ_ROCK, EQ_JAZZ, EQ_CLASSIC, EQ_BASS).
        """
        self.write_bytes([0x44])
        sleep(self.READ_DELAY)
        eq = self.read_bytes()
        return eq

    def get_looping(self):
        """
        Get current looping mode.

        (LOOP_ALL , LOOP_FOLDER, LOOP_ONE, LOOP_RAM, LOOP_ONE_STOP, LOOP_NONE).
        """
        self.write_bytes([0x45])
        sleep(self.READ_DELAY)
        looping = self.read_bytes()
        return looping

    def get_file_count(self, source):
        """
        Return the number of files on the specified media.

        Args:
            source (int): (SRC_SDCARD, SRC_BUILTIN).
        """
        if source == self.SRC_SDCARD:
            self.write_bytes([0x47])
        else:
            # SRC_BUILTIN
            self.write_bytes([0x49])
        sleep(self.READ_DELAY)
        count = self.read_bytes()
        return count

    def get_folder_count(self, source):
        """
        Return the number of folders on the specified media.

        Args:
            source (int): (SRC_SDCARD, SRC_BUILTIN).

        Notes:
            Only SD cards can have folders.
        """
        if source == self.SRC_SDCARD:
            self.write_bytes([0x53])
            count = self.read_bytes()
            return count
        else:
            return 0

    def get_file_index(self, source):
        """
        Get FAT file index of current file.

        Args:
            source (int): (SRC_SDCARD, SRC_BUILTIN).

        Notes:
            Refers to current playing or paused file.  If stopped refers
            to the next file to play.
        """
        if source == self.SRC_SDCARD:
            self.write_bytes([0x4B])
            sleep(self.READ_DELAY)
            count = self.read_bytes()
            return count
        else:
            # SRC_BUILTIN
            self.write_bytes([0x4D])
            sleep(self.READ_DELAY)
            count = self.read_bytes()
            return count + 1

    def get_position(self):
        """Get current position in seconds of current file."""
        self.write_bytes([0x50])
        sleep(self.READ_DELAY)
        position = self.read_bytes()
        return position

    def get_length(self):
        """Get length in seconds of current file."""
        self.write_bytes([0x51])
        sleep(self.READ_DELAY)
        length = self.read_bytes()
        return length

    def get_name(self):
        """
        Get the filename of the current file on the SD card.

        Notes:
            SD card must be active source.
        """
        self.write_bytes([0x52])
        sleep(self.READ_DELAY)
        return self.uart.read()

    def get_version(self):
        """Get version number."""
        self.write_bytes([0x46])
        sleep(self.READ_DELAY)
        version = self.read_bytes()
        return version

    def read_buffer(self):
        """Return UART buffer as bytes."""
        return self.uart.read()

    def read_bytes(self):
        """Return 4 bytes from UART port."""
        b = self.uart.read(4)
        print(b)
        if len(b) > 0:
            return int(b, 16)
        else:
            return -1

    def write_bytes(self, b):
        """
        Write byte(s) to the UART port.

        Args:
            b ([byte]): List of bytes to write to the UART port.
        """
        message_length = len(b) + 1
        data = [0x7E, message_length] + b + [0xEF]
        # print (','.join('0x{:02X}'.format(x) for x in data))
        self.uart.read()
        self.uart.write(bytes(data))
Esempio n. 21
0
            distance_judg = distance
            print(distance_judg, end='')
            print('m')

            #シリアル通信 距離によってシリアル通信で送る値を変える
            if distance_judg < 0.9:
                uart.write('1')

            elif distance_judg < 1.5:
                uart.write('2')

            elif distance_judg <= 2:
                uart.write('3')
            #print(i.rect())
            #img = img.resize(224,224)
            #a = img.draw_circle(224,224,20,fill=True)

            a = img.draw_rectangle(i.rect())

    else:

        uart.write('7')

    time.sleep()

UART.deinit()
#b = class_ws2812.set_led(0,(0,0,0))
#b = class_ws2812.display()

a = kpu.deinit(task)
Esempio n. 22
0
class ebyteE32:
    ''' class to interface an ESP32 via serial commands to the EBYTE E32 Series LoRa modules '''

    # UART ports
    PORT = {'U1': 1, 'U2': 2}
    # UART parity strings
    PARSTR = {'8N1': '00', '8O1': '01', '8E1': '10'}
    PARINV = {v: k for k, v in PARSTR.items()}
    # UART parity bits
    PARBIT = {'N': None, 'E': 0, 'O': 1}
    # UART baudrate
    BAUDRATE = {
        1200: '000',
        2400: '001',
        4800: '010',
        9600: '011',
        19200: '100',
        38400: '101',
        57600: '110',
        115200: '111'
    }
    BAUDRINV = {v: k for k, v in BAUDRATE.items()}
    # LoRa datarate
    DATARATE = {
        '0.3k': '000',
        '1.2k': '001',
        '2.4k': '010',
        '4.8k': '011',
        '9.6k': '100',
        '19.2k': '101'
    }
    DATARINV = {v: k for k, v in DATARATE.items()}
    # Commands
    CMDS = {
        'setConfigPwrDwnSave': 0xC0,
        'getConfig': 0xC1,
        'setConfigPwrDwnNoSave': 0xC2,
        'getVersion': 0xC3,
        'reset': 0xC4
    }
    # operation modes (set with M0 & M1)
    OPERMODE = {
        'normal': '00',
        'wakeup': '10',
        'powersave': '01',
        'sleep': '11'
    }
    # model frequency ranges (MHz)
    FREQ = {
        170: [160, 170, 173],
        400: [410, 470, 525],
        433: [410, 433, 441],
        868: [862, 868, 893],
        915: [900, 915, 931]
    }
    # version info frequency
    FREQV = {'0x32': 433, '0x38': 470, '0x45': 868, '0x44': 915, '0x46': 170}
    # model maximum transmision power
    # 20dBm = 100mW - 27dBm = 500 mW - 30dBm = 1000 mW (1 W)
    MAXPOW = {'T20': 0, 'T27': 1, 'T30': 2}
    # transmission mode
    TRANSMODE = {0: 'transparent', 1: 'fixed'}
    # IO drive mode
    IOMODE = {
        0: 'TXD AUX floating output, RXD floating input',
        1: 'TXD AUX push-pull output, RXD pull-up input'
    }
    # wireless wakeup times from sleep mode
    WUTIME = {
        0b000: '250ms',
        0b001: '500ms',
        0b010: '750ms',
        0b011: '1000ms',
        0b100: '1250ms',
        0b101: '1500ms',
        0b110: '1750ms',
        0b111: '2000ms'
    }
    # Forward Error Correction (FEC) mode
    FEC = {0: 'off', 1: 'on'}
    # transmission power T20/T27/T30 (dBm)
    TXPOWER = {
        0b00: ['20dBm', '27dBm', '30dBm'],
        0b01: ['17dBm', '24dBm', '27dBm'],
        0b10: ['14dBm', '21dBm', '24dBm'],
        0b11: ['10dBm', '18dBm', '21dBm']
    }

    def __init__(self,
                 PinM0,
                 PinM1,
                 PinAUX,
                 Model='868T20D',
                 Port='U1',
                 Baudrate=9600,
                 Parity='8N1',
                 AirDataRate='2.4k',
                 Address=0x0000,
                 Channel=0x06,
                 debug=False):
        ''' constructor for ebyte E32 LoRa module '''
        # configuration in dictionary
        self.config = {}
        self.config['model'] = Model  # E32 model (default 868T20D)
        self.config['port'] = Port  # UART channel on the ESP (default U1)
        self.config['baudrate'] = Baudrate  # UART baudrate (default 9600)
        self.config['parity'] = Parity  # UART Parity (default 8N1)
        self.config[
            'datarate'] = AirDataRate  # wireless baudrate (default 2.4k)
        self.config['address'] = Address  # target address (default 0x0000)
        self.config['channel'] = Channel  # target channel (0-31, default 0x06)
        self.calcFrequency(
        )  # calculate frequency (min frequency + channel*1 MHz)
        self.config[
            'transmode'] = 0  # transmission mode (default 0 - tranparent)
        self.config['iomode'] = 1  # IO mode (default 1 = not floating)
        self.config[
            'wutime'] = 0  # wakeup time from sleep mode (default 0 = 250ms)
        self.config['fec'] = 1  # forward error correction (default 1 = on)
        self.config[
            'txpower'] = 0  # transmission power (default 0 = 20dBm/100mW)
        #
        self.PinM0 = PinM0  # M0 pin number
        self.PinM1 = PinM1  # M1 pin number
        self.PinAUX = PinAUX  # AUX pin number
        self.M0 = None  # instance for M0 Pin (set operation mode)
        self.M1 = None  # instance for M1 Pin (set operation mode)
        self.AUX = None  # instance for AUX Pin (device status : 0=busy - 1=idle)
        self.serdev = None  # instance for UART
        self.debug = debug

    def start(self):
        ''' Start the ebyte E32 LoRa module '''
        try:
            # check parameters
            if int(self.config['model'].split('T')[0]) not in ebyteE32.FREQ:
                self.config['model'] = '868T20D'
            if self.config['port'] not in ebyteE32.PORT:
                self.config['port'] = 'U1'
            if int(self.config['baudrate']) not in ebyteE32.BAUDRATE:
                self.config['baudrate'] = 9600
            if self.config['parity'] not in ebyteE32.PARSTR:
                self.config['parity'] = '8N1'
            if self.config['datarate'] not in ebyteE32.DATARATE:
                self.config['datarate'] = '2.4k'
            if self.config['channel'] > 31:
                self.config['channel'] = 31
            # make UART instance
            self.serdev = UART(ebyteE32.PORT.get(self.config['port']))
            # init UART
            par = ebyteE32.PARBIT.get(str(self.config['parity'])[1])
            self.serdev.init(baudrate=self.config['baudrate'],
                             bits=8,
                             parity=par,
                             stop=1)
            if self.debug:
                print(self.serdev)
            # make operation mode & device status instances
            self.M0 = Pin(self.PinM0, Pin.OUT)
            self.M1 = Pin(self.PinM1, Pin.OUT)
            self.AUX = Pin(self.PinAUX, Pin.IN, Pin.PULL_UP)
            if self.debug:
                print(self.M0, self.M1, self.AUX)
            # set config to the ebyte E32 LoRa module
            self.setConfig('setConfigPwrDwnSave')
            return "OK"

        except Exception as E:
            if self.debug:
                print("error on start UART", E)
            return "NOK"

    def sendMessage(self, to_address, to_channel, payload, useChecksum=False):
        ''' Send the payload to ebyte E32 LoRa modules in transparent or fixed mode. The payload is a data dictionary to
            accomodate key value pairs commonly used to store sensor data and is converted to a JSON string before sending.
            The payload can be appended with a 2's complement checksum to validate correct transmission.
            - transparent mode : all modules with the same address and channel of the transmitter will receive the payload
            - fixed mode : only the module with this address and channel will receive the payload;
                           if the address is 0xFFFF all modules with the same channel will receive the payload'''
        try:
            # type of transmission
            if (to_address
                    == self.config['address']) and (to_channel
                                                    == self.config['channel']):
                # transparent transmission mode
                # all modules with the same address and channel will receive the payload
                self.setTransmissionMode(0)
            else:
                # fixed transmission mode
                # only the module with the target address and channel will receive the payload
                self.setTransmissionMode(1)
            # put into wakeup mode (includes preamble signals to wake up device in powersave or sleep mode)
            self.setOperationMode('wakeup')
            # check payload
            if type(payload) != dict:
                print('payload is not a dictionary')
                return 'NOK'
            # encode message
            msg = []
            if self.config[
                    'transmode'] == 1:  # only for fixed transmission mode
                msg.append(to_address // 256)  # high address byte
                msg.append(to_address % 256)  # low address byte
                msg.append(to_channel)  # channel
            js_payload = ujson.dumps(payload)  # convert payload to JSON string
            for i in range(len(js_payload)):  # message
                msg.append(ord(js_payload[i]))  # ascii code of character
            if useChecksum:  # attach 2's complement checksum
                msg.append(int(self.calcChecksum(js_payload), 16))
            # debug
            if self.debug:
                print(msg)
            # wait for idle module
            self.waitForDeviceIdle()
            # send the message
            self.serdev.write(bytes(msg))
            return "OK"

        except Exception as E:
            if self.debug:
                print('Error on sendMessage: ', E)
            return "NOK"

    def recvMessage(self, from_address, from_channel, useChecksum=False):
        ''' Receive payload messages from ebyte E32 LoRa modules in transparent or fixed mode. The payload is a JSON string
            of a data dictionary to accomodate key value pairs commonly used to store sensor data. If checksumming is used, the
            checksum of the received payload including the checksum byte should result in 0 for a correct transmission.
            - transparent mode : payload will be received if the module has the same address and channel of the transmitter
            - fixed mode : only payloads from transmitters with this address and channel will be received;
                           if the address is 0xFFFF, payloads from all transmitters with this channel will be received'''
        try:
            # type of transmission
            if (from_address
                    == self.config['address']) and (from_channel
                                                    == self.config['channel']):
                # transparent transmission mode
                # all modules with the same address and channel will receive the message
                self.setTransmissionMode(0)
            else:
                # fixed transmission mode
                # only the module with the target address and channel will receive the message
                self.setTransmissionMode(1)
            # put into normal mode
            self.setOperationMode('normal')
            # receive message
            js_payload = self.serdev.read()
            # debug
            if self.debug:
                print(js_payload)
            # did we receive anything ?
            if js_payload == None:
                # nothing
                return {'msg': None}
            else:
                # decode message
                msg = ''
                for i in range(len(js_payload)):
                    msg += chr(js_payload[i])
                # checksum check
                if useChecksum:
                    cs = int(self.calcChecksum(msg), 16)
                    if cs != 0:
                        # corrupt
                        return {'msg': 'corrupt message, checksum ' + str(cs)}
                    else:
                        # message ok, remove checksum
                        msg = msg[:-1]
                # JSON to dictionary
                message = ujson.loads(msg)
                return message

        except Exception as E:
            if self.debug:
                print('Error on recvMessage: ', E)
            return "NOK"

    def calcChecksum(self, payload):
        ''' Calculates checksum for sending/receiving payloads. Sums the ASCII character values mod256 and returns
            the lower byte of the two's complement of that value in hex notation. '''
        return '%2X' % (-(sum(ord(c) for c in payload) % 256) & 0xFF)

    def reset(self):
        ''' Reset the ebyte E32 Lora module '''
        try:
            # send the command
            res = self.sendCommand('reset')
            # discard result
            return "OK"

        except Exception as E:
            if self.debug:
                print("error on reset", E)
            return "NOK"

    def stop(self):
        ''' Stop the ebyte E32 LoRa module '''
        try:
            if self.serdev != None:
                self.serdev.deinit()
                del self.serdev
            return "OK"

        except Exception as E:
            if self.debug:
                print("error on stop UART", E)
            return "NOK"

    def sendCommand(self, command):
        ''' Send a command to the ebyte E32 LoRa module.
            The module has to be in sleep mode '''
        try:
            # put into sleep mode
            self.setOperationMode('sleep')
            # send command
            HexCmd = ebyteE32.CMDS.get(command)
            if HexCmd in [0xC0, 0xC2]:  # set config to device
                header = HexCmd
                HexCmd = self.encodeConfig()
                HexCmd[0] = header
            else:  # get config, get version, reset
                HexCmd = [HexCmd] * 3
            if self.debug:
                print(HexCmd)
            self.serdev.write(bytes(HexCmd))
            # wait for result
            utime.sleep_ms(50)
            # read result
            if command == 'reset':
                result = ''
            else:
                result = self.serdev.read()
                # wait for result
                utime.sleep_ms(50)
                # debug
                if self.debug:
                    print(result)
            return result

        except Exception as E:
            if self.debug:
                print('Error on sendCommand: ', E)
            return "NOK"

    def getVersion(self):
        ''' Get the version info from the ebyte E32 LoRa module '''
        try:
            # send the command
            result = self.sendCommand('getVersion')
            # check result
            if len(result) != 4:
                return "NOK"
            # decode result
            freq = ebyteE32.FREQV.get(hex(result[1]), 'unknown')
            # show version
            if result[0] == 0xc3:
                print('================= E32 MODULE ===================')
                print('model       \t%dMhz' % (freq))
                print('version     \t%d' % (result[2]))
                print('features    \t%d' % (result[3]))
                print('================================================')
            return "OK"

        except Exception as E:
            if self.debug:
                print('Error on getVersion: ', E)
            return "NOK"

    def getConfig(self):
        ''' Get config parameters from the ebyte E32 LoRa module '''
        try:
            # send the command
            result = self.sendCommand('getConfig')
            # check result
            if len(result) != 6:
                return "NOK"
            # decode result
            self.decodeConfig(result)
            # show config
            self.showConfig()
            return "OK"

        except Exception as E:
            if self.debug:
                print('Error on getConfig: ', E)
            return "NOK"

    def decodeConfig(self, message):
        ''' decode the config message from the ebyte E32 LoRa module to update the config dictionary '''
        # message byte 0 = header
        header = int(message[0])
        # message byte 1 & 2 = address
        self.config['address'] = int(message[1]) * 256 + int(message[2])
        # message byte 3 = speed (parity, baudrate, datarate)
        bits = '{0:08b}'.format(message[3])
        self.config['parity'] = ebyteE32.PARINV.get(bits[0:2])
        self.config['baudrate'] = ebyteE32.BAUDRINV.get(bits[2:5])
        self.config['datarate'] = ebyteE32.DATARINV.get(bits[5:])
        # message byte 4 = channel
        self.config['channel'] = int(message[4])
        # message byte 5 = option (transmode, iomode, wutime, fec, txpower)
        bits = '{0:08b}'.format(message[5])
        self.config['transmode'] = int(bits[0:1])
        self.config['iomode'] = int(bits[1:2])
        self.config['wutime'] = int(bits[2:5])
        self.config['fec'] = int(bits[5:6])
        self.config['txpower'] = int(bits[6:])

    def encodeConfig(self):
        ''' encode the config dictionary to create the config message of the ebyte E32 LoRa module '''
        # Initialize config message
        message = []
        # message byte 0 = header
        message.append(0xC0)
        # message byte 1 = high address
        message.append(self.config['address'] // 256)
        # message byte 2 = low address
        message.append(self.config['address'] % 256)
        # message byte 3 = speed (parity, baudrate, datarate)
        bits = '0b'
        bits += ebyteE32.PARSTR.get(self.config['parity'])
        bits += ebyteE32.BAUDRATE.get(self.config['baudrate'])
        bits += ebyteE32.DATARATE.get(self.config['datarate'])
        message.append(int(bits))
        # message byte 4 = channel
        message.append(self.config['channel'])
        # message byte 5 = option (transmode, iomode, wutime, fec, txpower)
        bits = '0b'
        bits += str(self.config['transmode'])
        bits += str(self.config['iomode'])
        bits += '{0:03b}'.format(self.config['wutime'])
        bits += str(self.config['fec'])
        bits += '{0:02b}'.format(self.config['txpower'])
        message.append(int(bits))
        return message

    def showConfig(self):
        ''' Show the config parameters of the ebyte E32 LoRa module on the shell '''
        print('=================== CONFIG =====================')
        print('model       \tE32-%s' % (self.config['model']))
        print('frequency   \t%dMhz' % (self.config['frequency']))
        print('address     \t0x%04x' % (self.config['address']))
        print('channel     \t0x%02x' % (self.config['channel']))
        print('datarate    \t%sbps' % (self.config['datarate']))
        print('port        \t%s' % (self.config['port']))
        print('baudrate    \t%dbps' % (self.config['baudrate']))
        print('parity      \t%s' % (self.config['parity']))
        print('transmission\t%s' %
              (ebyteE32.TRANSMODE.get(self.config['transmode'])))
        print('IO mode     \t%s' %
              (ebyteE32.IOMODE.get(self.config['iomode'])))
        print('wakeup time \t%s' %
              (ebyteE32.WUTIME.get(self.config['wutime'])))
        print('FEC         \t%s' % (ebyteE32.FEC.get(self.config['fec'])))
        maxp = ebyteE32.MAXPOW.get(self.config['model'][3:6], 0)
        print('TX power    \t%s' %
              (ebyteE32.TXPOWER.get(self.config['txpower'])[maxp]))
        print('================================================')

    def waitForDeviceIdle(self):
        ''' Wait for the E32 LoRa module to become idle (AUX pin high) '''
        count = 0
        # loop for device busy
        while not self.AUX.value():
            # increment count
            count += 1
            # maximum wait time 100 ms
            if count == 10:
                break
            # sleep for 10 ms
            utime.sleep_ms(10)

    def saveConfigToJson(self):
        ''' Save config dictionary to JSON file '''
        with open('E32config.json', 'w') as outfile:
            ujson.dump(self.config, outfile)

    def loadConfigFromJson(self):
        ''' Load config dictionary from JSON file '''
        with open('E32config.json', 'r') as infile:
            result = ujson.load(infile)
        print(self.config)

    def calcFrequency(self):
        ''' Calculate the frequency (= minimum frequency + channel * 1MHz)'''
        # get minimum and maximum frequency
        freqkey = int(self.config['model'].split('T')[0])
        minfreq = ebyteE32.FREQ.get(freqkey)[0]
        maxfreq = ebyteE32.FREQ.get(freqkey)[2]
        # calculate frequency
        freq = minfreq + self.config['channel']
        if freq > maxfreq:
            self.config['frequency'] = maxfreq
            self.config['channel'] = hex(maxfreq - minfreq)
        else:
            self.config['frequency'] = freq

    def setTransmissionMode(self, transmode):
        ''' Set the transmission mode of the E32 LoRa module '''
        if transmode != self.config['transmode']:
            self.config['transmode'] = transmode
            self.setConfig('setConfigPwrDwnSave')

    def setConfig(self, save_cmd):
        ''' Set config parameters for the ebyte E32 LoRa module '''
        try:
            # send the command
            result = self.sendCommand(save_cmd)
            # check result
            if len(result) != 6:
                return "NOK"
            # debug
            if self.debug:
                # decode result
                self.decodeConfig(result)
                # show config
                self.showConfig()
            # save config to json file
            self.saveConfigToJson()
            return "OK"

        except Exception as E:
            if self.debug:
                print('Error on setConfig: ', E)
            return "NOK"

    def setOperationMode(self, mode):
        ''' Set operation mode of the E32 LoRa module '''
        # get operation mode settings (default normal)
        bits = ebyteE32.OPERMODE.get(mode, '00')
        # set operation mode
        self.M0.value(int(bits[0]))
        self.M1.value(int(bits[1]))
        # wait a moment
        utime.sleep_ms(50)
Esempio n. 23
0
    uart0 = UART(0, 1000000)
    uart1 = UART(1, 1000000)

# next ones must raise
try:
    UART(0, 9600, parity=None, pins=('GP12', 'GP13', 'GP7'))
except Exception:
    print('Exception')

try:
    UART(0, 9600, parity=UART.ODD, pins=('GP12', 'GP7'))
except Exception:
    print('Exception')

uart0 = UART(0, 1000000)
uart0.deinit()
try:
    uart0.any()
except Exception:
    print('Exception')

try:
    uart0.read()
except Exception:
    print('Exception')

try:
    uart0.write('abc')
except Exception:
    print('Exception')
Esempio n. 24
0
class GPS():
    def __init__(self, mac=1, _baudrate=9600, _tx=22, _rx=21, _txbuf=1024, _rxbuf=1024):
        # create a new UART controller
        self.uart = UART(mac, _baudrate, tx=_tx, rx=_rx, txbuf=_txbuf, rxbuf=_rxbuf)
        
        # Used for finding new packets
        self.oldRXLength = 0
        self.currentRXLength = 0

        self.speed = 0

        #make a dictionary for RMC data
        self.RMCdata = {}
        self.RMCfound = False
    
    def __del__(self):
        self.uart.deinit()

    def format_RMCdata(self, data):
        # Time
        self.RMCdata['time'] = data[1][0:2] + ':' + data[1][2:4] + ':' + data[1][4:6]

        # Latitude
        hhmm_mmmm = data[3]
        self.RMCdata['latitude'] = float(hhmm_mmmm[0:2])
        fraction = '.' + hhmm_mmmm[2:4] + hhmm_mmmm[5:9]
        self.RMCdata['latitude'] = self.RMCdata['latitude'] + float(fraction)*100/60
        # N = +     S = -
        if(data[4] == 'S'):
            self.RMCdata['latitude'] = self.RMCdata['latitude'] * -1
        self.RMCdata['latitude'] = str(self.RMCdata['latitude'])

        #Longitude
        hhmm_mmmm = data[5][1:]
        self.RMCdata['longitude'] = float(hhmm_mmmm[0:2])
        fraction = '.' + hhmm_mmmm[2:4] + hhmm_mmmm[5:9]
        self.RMCdata['longitude'] = self.RMCdata['longitude'] + float(fraction)*100/60
        # E = +     W = -
        if(data[6] == 'W'):
            self.RMCdata['longitude'] = self.RMCdata['longitude'] * -1
        self.RMCdata['longitude'] = str(self.RMCdata['longitude'])

        # TODO: Let's revisit this...
        # #Speed and Course
        # self.RMCdata['speed'] = float(data[7])
        # self.RMCdata['course'] = float(data[8])
        self.speed = float(data[7])

        #Date
        self.RMCdata['date'] = str(2000 + int(data[9][4:6])) + '-' + data[9][2:4] + '-' + data[9][0:2]

    def parse_RMCdata(self, rawData):
        # search each line for RMC data set
        for d in rawData:
            if (d[3:6] == 'RMC') and (d[-4:] == '\\r\\n'):
                self.RMCfound = True
                break #we found RMC data
        
        if self.RMCfound:
            self.RMCfound = False
            data = d.split(',')

            if not (data[2] == 'V'):
                self.format_RMCdata(data)
        
        else:
            self.RMCdata = {}

    def get_RMCdata(self, defaultLogger: Logger):
        self.oldRXLength = self.currentRXLength
        self.currentRXLength = self.uart.any()

        if(self.currentRXLength == 0):
            self.RMCdata = {}
            return [self.RMCdata, None]

        else:
            data = self.uart.read(self.currentRXLength)
            rawData = list(d.replace('\r\n', '\\r\\n') for d in str(data).replace('\\r\\n', '\r\n').splitlines(True))
            try:
                self.parse_RMCdata(rawData)
            except Exception as e:
                self.RMCdata = {}
                #TODO: remove print
                print(e)
                defaultLogger.warning(str(e))
            return [self.RMCdata, self.speed]
Esempio n. 25
0
class DeepSleep:

    WPUA_ADDR = const(0x09)
    OPTION_REG_ADDR = const(0x0E)
    IOCAP_ADDR = const(0x1A)
    IOCAN_ADDR = const(0x1B)

    WAKE_STATUS_ADDR = const(0x40)
    MIN_BAT_ADDR = const(0x41)
    SLEEP_TIME_ADDR = const(0x42)
    CTRL_0_ADDR = const(0x45)

    EXP_RTC_PERIOD = const(7000)

    def __init__(self):
        self.uart = UART(1, baudrate=10000, pins=(COMM_PIN, ))
        self.clk_cal_factor = 1
        self.uart.read()
        # enable the weak pull-ups control
        self.clearbits(OPTION_REG_ADDR, 1 << 7)

    def _send(self, data):
        self.uart.write(bytes(data))

    def _start(self):
        self.uart.sendbreak(20)
        self._send([0x55])

    def _magic(self, address, and_val, or_val, xor_val, expected=None):
        self._start()
        self._send([address, and_val & 0xFF, or_val & 0xFF, xor_val & 0xFF])
        if expected is None:
            return self.uart.read()
        else:
            if expected > 0:
                return self.uart.read(expected)

    def _add_to_pin_mask(self, mask, pin):
        if pin == 'P10' or pin == 'G17':
            mask |= 0x01
        elif pin == 'P17' or pin == 'G31':
            mask |= 0x02
        elif pin == 'P18' or pin == 'G30':
            mask |= 0x08
        else:
            raise ValueError('Invalid Pin specified: {}'.format(pin))
        return mask

    def _create_pin_mask(self, pins):
        mask = 0
        if type(pins) is str:
            mask = self._add_to_pin_mask(mask, pins)
        else:
            for pin in pins:
                mask = self._add_to_pin_mask(mask, pin)
        return mask & PIN_MASK

    def poke(self, address, value):
        self._magic(address, 0, value, 0)

    def peek(self, address):
        return self._magic(address, 0xFF, 0, 0)[6]

    def setbits(self, address, mask):
        self._magic(address, 0xFF, mask, 0)

    def clearbits(self, address, mask):
        self._magic(address, ~mask, 0, 0)

    def togglebits(self, address, mask):
        self._magic(address, 0xFF, 0, mask)

    def calibrate(self):
        """ The microcontroller will send the value of CTRL_0 after setting the bit
            and then will send the following pattern through the data line:

               val | 1 | 0 | 1*| 0 | 1*| 0 | 1
               ms  | 1 | 1 | 1 | 1 | 8 | 1 | -

            The idea is to measure the real life duration of periods marked with *
            and substract them. That will remove any errors common to both measurements
            The result is 7 ms as generated by the PIC LF clock.
            It can be used to scale any future sleep value. """

        # setbits, but limit the number of received bytes to avoid confusion with pattern
        self._magic(CTRL_0_ADDR, 0xFF, 1 << 2, 0, 0)
        self.uart.deinit()
        self._pulses = pycom.pulses_get(COMM_PIN, 50)
        self.uart = UART(1, baudrate=10000, pins=(COMM_PIN, ))
        try:
            self.clk_cal_factor = (self._pulses[4][1] - self._pulses[1][1]) / EXP_RTC_PERIOD
        except:
            pass
        if self.clk_cal_factor > 1.25 or self.clk_cal_factor < 0.75:
            self.clk_cal_factor = 1

    def enable_auto_poweroff(self):
        self.setbits(CTRL_0_ADDR, 1 << 1)

    def enable_pullups(self, pins):
        mask = self._create_pin_mask(pins)
        self.setbits(WPUA_ADDR, mask)

    def disable_pullups(self, pins):
        mask = self._create_pin_mask(pins)
        self.clearbits(WPUA_ADDR, mask)

    def enable_wake_on_raise(self, pins):
        mask = self._create_pin_mask(pins)
        self.setbits(IOCAP_ADDR, mask)

    def disable_wake_on_raise(self, pins):
        mask = self._create_pin_mask(pins)
        self.clearbits(IOCAP_ADDR, mask)

    def enable_wake_on_fall(self, pins):
        mask = self._create_pin_mask(pins)
        self.setbits(IOCAN_ADDR, mask)

    def disable_wake_on_fall(self, pins):
        mask = self._create_pin_mask(pins)
        self.clearbits(IOCAN_ADDR, mask)

    def get_wake_status(self):
        # bits as they are returned from PIC:
        #   0: PIN 0 value after awake
        #   1: PIN 1 value after awake
        #   2: PIN 2 value after awake
        #   3: PIN 3 value after awake
        #   4: TIMEOUT
        #   5: POWER ON

        wake_r = self.peek(WAKE_STATUS_ADDR)
        return {'wake': wake_r & (TIMER_WAKE | POWER_ON_WAKE),
                'P10': wake_r & 0x01, 'P17': (wake_r & 0x02) >> 1,
                'P18': (wake_r & 0x08) >> 3}

    def set_min_voltage_limit(self, value):
        # voltage value passed in volts (e.g. 3.6) and round it to the nearest integer
        value = int(((256 * 2.048) + (value / 2)) / value)
        self.poke(MIN_BAT_ADDR, value)

    def go_to_sleep(self, seconds):
        gc.collect()
        while True:
            try:
                self.calibrate()
            except Exception:
                pass

            # the 1.024 factor is because the PIC LF operates at 31 KHz
            # WDT has a frequency divider to generate 1 ms
            # and then there is a binary prescaler, e.g., 1, 2, 4 ... 512, 1024 ms
            # hence the need for the constant

            # round to the nearest integer
            seconds = int((seconds / (1.024 * self.clk_cal_factor)) + 0.5)
            self.poke(SLEEP_TIME_ADDR, (seconds >> 16) & 0xFF)
            self.poke(SLEEP_TIME_ADDR + 1, (seconds >> 8) & 0xFF)
            self.poke(SLEEP_TIME_ADDR + 2, seconds & 0xFF)
            self.setbits(CTRL_0_ADDR, 1 << 0)

    def hw_reset(self):
        self.setbits(CTRL_0_ADDR, 1 << 4)
a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)

while (True):
    clock.tick()
    img = sensor.snapshot()
    code = kpu.run_yolo2(task, img)
    lcd.display(img)
    if code:
        for i in code:
            print(i)
            a = img.draw_rectangle(i.rect())
            b = lcd.display(img)
            img_buf = img.compress(quality=70)
            img_size1 = (img.size() & 0xFF0000) >> 16
            img_size2 = (img.size() & 0x00FF00) >> 8
            img_size3 = (img.size() & 0x0000FF) >> 0
            data_packet = bytearray([
                0xFF, 0xD8, 0xEA, 0x01, img_size1, img_size2, img_size3, 0x00,
                0x00, 0x00
            ])
            uart_Port.write(data_packet)
            uart_Port.write(img_buf)
            time.sleep(1.0)

a = kpu.deinit(task)

#   Send UART End
uart_Port.deinit()
del uart_Port
print("finish")
Esempio n. 27
0
class uModBusSerial:
    def __init__(self,
                 uart_id,
                 baudrate=9600,
                 data_bits=8,
                 stop_bits=1,
                 parity=None,
                 pins=None,
                 ctrl_pin=None):
        pinsLen = len(pins)
        if pins == None or pinsLen < 2 or pinsLen > 4 or pinsLen == 3:
            raise ValueError(
                'pins should contain pin names/numbers for: tx, rx, [rts, cts]'
            )
        tx = pins[0]
        rx = pins[1]
        if pinsLen == 4:
            rts = pins[2]
            cts = pins[3]
            self._uart = UART(uart_id, baudrate=baudrate, bits=data_bits, parity=parity, \
                          stop=stop_bits, timeout_char=10, tx=tx, rx=rx, rts=rts, cts=cts)
        else:
            self._uart = UART(uart_id, baudrate=baudrate, bits=data_bits, parity=parity, \
                            stop=stop_bits, timeout_char=10, tx=tx, rx=rx)
        #self._uart = UART(uart_id, baudrate=baudrate, bits=data_bits, parity=parity, \
        #                  stop=stop_bits, timeout_chars=10, pins=pins)
        if ctrl_pin is not None:
            self._ctrlPin = Pin(ctrl_pin, mode=Pin.OUT)
        else:
            self._ctrlPin = None
        self.char_time_ms = (1000 * (data_bits + stop_bits + 2)) // baudrate

    def _calculate_crc16(self, data):
        crc = 0xFFFF

        for char in data:
            crc = (crc >> 8) ^ Const.CRC16_TABLE[((crc) ^ char) & 0xFF]

        return struct.pack('<H', crc)

    def _bytes_to_bool(self, byte_list):
        bool_list = []
        for index, byte in enumerate(byte_list):
            bool_list.extend([bool(byte & (1 << n)) for n in range(8)])

        return bool_list

    def _to_short(self, byte_array, signed=True):
        response_quantity = int(len(byte_array) / 2)
        fmt = '>' + (('h' if signed else 'H') * response_quantity)

        return struct.unpack(fmt, byte_array)

    def _exit_read(self, response):
        if response[1] >= Const.ERROR_BIAS:
            if len(response) < Const.ERROR_RESP_LEN:
                return False
        elif (Const.READ_COILS <= response[1] <= Const.READ_INPUT_REGISTER):
            expected_len = Const.RESPONSE_HDR_LENGTH + 1 + response[
                2] + Const.CRC_LENGTH
            if len(response) < expected_len:
                return False
        elif len(response) < Const.FIXED_RESP_LEN:
            return False

        return True

    def _uart_read(self):
        response = bytearray()

        for x in range(1, 40):
            if self._uart.any():
                response.extend(self._uart.read())
                #response.extend(self._uart.readall())
                # variable length function codes may require multiple reads
                if self._exit_read(response):
                    break
            time.sleep(0.05)

        return response

    def _send_receive(self, modbus_pdu, slave_addr, count):
        serial_pdu = bytearray()
        serial_pdu.append(slave_addr)
        serial_pdu.extend(modbus_pdu)

        crc = self._calculate_crc16(serial_pdu)
        serial_pdu.extend(crc)

        # flush the Rx FIFO
        self._uart.read()
        if self._ctrlPin:
            self._ctrlPin(1)
        self._uart.write(serial_pdu)
        if self._ctrlPin:
            while not self._uart.wait_tx_done(2):
                machine.idle()
            time.sleep_ms(1 + self.char_time_ms)
            self._ctrlPin(0)

        return self._validate_resp_hdr(self._uart_read(), slave_addr,
                                       modbus_pdu[0], count)

    def _validate_resp_hdr(self, response, slave_addr, function_code, count):

        if len(response) == 0:
            raise OSError('no data received from slave')

        resp_crc = response[-Const.CRC_LENGTH:]
        expected_crc = self._calculate_crc16(response[0:len(response) -
                                                      Const.CRC_LENGTH])
        if (resp_crc[0] != expected_crc[0]) or (resp_crc[1] !=
                                                expected_crc[1]):
            raise OSError('invalid response CRC')

        if (response[0] != slave_addr):
            raise ValueError('wrong slave address')

        if (response[1] == (function_code + Const.ERROR_BIAS)):
            raise ValueError('slave returned exception code: {:d}'.format(
                response[2]))

        hdr_length = (Const.RESPONSE_HDR_LENGTH +
                      1) if count else Const.RESPONSE_HDR_LENGTH

        return response[hdr_length:len(response) - Const.CRC_LENGTH]

    def read_coils(self, slave_addr, starting_addr, coil_qty):
        modbus_pdu = functions.read_coils(starting_addr, coil_qty)

        resp_data = self._send_receive(modbus_pdu, slave_addr, True)
        status_pdu = self._bytes_to_bool(resp_data)

        return status_pdu

    def read_discrete_inputs(self, slave_addr, starting_addr, input_qty):
        modbus_pdu = functions.read_discrete_inputs(starting_addr, input_qty)

        resp_data = self._send_receive(modbus_pdu, slave_addr, True)
        status_pdu = self._bytes_to_bool(resp_data)

        return status_pdu

    def read_holding_registers(self,
                               slave_addr,
                               starting_addr,
                               register_qty,
                               signed=True):
        modbus_pdu = functions.read_holding_registers(starting_addr,
                                                      register_qty)

        resp_data = self._send_receive(modbus_pdu, slave_addr, True)
        register_value = self._to_short(resp_data, signed)

        return register_value

    def read_input_registers(self,
                             slave_addr,
                             starting_address,
                             register_quantity,
                             signed=True):
        modbus_pdu = functions.read_input_registers(starting_address,
                                                    register_quantity)

        resp_data = self._send_receive(modbus_pdu, slave_addr, True)
        register_value = self._to_short(resp_data, signed)

        return register_value

    def write_single_coil(self, slave_addr, output_address, output_value):
        modbus_pdu = functions.write_single_coil(output_address, output_value)

        resp_data = self._send_receive(modbus_pdu, slave_addr, False)
        operation_status = functions.validate_resp_data(
            resp_data,
            Const.WRITE_SINGLE_COIL,
            output_address,
            value=output_value,
            signed=False)

        return operation_status

    def write_single_register(self,
                              slave_addr,
                              register_address,
                              register_value,
                              signed=True):
        modbus_pdu = functions.write_single_register(register_address,
                                                     register_value, signed)

        resp_data = self._send_receive(modbus_pdu, slave_addr, False)
        operation_status = functions.validate_resp_data(
            resp_data,
            Const.WRITE_SINGLE_REGISTER,
            register_address,
            value=register_value,
            signed=signed)

        return operation_status

    def write_multiple_coils(self, slave_addr, starting_address,
                             output_values):
        modbus_pdu = functions.write_multiple_coils(starting_address,
                                                    output_values)

        resp_data = self._send_receive(modbus_pdu, slave_addr, False)
        operation_status = functions.validate_resp_data(
            resp_data,
            Const.WRITE_MULTIPLE_COILS,
            starting_address,
            quantity=len(output_values))

        return operation_status

    def write_multiple_registers(self,
                                 slave_addr,
                                 starting_address,
                                 register_values,
                                 signed=True):
        modbus_pdu = functions.write_multiple_registers(
            starting_address, register_values, signed)

        resp_data = self._send_receive(modbus_pdu, slave_addr, False)
        operation_status = functions.validate_resp_data(
            resp_data,
            Const.WRITE_MULTIPLE_REGISTERS,
            starting_address,
            quantity=len(register_values))

        return operation_status

    def close(self):
        if self._uart == None:
            return
        try:
            self._uart.deinit()
        except Exception:
            pass
Esempio n. 28
0
class SpikePrimeDevice(object):
    def __init__(self, tx_pin, rx_pin, timer=Timer.TIMER0, timer_channel=Timer.CHANNEL0,
            tx_gpio=GPIO.GPIO1, tx_fpioa_gpio=fm.fpioa.GPIO1, uart_num=UART.UART2):

        self.connected = False
        self.uart = None
        self.tx_pin_num = tx_pin
        self.rx_pin_num = rx_pin
        self.data = 0
        self.current_mode = 0
        self.textBuffer = bytearray(b'             ')
        self.timer_num = timer
        self.timer_channel_num = timer_channel
        self.timer = None
        self.tx_gpio = tx_gpio
        self.tx_fpioa_gpio = tx_fpioa_gpio
        self.uart_num = uart_num
        if uart_num == UART.UART2:
            self.uart_tx_fpioa_num = fm.fpioa.UART2_TX
            self.uart_rx_fpioa_num = fm.fpioa.UART2_RX
        elif uart_num == UART.UART1:
            self.uart_tx_fpioa_num = fm.fpioa.UART1_TX
            self.uart_rx_fpioa_num = fm.fpioa.UART1_RX

    def initialize(self):
        assert not self.connected

        print("connecting...")

        fm.register(self.tx_pin_num, self.tx_fpioa_gpio, force=True)
        uart_tx = GPIO(self.tx_gpio, GPIO.OUT)

        uart_tx.value(0)
        time.sleep_ms(500)
        uart_tx.value(1)

        fm.register(self.tx_pin_num, self.uart_tx_fpioa_num, force=True)
        fm.register(self.rx_pin_num, self.uart_rx_fpioa_num, force=True)

        self.uart = UART(self.uart_num, 2400, bits=8, parity=None, stop=1, timeout=10000, read_buf_len=4096)

        self.uart.write(b'\x00')
        self.uart.write(b'\x40\x3e\x81')
        self.uart.write(b'\x49\x07\x07\xb6')
        self.uart.write(b'\x52\x00\xc2\x01\x00\x6e')
        self.uart.write(b'\x5f\x00\x00\x00\x02\x00\x00\x00\x02\xa0')

        self.uart.write(b'\xa7\x00\x66\x6c\x6f\x61\x74\x5f\x61\x72\x72\x61\x79\x00\x00\x00\x00\x00\x0e')
        self.uart.write(b'\x9f\x01\x00\x00\x00\x00\x00\x00\xc8\x42\xeb')
        self.uart.write(b'\x9f\x02\x00\x00\x00\x00\x00\x00\xc8\x42\xe8')
        self.uart.write(b'\x9f\x03\x00\x00\x00\x00\x00\x00\xc8\x42\xe9')
        self.uart.write(b'\x87\x04\x00\x7c')
        self.uart.write(b'\x8f\x05\x10\x00\x65')
        self.uart.write(b'\x97\x80\x04\x03\x02\x01\xec')
        time.sleep_ms(5)

        self.uart.write(b'\xa6\x00\x69\x6e\x74\x33\x32\x5f\x61\x72\x72\x61\x79\x00\x00\x00\x00\x00\x0d')
        self.uart.write(b'\x9e\x01\x00\x00\x00\x00\x00\x00\xc8\x42\xea')
        self.uart.write(b'\x9e\x02\x00\x00\x00\x00\x00\x00\xc8\x42\xe9')
        self.uart.write(b'\x9e\x03\x00\x00\x00\x00\x00\x00\xc8\x42\xe8')
        self.uart.write(b'\x86\x04\x00\x7d')
        self.uart.write(b'\x8e\x05\x10\x00\x64')
        self.uart.write(b'\x96\x80\x04\x02\x03\x00\xec')
        time.sleep_ms(5)

        self.uart.write(b'\xa5\x00\x69\x6e\x74\x31\x36\x5f\x61\x72\x72\x61\x79\x00\x00\x00\x00\x00\x08')
        self.uart.write(b'\x9d\x01\x00\x00\x00\x00\x00\x00\xc8\x42\xe9')
        self.uart.write(b'\x9d\x02\x00\x00\x00\x00\x00\x00\xc8\x42\xea')
        self.uart.write(b'\x9d\x03\x00\x00\x00\x00\x00\x00\xc8\x42\xeb')
        self.uart.write(b'\x85\x04\x00\x7e')
        self.uart.write(b'\x8d\x05\x10\x00\x67')
        self.uart.write(b'\x95\x80\x04\x01\x03\x00\xec')
        time.sleep_ms(5)

        self.uart.write(b'\xa4\x00\x69\x6e\x74\x38\x5f\x61\x72\x72\x61\x79\x00\x00\x00\x00\x00\x00\x36')
        self.uart.write(b'\x9c\x01\x00\x00\x00\x00\x00\x00\xc8\x42\xe8')
        self.uart.write(b'\x9c\x02\x00\x00\x00\x00\x00\x00\xc8\x42\xeb')
        self.uart.write(b'\x9c\x03\x00\x00\x00\x00\x00\x00\xc8\x42\xea')
        self.uart.write(b'\x84\x04\x00\x7f')
        self.uart.write(b'\x8c\x05\x10\x00\x66')
        self.uart.write(b'\x94\x80\x04\x00\x03\x00\xec')
        time.sleep_ms(5)

        self.uart.write(b'\x9b\x00\x66\x6c\x6f\x61\x74\x00\x00\x00\x14')
        self.uart.write(b'\x9b\x01\x00\x00\x00\x00\x00\x00\xc8\x42\xef')
        self.uart.write(b'\x9b\x02\x00\x00\x00\x00\x00\x00\xc8\x42\xec')
        self.uart.write(b'\x9b\x03\x00\x00\x00\x00\x00\x00\xc8\x42\xed')
        self.uart.write(b'\x83\x04\x00\x78')
        self.uart.write(b'\x8b\x05\x10\x00\x61')
        self.uart.write(b'\x93\x80\x01\x03\x02\x01\xed')
        time.sleep_ms(5)

        self.uart.write(b'\x9a\x00\x69\x6e\x74\x33\x32\x00\x00\x00\x17')
        self.uart.write(b'\x9a\x01\x00\x00\x00\x00\x00\x00\xc8\x42\xee')
        self.uart.write(b'\x9a\x02\x00\x00\x00\x00\x00\x00\xc8\x42\xed')
        self.uart.write(b'\x9a\x03\x00\x00\x00\x00\x00\x00\xc8\x42\xec')
        self.uart.write(b'\x82\x04\x00\x79')
        self.uart.write(b'\x8a\x05\x10\x00\x60')
        self.uart.write(b'\x92\x80\x01\x02\x03\x00\xed')
        time.sleep_ms(5)

        self.uart.write(b'\x99\x00\x69\x6e\x74\x31\x36\x00\x00\x00\x12')
        self.uart.write(b'\x99\x01\x00\x00\x00\x00\x00\x00\xc8\x42\xed')
        self.uart.write(b'\x99\x02\x00\x00\x00\x00\x00\x00\xc8\x42\xee')
        self.uart.write(b'\x99\x03\x00\x00\x00\x00\x00\x00\xc8\x42\xef')
        self.uart.write(b'\x81\x04\x00\x7a')
        self.uart.write(b'\x89\x05\x10\x00\x63')
        self.uart.write(b'\x91\x80\x01\x01\x03\x00\xed')
        time.sleep_ms(5)

        self.uart.write(b'\x90\x00\x69\x6e\x74\x38\x24')
        self.uart.write(b'\x98\x01\x00\x00\x00\x00\x00\x00\xc8\x42\xec')
        self.uart.write(b'\x98\x02\x00\x00\x00\x00\x00\x00\xc8\x42\xef')
        self.uart.write(b'\x98\x03\x00\x00\x00\x00\x00\x00\xc8\x42\xee')
        self.uart.write(b'\x80\x04\x00\x7b')
        self.uart.write(b'\x88\x05\x10\x00\x62')
        self.uart.write(b'\x90\x80\x01\x00\x03\x00\xed')
        time.sleep_ms(5)

        self.uart.write(b'\x04')
        time.sleep_ms(5)

        print("waiting for ACK...")
        self.connected = self._wait_for_value(b'\x04')

        if self.connected:
            print("connected")
            self.uart.deinit()

            fm.register(self.tx_pin_num, self.tx_fpioa_gpio, force=True)
            uart_tx = GPIO(self.tx_gpio, GPIO.OUT)

            uart_tx.value(0)
            time.sleep_ms(10)

            fm.register(self.tx_pin_num, self.uart_tx_fpioa_num, force=True)
            fm.register(self.rx_pin_num, self.uart_rx_fpioa_num, force=True)

            self.uart = UART(self.uart_num, 115200, bits=8, parity=None, stop=1, timeout=10000, read_buf_len=4096)
            self.set_data(0)

            self.timer = Timer(self.timer_num, self.timer_channel_num,
                mode=Timer.MODE_PERIODIC, period=200, callback=self._handle_message_callback)
            self.timer.start()
        else:
            print("not connected")

        return self.connected

    def _wait_for_value(self, expected_value, timeout=2):
        starttime = time.time()
        currenttime = starttime
        status = False
        #count = 0
        while (currenttime - starttime) < timeout:
            time.sleep_ms(5)
            #print(count)
            #count += 1
            currenttime = time.time()
            if self.uart.any() > 0:
                data = self.uart.readchar()
                #print(data)
                if data == ord(expected_value):
                    status = True
                    break
        return status

    def set_data(self, data):
        self.data = data

    def _get_checksum(self, values):
        checksum = 0xFF
        for x in values:
            checksum ^= x
        return checksum

    def _send_value(self, data):
        value = data & 0xFF
        payload = bytes([0xC0, value, self._get_checksum([0xC0, value])])
        size = self.uart.write(payload)
        return size

    def _handle_message_callback(self, timer):
        if not self.connected:
            return

        while self.uart.any() > 0:
            x = self.uart.readchar()
            if x == 0:
                pass
            elif x == 0x02:
                pass
            elif x == 0x43:
                mode = self.uart.readchar()
                checksum = self.uart.readchar()
                if checksum == self._get_checksum([x, mode]):
                    self.current_mode = mode
            elif x == 0x46:
                zero = self.uart.readchar()
                b9 = self.uart.readchar()
                if zero == 0 and b9 == 0xb9:
                    size_mode = self.uart.readchar()
                    size = 2 ** ((size_mode & 0b111000) >> 3)
                    mode = size_mode & 0b111
                    checksum = self._get_checksum([x, zero, b9, size_mode])
                    for i in range(len(self.textBuffer)):
                        self.textBuffer[i] = ord(b' ')

                    for i in range(size):
                        self.textBuffer[i] = self.uart.readchar()
                        checksum ^= self.textBuffer[i]

                    expected_checksum = self.uart.readchar()
                    if expected_checksum == checksum:
                        print(self.textBuffer)
            elif x == 0x4C:
                thing = self.uart.readchar()
                checksum = self.uart.readchar()
                if checksum == self._get_checksum([x, thing]):
                    pass
            else:
                print(x)

        size = self._send_value(self.data)
        if not size:
            self.connected = False
Esempio n. 29
0
# check for memory leaks...
for i in range(0, 1000):
    uart1 = UART(1, 1000000)
    uart2 = UART(2, 1000000)

# next ones must raise
try:
    UART(1, 9600, parity=None, pins=('GP12', 'GP13', 'GP7'))
except Exception:
    print('Exception')

try:
    UART(1, 9600, parity=UART.ODD, pins=('GP12', 'GP7'))
except Exception:
    print('Exception')
'''
uart1 = UART(0, 1000000)
uart1.deinit()
try:
    uart0.any()
except Exception:
    print('Exception')

try:
    uart0.read()
except Exception:
    print('Exception')

try:
    uart0.write('abc')
except Exception:
Esempio n. 30
0
            dist_str = "%.1f"%(dist)
            print("[DISTANCE]: " + dist_str)
            img.draw_string(2,47,  dist_str,scale=3)
            lcd.display(img)
            continue
        name,dist = get_nearest(feature_list,plist)
        #print(clock.fps())
        if dist < 50 and name != "exclude":      #50 is modified from original value 200
            img.draw_rectangle(1,46,222,132,color=br.get_color(0,255,0),thickness=3)
            img.draw_string(2,47 +30,  "%s"%(name),scale=3)
            if old_name != name:
                namestring = str(name) + "\n"  #UART to StickC
                uart_Port.write(namestring)    #UART to StickC
                # print(name)                  #modified from original
                lcd.display(img)
                br.play_sound("/sd/voice/"+name+".wav")
                old_name = name
        else:
            old_name = ''

        # output
        img.draw_string(2,47,  "%.2f "%(dist),scale=3)
        lcd.display(img)
        kpu.fmap_free(fmap)
except KeyboardInterrupt:
    kpu.deinit(task)
    sys.exit()

uart_Port.deinit()     #UART to StickC
del uart_Port          #UART to StickC
Esempio n. 31
0
class CORGI85():
    def __init__(self):
        try:
            fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX)
            fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX)
            self.uart = UART(UART.UART2,
                             115200,
                             8,
                             None,
                             1,
                             timeout=1000,
                             read_buf_len=4096)
            print("Init CORGI85")
        except:
            print("Unable to init UART")

    def deinit(self):
        self.uart.deinit()
        del self.uart

    def reset(self):
        self.uart.write("\rRESET,\r")

    def wifi_check(self):

        data = self.uart.read()
        self.uart.write("\rWIFI_CHECK,\r")
        time.sleep_ms(100)
        data = self.uart.read()

        return int(data[0])

    def BLYNK_config(self):
        self.uart.write("\rBLYNK,0,\r")

    def BLYNK_Set_auth(self, auth):
        self.uart.write("\rBLYNK,1,")
        self.uart.write(auth)
        self.uart.write(",\r")

    def BLYNK_Set_host(self, host):
        self.uart.write("\rBLYNK,2,")
        self.uart.write(host)
        self.uart.write(",\r")

    def BLYNK_Set_port(self, port):
        self.uart.write("\rBLYNK,3,")
        self.uart.write(str(port))
        self.uart.write(",\r")

    def BLYNK_write_int(self, VPIN, value):
        self.uart.write("\rBLYNK,4,")
        self.uart.write(str(VPIN))
        self.uart.write(",")
        self.uart.write(str(value))
        self.uart.write(",\r")

    def BLYNK_write_float(self, VPIN, value):
        self.uart.write("\rBLYNK,5,")
        self.uart.write(str(VPIN))
        self.uart.write(",")
        self.uart.write(str(value))
        self.uart.write(",\r")

    def BLYNK_write_char(self, VPIN, value):
        self.uart.write("\rBLYNK,6,")
        self.uart.write(str(VPIN))
        self.uart.write(",")
        self.uart.write(str(value))
        self.uart.write(",\r")

    def BLYNK_noti(self, value):
        self.uart.write("\rBLYNK,7,")
        self.uart.write(str(value))
        self.uart.write(",\r")

    def BLYNK_read(self, VPIN):

        data = self.uart.read()
        self.uart.write("\rBLYNK,8,")
        self.uart.write(str(VPIN))
        self.uart.write(",\r")
        time.sleep_ms(10)
        data = self.uart.read()

        return data
Esempio n. 32
0
class RPLidar(object):

    def __init__(self, id = 2, baudrate=115200, timeout=5000, motoctl = 'X6'):
        self.uart = None
        self.motoctl = Pin(motoctl)
        self.motoctl_timer = Timer(2, freq = 20000)
        self.motoPWM_channel = self.motoctl_timer.channel(1, Timer.PWM, pin=self.motoctl)
        self.motoPWM_channel.pulse_width_percent(50)
        self.connect(id, baudrate, timeout)

        self._rxbuffer = bytearray(32)
        self._headings = array('H', [0 for i in range(READINGS_LENGTH)])#heading = heading[i]/64.0
        self._distances = array('H', [0 for i in range(READINGS_LENGTH)])#distance = distance[i]/4.0 #in mm
        self._readings_index = 0
        self._descriptor_queue = collections.deque((), 32) #File fifo
        self._next_data_type = None

        self._status = None
        self._error = None
        self._scanerrors = 0

    def connect(self, id, _baudrate, _timeout):
        self.uart = UART(id)
        self.uart.init(baudrate = _baudrate, bits=8, parity=None, stop=1, timeout = _timeout, read_buf_len=RXBUFFER_LENGTH)
        self.set_motor_pwm()

    def disconnect(self):
        self.uart.deinit()
        self.set_motor_pwm(0)
        

    def _send_request(self, command):
        if command == COMMAND_SCAN:
            self._descriptor_queue.append((SCAN_RESPONSE_LENGTH, False, SCAN_DATATYPE))
        elif command == COMMAND_GET_HEALTH:
            self._descriptor_queue.append((GET_HEALTH_RESPONSE_LENGTH, False, GET_HEALTH_DATATYPE))

        req = START_FLAG_1 + command
        self.uart.write(req)
        print('Command sent: %s' % req)

    def _read_response_descriptor(self):
        descriptor = self.uart.read(RESPONSE_DECRIPTOR_LENGTH)
        print('Recieved descriptor: ', descriptor)

        if(descriptor == None):
            print("Timeout")
            raise CommunicationError
        elif(len(descriptor) != RESPONSE_DECRIPTOR_LENGTH):
            print("Descriptor length mismatch")
            raise CommunicationError
        elif(not descriptor.startswith(START_FLAG_1 + START_FLAG_2)):
            print("Wrong descriptor starting bytes")
            raise CommunicationError

        data_response_lenght = int.from_bytes(descriptor[2:5], 'little') & ~(0b11<<28) #Remove 2btis from last byte that are reserved for send_mode

        send_mode = int.from_bytes(descriptor[5:6], 'little') & 0b11000000 #Extract the 2 bits from the byte
        is_single = send_mode == 0x0

        data_type = descriptor[6]

        print("Data response length : ", data_response_lenght)
        print("is single : ", is_single)
        print("data type : ", data_type)

        return data_response_lenght, is_single, data_type
    
    def _read_response(self, length):
        #print("Trying to read response: ",length," bytes")
        bytes_read = self.uart.readinto(self._rxbuffer, length)
        #print('Recieved data: ', self._rxbuffer)
        if bytes_read == None :
            print("Timout")
            raise CommunicationError
        if bytes_read != length:
            print('Wrong body size')
            raise CommunicationError

    def _serial_handler(self):

        if(bool(self._descriptor_queue)): #if descriptor queue is not empty, expecting a response descriptor
            data_response_lenght, is_single, data_type = self._descriptor_queue.popleft()
            if self._read_response_descriptor() !=  (data_response_lenght, is_single, data_type):
                print("Unexpected response descirptor")
                raise CommunicationError
            self._next_data_type = data_type
            return
        
        elif self._next_data_type == SCAN_DATATYPE: 
            self._read_response(SCAN_RESPONSE_LENGTH)

            S = self._rxbuffer[0] & 0b00000001
            not_S = (self._rxbuffer[0] & 0b00000010) >> 1
            C = self._rxbuffer[1] & 0b00000001
            if S == not_S:
                #print("Problem S = not_S")
                self._scanerrors += 1
                return
            #quality = data[0] >> 
            if C != 1:
                #print("Problem C != 1")
                self._scanerrors += 1
                return
            
            if(self._scanerrors > 10):#11 consecutive scan error, reseting lidar
                print("Many consecutive scan errors, reseting lidar")
                self.reset()
                self._scanerrors = 0
                self.start_scanning()
                return

            self._scanerrors = 0 #managed to read without error
            distance_q2 = (self._rxbuffer[3]) + (self._rxbuffer[4] << 8)
            if distance_q2 != 0:
                heading = ((self._rxbuffer[1] >> 1) + (self._rxbuffer[2] << 7))
                self._headings[self._readings_index] = heading
                self._distances[self._readings_index] = distance_q2
                self._readings_index += 1
                if(self._readings_index >= READINGS_LENGTH):
                    self._readings_index = 0
                
                
        elif self._next_data_type == 6:
            self._read_response(3)
            print(self._rxbuffer[0:1])
            self._status = int.from_bytes(self._rxbuffer[0:1], "little")
            self._error = int.from_bytes(self._rxbuffer[1:2], "little")
            
        


    def set_motor_pwm(self, pulse_width_percent=50):
        self.motoPWM_channel.pulse_width_percent(pulse_width_percent)

    def get_health(self):
        self._status = None
        self._error = None
        self._send_request(COMMAND_GET_HEALTH)

        while self._status == None or self._error == None:
            time.sleep(0.02)
        
        print("Status : ", self._status)
        print("Error : ", self._error)
        return self._status, self._error
            
    def stop(self):
        '''RPLIDAR will exit the current scanning state. 
            The laser diode and the measurement system will be disabled and the Idle state will be entered. 
            This request will be ignored when RPLIDAR is in the Idle or Protection Stop state.
            Since RPLIDAR won’t send response packet for this request, host systems should
            wait for at least 1 millisecond (ms) before sending another request'''
        print("Stopping scan")
        self.should_scan = False
        self._send_request(COMMAND_STOP)
        time.sleep(.002)
        if self.uart.any() > 0:
            print("Clearing buffer")
            self.uart.read() #clear uart buffer

    def reset(self):
        '''A reset operation will make RPLIDAR revert to a similar state as it has just been
            powered up. This request is useful when RPLIDAR has entered the Protection Stop
            state. After a core reset, RPLIDAR will return to the idle state which will accept the
            start scan request again.
            Since RPLIDAR won’t send response packet for this request, host systems should
            wait for at least 2 milliseconds (ms) before sending another request. '''
        print("Resseting RPLidar")
        self._send_request(COMMAND_RESET)
        time.sleep(1)
        if self.uart.any() > 0:
            print("Clearing buffer")
            self.uart.read(self.uart.any()) #clear uart buffer
        while bool(self._descriptor_queue):
            self._descriptor_queue.popleft()

    def start_scanning(self):
        self._send_request(COMMAND_SCAN)

    #Slow no preallocation
    def get_reading(self):
        reading = []
        for i in range(len(self._headings)):
            reading.append([self._headings[i]/64.0, self._distances[i]/4.0])
        return reading

    def get_headings_mv(self):
        return memoryview(self._headings)

    def get_distances_mv(self):
        return memoryview(self._distances)

    def update(self):
        inBuff = self.uart.any()
        if(inBuff > 0):            
            while  (bool(self._descriptor_queue) and inBuff >= RESPONSE_DECRIPTOR_LENGTH) or \
                (self._next_data_type == SCAN_DATATYPE and inBuff >= SCAN_RESPONSE_LENGTH) or \
                (self._next_data_type == GET_HEALTH_DATATYPE and inBuff >= GET_HEALTH_RESPONSE_LENGTH):
                    self._serial_handler()
                    inBuff = self.uart.any()
Esempio n. 33
0
class DeepSleep:

    WPUA_ADDR = const(0x09)
    OPTION_REG_ADDR = const(0x0E)
    IOCAP_ADDR = const(0x1A)
    IOCAN_ADDR = const(0x1B)

    WAKE_STATUS_ADDR = const(0x40)
    MIN_BAT_ADDR = const(0x41)
    SLEEP_TIME_ADDR = const(0x42)
    CTRL_0_ADDR = const(0x45)

    EXP_RTC_PERIOD = const(7000)

    def __init__(self):
        self.uart = UART(1, baudrate=10000, pins=(COMM_PIN, ), timeout_chars=5)
        self.clk_cal_factor = 1
        self.uart.read()
        # enable the weak pull-ups control
        self.clearbits(OPTION_REG_ADDR, 1 << 7)

    def _send(self, data):
        self.uart.write(bytes(data))

    def _start(self):
        self.uart.sendbreak(12)
        self._send([0x55])

    def _magic(self, address, and_val, or_val, xor_val, expected=None):
        self._start()
        self._send([address, and_val & 0xFF, or_val & 0xFF, xor_val & 0xFF])
        if expected is None:
            return self.uart.read()
        else:
            if expected > 0:
                return self.uart.read(expected)

    def _add_to_pin_mask(self, mask, pin):
        if pin == 'P10' or pin == 'G17':
            mask |= 0x01
        elif pin == 'P17' or pin == 'G31':
            mask |= 0x02
        elif pin == 'P18' or pin == 'G30':
            mask |= 0x08
        else:
            raise ValueError('Invalid Pin specified: {}'.format(pin))
        return mask

    def _create_pin_mask(self, pins):
        mask = 0
        if type(pins) is str:
            mask = self._add_to_pin_mask(mask, pins)
        else:
            for pin in pins:
                mask = self._add_to_pin_mask(mask, pin)
        return mask & PIN_MASK

    def poke(self, address, value):
        self._magic(address, 0, value, 0)

    def peek(self, address):
        try:
            return self._magic(address, 0xFF, 0, 0)[6]
        except:
            return self._magic(address, 0xFF, 0, 0)[6]

    def setbits(self, address, mask):
        self._magic(address, 0xFF, mask, 0)

    def clearbits(self, address, mask):
        self._magic(address, ~mask, 0, 0)

    def togglebits(self, address, mask):
        self._magic(address, 0xFF, 0, mask)

    def calibrate(self):
#         The microcontroller will send the value of CTRL_0 after setting the bit
#            and then will send the following pattern through the data line:
#
#               val | 1 | 0 | 1*| 0 | 1*| 0 | 1
#               ms  | 1 | 1 | 1 | 1 | 8 | 1 | -
#
#            The idea is to measure the real life duration of periods marked with *
#            and substract them. That will remove any errors common to both measurements
#            The result is 7 ms as generated by the PIC LF clock.
#            It can be used to scale any future sleep value.

        # setbits, but limit the number of received bytes to avoid confusion with pattern
        self._magic(CTRL_0_ADDR, 0xFF, 1 << 2, 0, 0)
        self.uart.deinit()
        self._pulses = pycom.pulses_get(COMM_PIN, 150)
        self.uart.init(baudrate=10000, pins=(COMM_PIN, ), timeout_chars=5)
        idx = 0
        for i in range(len(self._pulses)):
            if self._pulses[i][1] > EXP_RTC_PERIOD:
                idx = i
                break
        try:
            self.clk_cal_factor = (self._pulses[idx][1] - self._pulses[(idx - 1)][1]) / EXP_RTC_PERIOD
        except:
            self.clk_cal_factor = 1
        if self.clk_cal_factor > 1.25 or self.clk_cal_factor < 0.75:
            self.clk_cal_factor = 1

    def enable_auto_poweroff(self):
        self.setbits(CTRL_0_ADDR, 1 << 1)

    def enable_pullups(self, pins):
        mask = self._create_pin_mask(pins)
        self.setbits(WPUA_ADDR, mask)

    def disable_pullups(self, pins):
        mask = self._create_pin_mask(pins)
        self.clearbits(WPUA_ADDR, mask)

    def enable_wake_on_raise(self, pins):
        mask = self._create_pin_mask(pins)
        self.setbits(IOCAP_ADDR, mask)

    def disable_wake_on_raise(self, pins):
        mask = self._create_pin_mask(pins)
        self.clearbits(IOCAP_ADDR, mask)

    def enable_wake_on_fall(self, pins):
        mask = self._create_pin_mask(pins)
        self.setbits(IOCAN_ADDR, mask)

    def disable_wake_on_fall(self, pins):
        mask = self._create_pin_mask(pins)
        self.clearbits(IOCAN_ADDR, mask)

    def get_wake_status(self):
        # bits as they are returned from PIC:
        #   0: PIN 0 value after awake
        #   1: PIN 1 value after awake
        #   2: PIN 2 value after awake
        #   3: PIN 3 value after awake
        #   4: TIMEOUT
        #   5: POWER ON

        wake_r = self.peek(WAKE_STATUS_ADDR)
        return {'wake': wake_r & (TIMER_WAKE | POWER_ON_WAKE),
                'P10': wake_r & 0x01, 'P17': (wake_r & 0x02) >> 1,
                'P18': (wake_r & 0x08) >> 3}

    def set_min_voltage_limit(self, value):
        # voltage value passed in volts (e.g. 3.6) and round it to the nearest integer
        value = int(((256 * 2.048) + (value / 2)) / value)
        self.poke(MIN_BAT_ADDR, value)

    def go_to_sleep(self, seconds):
        gc.collect()
        while True:
            try:
                self.calibrate()
            except Exception:
                pass

            # the 1.024 factor is because the PIC LF operates at 31 KHz
            # WDT has a frequency divider to generate 1 ms
            # and then there is a binary prescaler, e.g., 1, 2, 4 ... 512, 1024 ms
            # hence the need for the constant

            # round to the nearest integer
            seconds = int((seconds / (1.024 * self.clk_cal_factor)) + 0.5)
            self.poke(SLEEP_TIME_ADDR, (seconds >> 16) & 0xFF)
            self.poke(SLEEP_TIME_ADDR + 1, (seconds >> 8) & 0xFF)
            self.poke(SLEEP_TIME_ADDR + 2, seconds & 0xFF)
            self.setbits(CTRL_0_ADDR, 1 << 0)

    def hw_reset(self):
        self.setbits(CTRL_0_ADDR, 1 << 4)