def sleep(self): print( "Machine entering sleep mode" ) #Used for debugging: prints statement before the PyBoard enters sleep mode self.myExtInt.enable( ) #Enables the external interrupt to ensure the PyBoard can be woken up if the external interrupt is triggered on pin 'X1' pyb.stop( ) # Puts the PyBoard into standby mode, see Pyb.stop() documentation from Micropython for more information about power consumption, etc.
def lpdelay(ms): # Low power delay. Note stop() kills USB global usb_connected rtc = pyb.RTC() if usb_connected: pyb.delay(ms) return rtc.wakeup(ms) pyb.stop() rtc.wakeup(None)
def lpdelay(ms): global usb_connected rtc = pyb.RTC() if usb_connected: pyb.delay(ms) return rtc.wakeup(ms) pyb.stop() rtc.wakeup(None)
def loop(self): gps_time = utime.ticks_ms() while True: # Check if new messages arrived after shutdown if self.shutdown and not self.can.any(0): pyb.stop() # soft sleep (500 uA) continue elif self.shutdown and self.can.any(0): self.shutdown = False self.gsm_sleep.value(0) self.sendGPSCmd(self.GPS_ON) # Main loop if not self.interrupt: # Free memory # gc.collect() ## Logging mode ## # Only log gps once a few seconds if utime.ticks_ms() - gps_time >= self.GPS_LOG_TIME: gps_time = utime.ticks_ms() # if module retrieved data: update and log if self.gps_uart.any(): self.gps.updateall(self.gps_uart.read()) self.log(str(self.rtc.datetime()), self.gps.latitude_string(), self.gps.longitude_string(), self.gps.speed_string()) # Log new incoming can messages try: # throws OSError can_id, _, _, can_data = self.can.recv( 0, timeout=self.SHUTOFF_TIME) # Filter for CAN Log if not self.can_filter or can_id in self.can_filter: self.log(str(self.rtc.datetime()), str(can_id), binascii.hexlify(can_data).decode('utf-8')) except OSError: # We timed out from can connection -> could mean car is shut down self.shutdown = True self.CAN_FILE.close() os.sync() self.gsm_sleep.value(1) self.sendGPSCmd(self.GPS_OFF) continue else: ## Attack mode ## self.CAN_FILE.close() # Close log file first os.sync() while self.interrupt: self.telegram.listen(self.message_handler)
def sleep(cause=""): """ Put modules to sleep """ print("Sleep time.") if cause: print("Cause: %s" %cause) # Set GSM slow clock to 1 #gsm.set_slow_clock() #gsm.sleep() gsm.stop() if gps.nofix: # Module running gps.sleep() pyb.stop() # Remember to enable RTC wakeup
def _run(self): rtc = pyb.RTC() rtc.wakeup(self._t_ms) t_ms = self._t_ms while True: if t_ms > 0: pyb.stop() yield if t_ms != self._t_ms: t_ms = self._t_ms if t_ms > 0: rtc.wakeup(t_ms) else: rtc.wakeup(None)
def _run(self): print('Low power mode is ON.') rtc = pyb.RTC() rtc.wakeup(self._t_ms) t_ms = self._t_ms while True: if t_ms > 0: pyb.stop() # Pending tasks run once, may change self._t_ms yield if t_ms != self._t_ms: # Has changed: update wakeup t_ms = self._t_ms if t_ms > 0: rtc.wakeup(t_ms) else: rtc.wakeup(None)
def go_sleep(self, interval): """Puts board in sleep mode. Params: now(int): current timestamp wakeup(int): wakeup timestamp """ self.sleep_led() self.enable_interrupts() remain = constants.WD_TIMEOUT - (utime.time() - self.lastfeed) * 1000 interval = interval * 1000 if interval - remain > -3000: interval = remain - 3000 self.rtc.wakeup(interval) # Set next rtc wakeup (ms). pyb.stop() self.pwr_led()
OpenMV @date: 2018.05.10 ''' # Quick reference for the openmvcam ''' 1.1 General board control ''' import pyb pyb.repl_uart(pyb.UART(3, 9600, timeout_char=1000)) # duplicate REPL on UART(3) pyb.wfi() # pause CPU, waiting for interrupt pyb.stop() # stop CPU, waiting for external interrupt ''' 1.2 Delay and timing ''' ''' 图像处理背景知识 ''' # ref: http://book.openmv.cc/ # https://blog.csdn.net/HZ_CloudRiver/article/details/78177307?locationNum=6&fps=1#t1
def main(v_flip = False, h_flip = False): global PIR_flag ## The 9 inch board has X19/X20 swapped. For this board, use the alternative code if TFT_SIZE == 9: adc = pyb.ADC(pyb.Pin.board.X19) # read battery voltage (large PCB) else: adc = pyb.ADC(pyb.Pin.board.X20) # read battery voltage vbus = pyb.Pin('USB_VBUS') if vbus.value(): USBSUPPLY = True else: USBSUPPLY = False usb = pyb.USB_VCP() batval = adc.read() if (TOOLOWBAT < batval < LOWBAT) and USBSUPPLY == False: # low battery, switch off while True: # stay there until reset pyb.stop() # go to sleep. Only the PIR Sensor may wake me up # Battery OK, or suppy by USB, start TFT. if TFT_SIZE == 7: mytft = tft.TFT("SSD1963", "AT070TN92", tft.PORTRAIT, v_flip, h_flip) elif TFT_SIZE == 4: mytft = tft.TFT("SSD1963", "LB04301", tft.LANDSCAPE, v_flip, h_flip) elif TFT_SIZE == 9: mytft = tft.TFT("SSD1963", "AT090TN10", tft.PORTRAIT, False, True) mytft.clrSCR() width, height = mytft.getScreensize() if TFT_SIZE == 9: ## The 9 inch board has X19/X20 swapped. For this board, use the alternative code extint = pyb.ExtInt(pyb.Pin.board.X20, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_DOWN, callback) ## large PCB else: extint = pyb.ExtInt(pyb.Pin.board.X19, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE, callback) extint.enable() files, has_banner, shuffle = get_files("/sd/serie", "/sd/zufall") start = COUNTER # reset timer once PIR_flag = False file_index = 1 # 1: do not start with banner, 0: Start with banner while True: TESTMODE = usb.isconnected() # On USB, run test mode # on every series start create a random shuffle if file_index == 0 and shuffle == True: files = list_shuffle(files) name = files[file_index] file_index = (file_index + 1) % len(files) ## on USB supply assume good battery if USBSUPPLY == False: batval = adc.read() else: batval = WARNBAT + 1 if TESTMODE: print("Battery: ", batval, ", Files: ", len(files), ", File: ", name) ## test for low battery, switch off if (TOOLOWBAT < batval < LOWBAT) and USBSUPPLY == False: tft_standby(mytft) while True: # stay there until reset pyb.stop() if (file_index % BANNER_COUNTER) == 1 and has_banner == True: displayfile(mytft, BANNER_NAME, width, height) display_batlevel(mytft, batval) pyb.delay(BANNER_TIME) if displayfile(mytft, name, width, height): display_batlevel(mytft, batval) pyb.delay(PICT_TIME) if PIR_flag == False: ## For one picture activity, check inactivity counter start -= 1 if start <= 0: # no activity, long enough if TESTMODE == False: tft_standby(mytft) # switch TFT off and ports inactive pyb.delay(200) pyb.stop() # go to sleep Only PIR Sensor may wake me up pyb.hard_reset() # will do all the re-init else: print("Should switch off here for a second") mytft.clrSCR() mytft.setTextStyle((255,255,255), None, 0, font14) mytft.setTextPos(0, 0) pyb.delay(100) batval = adc.read() mytft.printString("{:.3}V - {}".format(((batval * 3.3 * SCALING) / 4096) + OFFSET, file_index)) mytft.printNewline() mytft.printCR() mytft.printString("Should switch off here for a second") pyb.delay(3000) PIR_flag = False start = COUNTER # reset timer else: # activity. restart counter PIR_flag = False start = COUNTER # reset timer
if period == 0: if current==home: update() elif current == settings: settings.set_memfree(str(gc.mem_free())) period = (period + 1) % 150 if t: count = 300 else: count = count - 1 if count<=0 and not current == incoming and not current == call: #sleep lcd.set_power(0) phone.sleep(2) if not usb.isconnected(): pyb.stop() # wait for button or incoming call else: wokenby = 0 while wokenby == 0: pyb.delay(100) #wakeup count = 300 period = 145 lcd.set_power(1) lcd.set_orient(lcd160cr.PORTRAIT) lcd.set_brightness(bright_level) if wokenby ==1: phone.wakechars() phone.sleep(0) current.draw() wokenby==0
def main(v_flip = False, h_flip = False): global PIR_flag ## The 9 inch board has X19/X20 swapped. For this board, use the alternative code if TFT_SIZE == 9: adc = pyb.ADC(pyb.Pin.board.X19) # read battery voltage (large PCB) else: adc = pyb.ADC(pyb.Pin.board.X20) # read battery voltage vbus = pyb.Pin('USB_VBUS') if vbus.value(): USBSUPPLY = True else: USBSUPPLY = False usb = pyb.USB_VCP() batval = adc.read() if (TOOLOWBAT < batval < LOWBAT) and USBSUPPLY == False: # low battery, switch off while True: # stay there until reset pyb.stop() # go to sleep. Only the PIR Sensor may wake me up # Battery OK, or suppy by USB, start TFT. if TFT_SIZE == 7: mytft = tft.TFT("SSD1963", "AT070TN92", tft.PORTRAIT, v_flip, h_flip) elif TFT_SIZE == 4: mytft = tft.TFT("SSD1963", "LB04301", tft.LANDSCAPE, v_flip, h_flip) elif TFT_SIZE == 9: mytft = tft.TFT("SSD1963", "AT090TN10", tft.PORTRAIT, False, True) mytft.clrSCR() width, height = mytft.getScreensize() if TFT_SIZE == 9: ## The 9 inch board has X19/X20 swapped. For this board, use the alternative code extint = pyb.ExtInt(pyb.Pin.board.X20, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE, callback) ## large PCB else: extint = pyb.ExtInt(pyb.Pin.board.X19, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE, callback) extint.enable() total_cnt = 0 if TFT_SIZE == 4: os.chdir("img_272x480") else: os.chdir("img_480x800") files = os.listdir(".") start = COUNTER # reset timer once PIR_flag = False while True: TESTMODE = usb.isconnected() # On USB, run test mode while True: # get the next file name, but not banner.bmp myrng = pyb.rng() name = files[myrng % len(files)] if name != BANNER_NAME: break batval = adc.read() if TESTMODE: print("Battery: ", batval, ", Files: ", len(files), ", File: ", name, ", RNG: ", myrng) # test for low battery, switch off, or total count reached if (TOOLOWBAT < batval < LOWBAT) and USBSUPPLY == False: tft_standby(mytft) while True: # stay there until reset pyb.stop() if (total_cnt % BANNER_COUNTER) == 0: displayfile(mytft, BANNER_NAME, width, height) mytft.setTextPos(0, 0) if LOWBAT <= batval < WARNBAT: textcolor = (255,255,0) elif TOOLOWBAT < batval < LOWBAT: textcolor = (255,0,0) else: textcolor = (0,255,0) mytft.setTextStyle(textcolor, None, 0, font14) mytft.printString("{:.3}V - {} - {}".format(((batval * 3.3 * SCALING) / 4096) + OFFSET, batval, total_cnt)) pyb.delay(BANNER_TIME) total_cnt += 1 displayfile(mytft, name, width, height) pyb.delay(PICT_TIME) if PIR_flag == False: ## For one picture activity, check inactivity counter start -= 1 if start <= 0 or total_cnt > TOTAL_COUNTER: # no activity, long enough if TESTMODE == False: tft_standby(mytft) # switch TFT off and ports inactive pyb.delay(200) pyb.stop() # go to sleep Only PIR Sensor may wake me up pyb.hard_reset() # will do all the re-init else: print("Should switch off here for a second") mytft.clrSCR() mytft.setTextStyle((255,255,255), None, 0, font14) mytft.setTextPos(0, 0) pyb.delay(100) batval = adc.read() mytft.printString("{:.3}V - {}".format(((batval * 3.3 * SCALING) / 4096) + OFFSET, total_cnt)) mytft.printNewline() mytft.printCR() mytft.printString("Should switch off here for a second") pyb.delay(3000) if total_cnt > TOTAL_COUNTER: total_cnt = 0 PIR_flag = False start = COUNTER # reset timer else: # activity. restart counter PIR_flag = False start = COUNTER # reset timer
if period == 0: if current == home: update() elif current == settings: settings.set_memfree(str(gc.mem_free())) period = (period + 1) % 150 if t: count = 300 else: count = count - 1 if count <= 0 and not current == incoming and not current == call: #sleep lcd.set_power(0) phone.sleep(2) if not usb.isconnected(): pyb.stop() # wait for button or incoming call else: wokenby = 0 while wokenby == 0: pyb.delay(100) #wakeup count = 300 period = 145 lcd.set_power(1) lcd.set_orient(lcd160cr.PORTRAIT) lcd.set_brightness(bright_level) if wokenby == 1: phone.wakechars() phone.sleep(0) current.draw() wokenby == 0
def do_stop(): # stop the MCU pyb.stop() # woken; clear interrupt flags stm.mem32[stm.RTC + stm.RTC_ISR] &= ~(1 << 10) stm.mem32[stm.EXTI + stm.EXTI_PR] = 1 << 22
def enterLowPowerMode(): #must configure wakeup sources first pyb.stop() return
def main(): # Startup Load Configuration app_cfg = load_app_config() if not app_cfg: app_cfg = default_app_config() save_app_config(app_cfg) # LEDs for indicator led_red = pyb.LED(1) led_green = pyb.LED(2) led_blue = pyb.LED(3) # https://forum.micropython.org/viewtopic.php?t=6222 #usb_connected = pyb.USB_VCP().isconnected() #if usb_connected: # led_green.on() #else: # led_green.off() uart = machine.UART(1, 9600, bits=8, parity=None, stop=1, timeout=1000) nm3_modem = Nm3(uart) nm3_network = Nm3NetworkSimple(nm3_modem) temperature_adc = pyb.ADC(pyb.Pin.board.X1) light_adc = pyb.ADC(pyb.Pin.board.X2) # Set RTC to wakeup at a set interval rtc = pyb.RTC() rtc.init() # reinitialise - various bugs in firmware at present rtc.wakeup(app_cfg['sensing']['period_s'] * 1000) # milliseconds message_count = 0 while True: # Main Loop # LED On led_red.on() # 1. Power up external 3V3 regulator pyb.Pin.board.EN_3V3.on() # 2. Take sensor readings on ADC temperature_val = temperature_adc.read() light_val = light_adc.read() # 3. Build nm3 message message_string = 'Count=' + '{:04d}'.format( message_count) + ' Temperature=' + '{:04d}'.format( temperature_val) + ' Light=' + '{:04d}'.format(light_val) message_bytes = message_string.encode('utf-8') message_count = message_count + 1 # 4. Reinitialise the UART uart.init(baudrate=9600, bits=8, parity=None, stop=1, timeout=1000) # 5a. Send nm3 message - Unicast #nm3_modem.send_unicast_message(007, message_bytes) # 5b. Send nm3 message - Unicast with Ack plus retry #response_time = nm3_modem.send_unicast_message_with_ack(007, message_bytes) #retries = 3 #while response_time < 0 and retries > 0: # retries = retries - 1 # response_time = nm3_modem.send_unicast_message_with_ack(007, message_bytes) # 5c. Send nm3 message with network nm3_network.send_message(app_cfg['network']['gateway'], message_bytes) # 6. Power down external 3V3 regulator pyb.Pin.board.EN_3V3.off() # 7. # Power down UART uart.deinit() # deinit the bus # LED On led_red.off() # 8a. Light Sleep pyb.stop()
def log(t): """ A function for logging data to file at a regular interval. The function saves a line of text at each interval representing conductivity, temperature, pressure. The device then goes into standby mode for interval t. After interval t, the device awakes as if from a hard reset. The function is designed to be called from main.py. Puts device in standby mode upon completion. Parameters ---------- t: int logging interval (seconds) Example ------- To log data at 30 second intervals, save the following two lines as main.py >>> import logger >>> logger.log(30) Note ---- For more robust logging, the lines related to the watchdog timer can be enabled by removing the comments. However, once the watchdog timer is set, it will automatically reset the board after the watchdog timer interval unless the timer is fed. This ensures that the board will continue to log even if an error is encountered, but it can cause unexpected results if the user wishes to interact with the board over serial. """ #from machine import WDT #wdt = machine.WDT(timeout=30000) while True: #keep track of elapsed time start_time = time.time() log_time = start_time + t try: wdt.feed() except: pass #define clock object, set next wakeup time (in milliseconds) and get the time rtc = pyb.RTC() datetime = rtc.datetime() #light up LED to let user know it is on led = pyb.LED(2) led.on() #wait 15 seconds to allow user to jump in time.sleep(5) #flash LED to let user know reading is being taken led.off() time.sleep(0.5) led.on() #define clock object, set next wakeup time (in milliseconds) and get the time rtc = pyb.RTC() datetime = rtc.datetime() #define conductivity sensor gpio1 = Pin('X3', Pin.OUT_PP) #connected directly to electrode gpio2 = Pin('X4', Pin.OUT_PP) #connected to resistor connected to electrode adc1 = ADC('X5') #connected to middle pole not adjacent to adc2 = ADC('X6') #connected to middle pole adjacent to resistor adc3_current = ADC('X7') adc4_therm = ADC('X8') con_resistance = 250 therm_resistance = 20000 cell_const = 1 #run external calibration to get A b = 0 #run external calibration to get B t_1 = gpio1 t_2 = gpio2 conductivity_sensor = conductivity4pole.cond_sensor(gpio1,gpio2,adc1,adc2,adc3_current,adc4_therm,con_resistance,therm_resistance,cell_const,b,t_1,t_2) try: wdt.feed() except: pass #define pressure sensor in Pressure.py. Connect SCL to X9, SDA to X10, VCC to Y7, GND to Y8 pres_power = Pin('Y7', Pin.OUT_PP) pres_gnd = Pin('Y8', Pin.OUT_PP) i2c = machine.I2C(scl='X9', sda='X10', freq = 100000) #write header in textfile #headerline = 'YY/MM/DD,Hour:Min:Sec,count1,count2,r1,r2,temp,pressure\r\n' #f = open('datalogDuw.txt','a') #f.write(headerline) #f.close() #read values from sensors try: [r1, r2, T, k, icount1, probe3count1, probe4count1, icount2, probe3count2, probe4count2] = conductivity_sensor.measure() except: [r1, r2, T, k, icount1, probe3count1, probe4count1, icount2, probe3count2, probe4count2] = [-999,-999,-999,-999,-999,-999,-999,-999,-999,-999] try: [pres, ctemp] = pressure.MS5803(i2c, pres_power, pres_gnd) except: pres = -999 ctemp = -999 print('Pressure reading failed') try: wdt.feed() except: pass #write results to file outputtxt = ('%s/%s/%s,%s:%s:%s,' % (datetime[0], datetime[1], datetime[2], datetime[4], datetime[5], datetime[6])) outputtxt += ('%5.2f,%5.2f,' % (r1, r2)) outputtxt += ('%s,%s,' % (ctemp, pres)) #outputtxt += ('%s, %s, %s, %s, %s, %s,' % (icount1, probe3count1, probe4count1, icount2, probe3count2, probe4count2)) outputtxt += ('%s\r\n' % T) print (outputtxt) try: f = open('datalogCTD.txt','a') f.write(outputtxt) f.close() except: led1 = pyb.LED(1) led1.on() led2.on() led3 = pyb.LED(3) led.on() led4 = pyb.LED(4) led.on() time.sleep(1) sw = pyb.Switch() while time.time() < log_time: try: wdt.feed() except: pass led.on() time.sleep(0.005) led.off() if sw(): pyb.usb_mode(None) pyb.LED(3).on() time.sleep(5) pyb.usb_mode('VCP+MSC') try: wdt.feed() except: pass break rtc.wakeup(2000) pyb.stop()
def log(t): """ A function for logging data from an MS5803 sensor to file at a regular interval. The function saves a line of text at each interval representing conductivity, temperature, pressure. The device then goes into standby mode for interval t. After interval t, the device awakes as if from a hard reset. The function is designed to be called from main.py. Puts device in standby mode upon completion. Parameters ---------- t: int logging interval (seconds) Example ------- To log data at 30 second intervals, save the following two lines as main.py >>> import logger_pres_temp as logger >>> logger.log(30) Note ---- For more robust logging, the lines related to the watchdog timer can be enabled by removing the comments. However, once the watchdog timer is set, it will automatically reset the board after the watchdog timer interval unless the timer is fed. This ensures that the board will continue to log even if an error is encountered, but it can cause unexpected results if the user wishes to interact with the board over serial. """ #from machine import WDT #wdt = machine.WDT(timeout=30000) red = pyb.LED(1) green = pyb.LED(2) yellow = pyb.LED(3) blue = pyb.LED(4) green.on() blue.on() #wait 10 seconds to allow user to jump in time.sleep(10) green.off() blue.off() #write file header outputtxt = 'date,time,pressure(mbar),temperature(C)\r\n' try: f = open('datalogCTD.txt','a') f.write(outputtxt) f.close() except: #briefly turn all leds on if fails to write red.on() green.on() yellow.on() blue.on() time.sleep(1) red.off() green.off() blue.off() yellow.off() start_time = time.time() while True: try: wdt.feed() except: pass #flash LED to let user know reading is being taken green.off() time.sleep(0.5) green.on() #define clock object, set next wakeup time (in milliseconds) and get the time rtc = pyb.RTC() datetime = rtc.datetime() log_time = start_time + t start_time = log_time try: wdt.feed() except: pass #define pressure sensor in Pressure.py. Connect SCL to X9, SDA to X10, VCC to Y7, GND to Y8 pres_power = Pin('Y7', Pin.OUT_PP) pres_gnd = Pin('Y8', Pin.OUT_PP) i2c = machine.I2C(scl='Y10', sda='Y9', freq = 100000) #write header in textfile #headerline = 'YY/MM/DD,Hour:Min:Sec,count1,count2,r1,r2,temp,pressure\r\n' #f = open('datalogDuw.txt','a') #f.write(headerline) #f.close() #read values from sensors try: [pres, ctemp] = pressure.MS5803(i2c, pres_power, pres_gnd) except: pres = -999 ctemp = -999 print('Pressure reading failed') yellow.on() time.sleep(1) yellow.off() try: wdt.feed() except: pass #write results to file outputtxt = ('%s/%s/%s,%s:%s:%s,' % (datetime[0], datetime[1], datetime[2], datetime[4], datetime[5], datetime[6])) outputtxt += ('%s,%s\r\n' % (pres, ctemp)) print (outputtxt) try: f = open('datalogCTD.txt','a') f.write(outputtxt) f.close() except: #briefly turn all leds on if fails to write red.on() green.on() yellow.on() blue.on() time.sleep(1) red.off() green.off() blue.off() yellow.off() sw = pyb.Switch() while time.time() < log_time: try: wdt.feed() except: pass green.on() time.sleep(0.005) green.off() if sw(): pyb.usb_mode(None) yellow.on() time.sleep(5) pyb.usb_mode('VCP+MSC') try: wdt.feed() except: pass break rtc.wakeup(2000) pyb.stop()
def sleep(self): # print("Machine entering sleep mode") # self.myExtIntMicroswitch.enable() pyb.stop()
MicroPython Documentation @date: 2017.11.20 ''' ''' 1.1 General board control ''' import pyb pyb.repl_uart(pyb.UART(1, 9600)) pyb.wfi() pyb.freq() pyb.freq(60000000) pyb.stop() ''' 1.2 Delay and timing ''' import time time.sleep(1) # sleep for 1 second time.sleep_ms(500) time.sleep_us(10) start = time.ticks_ms() delta = time.ticks_diff(time.ticks_ms() - start) ''' 1.3 LED ''' from pyb import LED
def main(v_flip = False, h_flip = False): global PIR_flag ## The 9 inch board has X19/X20 swapped. For this board, use the alternative code if TFT_SIZE == 9: adc = pyb.ADC(pyb.Pin.board.X19) # read battery voltage (large PCB) else: adc = pyb.ADC(pyb.Pin.board.X20) # read battery voltage vbus = pyb.Pin('USB_VBUS') if vbus.value(): USBSUPPLY = True else: USBSUPPLY = False usb = pyb.USB_VCP() batval = adc.read() if (TOOLOWBAT < batval < LOWBAT) and USBSUPPLY == False: # low battery, switch off while True: # stay there until reset pyb.stop() # go to sleep. Only the PIR Sensor may wake me up # Battery OK, or suppy by USB, start TFT. if TFT_SIZE == 7: mytft = tft.TFT("SSD1963", "AT070TN92", tft.PORTRAIT, v_flip, h_flip) elif TFT_SIZE == 4: mytft = tft.TFT("SSD1963", "LB04301", tft.LANDSCAPE, v_flip, h_flip) elif TFT_SIZE == 9: mytft = tft.TFT("SSD1963", "AT090TN10", tft.PORTRAIT, False, True) mytft.clrSCR() width, height = mytft.getScreensize() if TFT_SIZE == 9: ## The 9 inch board has X19/X20 swapped. For this board, use the alternative code extint = pyb.ExtInt(pyb.Pin.board.X20, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_DOWN, callback) ## large PCB else: extint = pyb.ExtInt(pyb.Pin.board.X19, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE, callback) extint.enable() files, has_banner, shuffle = get_files("/sd/serie", "/sd/zufall") start = COUNTER # reset timer once PIR_flag = False file_index = 0 while True: TESTMODE = usb.isconnected() # On USB, run test mode # on every series start create a random shuffle if file_index == 0 and shuffle == True: files = list_shuffle(files) name = files[file_index] file_index = (file_index + 1) % len(files) ## on USB supply assume good battery if USBSUPPLY == False: batval = adc.read() else: batval = WARNBAT + 1 if TESTMODE: print("Battery: ", batval, ", Files: ", len(files), ", File: ", name) ## test for low battery, switch off if (TOOLOWBAT < batval < LOWBAT) and USBSUPPLY == False: tft_standby(mytft) while True: # stay there until reset pyb.stop() if (file_index % BANNER_COUNTER) == 1 and has_banner == True: displayfile(mytft, BANNER_NAME, width, height) display_batlevel(mytft, batval) pyb.delay(BANNER_TIME) if displayfile(mytft, name, width, height): display_batlevel(mytft, batval) pyb.delay(PICT_TIME) if PIR_flag == False: ## For one picture activity, check inactivity counter start -= 1 if start <= 0: # no activity, long enough if TESTMODE == False: tft_standby(mytft) # switch TFT off and ports inactive pyb.delay(200) pyb.stop() # go to sleep Only PIR Sensor may wake me up pyb.hard_reset() # will do all the re-init else: print("Should switch off here for a second") mytft.clrSCR() mytft.setTextStyle((255,255,255), None, 0, font14) mytft.setTextPos(0, 0) pyb.delay(100) batval = adc.read() mytft.printString("{:.3}V - {}".format(((batval * 3.3 * SCALING) / 4096) + OFFSET, file_index)) mytft.printNewline() mytft.printCR() mytft.printString("Should switch off here for a second") pyb.delay(3000) PIR_flag = False start = COUNTER # reset timer else: # activity. restart counter PIR_flag = False start = COUNTER # reset timer
def dostop(): while True: # LPDS disable regulator, disable ADC DAC ? pyb.stop() # pin interrupt or RTC could wake
def sleep(self): print("Machine entering sleep mode") self.myExtInt.enable() pyb.stop()