Skip to content

Commit dc58076

Browse files
committed
fix to scratch3
1 parent 18d19ae commit dc58076

3 files changed

Lines changed: 33 additions & 28 deletions

File tree

10.microbit/button.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
class Button:
44

5-
def __init__(self, pin):
5+
def __init__(self, pin_id):
66
from machine import Pin
7-
self.pin = Pin(pin, Pin.IN)
8-
9-
def get_presses(self, delay = 1):
10-
last_time, last_state, presses = time(), 0, 0
11-
while time() < last_time + delay:
12-
sleep_ms(50)
13-
if last_state == 0 and self.pin.value() == 1:
14-
last_state= 1
15-
if last_state == 1 and self.pin.value() == 0:
16-
last_state, presses = 0, presses + 1
17-
return presses
7+
self.pin = Pin(pin_id, Pin.IN)
8+
self.irq = self.pin.irq(trigger=Pin.IRQ_RISING, handler=self.__irq_sc)
9+
self.presses = 0
10+
11+
def __irq_sc(self, p):
12+
# print(self, p)
13+
self.presses += 1
14+
15+
def close(self):
16+
self.irq.trigger(0)
17+
18+
def reset(self):
19+
self.presses = 0
20+
21+
def get_presses(self):
22+
return self.presses
1823

1924
def is_pressed(self):
2025
return self.pin.value() == 0
2126

22-
def was_pressed(self, delay = 1):
23-
last_time, last_state = time(), self.pin.value()
24-
while time() < last_time + delay:
25-
sleep_ms(50)
26-
if last_state != self.pin.value():
27-
return True
28-
return False
27+
def was_pressed(self):
28+
return self.presses != 0
2929

3030
def unit_test():
3131
print('The unit test code is as follows')
@@ -36,11 +36,15 @@ def unit_test():
3636
print(\'button_a is_pressed \', button_a.is_pressed())\n\
3737
print(\'button_a get_presses \', button_a.get_presses())\n\
3838
')
39-
button_a = Button(35)
40-
while True:
41-
print('button_a was_pressed ', button_a.was_pressed())
42-
print('button_a is_pressed ', button_a.is_pressed())
43-
print('button_a get_presses ', button_a.get_presses())
39+
try:
40+
button_a = Button(35)
41+
while True:
42+
sleep_ms(100)
43+
print('button_a was_pressed ', button_a.was_pressed())
44+
print('button_a is_pressed ', button_a.is_pressed())
45+
print('button_a get_presses ', button_a.get_presses())
46+
finally:
47+
button_a.close()
4448

4549
if __name__ == '__main__':
4650
unit_test()

10.microbit/display.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ def _scroll(self, val, color=Red, delay=150):
360360
_thread.exit()
361361

362362
def clear(self):
363+
self.stop()
363364
self.Led.fill((0, 0, 0))
364365
self.Led.Show()
365366

10.microbit/pins.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ def read_digital(self):
1515

1616
def read_analog(self, ATTN = ADC.ATTN_0DB):
1717
if self.pin not in range(32,40):
18-
print("This pin feature is not supported")
19-
return 0
18+
# print("This pin feature is not supported")
19+
return None
2020
if self.adc is None:
2121
self.adc = ADC(Pin(self.pin, Pin.IN))
2222
self.adc.atten(ATTN)
2323
return self.adc.read()
2424

2525
def write_analog(self, value):
2626
if self.pin not in [25,26]:
27-
print("This pin feature is not supported")
28-
return 0
27+
# print("This pin feature is not supported")
28+
return None
2929
DAC(Pin(self.pin)).write(value)
3030

3131
def is_touched(self):

0 commit comments

Comments
 (0)