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