Skip to content

Commit

Permalink
Automation 2040w MicroPython PWM (#489)
Browse files Browse the repository at this point in the history
Co-authored-by: ZodiusInfuser <[email protected]>
  • Loading branch information
rabid-inventor and ZodiusInfuser authored Oct 6, 2022
1 parent 8280a6a commit 28b6698
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 61 deletions.
49 changes: 49 additions & 0 deletions micropython/examples/automation2040w/pwm_outputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import time
from automation import Automation2040W, SWITCH_A

"""
Demonstrates how to toggle each of Automation 2040 W's output terminals.
Press "A" to exit the program.
"""

TIME_PER_TOGGLE = 0.5 # How much time to wait between each toggle (in seconds)
OUTPUT_NAMES = ("O1", "O2", "O3") # The friendly names to give each digital output

# Create a new Automation2040W
board = Automation2040W()

# Enable the LED of the switch used to exit the loop
board.switch_led(SWITCH_A, 50) # Half Brightness

toggle = True
index = 0

# Toggle the outputs until the user switch is pressed
while not board.switch_pressed(SWITCH_A):

# Toggle an output
for output_percentage in range(101):

if toggle is True:
board.output(index, output_percentage)
else:
board.output(index, 100.0 - output_percentage)

# Print the state of all outputs
for i in range(board.NUM_OUTPUTS):
print(OUTPUT_NAMES[i], " = ", board.output_percent(i), sep="", end=", ")

# Print a new line
print()
time.sleep(0.01)

index += 1 # Move on to the next output
if index >= board.NUM_OUTPUTS:
index = 0 # Go back to the first output
toggle = not toggle # Invert the toggle value

time.sleep(TIME_PER_TOGGLE)

# Put the board back into a safe state
board.reset()
4 changes: 2 additions & 2 deletions micropython/examples/automation2040w/read_adcs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
from automation import Automation2040W, SWITCH_A, NUM_ADCS
from automation import Automation2040W, SWITCH_A

"""
Shows how to read the 3 ADC terminals of Automation 2040 W.
Expand All @@ -20,7 +20,7 @@
while not board.switch_pressed(SWITCH_A):

# Read each ADC in turn and print its voltage
for i in range(NUM_ADCS):
for i in range(board.NUM_ADCS):
voltage = board.read_adc(i)
print(ADC_NAMES[i], " = ", round(voltage, 3), sep="", end=", ")

Expand Down
9 changes: 7 additions & 2 deletions micropython/examples/automation2040w/read_inputs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import time
from automation import Automation2040W, SWITCH_A, NUM_INPUTS
from automation import Automation2040W, SWITCH_A

# Uncomment fo Automation2040W Mini and comment out above import
# from automation import Automation2040WMini, SWITCH_A

"""
Shows how to read the 3 Input terminals of Automation 2040 W.
Expand All @@ -12,6 +15,8 @@

# Create a new Automation2040W
board = Automation2040W()
# Uncomment fo Automation2040W Mini
# board = Automation2040WMini()

# Enable the LED of the switch used to exit the loop
board.switch_led(SWITCH_A, 50) # Half Brightness
Expand All @@ -20,7 +25,7 @@
while not board.switch_pressed(SWITCH_A):

# Read each input in turn and print its value
for i in range(NUM_INPUTS):
for i in range(board.NUM_INPUTS):
value = board.read_input(i)
print(INPUT_NAMES[i], " = ", value, sep="", end=", ")

Expand Down
4 changes: 2 additions & 2 deletions micropython/examples/automation2040w/switches_and_leds.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
from automation import Automation2040W, SWITCH_A, SWITCH_B, NUM_SWITCHES
from automation import Automation2040W, SWITCH_A, SWITCH_B

"""
An example of the user switches and LEDs on Automation 2040 W.
Expand All @@ -22,7 +22,7 @@
# Interact with the switches and LEDs until both are pressed simultaneously
while not board.switch_pressed(SWITCH_A) or not board.switch_pressed(SWITCH_B):

for i in range(NUM_SWITCHES):
for i in range(board.NUM_SWITCHES):
# Change the LED brightness based on switch's state
if board.switch_pressed(i):
print(SWITCH_NAMES[i], " = Pressed", sep="", end=", ")
Expand Down
8 changes: 4 additions & 4 deletions micropython/examples/automation2040w/toggle_outputs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
from automation import Automation2040W, SWITCH_A, NUM_OUTPUTS
from automation import Automation2040W, SWITCH_A

"""
Demonstrates how to toggle each of Automation 2040 W's output terminals.
Expand All @@ -26,14 +26,14 @@
board.output(index, toggle)

# Print the state of all outputs
for i in range(NUM_OUTPUTS):
print(OUTPUT_NAMES[i], " = ", board.output(i), sep="", end=", ")
for i in range(board.NUM_OUTPUTS):
print(OUTPUT_NAMES[i], " = ", bool(board.output(i)), sep="", end=", ")

# Print a new line
print()

index += 1 # Move on to the next output
if index >= NUM_OUTPUTS:
if index >= board.NUM_OUTPUTS:
index = 0 # Go back to the first output
toggle = not toggle # Invert the toggle value

Expand Down
20 changes: 14 additions & 6 deletions micropython/examples/automation2040w/toggle_relays.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
from automation import Automation2040W, SWITCH_A, NUM_RELAYS
from automation import Automation2040W, SWITCH_A

"""
Demonstrates how to toggle the actuation state of each of Automation 2040 W's relays.
Expand All @@ -12,6 +12,8 @@

# Create a new Automation2040W
board = Automation2040W()
# Uncomment fo Automation2040W Mini
# board = Automation2040WMini()

# Enable the LED of the switch used to exit the loop
board.switch_led(SWITCH_A, 50) # Half Brightness
Expand All @@ -23,17 +25,23 @@
while not board.switch_pressed(SWITCH_A):

# Toggle a relay
board.relay(index, toggle)
if board.NUM_RELAYS == 1:
board.relay(toggle)

# Print the state of all relays
for i in range(NUM_RELAYS):
print(RELAY_NAMES[i], " = ", board.relay(i), sep="", end=", ")
# Print the state of the relay
print(RELAY_NAMES[0], " = ", board.relay(), sep="", end=", ")
else:
board.relay(index, toggle)

# Print the state of all relays
for i in range(board.NUM_RELAYS):
print(RELAY_NAMES[i], " = ", board.relay(i), sep="", end=", ")

# Print a new line
print()

index += 1 # Move on to the next relay
if index >= NUM_RELAYS:
if index >= board.NUM_RELAYS:
index = 0 # Go back to the first relay
toggle = not toggle # Invert the toggle value

Expand Down
10 changes: 5 additions & 5 deletions micropython/examples/automation2040w/web_io_interface/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def status_handler(mode, status, ip):

print("Network: {}".format(WIFI_CONFIG.SSID))
status_text = "Connecting..."
board.conn_led(20)
board.conn_led(20.0)
if status is not None:
if status:
status_text = "Connection successful!"
Expand Down Expand Up @@ -119,12 +119,12 @@ def not_exists(self):

def get(self, data):
if 'one' in data.keys():
board.output(0, int(data['one']))
board.output(0, bool(data['one']))
if 'two' in data.keys():
board.output(1, int(data['two']))
board.output(1, bool(data['two']))
if 'three' in data.keys():
board.output(2, int(data['three']))
return {"one": board.output(0), "two": board.output(1), "three": board.output(2)}, 201
board.output(2, bool(data['three']))
return {"one": bool(board.output(0)), "two": bool(board.output(1)), "three": bool(board.output(2))}, 201

def post(self, data):
if 'one' in data.keys():
Expand Down
32 changes: 23 additions & 9 deletions micropython/modules_py/automation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This library offers convenient functions for interacting with your new [Pimoroni
- [Connectivity LED](#connectivity-led)
- [Actuating the Relays](#actuating-the-relays)
- [Setting the Outputs](#setting-the-outputs)
- [Reading as a percentage](#reading-as-a-percentage)
- [Changing the frequency](#changing-the-frequency)
- [Reading the Inputs](#reading-the-inputs)
- [Reading the ADCs](#reading-the-adcs)
- [Extra GPIOs](#extra-gpios)
Expand All @@ -32,9 +34,9 @@ From here, all features of Automation 2040W can be accessed by calling functions

Automation 2040W has two handy switches onboard, with neighbouring LEDs, offering a tactile way to interact with your program and be notified of actions that need attention.

To read one of the switches, call `.switch_pressed(switch)`, where `switch` is a value from `0` to `NUM_SWITCHES - 1`. This returns `True` when the specified switch is pressed, and `False` otherwise.
To read one of the switches, call `.switch_pressed(switch)`, where `switch` is a value from `0` to `.NUM_SWITCHES - 1`. This returns `True` when the specified switch is pressed, and `False` otherwise.

To set a switch's neighbouring LED, call `.switch_led(switch, brightness)`, where `switch` is a value from `0` to `NUM_SWITCHES - 1`, and `brightness` is either `True`, `False`, or a number from `0.0` to `100.0`.
To set a switch's neighbouring LED, call `.switch_led(switch, brightness)`, where `switch` is a value from `0` to `.NUM_SWITCHES - 1`, and `brightness` is either `True`, `False`, or a number from `0.0` to `100.0`.


To make it easier to use a specific switch or it's LED, the `automation` module contains these handy constants:
Expand All @@ -57,29 +59,39 @@ A relay can be actuated by calling `.actuate_relay(relay)`, or released by calli

The state of each relay can be read by calling `.relay(relay)`. This returns `True` if the relay is actuated, and `False` if it is released. The actuation state is also reflected by LEDs that neighbour each relay.

For all these functions, `relay` is a value from `0` to `NUM_RELAYS - 1`. To control a specific relay, the `automation` module contains these handy constants:
For all these functions, `relay` is a value from `0` to `.NUM_RELAYS - 1`. To control a specific relay, the `automation` module contains these handy constants:
* `RELAY_1` = `0`
* `RELAY_2` = `1`
* `RELAY_3` = `2`


### Setting the Outputs

Three sourcing outputs, capable of 2A+, are present on Automation 2040W.
Three sourcing outputs, capable of PWM at up to 2A, are present on Automation 2040W.

An output can be controlled by calling `.output(output, value)`, where `output` is a value from `0` to `NUM_OUTPUTS - 1`, and `value` is `True` or `False`.
An output can be controlled by calling `.output(output, value)`, where `output` is a value from `0` to `.NUM_OUTPUTS - 1`, and `value` is `True`, `False` or a number between `0.0` and `100.0`

The state of an output can be read by calling `.output(output)`, where `output` is a value from `0` to `NUM_OUTPUTS - 1`. This returns `True` if the output is active, and `False` if it is inactive. The state is also reflected by LEDs that neighbour each output terminal.
The state of an output can be read by calling `.output(output)`, where `output` is a value from `0` to `.NUM_OUTPUTS - 1`. This returns `True` if the output is on by any percent, or `False` if it is off. The state is also reflected by LEDs that neighbour each output terminal.

To control a specific output, the `automation` module contains these handy constants:
* `OUTPUT_1` = `0`
* `OUTPUT_2` = `1`
* `OUTPUT_3` = `2`


#### Reading as a percentage

If you prefer to know the current PWM setting of an output as a percentage, this can be accessed by calling `.output_precent(output)`, where `output` is a value from `0` to `.NUM_OUTPUTS - 1`. This will return a float between `0.0` and `100.0`.


#### Changing the frequency

The PWM frequency of the output can be set by calling `.change_output_freq(output, freq)`, where `output` is a value from `0` to `.NUM_OUTPUTS - 1` and `freq` is a frequency in Hz between `10.0` and `1000.0`. Values outside of this range will cause a `ValueError`.


### Reading the Inputs

Automation 2040W has four buffered digital inputs. These can be read by calling `.read_input(input)`, where `input` is a value from `0` to `NUM_INPUTS - 1`.
Automation 2040W has four buffered digital inputs. These can be read by calling `.read_input(input)`, where `input` is a value from `0` to `.NUM_INPUTS - 1`.

To read a specific input, the `automation` module contains these handy constants:
* `INPUT_1` = `0`
Expand All @@ -90,7 +102,7 @@ To read a specific input, the `automation` module contains these handy constants

### Reading the ADCs

Automation 2040W has three analog inputs, capable of reading up to 40V. The voltage on these can be read by calling `.read_adc(adc)`, where `adc` is a value from `0` to `NUM_ADCS - 1`.
Automation 2040W has three analog inputs, capable of reading up to 40V. The voltage on these can be read by calling `.read_adc(adc)`, where `adc` is a value from `0` to `.NUM_ADCS - 1`.

To read a specific adc, the `automation` module contains these handy constants:
* `ADC_1` = `0`
Expand All @@ -105,7 +117,7 @@ On the left hand side of Automation 2040W are three GPIO pins. These are 3.3V lo
* `GP1` = `1`
* `GP2` = `2`

There is also a `NUM_GPIOS` for times when any iteration needs to be performed.
There is also a `.NUM_GPIOS` for times when any iteration needs to be performed.


### Software Reset
Expand All @@ -128,6 +140,8 @@ actuate_relay(relay)
release_relay(relay)
output(output)
output(output, value)
output_percent(output)
change_output_freq(output, freq)
read_input(input)
read_adc(adc)
reset()
Expand Down
Loading

0 comments on commit 28b6698

Please sign in to comment.