-
Notifications
You must be signed in to change notification settings - Fork 0
/
CW01.py
87 lines (67 loc) · 2.03 KB
/
CW01.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
# Imports
from machine import Pin, I2C, Timer
import network
import time
import micropython
import sys
# Special for CW01
sdaPin = const(2)
sclPin = const(14)
redPin = const(12)
grnPin = const(13)
bluPin = const(5)
# Generic ESP8266
micropython.alloc_emergency_exception_buf(100)
class myI2C:
def __init__(self):
self.i2c = I2C(scl=Pin(sclPin), sda=Pin(sdaPin))
def scan(self):
print("Scanning xBus...", end="")
devices = self.i2c.scan()
if len(devices) > 0:
print("found:", len(devices), "xChip(s)", end="")
for device in devices:
print(",", hex(device), end="")
else:
print("no xChips found!", end="")
print()
class myNetwork:
def __init__(self):
self.wlan = network.WLAN(network.STA_IF)
self.wlan.active(True)
def scan(self):
print("Scanning networks...", end="")
SSIDs = self.wlan.scan()
if len(SSIDs) > 0:
print("found:", len(SSIDs), "networks", end="")
for SSID in SSIDs:
print(", {}({})".format(SSID[0].decode(), SSID[3]), end="")
else:
print("no network found!", end="")
print()
class myLEDs:
rgb = 0
def __init__(self):
self.leds = [Pin(redPin, Pin.OUT), Pin(grnPin, Pin.OUT), Pin(bluPin, Pin.OUT)]
self.pattern = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
def toggle(self):
self.rgb += 1
for led, val in zip(self.leds, self.pattern[self.rgb % 3]):
led.value(val)
def allOff(self):
for led in self.leds:
led.value(0)
xChip = myI2C()
xinabox = myNetwork()
LED = myLEDs()
tim = Timer(-1)
tim.init(period=100, callback=lambda t: LED.toggle())
for _ in range(10):
xChip.scan()
time.sleep(0.1)
xinabox.scan()
_, mpv, mpy = sys.implementation
mpv = ".".join(map(str, mpv))
print("This ran on Python v.{}, uPython v.{}, mpy v.{}".format(sys.version, mpv, mpy))
tim.deinit()
tim.init(period=50, mode=Timer.ONE_SHOT, callback=lambda t: LED.allOff())