Skip to content

Commit

Permalink
feat: use display to report current throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckeogh committed Apr 21, 2019
1 parent 26426a1 commit df94cb2
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 11 deletions.
8 changes: 4 additions & 4 deletions controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ bench = false

[dependencies]
cortex-m = "0.5.8"
cortex-m-rtfm = "0.4.2"
cortex-m-rtfm = { version = "0.4.2", features = ["timer-queue"] }
cortex-m-rt = "0.6.8"
embedded-hal = "0.2.2"
nrf52810-hal = { git = "https://github.com/chocol4te/nrf52-hal", rev = "5befd35", features = ["rt"] }
nb = "0.1.1"
fpa = "0.1.0"
byteorder = { version = "1.3.1", default-features = false }
panic-semihosting = "0.5.1"
bitflags = "1.0.4"
uuid = { version = "0.7.4", default-features = false }
rubble = { git = "https://github.com/jonas-schievink/rubble.git", rev = "0e49250" }
rubble-nrf52810 = { git = "https://github.com/jonas-schievink/rubble.git", rev = "0e49250" }
log = "0.4.6"
bbqueue = "0.3.2"
ssd1306 = "0.2.4"
embedded-graphics = "0.4.7"
alloc-cortex-m = "0.3.5"
121 changes: 114 additions & 7 deletions controller/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
#![no_std]
#![no_main]
#![feature(alloc)]
#![feature(global_allocator)]
#![feature(lang_items)]

// We need to import this crate explicitly so we have a panic handler
#[macro_use]
extern crate alloc;
extern crate alloc_cortex_m;
extern crate panic_semihosting;
#[macro_use]
extern crate cortex_m_rt as rt;

mod logger;

use {
crate::logger::{BbqLogger, StampedLogger},
alloc_cortex_m::CortexMHeap,
bbqueue::{bbq, BBQueue, Consumer},
core::fmt::Write,
embedded_graphics::{fonts::Font12x16, image::Image1BPP, prelude::*},
embedded_hal::adc::OneShot,
log::{info, LevelFilter},
nrf52810_hal::{
self as hal,
gpio::Level,
nrf52810_pac::{self as pac, UARTE0},
gpio::{
p0::{P0_02, P0_03},
Floating, Input, Level, Output, Pin, PushPull,
},
nrf52810_pac::{self as pac, SPIM0, UARTE0},
prelude::*,
saadc::{Gain, Oversample, Reference, Resistor, Resolution, Saadc, SaadcConfig, Time},
spim::{self, Frequency, Spim, MODE_0},
uarte::{Baudrate, Parity, Uarte},
},
rtfm::app,
Expand All @@ -36,8 +50,16 @@ use {
radio::{BleRadio, PacketBuffer},
timer::{BleTimer, StampSource},
},
ssd1306::{
displayrotation::DisplayRotation, interface::spi::SpiInterface,
mode::graphics::GraphicsMode, prelude::*,
},
core::alloc::Layout,
};

#[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();

type Logger = StampedLogger<StampSource<pac::TIMER0>, BbqLogger>;

/// Hardware interface for the BLE stack (nRF52810 implementation).
Expand All @@ -62,12 +84,20 @@ const APP: () = {
static mut SERIAL: Uarte<UARTE0> = ();
static mut LOG_SINK: Consumer = ();

static mut DISPLAY: GraphicsMode<SpiInterface<Spim<SPIM0>, Pin<Output<PushPull>>>> = ();

static mut ADC: Saadc = ();
static mut ADC_PIN: hal::gpio::p0::P0_02<hal::gpio::Input<hal::gpio::Floating>> = ();
static mut ADC_CONTROL_PIN: P0_02<Input<Floating>> = ();
static mut ADC_BATT_PIN: P0_03<Input<Floating>> = ();

#[init(resources = [BLE_TX_BUF, BLE_RX_BUF])]
fn init() {
//hprintln!("\n<< INIT >>\n").ok();
{
let start = rt::heap_start() as usize;
let size = 1024; // in bytes
unsafe { ALLOCATOR.init(start, size) }
}

{
// On reset the internal high frequency clock is used, but starting the HFCLK task
Expand Down Expand Up @@ -161,15 +191,53 @@ const APP: () = {
Saadc::new(device.SAADC, config)
};

let display = {
let spi = {
let mosi = Some(p0.p0_15.into_push_pull_output(Level::Low).degrade());
let sck = p0.p0_13.into_push_pull_output(Level::Low).degrade();

Spim::new(
device.SPIM0,
spim::Pins {
sck,
mosi,
miso: None,
},
Frequency::M8,
MODE_0,
0u8,
)
};

let dc = p0.p0_17.into_push_pull_output(Level::Low).degrade();
let mut display: GraphicsMode<_> = ssd1306::Builder::new()
.with_rotation(DisplayRotation::Rotate90)
.connect_spi(spi, dc)
.into();

// Reset display
let mut rst = p0.p0_19.into_push_pull_output(Level::High).degrade();
rst.set_low();
rst.set_high();

display.init().unwrap();
display.flush().unwrap();

display
};

RADIO = radio;
BLE_LL = ll;
BLE_R = resp;
BEACON_TIMER = device.TIMER1;
SERIAL = serial;
LOG_SINK = log_sink;

DISPLAY = display;

ADC = adc;
ADC_PIN = p0.p0_02.into_floating_input();
ADC_CONTROL_PIN = p0.p0_02.into_floating_input();
ADC_BATT_PIN = p0.p0_03.into_floating_input();
}

#[interrupt(resources = [RADIO, BLE_LL])]
Expand All @@ -181,16 +249,16 @@ const APP: () = {
}

/// Fire the beacon.
#[interrupt(resources = [BEACON_TIMER, RADIO, ADC, ADC_PIN])]
#[interrupt(resources = [BEACON_TIMER, RADIO, ADC, ADC_CONTROL_PIN, DISPLAY])]
fn TIMER1() {
// acknowledge event
resources.BEACON_TIMER.events_compare[0].reset();

let device_address = DeviceAddress::new([169, 255, 235, 206, 50, 121], AddressKind::Random);

let val: u16 = resources.ADC.read(resources.ADC_PIN).unwrap();
let val: u16 = resources.ADC.read(resources.ADC_CONTROL_PIN).unwrap();

info!("read val: {}", val);
//info!("read val: {}", val);

let beacon = Beacon::new(
device_address,
Expand All @@ -202,6 +270,20 @@ const APP: () = {
.unwrap();

beacon.broadcast(&mut *resources.RADIO);

resources.DISPLAY.clear();
resources.DISPLAY.draw(
Font12x16::render_str(&format!("{}%", val / 164))
.with_stroke(Some(1u8.into()))
.translate(Coord::new(16, 16))
.into_iter(),
);
resources.DISPLAY.draw(
Image1BPP::new(include_bytes!("./rust.raw"), 32, 32)
.translate(Coord::new(32, 96))
.into_iter(),
);
resources.DISPLAY.flush().unwrap();
}

#[idle(resources = [LOG_SINK, SERIAL, BLE_R])]
Expand All @@ -221,4 +303,29 @@ const APP: () = {
}
}
}

/*
#[task(resources = [DISPLAY, BOOL])]
fn update_display() {
info!("update_display task");
let im = Image1BPP::new(include_bytes!("./rust.raw"), 32, 32).translate(Coord::new(32, 96));
*resources.BOOL ^= true;
resources.DISPLAY.clear();
resources.DISPLAY.draw(im.into_iter());
resources.DISPLAY.flush().unwrap();
}
extern "C" {
fn PDM();
}
*/
};

#[lang = "oom"]
#[no_mangle]
pub fn rust_oom(layout: Layout) -> ! {
panic!();
}
Binary file added controller/src/rust.raw
Binary file not shown.

0 comments on commit df94cb2

Please sign in to comment.