def main(): # Initialiser menuen test_menu = M5StickMenu() # Definer punkter til menuen punkt1 = MenuElement("1. Haender", tryk, [1]) punkt2 = MenuElement("2. Ben", tryk, [2]) punkt3 = MenuElement("3. Fingre", tryk, [3]) # Foej punkterne til menuen test_menu.add_element(punkt1) test_menu.add_element(punkt2) test_menu.add_element(punkt3) test_menu.start() while True: if btnA.wasPressed(): test_menu = test_menu.down() if btnB.wasPressed(): test_menu = test_menu.enter() time.sleep(0.01)
def env_meter_update(meter_mode): while True: # 次の秒までの差分(ミリ秒)を求めてスリープ time.sleep_ms(1000 - int(time.time() % 1 * 1000)) # 各メーターおよび時計の更新 localtime = time.localtime() localtime_str = '{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format( *localtime[:6]) # 時計の表示を更新 clock.update() try: # DHT12 から湿度を取得 dht12.measure() h = dht12.humidity() # BMP280 から気温、気圧を取得 t, p = bmp280.values except Exception as e: print( '{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format( *time.localtime()[:6]), ' Script Name: ', __name__) print('Exception: ', e) t, h, p = None, None, None # それぞれのメーターに取得値を表示 if meter_mode == 0 or meter_mode == 1: t_meter.update(t) if meter_mode == 0 or meter_mode == 2: h_meter.update(h) if meter_mode == 0 or meter_mode == 3: p_meter.update(p) # Ambientへの送信は1分間隔で行う。 # localtime[5](秒) == 0 の時に送信 if localtime[5] == 0 and t is not None: try: am.send({ 'created': localtime_str, 'd1': dht12.temperature(), 'd2': h, 'd3': t, 'd4': p }) except Exception as e: print( '{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format( *time.localtime()[:6]), ' Script Name: ', __name__) print('Ambient send error: ', e) gc.collect() # button A が押された時は表示を切り替える if btnA.wasPressed(): return
def display_7seg_test(string, dist, width, outline=True): """Seven segment N digit display test.""" lcd.setRotation(1) lcd.font(lcd.FONT_Default) lcd.print("7-segment", 0, 0) lcd.font(lcd.FONT_7seg) lcd.attrib7seg(dist, width, outline, lcd.GREEN) fs = lcd.fontSize() for x in range(0, 160, fs[0] + 2): lcd.rect(x, 80 - fs[1], fs[0], fs[1], lcd.MAGENTA) lcd.text(0, 80 - fs[1], string, lcd.RED) lcd.font(lcd.FONT_Default) lcd.print("\n{}".format(fs), 0, 0) counter = 0 while not btnA.wasPressed(): lcd.print("{:.2f} V".format(axp.getBatVoltage()), lcd.RIGHT, 0) lcd.print("{}".format(counter), lcd.RIGHT, lcd.fontSize()[1]) counter += 1 sleep_ms(500)
def env_meter_update(meter_mode): while True: # 次の秒までの差分(ミリ秒)を求めてスリープ time.sleep_ms(1000 - int(time.time() % 1 * 1000)) # 各メーターおよび時計の更新 localtime = time.localtime() localtime_str = '{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format( *localtime[:6]) # 時計の表示を更新 clock.update() try: # DHT12 から湿度を取得 dht12.measure() h = dht12.humidity() # BMP280 から気温、気圧を取得 t, p = bmp280.values except Exception as e: print( '{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format( *time.localtime()[:6]), ' Script Name: ', __name__) print('Exception: ', e) t, h, p = None, None, None # MH-Z19B からCO2濃度・気温を取得 mhz19_value = bytearray(9) mhz19.write(READ_CO2_CONCENTRATION) mhz19.readinto(mhz19_value, len(mhz19_value)) checksum = mhz19_checksum(mhz19_value) c = None if mhz19_value[0] == 0xff and mhz19_value[1] == 0x86 and mhz19_value[ 8] == checksum: # CO2濃度を取得 c = mhz19_value[2] * 256 + mhz19_value[3] # 気温を取得 mhz19_t = mhz19_value[4] - 40 else: mhz19_response(mhz19_value, checksum) # それぞれのメーターに取得値を表示 if meter_mode == 0 or meter_mode == 1: t_meter.update(t) if meter_mode == 0 or meter_mode == 2: h_meter.update(h) if meter_mode == 0 or meter_mode == 3: p_meter.update(p) if meter_mode == 0 or meter_mode == 4: c_meter.update(c) # Ambientへの送信は1分間隔で行う。 # localtime[5](秒) == 0 の時に送信 if localtime[5] == 0 and t is not None: try: if c is None: am.send({ 'created': localtime_str, 'd1': dht12.temperature(), 'd2': h, 'd3': t, 'd4': p }) else: am.send({ 'created': localtime_str, 'd1': dht12.temperature(), 'd2': h, 'd3': t, 'd4': p, 'd6': c, 'd7': mhz19_t }) except Exception as e: print( '{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format( *time.localtime()[:6]), ' Script Name: ', __name__) print('Ambient send error: ', e) gc.collect() # button A が押された時は表示を切り替える if btnA.wasPressed(): return # button C が押された時は MH-Z19B のゼロキャリブレーションを行う if btnC.wasPressed(): mhz19.write(ZERO_POINT_CALIBRATION) mhz19.readinto(mhz19_value, len(mhz19_value)) checksum = mhz19_checksum(mhz19_value) mhz19_response(mhz19_value, checksum)
from m5stack import btnA from m5stack import rtc import os # Sæt tidspunkt, læs evt. siden "Tid og dato", for at se hvordan # du finder tidspunktet via internettet rtc.setTime(2020, 10, 23, 15, 48, 52) filename = "data.csv" # Opret filen, hvis den ikke findes if filename not in os.listdir(): print("File {} doesn't exist, creating.".format(filename)) file = open("data.csv", "w") file.write("date;time\n") file.close() while True: if btnA.wasPressed(): # Hvad er klokken? year, month, day, hour, minute, second = rtc.now() # Formater tidspunktet i et standard format timestring = "{:02d}:{:02d}:{:02d}".format(hour, minute, second) print(timestring) # Skriv tidspunktet til .csv filen file = open("data.csv", "a") file.write(timestring + "\n") file.close()
def checkClose(self): self.reqClose = btnA.wasPressed()
from m5stack import btnA redled = Pin(10, Pin.OUT) tmr_running = False def button_was_pressed(): global redled global tmr_running, tmr_start if tmr_running: delta = time.ticks_diff(time.ticks_us(), tmr_start) tmr_running = False print(delta) else: tmr_start = time.ticks_us() tmr_running = True redled.value(0) def button_was_released(): global redled redled.value(1) btnA.wasPressed(button_was_pressed) btnA.wasReleased(button_was_released) lcd.setRotation(1) lcd.print("Experiment 1", 0, 0)