Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adafruit Trinkey QT2040 support #217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# VSCode Settings
.vscode/

# MacOS Files
.DS_Store
44 changes: 40 additions & 4 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
# copyright (c) 2023 Dave Bailey
# Author: Dave Bailey (dbisu, @daveisu)
# Pico and Pico W board support
# Beta Trinkey QT2040 support

from board import *
import board
import digitalio
import storage

noStorage = False
noStoragePin = digitalio.DigitalInOut(GP15)
noStoragePin.switch_to_input(pull=digitalio.Pull.UP)
noStorageStatus = noStoragePin.value

# If GP15 is not connected, it will default to being pulled high (True)
# If GP is connected to GND, it will be low (False)
Expand All @@ -24,13 +21,52 @@
# GP15 not connected == USB NOT visible
# GP15 connected to GND == USB visible

# Trinkey QT2040:
# Boot button pressed during timeout == USB visible
# Boot button not pressed == USB not visible

if(board.board_id == 'raspberry_pi_pico'):
noStoragePin = digitalio.DigitalInOut(board.GP15)
noStoragePin.switch_to_input(pull=digitalio.Pull.UP)
noStorageStatus = noStoragePin.value
# On Pi Pico, default to USB visible
noStorage = not noStorageStatus
noStoragePin.deinit()
elif(board.board_id == 'raspberry_pi_pico_w'):
noStoragePin = digitalio.DigitalInOut(board.GP15)
noStoragePin.switch_to_input(pull=digitalio.Pull.UP)
noStorageStatus = noStoragePin.value
# on Pi Pico W, default to USB hidden by default
# so webapp can access storage
noStorage = noStorageStatus
noStoragePin.deinit()
elif (board.board_id == 'adafruit_qt2040_trinkey'):
# Import os to read the environment file
import os
# Import supervisor to wait for the timeout
import supervisor
# Listen for the BOOT button being pressed
button = digitalio.DigitalInOut(board.BUTTON)
# There is a hardware pullup on this pin, so no need to set it high
button.switch_to_input()
# Read the timeout from the environment file settings.toml. default to 500ms
wait_timeout = os.getenv('trinkey_storage_timeout', 500)
# Add the timeout to the current time to final timeout time
wait_timeout = supervisor.ticks_ms() + int(wait_timeout)
# Loop for the timeout seeing if the button is pressed
while supervisor.ticks_ms() < wait_timeout:
# By default the BOOT button is pulled high in hardware
# so button.value will be True unless the button is pressed
if not button.value:
print('Not disabling USB drive on Trinkey QT2040')
break
# If the loop completes that means the button was not pressed
else:
# And storage should be disabled
noStorage = True
# Deinit the BOOT button for later use
button.deinit()


if(noStorage == True):
# don't show USB drive to host PC
Expand Down
76 changes: 35 additions & 41 deletions code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,44 @@
# copyright (c) 2023 Dave Bailey
# Author: Dave Bailey (dbisu, @daveisu)
# Pico and Pico W board support

# Beta Trinkey QT2040 support

import supervisor


import time
import digitalio
from board import *
import board
from duckyinpython import *
if(board.board_id == 'raspberry_pi_pico_w'):
import wifi
from webapp import *

# Wait half a second to allow the device to be recognized by the host computer
wait_time = time.monotonic() + .5

# turn off automatically reloading when files are written to the pico
#supervisor.disable_autoreload()
supervisor.runtime.autoreload = False

payload = None
progStatus = False
progStatus = getProgrammingStatus()
print("progStatus", progStatus)
if(progStatus == False):
print("Finding payload")
# not in setup mode, inject the payload
payload = selectPayload()
else:
print("Update your payload")

# Wait for the half second timeout to expire before running the payload
while time.monotonic() < wait_time:
pass

# sleep at the start to allow the device to be recognized by the host computer
time.sleep(.5)
# If we are not in setup mode, run the payload
if progStatus == False:
print("Running ", payload)
runScript(payload)
print("Done")

def startWiFi():
import ipaddress
Expand All @@ -37,46 +58,19 @@ def startWiFi():
PORT = 80 # Port to listen on
print(HOST,PORT)

# turn off automatically reloading when files are written to the pico
#supervisor.disable_autoreload()
supervisor.runtime.autoreload = False

if(board.board_id == 'raspberry_pi_pico'):
led = pwmio.PWMOut(board.LED, frequency=5000, duty_cycle=0)
elif(board.board_id == 'raspberry_pi_pico_w'):
led = digitalio.DigitalInOut(board.LED)
led.switch_to_output()


progStatus = False
progStatus = getProgrammingStatus()
print("progStatus", progStatus)
if(progStatus == False):
print("Finding payload")
# not in setup mode, inject the payload
payload = selectPayload()
print("Running ", payload)
runScript(payload)

print("Done")
else:
print("Update your payload")

led_state = False

async def main_loop():
global led,button1

button_task = asyncio.create_task(monitor_buttons(button1))
tasks = []
button_task = asyncio.create_task(monitor_buttons())
tasks.append(button_task)
led_task = asyncio.create_task(blink_led())
tasks.append(led_task)
if(board.board_id == 'raspberry_pi_pico_w'):
pico_led_task = asyncio.create_task(blink_pico_w_led(led))
print("Starting Wifi")
startWiFi()
print("Starting Web Service")
webservice_task = asyncio.create_task(startWebService())
await asyncio.gather(pico_led_task, button_task, webservice_task)
else:
pico_led_task = asyncio.create_task(blink_pico_led(led))
await asyncio.gather(pico_led_task, button_task)
tasks.append(webservice_task)
# Pass the task list as *args to wait for them all to complete
await asyncio.gather(*tasks)

asyncio.run(main_loop())
Loading