Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: Add support to navigator v2 in pi4 and navigator v1 to pi5 #96

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 167 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,32 @@ mod rgb;

use peripherals::*;

use crate::bmp280::Bmp280Device;
use crate::icm20689::Icm20689Device;
use ads1115::Ads1115Device;
use ak09915::Ak09915Device;
use bmp280::Bmp280Device;
use bmp390::Bmp390Device;
use icm20689::Icm20689Device;
use leak::LeakDetector;
use led::LedController;
use pca9685::Pca9685Device;
use rgb::RgbController;

// add docs ( difference btwen boards)
#[derive(Debug, Default, Clone, Copy)]
pub enum NavigatorVersion {
#[default]
V1 = 1,
V2,
}

// add docs ( explicit difference btwen overlays)
#[derive(Debug, Default, Clone, Copy)]
pub enum PiVersion {
#[default]
Pi4 = 4,
Pi5,
}

/// Set of available options to select ADC's channel.
#[derive(Debug, Clone, Copy)]
pub enum AdcChannel {
Expand Down Expand Up @@ -101,6 +117,8 @@ impl Navigator {

pub fn create() -> NavigatorBuilder {
NavigatorBuilder {
navigator: Default::default(),
pi: Default::default(),
rgb_led_strip_size: 1, // There is only a single LED on the board
}
}
Expand Down Expand Up @@ -372,24 +390,117 @@ impl Navigator {
}

pub struct NavigatorBuilder {
navigator: NavigatorVersion,
pi: PiVersion,
rgb_led_strip_size: usize,
}

impl NavigatorBuilder {
pub fn with_navigator(mut self, navigator: NavigatorVersion) -> Self {
self.navigator = navigator;
self
}

pub fn with_pi(mut self, pi: PiVersion) -> Self {
self.pi = pi;
self
}

pub fn with_rgb_led_strip_size(mut self, size: usize) -> Self {
self.rgb_led_strip_size = size;
self
}

pub fn build(self) -> Navigator {
match self.pi {
PiVersion::Pi4 => match self.navigator {
NavigatorVersion::V1 => self.build_navigator_v1_pi4(),
NavigatorVersion::V2 => self.build_navigator_v2_pi4(),
},
PiVersion::Pi5 => match self.navigator {
NavigatorVersion::V1 => self.build_navigator_v1_pi5(),
NavigatorVersion::V2 => self.build_navigator_v2_pi5(),
},
}
}

pub fn build_navigator_v1_pi4(self) -> Navigator {
let mut devices: Vec<Box<dyn AnyHardware>> = vec![
Box::new(Ads1115Device::builder().build().unwrap()),
Box::new(Ak09915Device::builder().build().unwrap()),
Box::new(Bmp280Device::builder().build().unwrap()),
Box::new(Icm20689Device::builder().build().unwrap()),
Box::new(LeakDetector::builder().build().unwrap()),
Box::new(
Ads1115Device::builder()
.build()
.expect("Failed to create Ads1115"),
),
Box::new(
Ak09915Device::builder()
.build()
.expect("Failed to create Ak09915"),
),
Box::new(
Bmp280Device::builder()
.build()
.expect("Failed to create Bmp280"),
),
Box::new(
Icm20689Device::builder()
.build()
.expect("Failed to create Icm20689"),
),
Box::new(
LeakDetector::builder()
.build()
.expect("Failed to create LedDetector"),
),
Box::new(LedController::builder().build()),
Box::new(
Pca9685Device::builder()
.build()
.expect("Failed to create Pca9685"),
),
];

let rgb_device = RgbController::builder()
.with_led_count(self.rgb_led_strip_size)
.build()
.unwrap();
devices.push(Box::new(rgb_device));

Navigator { devices }
}

pub fn build_navigator_v2_pi4(self) -> Navigator {
let mut devices: Vec<Box<dyn AnyHardware>> = vec![
Box::new(
Ads1115Device::builder()
.build()
.expect("Failed to create Ads1115"),
),
Box::new(
Ak09915Device::builder()
.build()
.expect("Failed to create Ak09915"),
),
Box::new(
Bmp390Device::builder()
.build()
.expect("Failed to create Bmp390"),
),
Box::new(
Icm20689Device::builder()
.build()
.expect("Failed to create Icm20689"),
),
Box::new(
LeakDetector::builder()
.build()
.expect("Failed to create LedDetector"),
),
Box::new(LedController::builder().build()),
Box::new(Pca9685Device::builder().build().unwrap()),
Box::new(
Pca9685Device::builder()
.build()
.expect("Failed to create Pca9685"),
),
];

let rgb_device = RgbController::builder()
Expand All @@ -401,6 +512,54 @@ impl NavigatorBuilder {
Navigator { devices }
}

pub fn build_navigator_v1_pi5(self) -> Navigator {
let gpiochip = "/dev/gpiochip4";
let devices: Vec<Box<dyn AnyHardware>> = vec![
Box::new(
Ads1115Device::builder()
.build()
.expect("Failed to create Ads1115"),
),
Box::new(
Ak09915Device::builder()
.build()
.expect("Failed to create Ak09915"),
),
Box::new(
Bmp280Device::builder()
.build()
.expect("Failed to create Bmp390"),
),
Box::new(
Icm20689Device::builder()
.build()
.expect("Failed to create Icm20689"),
),
Box::new(
LeakDetector::builder()
.with_gpiochip(gpiochip)
.build()
.expect("Failed to create LedDetector"),
),
Box::new(LedController::builder().with_gpiochip(gpiochip).build()),
Box::new(
Pca9685Device::builder()
.with_gpiochip(gpiochip)
.with_i2c_bus("/dev/i2c-3")
.build()
.expect("Failed to create Pca9685"),
),
Box::new(
RgbController::builder()
.with_led_count(self.rgb_led_strip_size)
.build()
.expect("Failed to create RgbController"),
),
];

Navigator { devices }
}

pub fn build_navigator_v2_pi5(self) -> Navigator {
let gpiochip = "/dev/gpiochip4";
let devices: Vec<Box<dyn AnyHardware>> = vec![
Expand Down
Loading