application: Use the previous/next buttons to cycle the current view.
[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 next_view(view: View) -> View {
92     match view {
93         View::Time => View::Distance,
94         View::Distance => View::Time,
95     }
96 }
97
98 fn previous_view(view: View) -> View {
99     match view {
100         View::Time => View::Distance,
101         View::Distance => View::Time,
102     }
103 }
104
105 fn reset_requested() -> bool {
106     let pta1 = gpio::get(gpio::GPIOA) & (1 << 1);
107     let pte25 = gpio::get(gpio::GPIOE) & (1 << 25);
108
109     (pta1 | pte25) == 0
110 }
111
112 #[inline(never)]
113 #[panic_handler]
114 fn panic(_info: &core::panic::PanicInfo) -> ! {
115     loop {
116         if reset_requested() {
117             nvic::system_reset();
118         }
119     }
120 }
121
122 fn configure_push_buttons() {
123     // Configure lower right push button.
124     gpio::set_direction(gpio::GPIOA, 1 << 1, gpio::Direction::Input);
125     port::set_af(port::PORTA, 1, 1);
126     port::set_pull(port::PORTA, 1, port::Pull::Up);
127
128     // Configure upper right push button.
129     gpio::set_direction(gpio::GPIOA, 1 << 12, gpio::Direction::Input);
130     port::set_af(port::PORTA, 12, 1);
131     port::set_pull(port::PORTA, 12, port::Pull::Up);
132
133     // Configure lower left push button.
134     gpio::set_direction(gpio::GPIOE, 1 << 24, gpio::Direction::Input);
135     port::set_af(port::PORTE, 24, 1);
136     port::set_pull(port::PORTE, 24, port::Pull::Up);
137
138     // Configure upper left push button.
139     gpio::set_direction(gpio::GPIOE, 1 << 25, gpio::Direction::Input);
140     port::set_af(port::PORTE, 25, 1);
141     port::set_pull(port::PORTE, 25, port::Pull::Up);
142
143     // Configure middle left push button.
144     gpio::set_direction(gpio::GPIOE, 1 << 31, gpio::Direction::Input);
145     port::set_af(port::PORTE, 31, 1);
146     port::set_pull(port::PORTE, 31, port::Pull::Up);
147 }
148
149 fn uart0_try_read() -> Option<u8> {
150     extern {
151         static mut uart0_rx_buf: Ringbuf;
152     }
153
154     unsafe {
155         if uart0_rx_buf.is_empty() {
156             None
157         } else {
158             Some(uart0_rx_buf.read())
159         }
160     }
161 }
162
163 #[no_mangle]
164 pub unsafe extern "C" fn _start() -> ! {
165     clock::configure();
166     clock::enable_osc0();
167     systick::init();
168     port::init();
169
170     // Configure pins for I2C0.
171     port::set_af(port::PORTC, 8, 2);
172     port::set_af(port::PORTC, 9, 2);
173
174     i2c::configure(i2c::I2C0);
175
176     nvic::disable_irq(8); // I2C0
177
178     // Configure pin for the display's reset line.
179     gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
180     port::set_af(port::PORTB, 16, 1);
181
182     // Configure pin for the MX25L's chip select line.
183     gpio::set_direction(gpio::GPIOD, 1 << 0, gpio::Direction::Output);
184     port::set_af(port::PORTD, 0, 1);
185     gpio::set(gpio::GPIOD, 1 << 0);
186
187     // Configure pins for SPI0.
188     port::set_af(port::PORTD, 1, 2);
189     port::set_af(port::PORTD, 2, 5);
190     port::set_af(port::PORTD, 3, 5);
191
192     spi::configure(spi::SPI0);
193
194     nvic::disable_irq(10); // SPI0
195
196     // Configure pins for UART0.
197     port::set_af(port::PORTE, 20, 4);
198     port::set_af(port::PORTE, 21, 4);
199
200     // Configure pin for the GPS's reset line.
201     gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output);
202     port::set_af(port::PORTB, 1, 1);
203
204     configure_push_buttons();
205
206     enable_interrupts();
207
208     usb_serial::init(0xf055, 0x635d);
209
210     cdc_tx_buf.write(b"\n");
211     cdc_tx_buf.flush();
212
213     let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
214
215     display.init();
216     display.clear();
217
218     let mut screen = screen::Screen::new();
219
220     // Hold GPS in reset while configuring its UART.
221     gpio::clear(gpio::GPIOB, 1);
222     systick::delay_ms(50);
223     uart::configure(uart::UART0);
224     systick::delay_ms(50);
225     gpio::set(gpio::GPIOB, 1);
226
227     nvic::enable_irq(12); // UART0
228
229     let mut shell = Shell::new(&mut cdc_tx_buf);
230
231     let mut mx25l = Mx25l::new(gpio::GPIOD, 1 << 0);
232
233     let mut logger = Logger::new(&mut mx25l);
234     logger.init();
235
236     let mut pta12 = button::Button::new(gpio::GPIOA, 1 << 12);
237     let mut pte24 = button::Button::new(gpio::GPIOE, 1 << 24);
238     let mut pta01 = button::Button::new(gpio::GPIOA, 1 << 1);
239     let mut is_recording = false;
240
241     let mut gps = gps::Gps::new();
242
243     let mut gps_has_fix = false;
244     let mut gps_has_fix_ticks = 0;
245
246     let mut heart_icon_timer = Timer::new(1000);
247     let mut gps_icon_timer = Timer::new(500);
248
249     let mut prev_tap = gps::TimeAndPos::new();
250
251     let mut view = View::Time;
252
253     let mut time_view = views::TimeView::new();
254     let mut distance_view = views::DistanceView::new();
255
256     let mut model = model::Model::new();
257
258     loop {
259         let mut tap = gps::TimeAndPos::new();
260         let old_gps_has_fix = gps_has_fix;
261
262         while gps.update(&mut tap, uart0_try_read) {
263             if is_recording {
264                 logger.log(&prev_tap, &tap);
265
266                 model.update(model::Field::Distance(logger.total_distance_cm));
267             }
268
269             model.update(model::Field::UnixTime(tap.unix_time));
270
271             prev_tap = tap;
272
273             gps_has_fix = true;
274             gps_has_fix_ticks = systick::now();
275         }
276
277         // Did GPS fix information expire?
278         if gps_has_fix && systick::has_timeout_ms(gps_has_fix_ticks, 1500) {
279             gps_has_fix = false;
280         }
281
282         if gps_has_fix && !old_gps_has_fix {
283             display.show_icon(display::Icon::SatelliteBody);
284             display.show_icon(display::Icon::SatelliteWave1);
285             display.show_icon(display::Icon::SatelliteWave2);
286         } else if !gps_has_fix {
287             gps_icon_timer.update(|state| {
288                 if state == 1 {
289                     display.show_icon(display::Icon::SatelliteWave1);
290                     2
291                 } else if state == 2 {
292                     display.show_icon(display::Icon::SatelliteWave2);
293                     3
294                 } else if state == 3 {
295                     display.hide_icon(display::Icon::SatelliteBody);
296                     display.hide_icon(display::Icon::SatelliteWave1);
297                     display.hide_icon(display::Icon::SatelliteWave2);
298                     0
299                 } else {
300                     display.show_icon(display::Icon::SatelliteBody);
301                     1
302                 }
303             });
304         }
305
306         match view {
307             View::Time => {
308                 if time_view.draw(&mut screen, &mut model) {
309                     display.draw(&screen);
310                 }
311             },
312             View::Distance => {
313                 if distance_view.draw(&mut screen, &mut model) {
314                     display.draw(&screen);
315                 }
316             },
317         }
318
319         heart_icon_timer.update(|state| {
320             if state == 1 {
321                 display.hide_icon(display::Icon::Heart);
322                 0
323             } else {
324                 display.show_icon(display::Icon::Heart);
325                 1
326             }
327         });
328
329         shell.update(&mut logger);
330
331         if pta12.has_been_held_for_ms(1500) {
332             is_recording = !is_recording;
333
334             if is_recording {
335                 logger.start_recording(&prev_tap);
336
337                 view = View::Distance;
338             } else {
339                 logger.stop_recording(&prev_tap);
340
341                 view = View::Time;
342             }
343
344             model.reset();
345         }
346
347         let new_view = if pte24.has_been_held_for_ms(250) {
348             previous_view(view)
349         } else if pta01.has_been_held_for_ms(250) {
350             next_view(view)
351         } else {
352             view
353         };
354
355         if view != new_view {
356             view = new_view;
357
358             match view {
359                 View::Time => time_view.invalidate(),
360                 View::Distance => distance_view.invalidate(),
361             }
362         }
363
364         if reset_requested() {
365             nvic::system_reset();
366         }
367     }
368 }