92429e5d4a404d05ed0a4ef9bba4a544cabe60ff
[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
32 use common::buffer::Buffer;
33 use common::clock;
34 use common::systick;
35 use common::port;
36 use common::gpio;
37 use common::nvic;
38 use common::i2c;
39 use common::uart;
40 use common::usb_serial;
41 use common::display;
42 use common::gps;
43 use common::screen;
44 use common::time::Time;
45
46 extern {
47     fn enable_interrupts();
48
49     static mut cdc_tx_buf: Buffer;
50 }
51
52 struct Timer {
53     state: u32,
54     delay_ms: u32,
55     last_update_ticks: u32,
56 }
57
58 impl Timer {
59     pub fn new(delay_ms: u32) -> Timer {
60         Timer {
61             state: 0,
62             delay_ms: delay_ms,
63             last_update_ticks: systick::now(),
64         }
65     }
66
67     pub fn update<F>(&mut self, func: F)
68         where F: FnOnce(u32) -> u32
69     {
70         if systick::has_timeout_ms(self.last_update_ticks, self.delay_ms) {
71             self.state = func(self.state);
72
73             self.last_update_ticks = systick::now();
74         }
75     }
76 }
77
78 #[inline(never)]
79 #[panic_handler]
80 fn panic(_info: &core::panic::PanicInfo) -> ! {
81     loop {
82         if (gpio::get(gpio::GPIOA) & (1 << 12)) == 0 {
83             nvic::system_reset();
84         }
85     }
86 }
87
88 #[no_mangle]
89 pub unsafe extern "C" fn _start() -> ! {
90     clock::configure();
91     clock::enable_osc0();
92     systick::init();
93     port::init();
94
95     // Configure pins for I2C0.
96     port::set_af(port::PORTC, 8, 2);
97     port::set_af(port::PORTC, 9, 2);
98
99     i2c::configure(i2c::I2C0);
100
101     nvic::disable_irq(8); // I2C0
102
103     // Configure pin for the display's reset line.
104     gpio::set_direction(gpio::GPIOB, 1 << 16, gpio::Direction::Output);
105     port::set_af(port::PORTB, 16, 1);
106
107     // Configure pins for UART0.
108     port::set_af(port::PORTE, 20, 4);
109     port::set_af(port::PORTE, 21, 4);
110
111     // Configure pin for the GPS's reset line.
112     gpio::set_direction(gpio::GPIOB, 1 << 1, gpio::Direction::Output);
113     port::set_af(port::PORTB, 1, 1);
114
115     // Configure upper right push button.
116     gpio::set_direction(gpio::GPIOA, 1 << 12, gpio::Direction::Input);
117     port::set_af(port::PORTA, 12, 1);
118     port::set_pull(port::PORTA, 12, port::Pull::Up);
119
120     enable_interrupts();
121
122     usb_serial::init(0xf055, 0x635d);
123
124     cdc_tx_buf.write(b"\n");
125     cdc_tx_buf.flush();
126
127     let mut display = display::Display::new(gpio::GPIOB, 1 << 16, 0x3c);
128
129     display.init();
130     display.clear();
131
132     let mut screen = screen::Screen::new();
133
134     // Hold GPS in reset while configuring its UART.
135     gpio::clear(gpio::GPIOB, 1);
136     systick::delay_ms(50);
137     uart::configure(uart::UART0);
138     systick::delay_ms(50);
139     gpio::set(gpio::GPIOB, 1);
140
141     nvic::enable_irq(12); // UART0
142
143     let mut gps = gps::Gps::new();
144
145     let mut gps_has_fix = false;
146     let mut gps_has_fix_ticks = 0;
147
148     let mut heart_icon_timer = Timer::new(1000);
149     let mut gps_icon_timer = Timer::new(500);
150
151     let mut prev_tap = gps::TimeAndPos::new();
152
153     loop {
154         let mut tap = gps::TimeAndPos::new();
155         let mut show_time = false;
156         let old_gps_has_fix = gps_has_fix;
157
158         while gps.update(&mut tap) {
159             prev_tap = tap;
160
161             show_time = true;
162
163             gps_has_fix = true;
164             gps_has_fix_ticks = systick::now();
165         }
166
167         // Did GPS fix information expire?
168         if gps_has_fix && systick::has_timeout_ms(gps_has_fix_ticks, 1500) {
169             gps_has_fix = false;
170         }
171
172         if gps_has_fix && !old_gps_has_fix {
173             display.show_icon(display::Icon::SatelliteBody);
174             display.show_icon(display::Icon::SatelliteWave1);
175             display.show_icon(display::Icon::SatelliteWave2);
176         } else if !gps_has_fix {
177             gps_icon_timer.update(|state| {
178                 if state == 1 {
179                     display.show_icon(display::Icon::SatelliteWave1);
180                     2
181                 } else if state == 2 {
182                     display.show_icon(display::Icon::SatelliteWave2);
183                     3
184                 } else if state == 3 {
185                     display.hide_icon(display::Icon::SatelliteBody);
186                     display.hide_icon(display::Icon::SatelliteWave1);
187                     display.hide_icon(display::Icon::SatelliteWave2);
188                     0
189                 } else {
190                     display.show_icon(display::Icon::SatelliteBody);
191                     1
192                 }
193             });
194         }
195
196         if show_time {
197             if let Some(tm) = Time::from_unix_time(prev_tap.unix_time) {
198                 let mut time_s = [b' '; 8];
199                 tm.fmt_time(&mut time_s);
200
201                 screen.clear();
202                 screen.draw_text(&time_s);
203
204                 display.draw(&screen);
205             }
206         }
207
208         heart_icon_timer.update(|state| {
209             if state == 1 {
210                 display.hide_icon(display::Icon::Heart);
211                 0
212             } else {
213                 display.show_icon(display::Icon::Heart);
214                 1
215             }
216         });
217
218         if (gpio::get(gpio::GPIOA) & (1 << 12)) == 0 {
219             nvic::system_reset();
220         }
221     }
222 }