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

Allow the ADC1 to be shared by multiple structs or methods #42

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions mightybuga_bsc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#![no_std]
#![allow(non_camel_case_types)]

use hal::gpio::PullDown;
// reexport hal crates to allow users to directly refer to them
// like in https://github.com/therealprof/nucleo-f103rb/blob/master/src/lib.rs
pub use stm32f1xx_hal as hal;

use hal::adc::Adc;
use hal::pac::*;
use hal::gpio::PullDown;
use hal::prelude::*;
use hal::serial::*;
use hal::timer::SysDelay;

use core::cell::RefCell;

use engine::engine::Engine;
use engine::motor::Motor;
use stm32f1xx_hal::timer::PwmChannel;
Expand Down Expand Up @@ -173,11 +175,9 @@ impl Mightybuga_BSC {
sensor_5: gpioa.pa5.into_analog(&mut gpioa.crl),
sensor_6: gpioa.pa6.into_analog(&mut gpioa.crl),
sensor_7: gpioa.pa7.into_analog(&mut gpioa.crl),
adc: adc1,
adc: RefCell::new(adc1),
};

// Return the initialized struct

// Return the initialized struct
Ok(Mightybuga_BSC {
led_d1: d1,
Expand All @@ -186,9 +186,9 @@ impl Mightybuga_BSC {
delay,
buzzer,
engine,
btn_1: btn_1,
btn_2: btn_2,
btn_3: btn_3,
btn_1,
btn_2,
btn_3,
light_sensor_array,
})
}
Expand Down
19 changes: 10 additions & 9 deletions mightybuga_bsc/src/light_sensor_array.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::cell::RefCell;
use crate::hal::{
adc::Adc,
gpio::{Analog, Output, Pin},
Expand All @@ -22,20 +23,20 @@ pub struct LightSensorArray {
pub sensor_6: Pin<'A', 6, Analog>,
pub sensor_7: Pin<'A', 7, Analog>,

pub adc: Adc<ADC1>,
pub adc: RefCell<Adc<ADC1>>,
}

impl light_sensor_array_controller::LightSensorArrayController for LightSensorArray {
fn get_light_map(&mut self) -> [u16; 8] {
let light_map = [
self.adc.read(&mut self.sensor_0).unwrap(),
self.adc.read(&mut self.sensor_1).unwrap(),
self.adc.read(&mut self.sensor_2).unwrap(),
self.adc.read(&mut self.sensor_3).unwrap(),
self.adc.read(&mut self.sensor_4).unwrap(),
self.adc.read(&mut self.sensor_5).unwrap(),
self.adc.read(&mut self.sensor_6).unwrap(),
self.adc.read(&mut self.sensor_7).unwrap(),
self.adc.get_mut().read(&mut self.sensor_0).unwrap(),
self.adc.get_mut().read(&mut self.sensor_1).unwrap(),
self.adc.get_mut().read(&mut self.sensor_2).unwrap(),
self.adc.get_mut().read(&mut self.sensor_3).unwrap(),
self.adc.get_mut().read(&mut self.sensor_4).unwrap(),
self.adc.get_mut().read(&mut self.sensor_5).unwrap(),
self.adc.get_mut().read(&mut self.sensor_6).unwrap(),
self.adc.get_mut().read(&mut self.sensor_7).unwrap(),
];

light_map
Expand Down
Loading