Skip to content

Commit 61a736b

Browse files
committed
- Modified button example to be the task detailed in the pyControl manuscript. Added version of button example using named rather than * imports to allow users to compare the resulting code.
1 parent 457c80b commit 61a736b

File tree

3 files changed

+84
-36
lines changed

3 files changed

+84
-36
lines changed

pyControl/utility.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Module imported by the user into their task file which contains the
2+
# functions, classes and variables used in task files.
3+
14
import pyb
25
import math
36
from . import framework as fw

tasks/example/button.py

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
1-
# A simple state machine which turns the blue LED on the pyboard on and off when the
2-
# usr pushbutton on the pyboard is pressed. Does not require any hardware except a
3-
# micropython board.
1+
# A simple state machine which turns the blue LED on the pyboard on for 1
2+
# second when the usr pushbutton on the pyboard is pressed three times.
3+
# Does not require any hardware except a micropython board.
44

5-
from pyControl.utility import *
6-
from devices import *
7-
8-
# Define hardware (normally done in seperate hardware definition file).
9-
10-
pyboard_button = Digital_input('X17', falling_event='button_press', pull='up') # USR button on pyboard.
11-
12-
blue_LED = Digital_output('B4')
13-
14-
# States and events.
5+
from pyControl.utility import *
6+
from devices import *
157

16-
states= ['LED_on',
17-
'LED_off']
18-
19-
events = ['button_press']
20-
21-
initial_state = 'LED_off'
22-
23-
# Define behaviour.
24-
25-
def LED_on(event):
26-
if event == 'entry':
27-
blue_LED.on()
28-
elif event == 'exit':
29-
blue_LED.off()
30-
elif event == 'button_press':
31-
goto_state('LED_off')
32-
33-
def LED_off(event):
34-
if event == 'button_press':
35-
goto_state('LED_on')
36-
37-
def run_end(): # Turn off hardware at end of run.
38-
blue_LED.off()
8+
# Define hardware
9+
10+
button = Digital_input('X17', rising_event='button_press', pull='up') # pyboard usr button.
11+
LED = Digital_output('B4')
12+
13+
# States and events.
14+
15+
states = ['LED_on',
16+
'LED_off']
17+
18+
events = ['button_press']
19+
20+
initial_state = 'LED_off'
21+
22+
# Variables
23+
24+
v.press_n = 0
25+
26+
# State behaviour functions.
27+
28+
def LED_off(event):
29+
if event == 'button_press':
30+
v.press_n = v.press_n + 1
31+
print('Press number {}'.format(v.press_n))
32+
if v.press_n == 3:
33+
goto_state('LED_on')
34+
35+
def LED_on(event):
36+
if event == 'entry':
37+
LED.on()
38+
timed_goto_state('LED_off', 1*second)
39+
v.press_n = 0
40+
elif event == 'exit':
41+
LED.off()

tasks/example/button_named_imports.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Identical to the button example but using named imports rather
2+
# than the 'from module import *' syntax. This way is more
3+
# 'Pythonic' but results somewhat more verbose task code. You can
4+
# use whichever import style you prefer in your task code.
5+
6+
import pyControl.utility as pc
7+
import devices as dv
8+
9+
# Define hardware
10+
11+
button = dv.Digital_input('X17', rising_event='button_press', pull='up') # pyboard usr button.
12+
LED = dv.Digital_output('B4')
13+
14+
# States and events.
15+
16+
states = ['LED_on',
17+
'LED_off']
18+
19+
events = ['button_press']
20+
21+
initial_state = 'LED_off'
22+
23+
# Variables
24+
25+
pc.v.press_n = 0
26+
27+
# State behaviour functions.
28+
29+
def LED_off(event):
30+
if event == 'button_press':
31+
pc.v.press_n = pc.v.press_n + 1
32+
pc.print('Press number {}'.format(pc.v.press_n))
33+
if pc.v.press_n == 3:
34+
pc.goto_state('LED_on')
35+
36+
def LED_on(event):
37+
if event == 'entry':
38+
LED.on()
39+
pc.timed_goto_state('LED_off', 1*pc.second)
40+
pc.v.press_n = 0
41+
elif event == 'exit':
42+
LED.off()

0 commit comments

Comments
 (0)