-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathblink.py
117 lines (98 loc) · 4.28 KB
/
blink.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import time
import sys
from pydos_hw import Pydos_hw
from pydos_ui import Pydos_ui
if sys.implementation.name.upper() == 'MICROPYTHON':
from machine import Pin
# from pydos_bcfg import Pydos_pins
elif sys.implementation.name.upper() == 'CIRCUITPYTHON':
import board
from digitalio import DigitalInOut, Direction
def blink(ledPin=None):
cmnd = ""
minus = False
if ledPin == None or ledPin == "":
ledPin = Pydos_hw.led
elif sys.implementation.name.upper() == 'CIRCUITPYTHON':
ledPin = getattr(board,str(ledPin).upper().replace("'","").replace('"',''))
else:
if type(ledPin) == str:
if ledPin[:1] == '-':
minus = True
try:
ledPin = int(ledPin)
except:
if type(ledPin) == str:
ledPin = ledPin.upper()
# Micropython: pass a negative pin value if the led turns off with a value of 1
if (type(ledPin) == int and ledPin < 0) or minus:
ledPin = abs(ledPin)
ledOff = True
else:
ledOff = False
if sys.implementation.name.upper() == 'MICROPYTHON':
# nano connect is special case becuase LED uses the SPI SCK pin
if sys.implementation._machine in ['Arduino Nano RP2040 Connect with RP2040','Teensy 4.1 with MIMXRT1062DVJ6A']:
# ledPin = [6,13][(['Arduino Nano RP2040 Connect with RP2040','Teensy 4.1 with MIMXRT1062DVJ6A'].index(sys.implementation._machine))]
for i in range(len(Pydos_hw.SDdrive)):
if Pydos_hw.SCK[i] == ledPin:
if Pydos_hw.SDdrive[i] != None:
from os import umount
print("Unmounting SD card ("+Pydos_hw.SDdrive[i]+") because of shared SCK pin")
umount(Pydos_hw.SDdrive[i]) # Nano Connect uses LED pin for SPI SCK
Pydos_hw.SDdrive[i] = None
# If anything has used the SPI interface need to free up SCK
Pydos_hw.SPI_deinit(i)
break
if ledPin != None:
led = Pin(ledPin, Pin.OUT)
else:
raise ValueError("LED pin not defined in pydos_bcfg.py!")
elif sys.implementation.name.upper() == 'CIRCUITPYTHON':
# nano connect/Tennsy 4.1 are special cases becuase LED uses the SPI SCK pin
if board.board_id in ["arduino_nano_rp2040_connect","teensy41"]:
for i in range(len(Pydos_hw.SDdrive)):
if Pydos_hw.SCK[i] == ledPin:
if Pydos_hw.SDdrive[i] != None:
from storage import umount
print("Unmounting SD card ("+Pydos_hw.SDdrive[i]+") because of shared SCK pin")
umount(Pydos_hw.SDdrive[i]) # Nano Connect uses LED pin for SPI SCK
if "deinit" in dir(Pydos_hw.SD[i]):
Pydos_hw.SD[i].deinit()
Pydos_hw.SD[i] = None
Pydos_hw.SDdrive[i] = None
# If anything has used the SPI interface need to free up SCK
Pydos_hw.SPI_deinit(i)
break
# LED setup for onboard LED
if ledPin:
led = DigitalInOut(ledPin)
led.direction = Direction.OUTPUT
else:
print("LED pin not defined in pydos_bcfg.py!")
led = None
cmnd = "Q"
if cmnd != "Q":
print("listening..., Enter q to quit")
blinkstate = True
while cmnd.upper() != "Q":
while Pydos_ui.serial_bytes_available():
cmnd = Pydos_ui.read_keyboard(1)
print(cmnd, end="", sep="")
if cmnd in "qQ":
break
if sys.implementation.name.upper() == "MICROPYTHON":
led.value(blinkstate)
elif sys.implementation.name.upper() == "CIRCUITPYTHON":
led.value = blinkstate
blinkstate = not blinkstate
time.sleep(1)
if sys.implementation.name.upper() == "CIRCUITPYTHON":
if led:
led.deinit()
else:
led.value(ledOff)
if __name__ == "PyDOS":
blink(passedIn)
else:
print("Enter 'blink.blink(pinNo=Default)' in the REPL to run.")