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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use core::cell::Cell;
use kernel::{AppId, Callback, Driver, Grant};
use kernel::ReturnCode;
use kernel::hil;
pub const DRIVER_NUM: usize = 0x60000;
#[derive(Default)]
pub struct App {
callback: Option<Callback>,
subscribed: bool,
}
pub struct TemperatureSensor<'a> {
driver: &'a hil::sensors::TemperatureDriver,
apps: Grant<App>,
busy: Cell<bool>,
}
impl<'a> TemperatureSensor<'a> {
pub fn new(
driver: &'a hil::sensors::TemperatureDriver,
grant: Grant<App>,
) -> TemperatureSensor<'a> {
TemperatureSensor {
driver: driver,
apps: grant,
busy: Cell::new(false),
}
}
fn enqueue_command(&self, appid: AppId) -> ReturnCode {
self.apps
.enter(appid, |app, _| {
if !self.busy.get() {
app.subscribed = true;
self.busy.set(true);
self.driver.read_temperature()
} else {
ReturnCode::EBUSY
}
})
.unwrap_or_else(|err| err.into())
}
fn configure_callback(&self, callback: Callback) -> ReturnCode {
self.apps
.enter(callback.app_id(), |app, _| {
app.callback = Some(callback);
ReturnCode::SUCCESS
})
.unwrap_or_else(|err| err.into())
}
}
impl<'a> hil::sensors::TemperatureClient for TemperatureSensor<'a> {
fn callback(&self, temp_val: usize) {
for cntr in self.apps.iter() {
cntr.enter(|app, _| {
if app.subscribed {
self.busy.set(false);
app.subscribed = false;
app.callback.map(|mut cb| cb.schedule(temp_val, 0, 0));
}
});
}
}
}
impl<'a> Driver for TemperatureSensor<'a> {
fn subscribe(&self, subscribe_num: usize, callback: Callback) -> ReturnCode {
match subscribe_num {
0 => self.configure_callback(callback),
_ => ReturnCode::ENOSUPPORT,
}
}
fn command(&self, command_num: usize, _: usize, _: usize, appid: AppId) -> ReturnCode {
match command_num {
0 => ReturnCode::SUCCESS,
1 => self.enqueue_command(appid),
_ => ReturnCode::ENOSUPPORT,
}
}
}