def distance_mm(self): spi.init(baudrate=125000, sclk=self.sclk_pin, mosi=self.trigger_pin, miso=self.echo_pin) pre = 0 post = 0 k = -1 length = 500 resp = bytearray(length) resp[0] = 0xFF spi.write_readinto(resp, resp) # find first non zero value try: i, value = next((ind, v) for ind, v in enumerate(resp) if v) except StopIteration: i = -1 if i > 0: pre = bin(value).count("1") # find first non full high value afterwards try: k, value = next((ind, v) for ind, v in enumerate(resp[i:length - 2]) if resp[i + ind + 1] == 0) post = bin(value).count("1") if k else 0 k = k + i except StopIteration: i = -1 dist = -1 if i < 0 else round( ((pre + (k - i) * 8. + post) * 8 * 0.172) / 2) return dist
def init(self): #set buf with setup commands, turn on display, clear display and make ready for commands buf = bytearray([self.FUNCTION_SET1_CMD, self.DISPLAY_ON, self.CLEAR_DISPLAY, 0x06]) #set the spi pins and frequency spi.init(baudrate=250000, bits=8, mode=0, sclk=pin13, mosi=pin15, miso=pin14) sleep(1000) #send setup to the LCD spi.write(buf) self.initalised = True
def __init__(self): c = b'\xAE\xA4\xD5\xF0\xA8\x3F\xD3\x00\x00\x8D' \ b'\x14\x20\x00\x21\x00\x7F\x22\x00\x3F\xa1' \ b'\xc8\xDA\x12\x81\xCF\xd9\xF1\xDB\x40\xA6\xd6\x00\xaf' pin14.write_digital(0) spi.init(miso=pin15, baudrate=8000000) pin14.write_digital(1) self.__cmd(c) self.screen = bytearray(1024)
def NFC_setup(): # display.off() spi.init(baudrate=1000000, bits=8, mode=0, sclk=pin13, mosi=pin15, miso=pin14) print("SPI Init") CS_PIN.write_digital(1) if RST_PIN.read_digital() == 0: RST_PIN.write_digital(0) # give a few microseconds for nfc to reset utime.sleep_us(2) RST_PIN.write_digital(1) utime.sleep_us(50000) print("version:") byt = NFC_read_register(0x37 << 1) print(byt) NFC_write_register(TxModeRegister, 0x00) NFC_write_register(RxModeRegister, 0x00) # Reset ModWidthReg NFC_write_register(ModWidthRegister, 0x26) # TAuto=1; timer starts automatically at the end of the transmission # in all communication modes at all speeds NFC_write_register(TModeRegister, 0x80) # TPreScaler = TModeReg[3..0]:TPrescalerReg, ie 0x0A9 = 169 => # f_timer=40kHz,ie a timer period of 25μs. NFC_write_register(TPrescalarRegister, 0xA9) # Reload timer with 0x3E8 = 1000, ie 25ms before timeout. NFC_write_register(TReloadRegisterH, 0x03) NFC_write_register(TReloadRegisterL, 0xE8) # Default 0x00. Force a 100 % ASK modulation independent of # the ModGsPReg register setting NFC_write_register(TxASKRegister, 0x40) # Default 0x3F. Set the preset value for the CRC coprocessor # for the CalcCRC command to 0x6363 (ISO 14443-3 part 6.2.4) NFC_write_register(ModeRegister, 0x3D) NFC_antenna_on()
def init_spi(): global g_spi_initialized if g_spi_initialized is False: spi.init(baudrate=SPI_BAUDRATE, sclk=pin16, mosi=pin16, miso=pin15) g_spi_initialized = True
def __init__(self, spi, cs): self.spi = spi self.cs = cs self.buffer = bytearray(8) spi.init() self.init()
def __init__(self): spi.init() pin16.write_digital(1) # setup CS and set to high pass