Skip to content
Snippets Groups Projects
Select Git revision
  • d4dfaa1dd088b6c0effe5c83e1a95aacc187b0c5
  • main default protected
  • blm_dev_chan
  • release/1.4.0 protected
  • widgets_draw
  • return_of_melodic_demo
  • task_cleanup
  • mixer2
  • dx/fb-save-restore
  • dx/dldldld
  • fpletz/flake
  • dx/jacksense-headset-mic-only
  • release/1.3.0 protected
  • fil3s-limit-filesize
  • allow-reloading-sunmenu
  • wifi-json-error-handling
  • app_text_viewer
  • shoegaze-fps
  • media_has_video_has_audio
  • fil3s-media
  • more-accurate-battery
  • v1.4.0
  • v1.3.0
  • v1.2.0
  • v1.2.0+rc1
  • v1.1.1
  • v1.1.0
  • v1.1.0+rc1
  • v1.0.0
  • v1.0.0+rc6
  • v1.0.0+rc5
  • v1.0.0+rc4
  • v1.0.0+rc3
  • v1.0.0+rc2
  • v1.0.0+rc1
35 results

__init__.py

Blame
  • ble.py 1.73 KiB
    import os
    import display
    import utime
    import buttons
    
    CONFIG_NAME = "ble.txt"
    ACTIVE_STRING = "active=true"
    INACTIVE_STRING = "active=false"
    
    
    def init():
        if CONFIG_NAME not in os.listdir("."):
            with open(CONFIG_NAME, "w") as f:
                f.write(INACTIVE_STRING)
    
    
    def triangle(disp, x, y, left):
        yf = 1 if left else -1
        scale = 6
        disp.line(x - scale * yf, int(y + scale / 2), x, y)
        disp.line(x, y, x, y + scale)
        disp.line(x, y + scale, x - scale * yf, y + int(scale / 2))
    
    
    def toggle():
        content = INACTIVE_STRING if is_active() else ACTIVE_STRING
        with open(CONFIG_NAME, "w") as f:
            f.write(content)
    
        disp.clear()
        disp.print("resetting", posy=0, fg=[0, 255, 255])
        disp.print("to toggle", posy=20, fg=[0, 255, 255])
        disp.print("BLE state", posy=40, fg=[0, 255, 255])
        disp.update()
        os.reset()
    
    
    def is_active():
        with open(CONFIG_NAME, "r") as f:
            state = f.readlines()[0]
            if len(state) < len(ACTIVE_STRING):
                return False
            state = state[0 : len(ACTIVE_STRING)]
            return state == ACTIVE_STRING
    
    
    def headline():
        disp.print("BLE", posy=0, fg=[0, 255, 255])
        if is_active():
            disp.print("active", posy=20, fg=[0, 255, 255])
        else:
            disp.print("inactive", posy=20, fg=[0, 255, 255])
    
    
    def selector():
        triangle(disp, 148, 46, False)
        disp.print("toggle", posx=25, posy=40, fg=[0, 255, 0])
    
    
    disp = display.open()
    button_pressed = True
    init()
    
    while True:
        disp.clear()
        headline()
        v = buttons.read(buttons.TOP_RIGHT)
        if v == 0:
            button_pressed = False
    
        if not button_pressed and v & buttons.TOP_RIGHT != 0:
            button_pressed = True
            toggle()
    
        selector()
        disp.update()
        utime.sleep(0.1)