Capitalize the resource names for consistency

Since the resources are implemented via static mut's, it make sense
stylewise to capitalize them.
This commit is contained in:
Paul van Tilburg 2019-06-24 22:32:40 +02:00
parent e0f6a87cf9
commit d8e135d245
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
1 changed files with 49 additions and 47 deletions

View File

@ -40,14 +40,14 @@ const PERIOD: u32 = 8_000_000;
#[app(device = hal::stm32)] #[app(device = hal::stm32)]
const APP: () = { const APP: () = {
static mut button: UserButton = (); static mut ACCEL: Accelerometer = ();
static mut buffer: Vec<u8, U8> = (); static mut ACCEL_CS: AccelerometerCs = ();
static mut led_ring: LedRing<Led> = (); static mut BUFFER: Vec<u8, U8> = ();
static mut exti: EXTI = (); static mut BUTTON: UserButton = ();
static mut serial_rx: SerialRx = (); static mut EXTI_CNTR: EXTI = ();
static mut serial_tx: SerialTx = (); static mut LED_RING: LedRing<Led> = ();
static mut accel: Accelerometer = (); static mut SERIAL_RX: SerialRx = ();
static mut accel_cs: AccelerometerCs = (); static mut SERIAL_TX: SerialTx = ();
/// Initializes the application by setting up the LED ring, user button, serial /// Initializes the application by setting up the LED ring, user button, serial
/// interface and accelerometer. /// interface and accelerometer.
@ -69,11 +69,11 @@ const APP: () = {
} }
// Set up the EXTI0 interrupt for the user button. // Set up the EXTI0 interrupt for the user button.
let mut exti = device.EXTI; let mut exti_cntr = device.EXTI;
let gpioa = device.GPIOA.split(); let gpioa = device.GPIOA.split();
let mut button = gpioa.pa0.into_floating_input(); let mut button = gpioa.pa0.into_floating_input();
button.enable_interrupt(&mut exti); button.enable_interrupt(&mut exti_cntr);
button.trigger_on_edge(&mut exti, Edge::RISING); button.trigger_on_edge(&mut exti_cntr, Edge::RISING);
// Set up the serial interface and the USART2 interrupt. // Set up the serial interface and the USART2 interrupt.
let tx = gpioa.pa2.into_alternate_af7(); let tx = gpioa.pa2.into_alternate_af7();
@ -110,21 +110,21 @@ const APP: () = {
writeln!(serial_tx, "init\r").unwrap(); writeln!(serial_tx, "init\r").unwrap();
init::LateResources { init::LateResources {
button, ACCEL: accel,
buffer, ACCEL_CS: accel_cs,
exti, BUFFER: buffer,
led_ring, BUTTON: button,
serial_tx, EXTI_CNTR: exti_cntr,
serial_rx, LED_RING: led_ring,
accel, SERIAL_RX: serial_rx,
accel_cs, SERIAL_TX: serial_tx,
} }
} }
/// Task that advances the LED ring one step and schedules the next trigger (if enabled). /// Task that advances the LED ring one step and schedules the next trigger (if enabled).
#[task(schedule = [cycle_leds], resources = [led_ring])] #[task(schedule = [cycle_leds], resources = [LED_RING])]
fn cycle_leds() { fn cycle_leds() {
resources.led_ring.lock(|led_ring| { resources.LED_RING.lock(|led_ring| {
if led_ring.is_mode_cycle() { if led_ring.is_mode_cycle() {
led_ring.advance(); led_ring.advance();
schedule.cycle_leds(scheduled + PERIOD.cycles()).unwrap(); schedule.cycle_leds(scheduled + PERIOD.cycles()).unwrap();
@ -134,23 +134,23 @@ const APP: () = {
/// Task that performs an accelerometers measurement and adjusts the LED ring accordingly /// Task that performs an accelerometers measurement and adjusts the LED ring accordingly
/// and schedules the next trigger (if enabled). /// and schedules the next trigger (if enabled).
#[task(schedule = [accel_leds], resources = [accel, accel_cs, led_ring, serial_tx])] #[task(schedule = [accel_leds], resources = [ACCEL, ACCEL_CS, LED_RING, SERIAL_TX])]
fn accel_leds() { fn accel_leds() {
resources.accel_cs.set_low(); resources.ACCEL_CS.set_low();
let read_command = (1 << 7) | (1 << 6) | 0x29; let read_command = (1 << 7) | (1 << 6) | 0x29;
let mut commands = [read_command, 0x0, 0x0, 0x0]; let mut commands = [read_command, 0x0, 0x0, 0x0];
let result = resources.accel.transfer(&mut commands[..]).unwrap(); let result = resources.ACCEL.transfer(&mut commands[..]).unwrap();
let acc_x = result[1] as i8; let acc_x = result[1] as i8;
let acc_y = result[3] as i8; let acc_y = result[3] as i8;
resources.accel_cs.set_high(); resources.ACCEL_CS.set_high();
if acc_x == 0 && acc_y == 0 { if acc_x == 0 && acc_y == 0 {
resources resources
.serial_tx .SERIAL_TX
.lock(|serial_tx| writeln!(serial_tx, "level\r").unwrap()); .lock(|serial_tx| writeln!(serial_tx, "level\r").unwrap());
} }
resources.led_ring.lock(|led_ring| { resources.LED_RING.lock(|led_ring| {
if led_ring.is_mode_accel() { if led_ring.is_mode_accel() {
let directions = [acc_y < 0, acc_x < 0, acc_y > 0, acc_x > 0]; let directions = [acc_y < 0, acc_x < 0, acc_y > 0, acc_x > 0];
led_ring.specific_on(directions); led_ring.specific_on(directions);
@ -161,16 +161,18 @@ const APP: () = {
/// Interrupt handler that writes that the button is pressed to the serial interface /// Interrupt handler that writes that the button is pressed to the serial interface
/// and reverses the LED ring cycle direction. /// and reverses the LED ring cycle direction.
#[interrupt(binds = EXTI0, resources = [button, exti, led_ring, serial_tx])] #[interrupt(binds = EXTI0, resources = [BUTTON, EXTI_CNTR, LED_RING, SERIAL_TX])]
fn button_pressed() { fn button_pressed() {
resources.led_ring.lock(|led_ring| led_ring.reverse()); resources.LED_RING.lock(|led_ring| led_ring.reverse());
// Write the fact that the button has been pressed to the serial port. // Write the fact that the button has been pressed to the serial port.
resources resources
.serial_tx .SERIAL_TX
.lock(|serial_tx| writeln!(serial_tx, "button\r").unwrap()); .lock(|serial_tx| writeln!(serial_tx, "button\r").unwrap());
resources.button.clear_interrupt_pending_bit(resources.exti); resources
.BUTTON
.clear_interrupt_pending_bit(resources.EXTI_CNTR);
} }
/// Interrupt handler that reads data from the serial connection and handles commands /// Interrupt handler that reads data from the serial connection and handles commands
@ -178,55 +180,55 @@ const APP: () = {
#[interrupt( #[interrupt(
binds = USART2, binds = USART2,
priority = 2, priority = 2,
resources = [buffer, led_ring, serial_rx, serial_tx], resources = [BUFFER, LED_RING, SERIAL_RX, SERIAL_TX],
spawn = [accel_leds, cycle_leds] spawn = [accel_leds, cycle_leds]
)] )]
fn handle_serial() { fn handle_serial() {
let buffer = resources.buffer; let buffer = resources.BUFFER;
// Read a byte from the serial port and write it back. // Read a byte from the serial port and write it back.
let byte = resources.serial_rx.read().unwrap(); let byte = resources.SERIAL_RX.read().unwrap();
block!(resources.serial_tx.write(byte)).unwrap(); block!(resources.SERIAL_TX.write(byte)).unwrap();
//hprintln!("serial: {}", byte).unwrap(); //hprintln!("serial: {}", byte).unwrap();
// Handle the command in the buffer for newline or backspace, otherwise append to the // Handle the command in the buffer for newline or backspace, otherwise append to the
// buffer. // buffer.
if byte == b'\r' { if byte == b'\r' {
block!(resources.serial_tx.write(b'\n')).unwrap(); block!(resources.SERIAL_TX.write(b'\n')).unwrap();
match &buffer[..] { match &buffer[..] {
b"flip" => { b"flip" => {
resources.led_ring.reverse(); resources.LED_RING.reverse();
} }
b"stop" => { b"stop" => {
resources.led_ring.disable(); resources.LED_RING.disable();
} }
b"cycle" => { b"cycle" => {
resources.led_ring.enable_cycle(); resources.LED_RING.enable_cycle();
spawn.cycle_leds().unwrap(); spawn.cycle_leds().unwrap();
} }
b"accel" => { b"accel" => {
resources.led_ring.enable_accel(); resources.LED_RING.enable_accel();
spawn.accel_leds().unwrap(); spawn.accel_leds().unwrap();
} }
b"off" => { b"off" => {
resources.led_ring.disable(); resources.LED_RING.disable();
resources.led_ring.all_off(); resources.LED_RING.all_off();
} }
b"on" => { b"on" => {
resources.led_ring.disable(); resources.LED_RING.disable();
resources.led_ring.all_on(); resources.LED_RING.all_on();
} }
_ => { _ => {
writeln!(resources.serial_tx, "?\r").unwrap(); writeln!(resources.SERIAL_TX, "?\r").unwrap();
} }
} }
buffer.clear(); buffer.clear();
} else if byte == 0x7F { } else if byte == 0x7F {
buffer.pop(); buffer.pop();
block!(resources.serial_tx.write(b'\r')).unwrap(); block!(resources.SERIAL_TX.write(b'\r')).unwrap();
for byte in buffer { for byte in buffer {
block!(resources.serial_tx.write(*byte)).unwrap(); block!(resources.SERIAL_TX.write(*byte)).unwrap();
} }
} else { } else {
if buffer.push(byte).is_err() { if buffer.push(byte).is_err() {