def dispatch(obj): #dht11 = obj["dht11"] #dht11.measure() #temp1 = dht11.temperature() #humi = dht11.humidity() bmp180 = obj["bmp180"] temp2 = bmp180.readTemperature() press = bmp180.readPressure() time = utime.time_ns() html = """ <html> <head> <title>ESP32 Example</title> </head> <body> <table> <tr><th colspan=2>DHT11</th></tr> <tr><td><b>Temperature</b></td><td>{0} C</td></tr> <tr><td><b>Humidity</b></td><td>{1} %</td></tr> <tr><th colspan=2>BMP180</th></tr> <tr><td><b>Temperature</b></td><td>{2} C</td></tr> <tr><td><b>Pressure</b></td><td>{3} Pa</td></tr> <tr><th colspan=2>{4}</th></tr> </table> </body> </html> """.format("???", "???", temp2, press, time) return html
def main(): tag = Tag() tag.setup() start = utime.time_ns() try: tag.run() except KeyboardInterrupt: tag.dw1000.stop() end = utime.time_ns() delta = end - start print('Delta', delta, '\n' 'Send', tag.send, '\n' 'Acked', tag.acked, '\n' 'Timeouts', tag.timeouts)
def unixTimestamp(): """ Get a unix timestamp The timestamp is returned as floating point with whole seconds before the comma. Returns: (float): Timestamp """ return utime.time_ns()
# test utime.time_ns() try: import utime utime.sleep_us utime.time_ns except (ImportError, AttributeError): print("SKIP") raise SystemExit t0 = utime.time_ns() utime.sleep_us(1000) t1 = utime.time_ns() # Check that time_ns increases. print(t0 < t1) # Check that time_ns counts correctly, but be very lenient with the upper bound (50ms). if 950000 < t1 - t0 < 50000000: print(True) else: print(t0, t1, t1 - t0)
def game(right_pin, left_pin): import random random.seed(utime.time()) # display lcd = LCD(22,21) width, height = 20,4 #symbols: lcd.custom_char(0, [0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F]) # chr(0) - body lcd.custom_char(1, [0x04, 0x08, 0x04, 0x0E, 0x11, 0x11, 0x11, 0x0E]) # chr(1) - apple # game settings MENU, PLAY, LOSE, WIN = 0,1,2,3 game_state = MENU m = [(1,0), (0,-1),(-1,0),(0,1)] # RIGHT, DOWN, LEFT, UP md = 0 # m[md] now = 0 # timer # Меняя значение переменной refresh_rate можно менять скорость игры (меньше значение - выше скорость) # Так же в игру можно добавить уровни сложности, меняя скорость по мере увеличения длины змейки refresh_time = 500000000 # nanosecond (1sec = 1.000.000.000 nsec) RpushFlag,LpushFlag = False,False xd,yd = 0,0 xA,yA = 0,0 # snake settings lenS = [[0,0]] # start position of snake, len = 1 xHead,yHead = lenS[-1] # head coordinate xEnd,yEnd = lenS[0] # tail coordinate # apple settings newApple = True def snake_move(): nonlocal m, md, lenS, width, height, xHead, yHead, xd, yd, xEnd, yEnd xEnd,yEnd = lenS[0] for i in range(len(lenS)-1): lenS[i] = lenS[i+1] xd,yd = m[md] xHead,yHead = lenS[-1] xHead += xd if xHead > width-1: xHead = 0 elif xHead < 0: xHead = width-1 yHead += yd if yHead > height-1: yHead = 0 elif yHead < 0: yHead = height-1 lenS[-1] = [xHead,yHead] lcd.move_to(xEnd,yEnd) lcd.putchar(' ') for xS,yS in lenS: lcd.move_to(xS,yS) lcd.putchar(chr(0)) def apple(): nonlocal lenS, xA, yA, newApple while True: if newApple == True: newApple = False xA, yA = random.randint(0,width-1), random.randint(0,height-1) if [xA,yA] not in lenS: break else: newApple = True lcd.move_to(xA,yA) lcd.putchar(chr(1)) def button_check(right_pin, left_pin): nonlocal md, RpushFlag, LpushFlag if button(right_pin) == 1 and RpushFlag == False: md += 1 if md > 3: md = 0 elif button(left_pin) == 1 and LpushFlag == False: md -= 1 if md < 0: md = 3 RpushFlag = button(right_pin) LpushFlag = button(left_pin) # !!!!!!! # на выходе состояние флага будет True, значит для # смены его состояния надо пройти цикл ещё раз. Если # в это время кнопка будет нажата, то флаг будет True # и нажатие будет проигнорировано. Решить этот момент # !!!!!!! def check_pos(): nonlocal xHead, yHead, lenS, game_state for i in range(len(lenS)-1): xS,yS = lenS[i] if [xHead,yHead] == [xS,yS]: game_state = LOSE # Начало игры while True: if game_state == PLAY: button_check(right_pin, left_pin) # если змейка имеет максимальную длину if len(lenS) == 79: game_state = WIN # функция создания яблока if [xA,yA] == [xHead,yHead]: lenS.insert(0, [xEnd-xd, yEnd-yd]) newApple = True apple() # обновление экрана if now + refresh_time < utime.time_ns(): snake_move() check_pos() now = utime.time_ns() elif game_state == LOSE: game_state = MENU lcd.clear() lcd.move_to(5,0) lcd.putstr('Game over!') lcd.move_to(0,2) lcd.putstr('Your score: ') lcd.putstr(str(len(lenS))) utime.sleep_ms(3000) elif game_state == WIN: game_state = MENU lcd.clear() lcd.move_to(5,0) lcd.putstr('You win!') lcd.move_to(2,2) lcd.putstr('Your score: ') lcd.putstr(str(len(lenS))) utime.sleep_ms(3000) else: lcd.clear() lcd.move_to(2,1) lcd.putstr('Snake: The Game') lcd.move_to(2,3) lcd.putstr('Push any button') while True: if button(right_pin) == 1 or button(left_pin) == 1: game_state = PLAY now = utime.time_ns() lenS = [[0,0]] xHead,yHead = lenS[-1] newApple = True lcd.clear() apple() lcd.move_to(xHead,yHead) lcd.putchar(chr(0)) break