2b2f8f66bffd3b4441b13377d7e6c57f6ce88c0f
[gps-watch.git] / src / application / main.rs
1 /*
2  * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 #![no_std]
25 #![no_main]
26 #[link(name="libcommon.rlib")]
27
28 extern crate common;
29
30 mod uart0;
31 mod button;
32 mod model;
33 mod views;
34
35 use common::buffer::Buffer;
36 use common::ringbuf::Ringbuf;
37 use common::clock;
38 use common::systick;
39 use common::port;
40 use common::gpio;
41 use common::nvic;
42 use common::i2c;
43 use common::spi;
44 use common::uart;
45 use common::usb_serial;
46 use common::display;
47 use common::gps;
48 use common::screen;
49 use common::mx25l::Mx25l;
50 use common::shell::Shell;
51 use common::logger::Logger;
52
53 extern {
54     fn enable_interrupts();
55
56     static mut cdc_tx_buf: Buffer;
57 }
58
59 #[derive(Clone, Copy, PartialEq)]
60 enum View {
61     Time,
62     Distance,
63 }
64
65 struct Timer {
66     state: u32,
67     delay_ms: u32,
68     last_update_ticks: u32,
69 }
70
71 impl Timer {
72     pub fn new(delay_ms: u32) -> Timer {
73         Timer {
74             state: 0,
75             delay_ms: delay_ms,
76             last_update_ticks: systick::now(),
77         }
78     }
79
80     pub fn update<F>(&mut self, func: F)
81         where F: FnOnce(u32) -> u32
82     {
83         if systick::has_timeout_ms(self.last_update_ticks, self.delay_ms) {
84             self.state = func(self.state);
85
86             self.last_update_ticks = systick::now();
87         }
88     }
89 }
90
91 fn reset_requested() -> bool {
92     let pta1 = gpio::get(gpio::GPIOA) & (1 << 1);
93     let pte25 = gpio::get(gpio::GPIOE) & (1 << 25);
94
95     (pta1 | pte25) == 0
96 }
97
98 #[inline(never)]
99 #[panic_handler]
100 fn panic(_info: &core::panic::PanicInfo) -> ! {
101     loop {
102         if reset_requested() {
103             nvic::system_reset();
104         }
105     }
106 }
107
108 fn configure_push_buttons() {
109     // Configure lower right push button.
110     gpio::set_direction(gpio::GPIOA, 1 << 1, gpio::Direction::Input);
111     port::set_af(port::PORTA, 1, 1);
112     port::set_pull(port::PORTA, 1, port::Pull::Up);
113
114     // Configure upper right push button.
115     gpio::set_direction(gpio::GPIOA, 1 << 12, gpio::Direction::Input);
116     port::set_af(port::PORTA, 12, 1);
117     port::set_pull(port::PORTA, 12, port::Pull::Up);
118
119     // Configure lower left push button.
120     gpio::set_direction(gpio::GPIOE, 1 << 24, gpio::Direction::Input);
121     port::set_af(port::PORTE, 24, 1);
122     port::set_pull(port::PORTE, 24, port::Pull::Up);
123
124     // Configure upper left push button.
125     gpio::set_direction(gpio::GPIOE, 1 << 25, gpio::Direction::Input);
126     port::set_af(port::PORTE, 25, 1);
127     port::set_pull(port::PORTE, 25, port::Pull::Up);
128
129     // Configure middle left push button.
130     gpio::set_direction(gpio::GPIOE, 1 << 31, gpio::Direction::Input);
131     port::set_af(port::PORTE, 31, 1);
132     port::set_pull(port::PORTE, 31, port::Pull::Up);
133 }
134
135 fn uart0_try_read() -> Option<u8> {
136     extern {
137         static mut uart0_rx_buf: Ringbuf;
138     }
139
140     unsafe {
141         if uart0_rx_buf.is_empty() {
142             None
143         } else {
144             Some(uart0_rx_buf.read())
145         }
146     }
147 }
148
149 #[no_mangle]
150 pub unsafe extern "C" fn _start() -> ! {
151     clock::configure();
152     clock::enable_osc0();
153     systick::init();
154     port::init();
155
156     // Configure pins for I2C0.
157     port::set_af(port::PORTC, 8, 2);
158     port::set_af(port::PORTC, 9, 2);
159
160     i2c::configure(i2c::I2C0);
161
162     nvic::disable_irq(8); // I2C0
163
164     // Configure pin for the display's reset line.
165     gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
166     port::set_af(port::PORTB, 16, 1);
167
168     // Configure pin for the MX25L's chip select line.
169     gpio::set_direction(gpio::GPIOD, 1 << 0, gpio::Direction::Output);
170     port::set_af(port::PORTD, 0, 1);
171     gpio::set(gpio::GPIOD, 1 << 0);
172
173     // Configure pins for SPI0.
174     port::set_af(port::PORTD, 1, 2);
175     port::set_af(port::PORTD, 2, 5);
176     port::set_af(port::PORTD, 3, 5);
177
178     spi::configure(spi::SPI0);
179
180     nvic::disable_irq(10); // SPI0
181
182     // Configure pins for UART0.
183     port::set_af(port::PORTE, 20, 4);
184     port::set_af(port::PORTE, 21, 4);
185
186     // Configure pin for the GPS's reset line.
187     gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output);
188     port::set_af(port::PORTB, 1, 1);
189
190     configure_push_buttons();
191
192     enable_interrupts();
193
194     usb_serial::init(0xf055, 0x635d);
195
196     cdc_tx_buf.write(b"\n");
197     cdc_tx_buf.flush();
198
199     let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
200
201     display.init();
202     display.clear();
203
204     let mut screen = screen::Screen::new();
205
206     // Hold GPS in reset while configuring its UART.
207     gpio::clear(gpio::GPIOB, 1);
208     systick::delay_ms(50);
209     uart::configure(uart::UART0);
210     systick::delay_ms(50);
211     gpio::set(gpio::GPIOB, 1);
212
213     nvic::enable_irq(12); // UART0
214
215     let mut shell = Shell::new(&mut cdc_tx_buf);
216
217     let mut mx25l = Mx25l::new(gpio::GPIOD, 1 << 0);
218
219     let mut logger = Logger::new(&mut mx25l);
220     logger.init();
221
222     let mut pta12 = button::Button::new(gpio::GPIOA, 1 << 12);
223     let mut is_recording = false;
224
225     let mut gps = gps::Gps::new();
226
227     let mut gps_has_fix = false;
228     let mut gps_has_fix_ticks = 0;
229
230     let mut heart_icon_timer = Timer::new(1000);
231     let mut gps_icon_timer = Timer::new(500);
232
233     let mut prev_tap = gps::TimeAndPos::new();
234
235     let mut view = View::Time;
236
237     let mut time_view = views::TimeView::new();
238     let mut distance_view = views::DistanceView::new();
239
240     let mut model = model::Model::new();
241
242     loop {
243         let mut tap = gps::TimeAndPos::new();
244         let old_gps_has_fix = gps_has_fix;
245
246         while gps.update(&mut tap, uart0_try_read) {
247             if is_recording {
248                 logger.log(&prev_tap, &tap);
249
250                 model.update(model::Field::Distance(logger.total_distance_cm));
251             }
252
253             model.update(model::Field::UnixTime(tap.unix_time));
254
255             prev_tap = tap;
256
257             gps_has_fix = true;
258             gps_has_fix_ticks = systick::now();
259         }
260
261         // Did GPS fix information expire?
262         if gps_has_fix && systick::has_timeout_ms(gps_has_fix_ticks, 1500) {
263             gps_has_fix = false;
264         }
265
266         if gps_has_fix && !old_gps_has_fix {
267             display.show_icon(display::Icon::SatelliteBody);
268             display.show_icon(display::Icon::SatelliteWave1);
269             display.show_icon(display::Icon::SatelliteWave2);
270         } else if !gps_has_fix {
271             gps_icon_timer.update(|state| {
272                 if state == 1 {
273                     display.show_icon(display::Icon::SatelliteWave1);
274                     2
275                 } else if state == 2 {
276                     display.show_icon(display::Icon::SatelliteWave2);
277                     3
278                 } else if state == 3 {
279                     display.hide_icon(display::Icon::SatelliteBody);
280                     display.hide_icon(display::Icon::SatelliteWave1);
281                     display.hide_icon(display::Icon::SatelliteWave2);
282                     0
283                 } else {
284                     display.show_icon(display::Icon::SatelliteBody);
285                     1
286                 }
287             });
288         }
289
290         match view {
291             View::Time => {
292                 if time_view.draw(&mut screen, &mut model) {
293                     display.draw(&screen);
294                 }
295             },
296             View::Distance => {
297                 if distance_view.draw(&mut screen, &mut model) {
298                     display.draw(&screen);
299                 }
300             },
301         }
302
303         heart_icon_timer.update(|state| {
304             if state == 1 {
305                 display.hide_icon(display::Icon::Heart);
306                 0
307             } else {
308                 display.show_icon(display::Icon::Heart);
309                 1
310             }
311         });
312
313         shell.update(&mut logger);
314
315         if pta12.has_been_held_for_ms(1500) {
316             is_recording = !is_recording;
317
318             if is_recording {
319                 logger.start_recording(&prev_tap);
320
321                 view = View::Distance;
322             } else {
323                 logger.stop_recording(&prev_tap);
324
325                 view = View::Time;
326             }
327
328             model.reset();
329         }
330
331         if reset_requested() {
332             nvic::system_reset();
333         }
334     }
335 }