Skip to content

Commit

Permalink
Allow targeting of leds by name
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenKockel committed Oct 6, 2023
1 parent 65137ad commit 380abcf
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,23 @@ A pin can also be read from. Resulting in `0` or `1` for digital pins and a valu
pyduin p A0 read
```

#### Control the builtin leds

The builtin leds defined in the pinfile can be addressed by their corresponding names

```bash
pyduin -B foo led1 {on|off}
```
Pyduin determines the correct read command in the background depending on the pins nature.

#### Get firmware version from the Arduino

```
```bash
pyduin --buddy uber firmware version [device|available]
```
#### Get free memory from the Arduino

```
```bash
pyduin --buddy uber free
```

Expand Down
4 changes: 4 additions & 0 deletions src/pyduin/arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def get_pin(self, pin):
pin = self.boardfile.normalize_pin_id(pin)
return self.Pins[pin]

def get_led(self, led:int):
""" Return the pin id of an led """
return self.boardfile.led_to_pin(led)

def close_serial_connection(self):
"""
Close the serial connection to the arduino.
Expand Down
10 changes: 9 additions & 1 deletion src/pyduin/arduino_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ def main(): # pylint: disable=too-many-locals,too-many-statements,too-many-branc
subparsers.add_parser("dependencies", help="Check dependencies")
subparsers.add_parser("versions", help="List versions", aliases=['v'])
subparsers.add_parser("free", help="Get free memory from device", aliases='f')
ledparser = subparsers.add_parser("led", help="Interact with builtin LEDs (if available).")
ledparser.add_argument('led', help='The id of the LED to interact with.', type=int)
ledparser.add_argument('action', choices=['on','off'])
firmware_parser = subparsers.add_parser("firmware", help="Firmware options", aliases=['fw'])
fwsubparsers = firmware_parser.add_subparsers(help='Available sub-commands', dest="fwcmd")
firmwareversion_parser = fwsubparsers.add_parser('version', aliases=['v'],
Expand Down Expand Up @@ -290,7 +293,7 @@ def main(): # pylint: disable=too-many-locals,too-many-statements,too-many-branc
arduino = get_arduino(config)
prepare_buildenv(arduino, config, args)
#args.pin = arduino.boardfile.normalize_pin_id(args.pin)

print(args)

if args.cmd in ('versions', 'v'):
print(versions(arduino, config['workdir']))
Expand Down Expand Up @@ -320,6 +323,11 @@ def main(): # pylint: disable=too-many-locals,too-many-statements,too-many-branc
lint_firmware()
update_firmware(arduino)
sys.exit(0)
elif args.cmd == 'led':
pin_id = arduino.get_led(args.led)
pin = arduino.get_pin(pin_id)
pin.set_mode('output')
res = pin.high() if args.action == 'on' else pin.low()
elif args.cmd in ('pin', 'p'):
if args.pincmd in ('high', 'low', 'h', 'l', 'pwm', 'p'):
act = args.pincmd
Expand Down
2 changes: 1 addition & 1 deletion src/pyduin/data/boardfiles/nanoatmega328.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pins:
- physical_id: 13
alias: D13
extra:
- led
- led1
- sck
- physical_id: 14
extra:
Expand Down
1 change: 1 addition & 0 deletions src/pyduin/data/boardfiles/uno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pins:
- physical_id: 13
extra:
- sck
- led1
- physical_id: 14
extra:
- analog
Expand Down
20 changes: 17 additions & 3 deletions src/pyduin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ class DeviceConfigError(BaseException):
"""

class PinNotFoundError(BaseException):
""" Error class to throw, when a pin cannot be found"""
""" Error class to throw, when a pin cannot be found """
def __init__(self, pin, *args, **kwargs):
msg=f'Pin {pin} cannot be resolved to a pin on the device.'
msg = f'Pin {pin} cannot be resolved to a pin on the device.'
super().__init__(msg, *args, **kwargs)

class LEDNotFoundError(BaseException):
""" Error class to be thrown when an led cannot be resolved to a pin """
def __init__(self, led, *args, **kwargs):
msg = f'LED {led} cannot be resolved to a pin on the device.'
super().__init__(msg, *args, **kwargs)

class PyduinUtils:
Expand Down Expand Up @@ -186,7 +192,7 @@ def __init__(self, boardfile):
if 'pwm' in extra:
self._pwm_pins.append(pin_id)

for match in list(filter(re.compile("led").match, extra)):
for match in list(filter(re.compile("led[0-9]+").match, extra)):
self._leds.append({match: pin_id})
# spi
for match in list(filter(re.compile("sda|scl").match, extra)):
Expand Down Expand Up @@ -276,6 +282,14 @@ def baudrate(self):
""" Return the baudrate used to connect to this board """
return self._baudrate

def led_to_pin(self, led_id):
""" Resolve led[0-9] back to an actual pin id """
led = f'led{led_id}'
pin = list(filter(lambda x: led in x ,self._leds))
if pin:
return pin[0][led]
raise LEDNotFoundError(led)

def normalize_pin_id(self, pin_id):
""" Return the physical_id of a pin. This function is used to
translate back pin alias names such as A[09]+ to a pin number
Expand Down
6 changes: 5 additions & 1 deletion tests/test_boardfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ def test_pwm_pins(boardfile_fixture):
assert boardfile_fixture.pwm_pins == expected

def test_led_pins(boardfile_fixture):
expected = [{'led1': 3},{'led10': 5},{'led': 13}]
expected = [{'led1': 3}, {'led10': 5}]
assert boardfile_fixture.leds == expected

def test_led_to_pin(boardfile_fixture):
assert boardfile_fixture.led_to_pin('1') == 3
assert boardfile_fixture.led_to_pin('10') == 5

def test_i2c_interfaces(boardfile_fixture):
expected = {'1': {'sda1': 7, 'scl1': 8},
'20': {'sda20': 10, 'scl20': 11},
Expand Down

0 comments on commit 380abcf

Please sign in to comment.