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

Only use safe functions in main #6

Open
wants to merge 3 commits into
base: teensy_3_5_test
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
build:
@xargo build --release
xargo build --release --target thumbv7em-none-eabi

flash: build
arm-none-eabi-objcopy -O ihex -R .eeprom target/thumbv7em-none-eabi/release/teensy3-rs-demo target/hex
teensy-loader-cli -w -s --mcu=mk20dx256 target/hex
teensy-loader-cli -w -s --mcu=mk64fx512 target/hex

monitor:
screen /dev/ttyACM0 115200

clean:
xargo clean
Expand Down
45 changes: 26 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,45 @@
#[macro_use]
extern crate teensy3;

use teensy3::bindings;
use teensy3::serial::Serial;
use teensy3::util::{
digital_write,
analog_read,
analog_write,
pin_mode,
delay,
PinMode
};

mod motor;

#[no_mangle]
pub unsafe extern fn main() {
pub extern fn main() {
// Blink Loop

bindings::pinMode(13, bindings::OUTPUT as u8);
bindings::digitalWrite(13, bindings::LOW as u8);
let mut ser = Serial{};
pin_mode(13, PinMode::Output);
pin_mode(14, PinMode::Input);
digital_write(13, true);

loop {
// Show we are alive
alive();
let motor = motor::HBridge::new(20, (19, 18));

// If the serial write fails, we will halt (no more alive blinks)
hello(&ser).unwrap();
let ser = Serial{};

// Don't spam the console
bindings::delay(1000);
loop {
motor.set_state(motor::HBridgeState::Forward, (analog_read(14) / 4) as u8);
delay(10);
}
}

/// Blink the light twice to know we're alive
pub unsafe fn alive() {
pub fn alive() {
for _ in 0..2 {
bindings::digitalWrite(13, bindings::LOW as u8);
bindings::delay(200);
bindings::digitalWrite(13, bindings::HIGH as u8);
bindings::delay(200);
bindings::digitalWrite(13, bindings::LOW as u8);
bindings::delay(200);
digital_write(13, false);
delay(200);
digital_write(13, true);
delay(200);
digital_write(13, false);
delay(200);
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/motor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use teensy3::util::{
digital_write,
analog_write,
pin_mode,
PinMode
};


/**
Struct for controlling a SN754410 H bridge motor driver
*/
pub struct HBridge
{
enable_pin: u8,
input_pins: (u8, u8),
}

pub enum HBridgeState {
Disable,
Forward,
Backward,
}


impl HBridge
{
pub fn new(enable_pin: u8, input_pins: (u8, u8)) -> HBridge
{
pin_mode(enable_pin, PinMode::Output);
pin_mode(input_pins.0, PinMode::Output);
pin_mode(input_pins.1, PinMode::Output);

HBridge {
enable_pin: enable_pin,
input_pins: input_pins,
}
}

pub fn set_state(&self, state: HBridgeState, speed: u8)
{
match state
{
HBridgeState::Disable =>
{
digital_write(self.enable_pin, false);
},
HBridgeState::Forward =>
{
analog_write(self.enable_pin, speed);
digital_write(self.input_pins.0, true);
digital_write(self.input_pins.1, false);
},
HBridgeState::Backward =>
{
analog_write(self.enable_pin, speed);
digital_write(self.enable_pin, true);
digital_write(self.input_pins.0, false);
digital_write(self.input_pins.1, true);
}
}
}
}