-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial_demo_api.rs
122 lines (103 loc) · 2.01 KB
/
serial_demo_api.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use core::prelude::*;
use core::slice;
use collections::Vec;
extern {
fn stm32_delay(millis: u32);
fn usart2_send_string(str: *const u8, len: u16);
fn usart2_send_byte(byte: u8);
fn usart2_try_get_byte() -> i16;
fn stm32_toggle_led(led: u8);
fn stm32_enable_led(led: u8);
fn stm32_disable_led(led: u8);
}
pub fn delay(millis: u32) {
unsafe {
stm32_delay(millis);
}
}
#[derive(Copy, Clone)]
pub enum STM32_USART_Device {
USART2
}
#[derive(Copy, Clone)]
pub struct STM32_USART {
device: STM32_USART_Device
}
impl STM32_USART {
pub fn new(device: STM32_USART_Device) -> STM32_USART {
STM32_USART {
device: device
}
}
pub fn print(&self, str: &str) {
let bytes = str.bytes().collect::<Vec<u8>>();
self.print_bytes(bytes.as_slice());
}
pub fn print_bytes(&self, bytes: &[u8]) {
unsafe {
match self.device {
STM32_USART_Device::USART2 => usart2_send_string(bytes.as_ptr(), bytes.len() as u16)
}
}
}
pub fn println(&self, str: &str) {
self.print(str);
self.print("\r\n");
}
pub fn send_byte(&self, byte: u8) {
unsafe {
match self.device {
STM32_USART_Device::USART2 => usart2_send_byte(byte)
}
}
}
pub fn try_read_byte(&self) -> Option<u8> {
unsafe {
let r = usart2_try_get_byte();
if r == -1 { return None; }
return Some(r as u8);
}
}
}
use cli::{CliTerminal, CliPromptTerminal};
impl CliTerminal for STM32_USART {
fn output_line(&mut self, line: &str) {
self.println(line);
}
}
impl CliPromptTerminal for STM32_USART {
fn print_bytes(&self, bytes: &[u8]) {
self.print_bytes(bytes);
}
}
pub enum STM32Led {
Red,
Green,
Blue,
Orange
}
impl STM32Led {
fn to_api(&self) -> u8 {
match *self {
STM32Led::Red => 1,
STM32Led::Green => 2,
STM32Led::Blue => 3,
STM32Led::Orange => 0
}
}
}
pub fn stm32_toggle(led: STM32Led) {
unsafe {
stm32_toggle_led(led.to_api());
}
}
pub fn stm32_enable(led: STM32Led) {
unsafe {
stm32_enable_led(led.to_api());
}
}
pub fn stm32_disable(led: STM32Led) {
unsafe {
stm32_disable_led(led.to_api());
}
}