Skip to content

Commit 34803f7

Browse files
authored
Merge pull request #5 from arduino/dev
Dev
2 parents 537fe74 + 144813e commit 34803f7

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
A module to simplify and help writing MicroPython programs using the setup()/loop() paradigm.
44

55
## Commands
6+
67
This module also wraps machine functions in easy-to-use methods
78

89
### pin_mode(PIN_NUMBER/ID, MODE)
@@ -31,7 +32,6 @@ digital_read('A3')
3132

3233
return a value of `1` or `0` depending on the signal attached to the specified pins, for instance a button or a digital sensor.
3334

34-
3535
### digital_write(PIN_NUMBER/ID, VALUE)
3636

3737
Writes the digital value (`HIGH|LOW|True|False|1|0`) to the pin with Number or ID specified.
@@ -45,7 +45,6 @@ digital_write('A3', 0)
4545

4646
Will set the pin to the specified value.
4747

48-
4948
### analog_read(PIN_NUMBER/ID)
5049

5150
Reads the analog value for the Voltage applied to the pinpin with Number or ID specified.
@@ -75,7 +74,7 @@ analog_write('A3', 64)
7574
Will generate a modulated signal on the specified Pin.
7675
Can be used to control small motors with low current needs as well as servo motors.
7776

78-
#### IMPORTANT:
77+
#### IMPORTANT
7978

8079
The numeric value for PIN_NUMBER is usually the processor's GPIO number, while values enclosed in quotes are "named pins" and are platform/implementation specific, not guaranteed to be valid.
8180
A `ValueError` exception with label "invalid pin" is thrown if the pin number or ID is not valid.
@@ -85,7 +84,6 @@ A `ValueError` exception with label "invalid pin" is thrown if the pin number or
8584
Will halt the execution of your program for the amount of _milliseconds_ specified in the parameter.
8685
It is to be considered a code-blocking command.
8786

88-
8987
## Usage
9088

9189
The structure of an Arduino MicroPython program will look as follows:
@@ -134,9 +132,11 @@ This brings the implemented runtime commands to the three described below
134132
Is run _once_ and should contain initialisation code.
135133

136134
### loop()
135+
137136
Is run indefinitely until the program stops.
138137

139138
### cleanup()
139+
140140
Is run _once_ when the program stops.
141141
It should contain code such as resetting the value of variables, stopping timers, causing threads to stop running.
142142

@@ -161,6 +161,7 @@ start(setup, loop)
161161
## Utilities
162162

163163
Some utility methods are provided and are still in development:
164+
164165
* `map(x, in_min, in_max, out_min, out_max)`
165166
will remap the value `x` from its input range to an output range
166167
* `mapi(x, in_min, in_max, out_min, out_max)`
@@ -187,4 +188,3 @@ create_sketch('main')
187188

188189
If the destination `.py` file exists, a timestamp in _microseconds_ will be appended to the name.
189190
The method returns the Python file's full path.
190-

arduino/arduino.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,19 @@ def cleanup():
129129
print()
130130

131131

132-
def frame_counter(_arg):
132+
def frame_counter():
133133
global frame_count
134134
frame_count += 1
135-
sleep_ms(1)
135+
try:
136+
sleep_ms(1)
137+
return True
138+
except (Exception, KeyboardInterrupt) as e:
139+
if cleanup is not None:
140+
cleanup()
141+
if not isinstance(e, KeyboardInterrupt):
142+
raise e
143+
return False
144+
136145

137146
# RUNTIME
138147
def start(setup=None, loop=None, cleanup = None, preload = None):
@@ -144,10 +153,11 @@ def start(setup=None, loop=None, cleanup = None, preload = None):
144153
try:
145154
if loop is not None:
146155
loop()
147-
ticks_st = ticks_us()
148-
while True:
149-
if ticks_us() - ticks_st > 1000:
156+
if not frame_counter():
157+
if cleanup is not None:
158+
cleanup()
150159
break
160+
151161
except (Exception, KeyboardInterrupt) as e:
152162
if cleanup is not None:
153163
cleanup()

install.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#!/bin/bash
22
#
3-
# Install mrequests to a MicroPython board using mpremote
3+
# Install Arduino Runtime to a MicroPython board using mpremote.
4+
# This script accepts an optional argument to compile .py files to .mpy.
5+
# Simply run the script with the optional argument:
6+
#
7+
# ./install.sh mpy
48

5-
MODULES=('__init__.py' 'amp_fields.py' 'arduino_utils.py' 'common.py' 'network_utils.py')
69

7-
SRCDIR="arduino"
8-
LIBDIR="lib"
910
PKGDIR="arduino"
11+
SRCDIR=$PKGDIR
12+
LIBDIR="lib"
1013

11-
# Create the root lib folder
12-
# Will generate an error in the output if it already exists
14+
# File system operations such as "mpremote mkdir" or "mpremote rm"
15+
# will generate an error if the folder exists or if the file does not exist.
16+
# These errors can be ignored.
1317
#
1418
# Traceback (most recent call last):
1519
# File "<stdin>", line 2, in <module>

0 commit comments

Comments
 (0)