def __init__(self): """Init""" machine.WDT(True) self.tft = self.initDisplay() self.tft.clear() self.encoder_state_machine = RotaryIRQ(25, 26, min_val=0, max_val=2, reverse=True, range_mode=Rotary.RANGE_WRAP) self.encoder_grinder_time = None self.run = True self.update_display = True self.print_s = 0.0 self.encoder_value = 0 self.state = 0 self.state_old = 0 self.edit_state = False self.cup = Cup() self.single_sec = machine.nvs_getint("single_sec") self.double_sec = machine.nvs_getint("double_sec") self.cps = machine.nvs_getint("cps") self.seconds = 0.1 self.edit_cps = False self.pin_start = machine.Pin(33, mode=machine.Pin.IN, pull=machine.Pin.PULL_DOWN, handler=self.__startGrinding, trigger=machine.Pin.IRQ_HILEVEL, acttime=0, debounce=500000) self.pin_menu = machine.Pin(27, mode=machine.Pin.IN, pull=machine.Pin.PULL_DOWN, handler=self.setCPS, trigger=machine.Pin.IRQ_FALLING, acttime=0, debounce=500000) self.pin_out = machine.Pin(32, mode=machine.Pin.INOUT) self.pin_out.value(False)
def onWiFiOpen(self): if(not machine.nvs_getint("wifiEnabled")): return menu = uui.MenuScreen(self, 4, self.onWiFiSelect) aps = self.wlan.scan() for ap in aps: menu.addEntry(ap[0]) self.setScreen(menu)
def __init__(self, parent): super().__init__(parent, 20, 4) self.wlan = network.WLAN(network.STA_IF) self.wifiEnable = self.add(uui.CheckBox(self, self.onWiFiChange, machine.nvs_getint("wifiEnabled"), format = b"Enabled [%s]")) self.wifiEnable.setPos(1, 0) self.wifiButton = self.add(uui.Button(self, b"WiFi: %s" % (wlanState.ssid or b"None"), self.onWiFiOpen), False) self.wifiButton.setPos(1, 1) self.ipstate = self.add(uui.TextPanel(self, b"IP: %s" % self.wlan.ifconfig()[0])) self.ipstate.setPos(1, 2) self.wifiStatus = self.add(uui.TextPanel(self))
def populate_apps(): global apps, current_index apps = [] try: userApps = os.listdir('apps') userApps.reverse() except OSError: userApps = [] for app in userApps: add_app(app, read_metadata(app)) add_app("snake", { "name": "Snake", "category": "system", "icon": icon_snake }) # add_app("activities", {"name": "Activities", "category": "system", "icon": icon_activities}) add_app("clock", { "name": "Clock", "category": "system", "icon": icon_clock }) add_app("nickname", { "name": "Nickname", "category": "system", "icon": icon_nickname }) add_app("appstore", { "name": "App store", "category": "system", "icon": icon_appstore }) add_app("setupwifi", { "name": "Set up wifi", "category": "system", "icon": icon_settings }) add_app("update", { "name": "Firmware update", "category": "system", "icon": icon_settings }) add_app("updateapps", { "name": "App updates", "category": "system", "icon": icon_settings }) current_index = machine.nvs_getint('launcher', 'index') or 0 if current_index >= len(apps): current_index = 0
def load_mode(): """TODO: Docstring for saved_mode. :returns: TODO """ print('Loading previous mode') if 'current_mode' in locals(): if current_mode: saved_mode = current_mode else: saved_mode = machine.nvs_getint('current_mode') return saved_mode else: return
def play(filename_or_url, volume=None, loop=False, sync_beat=None, start_at_next=None, on_finished=None): if volume is None: volume = machine.nvs_getint('system', 'volume') or 255 _start_audio_if_needed() channel_id = _add_channel(filename_or_url, on_finished) if channel_id is None or channel_id < 0: print('Failed to start audio channel') return channel_id sndmixer.volume(channel_id, volume) if loop: sndmixer.loop(channel_id, True) if sync_beat is not None and start_at_next is not None: sndmixer.start_beat_sync(sync_beat) sndmixer.start_at_next(channel_id, start_at_next) else: sndmixer.play(channel_id) return channel_id
def pushMapping(newMapping=None): ''' Push a new button mapping on the stack ''' global __cb if newMapping == None: newMapping = { BTN_UP: None, BTN_DOWN: None, BTN_LEFT: None, BTN_RIGHT: None, BTN_A: None, BTN_B: None, BTN_SELECT: None, BTN_START: None } if machine.nvs_getint("system", 'factory_checked'): newMapping[BTN_START] = __cbReboot __cb.append(newMapping)
def play(index): global global_playing, global_file, global_channels, global_filenames, MAX_FILES if index < 0 or index > MAX_FILES: print("Play: invalid index") if global_playing[index]: print("Play: already playing") return channel = sndmixer.mp3_stream(global_file[index]) if not channel: print("Play: invalid channel id") return try: volume = machine.nvs_getint("system", "volume") or 255 print("Volume:",volume) sndmixer.volume(channel, volume) sndmixer.play(channel) except: pass global_channels[index] = channel global_playing[index] = True
import machine, sys, system, time, os rtc = machine.RTC() rtc.write(0, 0) rtc.write(1, 0) sdPower = machine.Pin(19, machine.Pin.OUT) sdPower.value(True) time.sleep(0.05) os.mountsd() __chk_recovery = False if machine.nvs_getint("system", 'factory_checked'): try: import buttons try: #Use the START button if available recovery_button = buttons.BTN_START except: #Else use the B button recovery_button = buttons.BTN_B __chk_recovery = machine.wake_reason() == ( 7, 0) and buttons.value(recovery_button) except: pass #Application starting if __chk_recovery: app = "dashboard.recovery" elif machine.nvs_getint('system', 'force_sponsors'):
import esp, machine, sys, system, os esp.rtcmem_write(0, 0) esp.rtcmem_write(1, 0) #Application starting app = esp.rtcmem_read_string() if app: esp.rtcmem_write_string("") else: if not machine.nvs_getint("system", 'factory_checked'): app = "factory_checks" else: app = machine.nvs_getstr("system", 'default_app') #if not app: #This generic set of modules has no default app #app = 'dashboard.home' try: print("Starting app '%s'..." % app) system.__current_app__ = app if app: __import__(app) except BaseException as e: print("Fatal exception in the running app!") sys.print_exception(e)
import network, time, machine, consts _STA_IF = network.WLAN(network.STA_IF) _AP_IF = network.WLAN(network.AP_IF) _DEFAULT_TIMEOUT = machine.nvs_getint("system", "wifi.timeout") or 10 _DEFAULT_SSID = machine.nvs_getstr("system", "wifi.ssid") _DEFAULT_PASSWORD = machine.nvs_getstr("system", "wifi.password") if not _DEFAULT_SSID: _DEFAULT_SSID = consts.WIFI_SSID _DEFAULT_PASSWORD = consts.WIFI_PASSWORD # STATION MODE # ------------ def connect(*args): ''' Connect to a WiFi network :param ssid: optional, ssid of network to connect to :param password: optional, password of network to connect to ''' _STA_IF.active(True) if len(args) == 0: if _DEFAULT_PASSWORD: _STA_IF.connect(_DEFAULT_SSID, _DEFAULT_PASSWORD) else: _STA_IF.connect(_DEFAULT_SSID) elif len(args) == 1: _STA_IF.connect(args[0])
system.start = hijacked_start ## Make badge sleep in undervoltage conditions virtualtimers.activate(1000) # low resolution needed def _vcc_callback(): try: vcc = system.get_vcc_bat() if vcc != None: if vcc < 3300: __import__('deepsleep') deepsleep.vcc_low() finally: # Return 10000 to start again in 10 seconds gc.collect() return 10000 virtualtimers.new(10000, _vcc_callback, hfpm=True) ## Dirty fix for upgrade path of existing CZ19 badges if machine.nvs_getint("system", 'factory_checked') == 1: machine.nvs_setint("system", 'factory_checked', 2) del folders, uos gc.collect() gc.mem_free()
def telnet(): user = machine.nvs_getstr("system", "telnet.user") or "micro" password = machine.nvs_getstr("system", "telnet.password") or "python" timeout = machine.nvs_getint("system", "telnet.timeout") or 300 network.telnet.start(user=user, password=password, timeout=timeout)
def get_last_updated(): last_updated = -1 return machine.nvs_getint('activities', 'lastUpdate') or last_updated
# Works with non-volatile storage (NVS) import machine def retrieve_int(integer) # Get saved integer key return machine.nvs_getint(integer) def retrieve_str(string) return machine.nvs_getstr(string)
buttons.register(defines.BTN_A, input_A) buttons.register(defines.BTN_B, input_B) buttons.register(defines.BTN_UP, input_up) buttons.register(defines.BTN_DOWN, input_down) buttons.register(defines.BTN_LEFT, input_left) buttons.register(defines.BTN_RIGHT, input_right) populate_apps() render_current_app() start() init_power_management() # Install CZ countdown app to replace activities app if not machine.nvs_getint('system', 'czcount_inst'): import uinterface if uinterface.connect_wifi(): import woezel uinterface.loading_text('Installing CZ20 countdown') if woezel.is_installed('campzone_2020_countdown') or woezel.install( 'campzone_2020_countdown'): machine.nvs_setint('system', 'czcount_inst', 1) system.reboot() else: rgb.clear() uinterface.skippabletext('Installation failed') render_current_app() menu = term_menu.UartMenu(deepsleep.start_sleeping, pm)
ulp_pulse_count = ULP_MEM_BASE + 0 ulp_debounce = ULP_MEM_BASE + 4 machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP) pin21 = machine.Pin(21, machine.Pin.OUT) print("\nLoading ULP...\n") binary = src_to_binary(source) ulp = machine.ULP() ulp.set_wakeup_period(0, 100) # use timer0, wakeup after ~100us ulp.load_binary(load_addr, binary) if machine.nvs_getint('pulse_count_nv') == None: machine.nvs_setint('pulse_count_nv', 0) init_count = machine.nvs_getint('pulse_count_nv') pulse_count = init_count # init pulse counts pulse_gen_count = init_count mem32[ulp_pulse_count] = init_count mem32[ulp_debounce] = 3 # 3 sample debounce ulp.run(entry_addr) print("\n\nPulse Count Test: \n----------------- \n") while True:
import machine, sys, system, time, os, buttons import _device as device rtc = machine.RTC() rtc.write(0, 0) rtc.write(1, 0) device.prepareForWakeup() __chk_recovery = False __chk_develop = False if machine.wake_reason() == (7, 0) and machine.nvs_getint( "system", 'factory_checked'): try: recovery_button = buttons.BTN_START #Use the START button if available except: recovery_button = buttons.BTN_B #Else use the B button __chk_recovery = buttons.value(recovery_button) __chk_develop = buttons.value(buttons.BTN_LEFT) if __chk_recovery: app = "dashboard.recovery" elif __chk_develop: app = "dev" else: app = rtc.read_string() if not app: if not machine.nvs_getint("system", 'factory_checked') == 2: app = "factory_checks" else:
def getbrightness(): return machine.nvs_getint('system', 'brightness')
import machine, sys, system, time rtc = machine.RTC() rtc.write(0, 0) rtc.write(1, 0) __chk_recovery = False fc_level = machine.nvs_getint("system", 'factory_checked') or 0 recovery_button = None if fc_level >= 3: try: import buttons try: #Use the START button if available recovery_button = buttons.BTN_START except: #Else use the B button recovery_button = buttons.BTN_B __chk_recovery = machine.wake_reason() == ( 7, 0) and buttons.value(recovery_button) except: pass #Application starting if __chk_recovery: app = "dashboard.recovery" else: app = rtc.read_string() if not app: if fc_level < 3:
def _get_last_updated(): last_updated = -1 return machine.nvs_getint('system', 'lastUpdate') or last_updated
import machine, display, time, system # SHA2017 "factory" tool # Does function as factory tool but also implements an upgrade path from our old firmware currentState = machine.nvs_getint('system', 'factory_checked') or 0 if currentState < 2: display.drawFill(0xFFFFFF) display.drawText(0, 0, "Welcome to the BADGE.TEAM platform firmware!", 0x000000, "7x5") display.drawText(0, 6, "Please wait while we're setting things up...", 0x000000, "7x5") display.flush() time.sleep(2) # Check if we have upgraded from a legacy firmware legacy_mpr0 = machine.nvs_get_u16("badge", "mpr121.base.0") if legacy_mpr0: display.drawFill(0xFFFFFF) display.drawText(0, 0, "Welcome to the BADGE.TEAM platform firmware!", 0x000000, "7x5") display.drawText(0, 6, "You have upgraded from an older firmware,", 0x000000, "7x5") display.drawText(0, 12, "now migrating your settings...", 0x000000, "7x5") display.flush() legacy_wifi_ssid = machine.nvs_getstr("badge", "wifi.ssid") legacy_wifi_password = machine.nvs_getstr("badge", "wifi.password") legacy_eink_type = machine.nvs_getstr("badge", "eink.dev.type")
import network, time, machine, consts sta_if = network.WLAN(network.STA_IF) timeout = machine.nvs_getint("system", "wifi.timeout") or 10 defaultSsid = machine.nvs_getstr("system", "wifi.ssid") defaultPassword = machine.nvs_getstr("system", "wifi.password") if not defaultSsid: defaultSsid = consts.WIFI_SSID defaultPassword = consts.WIFI_PASSWORD def connect(ssid=defaultSsid, password=defaultPassword): try: sta_if.active(True) if ssid and password: sta_if.connect(ssid, password) elif ssid: sta_if.connect(ssid) except BaseException as e: print("Error while connecting to WiFi!") print(e) def disconnect(): sta_if.disconnect() def status(): return sta_if.isconnected()