4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Andrea Righi <arighi@develer.com>
35 * \brief DevelGPS demo application with gps.
38 #include "hw/hw_led.h"
42 #include <drv/lcd_rit128x96.h>
43 #include <drv/timer.h>
45 #include <drv/flash.h>
48 #include <io/kblock.h>
49 #include <io/kfile_block.h>
51 #include <kern/proc.h>
58 #include <gfx/gfx_p.h> /* BM_PLOT() */
66 #define KEY_MASK (K_UP | K_DOWN | K_LEFT | K_RIGHT | K_OK)
67 extern Font font_gohu;
68 static uint8_t raster[RAST_SIZE(LCD_WIDTH, LCD_HEIGHT)];
69 static Bitmap lcd_bitmap;
71 #define SCRSVR_TIME 60000
72 static ticks_t scrsvr_timestamp;
73 static bool is_lcd_off;
75 /* Serial and NMEA stuff */
76 static Serial ser_port;
77 static nmeap_context_t nmea;
81 static bool nmea_update;
85 static long target_lat, target_lon;
88 #define GPS_POS_MAGIC 0xdeadbeef
89 static Flash flash_blk;
90 static KFileBlock flash;
95 long target_lat, target_lon;
98 static void flash_load_target(void)
100 struct SettingsData data;
102 kfile_seek(&flash.fd, -sizeof(data), KSM_SEEK_END);
103 kfile_read(&flash.fd, &data, sizeof(data));
105 if (data.magic == GPS_POS_MAGIC)
107 target_lat = data.target_lat;
108 target_lon = data.target_lon;
112 static void flash_save_target(void)
114 struct SettingsData data;
116 data.magic = GPS_POS_MAGIC;
117 data.target_lat = target_lat;
118 data.target_lon = target_lon;
120 kfile_seek(&flash.fd, -sizeof(data), KSM_SEEK_END);
121 kfile_write(&flash.fd, &data, sizeof(data));
122 kfile_flush(&flash.fd);
126 /* Display management */
127 INLINE void video_off(void)
129 unsigned long delta =
130 (long)ticks_to_ms(timer_clock_unlocked()) -
131 (long)scrsvr_timestamp;
133 if (!is_lcd_off && delta > SCRSVR_TIME)
140 INLINE void video_on(void)
147 scrsvr_timestamp = ticks_to_ms(timer_clock_unlocked());
150 INLINE void repaint(Bitmap *bm)
152 rit128x96_blitBitmap(bm);
155 INLINE keymask_t keypad_peek(void)
157 keymask_t key = kbd_peek();
168 /* Status LED thread */
169 static void NORETURN led_process(void)
187 static void nmea_callback(nmeap_context_t *context, void *data, void *user_data)
193 lat = (long)gga.latitude;
194 lon = (long)gga.longitude;
199 static void NORETURN ser_process(void)
203 nmea_poll(&nmea, &ser_port.fd);
204 kfile_clearerr(&ser_port.fd);
208 /* Target position screen */
209 static void target(Bitmap *bm)
211 const long STEP = 10000000,
212 MAX_LAT = 90000000, MIN_LAT = -90000000,
213 MAX_LON = 180000000, MIN_LON = -180000000;
214 long step = STEP, target;
215 int row = 0, pos = 1;
220 text_xprintf(bm, 0, 0,
221 STYLEF_UNDERLINE | TEXT_CENTER | TEXT_FILL,
227 text_xprintf(bm, 3, 0, TEXT_FILL,
228 "Lat: %02ld.%06ld %c %s",
229 ABS(target_lat) / 1000000L,
230 ABS(target_lat) % 1000000,
231 target_lat >= 0 ? 'N' : 'S',
232 row == 0 ? "<-" : "");
233 text_xprintf(bm, row ? 4 : 6, 0,
234 TEXT_FILL | TEXT_CENTER, " ");
235 text_xprintf(bm, row ? 6 : 4, 0,
238 pos + 7 : pos + 6, '^');
239 text_xprintf(bm, 5, 0, TEXT_FILL,
240 "Lon: %03ld.%06ld %c %s",
241 ABS(target_lon) / 1000000L,
242 ABS(target_lon) % 1000000L,
243 target_lon >= 0 ? 'E' : 'W',
244 row == 1 ? "<-" : "");
252 target = target_lat + step;
253 if (target <= MAX_LAT)
255 if (target_lat < 0 && target > 0)
256 target_lat = ABS(target_lat) +
264 target = target_lon + step;
265 if (target <= MAX_LON)
267 if (target_lon < 0 && target > 0)
268 target_lon = ABS(target_lon) +
275 else if (key & K_DOWN)
279 target = target_lat - step;
280 if (target >= MIN_LAT)
282 if (target_lat > 0 && target < 0)
283 target_lat = -ABS(target_lat) -
291 target = target_lon - step;
292 if (target >= MIN_LON)
294 if (target_lon > 0 && target < 0)
295 target_lon = -ABS(target_lon) -
302 else if (key & K_LEFT)
310 else if (key & K_RIGHT)
322 /* Move to longigude */
329 /* Compass management */
330 static void draw_compass(Bitmap *bm)
332 const int R = LCD_HEIGHT / 3, R_SMALL = 10;
333 long x, y, x1, y1, x2, y2, d;
336 d = distance(lat / 1E6, lon / 1E6, target_lat / 1E6, target_lon / 1E6);
338 x = (long)((float)R * (1 + sin(deg2rad((float)prev_b))));
339 y = (long)((float)R * (1 - cos(deg2rad((float)prev_b))));
341 x1 = R - R_SMALL + (long)((float)R_SMALL *
342 (1 + sin(deg2rad((float)prev_b - 120.0))));
343 y1 = R - R_SMALL + (long)((float)R_SMALL *
344 (1 - cos(deg2rad((float)prev_b - 120.0))));
345 x2 = R - R_SMALL + (long)((float)R_SMALL *
346 (1 + sin(deg2rad((float)prev_b + 120.0))));
347 y2 = R - R_SMALL + (long)((float)R_SMALL *
348 (1 - cos(deg2rad((float)prev_b + 120.0))));
351 /* Print direction heading and degrees */
352 text_xprintf(bm, 0, 5, 0, "%s", "N");
353 text_xprintf(bm, 0, 0, TEXT_RIGHT, "%s", compass_heading(prev_b));
354 text_xprintf(bm, 1, 0, TEXT_RIGHT, "%ld deg.", prev_b);
356 text_xprintf(bm, 2, 0, TEXT_RIGHT, "%ld %s",
357 d >= 1000 ? d / 1000 : d,
358 d >= 1000 ? "Km" : "m");
359 /* Print current and target position */
360 text_xprintf(bm, 6, 0, TEXT_FILL, "%02ld.%06ld%c%03ld.%06ld%c",
363 lat >= 0 ? 'N' : 'S',
366 lon >= 0 ? 'E' : 'W');
367 text_xprintf(bm, 7, 0, TEXT_FILL, "%02ld.%06ld%c%03ld.%06ld%c",
368 ABS(target_lat) / 1000000L,
369 ABS(target_lat) % 1000000,
370 target_lat >= 0 ? 'N' : 'S',
371 ABS(target_lon) / 1000000L,
372 ABS(target_lon) % 1000000L,
373 target_lon >= 0 ? 'E' : 'W');
374 /* Draw the circle */
375 for (i = 0; i < 360; i++)
377 (long)((float)R * (1 + sin(deg2rad((float)i)))),
378 (long)((float)R * (1 - cos(deg2rad((float)i)))));
379 /* Draw the needle */
380 gfx_rectFill(bm, R - 2, R - 2, R + 2, R + 2);
381 gfx_line(bm, R, R, x1, y1);
382 gfx_line(bm, R, R, x2, y2);
383 gfx_line(bm, x1, y1, x, y);
384 gfx_line(bm, x2, y2, x, y);
389 static void compass(Bitmap *bm)
398 b = bearing(lat / 1E6, lon / 1E6,
399 target_lat / 1E6, target_lon / 1E6);
400 inc = ABS(b - prev_b) < 360 - ABS(b - prev_b) ? 1 : -1;
401 /* Compass animation */
406 prev_b = (prev_b + inc) % 360;
411 if (keypad_peek() & KEY_MASK)
417 if (keypad_peek() & KEY_MASK)
422 /* GPS receiver status */
423 static const char *gps_fix[] =
436 static void gps_data(Bitmap *bm)
449 text_xprintf(bm, 3, 0,
450 TEXT_CENTER | TEXT_FILL, "No GPS data");
454 gmtime_r(&rmc.time, &tm);
456 text_xprintf(bm, 0, 0, TEXT_FILL,
460 lat >= 0 ? 'N' : 'S');
461 text_xprintf(bm, 1, 0, TEXT_FILL,
465 lon >= 0 ? 'E' : 'W');
466 text_xprintf(bm, 2, 0, TEXT_FILL,
467 "Alt. %ld", gga.altitude);
468 text_xprintf(bm, 3, 0, TEXT_FILL,
469 "Speed: %d", vtg.km_speed);
470 if (gga.quality < countof(gps_fix))
471 text_xprintf(bm, 4, 0, TEXT_FILL,
473 gps_fix[gga.quality]);
475 text_xprintf(bm, 4, 0, TEXT_FILL,
476 "Fix: %d", gga.quality);
477 text_xprintf(bm, 5, 0, TEXT_FILL,
480 strftime(buf, sizeof(buf),
481 "Date: %Y-%m-%d %a", &tm);
482 text_xprintf(bm, 6, 0, TEXT_FILL, "%s", buf);
483 strftime(buf, sizeof(buf),
484 "Time: %H:%M:%S (UTC)", &tm);
485 text_xprintf(bm, 7, 0, TEXT_FILL, "%s", buf);
489 if (keypad_peek() & KEY_MASK)
496 static void about(Bitmap *bm)
500 text_xprintf(bm, 7, 0,
501 STYLEF_UNDERLINE | TEXT_CENTER | TEXT_FILL,
502 "http://www.bertos.org");
506 const uint8_t *p = &logo[BITMAP_HEADER_SIZE];
507 uint8_t h = logo[BITMAP_HEIGHT_OFFSET];
508 uint8_t w = logo[BITMAP_WIDTH_OFFSET];
511 for (r = 0; r < h; r++)
514 (128 - w) / 2, 70 - r, w, 1);
518 while (!(keypad_peek() & KEY_MASK))
522 static struct MenuItem main_items[] =
524 {(const_iptr_t)"Target", 0, (MenuHook)target, (iptr_t)&lcd_bitmap},
525 {(const_iptr_t)"Compass", 0, (MenuHook)compass, (iptr_t)&lcd_bitmap},
526 {(const_iptr_t)"GPS data", 0, (MenuHook)gps_data, (iptr_t)&lcd_bitmap},
527 {(const_iptr_t)"About...", 0, (MenuHook)about, (iptr_t)&lcd_bitmap},
528 {(const_iptr_t)0, 0, NULL, (iptr_t)NULL}
531 static struct Menu main_menu =
533 main_items, "DevelGPS v0.1", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0, rit128x96_blitBitmap
536 static void init(void)
544 scrsvr_timestamp = ticks_to_ms(timer_clock_unlocked());
547 flash_init(&flash_blk, 0);
548 kfileblock_init(&flash, &flash_blk.blk);
551 ser_init(&ser_port, SER_UART1);
552 ser_setbaudrate(&ser_port, 38400);
554 nmeap_init(&nmea, NULL);
555 nmeap_addParser(&nmea, "GPGGA", nmea_gpgga, nmea_callback, &gga);
556 nmeap_addParser(&nmea, "GPRMC", nmea_gprmc, nmea_callback, &rmc);
557 nmeap_addParser(&nmea, "GPVTG", nmea_gpvtg, nmea_callback, &vtg);
560 gfx_bitmapInit(&lcd_bitmap, raster, LCD_WIDTH, LCD_HEIGHT);
561 gfx_setFont(&lcd_bitmap, &font_gohu);
562 repaint(&lcd_bitmap);
571 proc_new(led_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
572 proc_new(ser_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
576 iptr_t res = menu_handle(&main_menu);
577 if (res != MENU_TIMEOUT)