Skip to content

Commit 447183d

Browse files
committed
lib: Add Arduino Opta expansion protocol support.
1 parent 1c485b0 commit 447183d

File tree

3 files changed

+559
-0
lines changed

3 files changed

+559
-0
lines changed

lib/opta/example.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# This file is part of the blueprint package.
2+
# Copyright (c) 2024 Arduino SA
3+
# This Source Code Form is subject to the terms of the Mozilla Public
4+
# License, v. 2.0. If a copy of the MPL was not distributed with this
5+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
import opta
7+
import time
8+
import logging
9+
10+
11+
if __name__ == "__main__":
12+
logging.basicConfig(
13+
datefmt="%H:%M:%S",
14+
format="%(asctime)s.%(msecs)03d %(message)s",
15+
level=logging.INFO # Switch to DEBUG to see raw commands
16+
)
17+
18+
opta = opta.Opta(bus_id=3)
19+
20+
# enum_devices initializes the bus, resets all expansions, and returns a list of
21+
# detected expansions. NOTE: keep a reference to the list of expansion for later
22+
# use, as every time this function is called it restarts the enumeration process.
23+
for e in opta.enum_devices():
24+
print("")
25+
logging.info(f"Expansion type: {e.type} address: 0x{e.addr:02X} name: {e.name}")
26+
27+
# Read firmware version.
28+
major, minor, patch = e.firmware_version()
29+
logging.info(f"Firmware Version Major: {major} Minor: {minor} Patch: {patch}")
30+
31+
# Read product ID.
32+
pid = e.product_id()
33+
logging.info("Product ID bytes: " + ", ".join(["0x%02X"%(a) for a in pid[0:8]]))
34+
35+
# Read first 32 of flash
36+
#logging.info(e.flash(address=0, size=32))
37+
38+
if e.type == "digital":
39+
# Write digital pins. If the default state and timeout (in milliseconds) are
40+
# also specified, the pins will revert to the specified default state after
41+
# the timeout expires.
42+
e.digital(pins=0b10101010, default=0b01010101, timeout=3000)
43+
44+
# If no args are specified, digital() returns all digital pins.
45+
pins = e.digital()
46+
logging.info(f"Digital pins: 0b{pins:08b}")
47+
48+
# Read analog pins.
49+
logging.info("Analog pin [0 ]: %d" % e.analog(channel=0))
50+
logging.info("Analog pins [0..7]: " + str(e.analog()))
51+
52+
if e.type == "analog":
53+
# Configure LEDs on Opta Analog
54+
e.digital(pins=0b10011001)
55+
56+
# Configure analog channels. Note almost all of the possible args are used here
57+
# for demonstration purposes, however only a few are actually required (such as
58+
# the channel type and mode), most of the other args have reasonable defaults.
59+
60+
# Configure channel (0) as PWM.
61+
e.analog(channel=0, channel_type="pwm", period=1000, duty=50)
62+
63+
# Configure channel (2) as DAC.
64+
e.analog(channel=2, channel_type="dac", channel_mode="voltage", value=7540)
65+
66+
# Configure channel (3) as ADC.
67+
e.analog(channel=3, channel_type="adc", channel_mode="voltage", pulldown=True,
68+
rejection=False, diagnostic=False, average=0, secondary=False)
69+
70+
# Configure channel (4) as RTD.
71+
e.analog(channel=4, channel_type="rtd", use_3_wire=False, current_ma=1.2, update_time=0)
72+
73+
# Configure channel (5) as digital input.
74+
e.analog(channel=5, channel_type="din", comparator=True, inverted=False, filtered=True,
75+
scale=False, sink=1, threshold=9, debounce_mode="simple", debounce_time=24)
76+
77+
# Read ADC channel (3).
78+
# This should print 65535 if channels 2 and 3 are connected.
79+
logging.info("ADC channel [3 ]: %d" % e.analog(channel=3))
80+
81+
# Read all analog channels.
82+
logging.info("Analog channels [0..7]: " + str(e.analog()))
83+
84+
# Read RTD channel (4).
85+
logging.info("RTD channel [4 ]: %.1f ohm" % e.analog(channel=4))
86+
87+
# Read DIN channel (5).
88+
logging.info("DIN channel [5 ]: %d" % e.analog(channel=5))
89+
90+
# Read all analog channels.
91+
logging.info("Analog channels [0..7]: " + str(e.analog()))
92+
93+
# Configure channel (2) as DAC.
94+
e.analog(channel=2, channel_type="dac", channel_mode="voltage", value=3770)
95+
96+
# Configure channel (3) as ADC.
97+
e.analog(channel=3, channel_type="adc", channel_mode="voltage", pulldown=True,
98+
rejection=False, diagnostic=False, average=0, secondary=False)
99+
100+
logging.info("ADC channel [3 ]: %d" % e.analog(channel=3))

lib/opta/manifest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
metadata(
2+
description="Modules for Arduino Opta.",
3+
version="0.0.1",
4+
)
5+
6+
module("opta.py")

0 commit comments

Comments
 (0)