-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
337 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
model: nrf52840dk | ||
hw_rev: '3.2' | ||
serial_number: 0xfoobar | ||
pin_mappings: | ||
P0.13: | ||
io_interface: raspberrypi5gpio | ||
io_pin_spec: 20 | ||
target_pin_function: LED1 | ||
target_pin_mode: output | ||
target_pin_active: low | ||
P0.14: | ||
io_interface: raspberrypi5gpio | ||
io_pin_spec: 19 | ||
target_pin_function: LED2 | ||
target_pin_mode: output | ||
target_pin_active: low | ||
P0.11: | ||
io_interface: raspberrypi5gpio | ||
io_pin_spec: 21 | ||
target_pin_function: BUTTON1 | ||
target_pin_mode: input | ||
target_pin_active: low | ||
P0.12: | ||
io_interface: raspberrypi5gpio | ||
io_pin_spec: 26 | ||
target_pin_function: BUTTON2 | ||
target_pin_mode: input | ||
target_pin_active: low |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Licensed under the Apache License, Version 2.0 or the MIT License. | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
# Copyright Tock Contributors 2024. | ||
|
||
import logging | ||
|
||
from gpio.interfaces.raspberry_pi5_gpio import RaspberryPi5GPIO | ||
from gpio.interfaces.mock_gpio import MockGPIO | ||
|
||
|
||
class GPIO: | ||
def __init__(self, target_spec): | ||
self.target_spec = target_spec | ||
self.gpio_interfaces = {} | ||
# Initialize GPIO interfaces based on the target spec | ||
for pin_label, pin_mapping in self.target_spec.get("pin_mappings", {}).items(): | ||
interface_name = pin_mapping["io_interface"] | ||
if interface_name not in self.gpio_interfaces: | ||
interface_class = self.load_interface_class(interface_name) | ||
self.gpio_interfaces[interface_name] = interface_class() | ||
|
||
def load_interface_class(self, interface_name): | ||
# Map interface names to classes | ||
interface_classes = { | ||
"raspberrypi5gpio": RaspberryPi5GPIO, | ||
"mock_gpio": MockGPIO, | ||
} | ||
if interface_name in interface_classes: | ||
return interface_classes[interface_name] | ||
else: | ||
raise ValueError(f"Unknown GPIO interface: {interface_name}") | ||
|
||
def pin(self, pin_label): | ||
# Get the pin mapping from the target spec | ||
pin_mapping = self.target_spec["pin_mappings"].get(pin_label) | ||
if not pin_mapping: | ||
raise ValueError(f"Unknown pin label: {pin_label}") | ||
interface_name = pin_mapping["io_interface"] | ||
interface = self.gpio_interfaces.get(interface_name) | ||
if not interface: | ||
raise ValueError(f"No GPIO interface for {interface_name}") | ||
return interface.pin(pin_label, pin_mapping) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Licensed under the Apache License, Version 2.0 or the MIT License. | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
# Copyright Tock Contributors 2024. | ||
|
||
from .raspberry_pi5_gpio import RaspberryPi5GPIO | ||
from .mock_gpio import MockGPIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Licensed under the Apache License, Version 2.0 or the MIT License. | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
# Copyright Tock Contributors 2024. | ||
|
||
import logging | ||
|
||
|
||
class MockGPIO: | ||
def __init__(self): | ||
self.pins = {} | ||
|
||
def pin(self, target_pin_label, target_pin_mapping): | ||
if target_pin_label not in self.pins: | ||
pin = MockGPIOPin(target_pin_label) | ||
self.pins[target_pin_label] = pin | ||
else: | ||
pin = self.pins[target_pin_label] | ||
return pin | ||
|
||
def cleanup(self): | ||
pass # Nothing to clean up in mock | ||
|
||
|
||
class MockGPIOPin: | ||
def __init__(self, pin_label): | ||
self.pin_label = pin_label | ||
self.mode = None | ||
self.value = None | ||
|
||
def set_mode(self, mode): | ||
self.mode = mode | ||
logging.info(f"Pin {self.pin_label} set to mode {mode}") | ||
|
||
def read(self): | ||
logging.info(f"Pin {self.pin_label} read value {self.value}") | ||
return self.value | ||
|
||
def write(self, value): | ||
self.value = value | ||
logging.info(f"Pin {self.pin_label} write value {value}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Licensed under the Apache License, Version 2.0 or the MIT License. | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
# Copyright Tock Contributors 2024. | ||
|
||
import logging | ||
from gpiozero import LED, Button, DigitalOutputDevice, DigitalInputDevice | ||
|
||
|
||
class RaspberryPi5GPIO: | ||
def __init__(self): | ||
self.pins = {} | ||
|
||
def pin(self, _target_pin_label, target_pin_mapping): | ||
gpio_pin_number = int(target_pin_mapping["io_pin_spec"]) | ||
if gpio_pin_number not in self.pins: | ||
pin = RaspberryPiGPIOPin(gpio_pin_number) | ||
self.pins[gpio_pin_number] = pin | ||
else: | ||
pin = self.pins[gpio_pin_number] | ||
return pin | ||
|
||
def cleanup(self): | ||
for pin in self.pins.values(): | ||
pin.close() | ||
self.pins.clear() | ||
|
||
|
||
class RaspberryPiGPIOPin: | ||
def __init__(self, gpio_pin_number): | ||
self.gpio_pin_number = gpio_pin_number | ||
self.device = None # Will be initialized based on mode | ||
self.mode = None | ||
|
||
def set_mode(self, mode): | ||
if mode == "input": | ||
if self.device: | ||
self.device.close() | ||
self.device = DigitalInputDevice(self.gpio_pin_number) | ||
self.mode = mode | ||
elif mode == "output": | ||
if self.device: | ||
self.device.close() | ||
self.device = DigitalOutputDevice(self.gpio_pin_number) | ||
self.mode = mode | ||
else: | ||
raise ValueError(f"Unknown mode: {mode}") | ||
|
||
def read(self): | ||
if self.mode != "input": | ||
raise RuntimeError("Pin is not set to input mode") | ||
value = self.device.value | ||
logging.debug(f"Read value {value} from pin {self.gpio_pin_number}") | ||
return value | ||
|
||
def write(self, value): | ||
if self.mode != "output": | ||
raise RuntimeError("Pin is not set to output mode") | ||
self.device.value = value | ||
logging.debug(f"Wrote value {value} to pin {self.gpio_pin_number}") | ||
|
||
def close(self): | ||
if self.device: | ||
self.device.close() | ||
self.device = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
argcomplete==3.5.1 | ||
colorama==0.4.6 | ||
colorzero==2.0 | ||
crcmod==1.7 | ||
gpiozero==2.0.1 | ||
pexpect==4.9.0 | ||
prompt-toolkit==3.0.36 | ||
ptyprocess==0.7.0 | ||
pycryptodome==3.21.0 | ||
pyserial==3.5 | ||
PyYAML==6.0.2 | ||
questionary==2.0.1 | ||
siphash==0.0.1 | ||
tockloader==1.13.0 | ||
toml==0.10.2 | ||
tqdm==4.66.5 | ||
tqdm==4.66.6 | ||
wcwidth==0.2.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
pexpect | ||
pyserial | ||
tockloader | ||
gpiozero | ||
pyyaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
model: nrf52840dk | ||
hw_rev: '3.2' | ||
serial_number: 0xfoobar | ||
pin_mappings: | ||
P0.13: | ||
io_interface: raspberrypi5gpio | ||
io_pin_spec: 20 | ||
target_pin_function: LED1 | ||
target_pin_mode: output | ||
target_pin_active: low | ||
P0.14: | ||
io_interface: raspberrypi5gpio | ||
io_pin_spec: 19 | ||
target_pin_function: LED2 | ||
target_pin_mode: output | ||
target_pin_active: low | ||
P0.11: | ||
io_interface: raspberrypi5gpio | ||
io_pin_spec: 21 | ||
target_pin_function: BUTTON1 | ||
target_pin_mode: input | ||
target_pin_active: low | ||
P0.12: | ||
io_interface: raspberrypi5gpio | ||
io_pin_spec: 26 | ||
target_pin_function: BUTTON2 | ||
target_pin_mode: input | ||
target_pin_active: low |
Oops, something went wrong.