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 <kern/proc.h>
55 #include <gfx/gfx_p.h> /* BM_PLOT() */
63 #define KEY_MASK (K_UP | K_DOWN | K_LEFT | K_RIGHT | K_OK)
64 extern Font font_gohu;
65 static uint8_t raster[RAST_SIZE(LCD_WIDTH, LCD_HEIGHT)];
66 static Bitmap lcd_bitmap;
68 #define SCRSVR_TIME 60000
69 static ticks_t scrsvr_timestamp;
70 static bool is_lcd_off;
72 /* Serial and NMEA stuff */
73 static Serial ser_port;
74 static nmeap_context_t nmea;
78 static bool nmea_update;
82 static long target_lat, target_lon;
85 #define GPS_POS_MAGIC 0xdeadbeef
88 static void flash_load_target(void)
92 kfile_seek(&flash.fd, -FLASH_PAGE_SIZE_BYTES, KSM_SEEK_END);
93 kfile_read(&flash.fd, &magic, sizeof(magic));
94 if (magic == GPS_POS_MAGIC)
96 kfile_read(&flash.fd, &target_lat, sizeof(target_lat));
97 kfile_read(&flash.fd, &target_lon, sizeof(target_lon));
101 static void flash_save_target(void)
103 const uint32_t magic = GPS_POS_MAGIC;
105 kfile_seek(&flash.fd, -FLASH_PAGE_SIZE_BYTES, KSM_SEEK_END);
106 kfile_write(&flash.fd, &magic, sizeof(magic));
107 kfile_write(&flash.fd, &target_lat, sizeof(target_lat));
108 kfile_write(&flash.fd, &target_lon, sizeof(target_lon));
109 kfile_flush(&flash.fd);
113 /* Display management */
114 INLINE void video_off(void)
116 unsigned long delta =
117 (long)ticks_to_ms(timer_clock_unlocked()) -
118 (long)scrsvr_timestamp;
120 if (!is_lcd_off && delta > SCRSVR_TIME)
127 INLINE void video_on(void)
134 scrsvr_timestamp = ticks_to_ms(timer_clock_unlocked());
137 INLINE void repaint(Bitmap *bm)
139 rit128x96_blitBitmap(bm);
142 INLINE keymask_t keypad_peek(void)
144 keymask_t key = kbd_peek();
155 /* Status LED thread */
156 static void NORETURN led_process(void)
174 static void nmea_callback(nmeap_context_t *context, void *data, void *user_data)
180 lat = (long)gga.latitude;
181 lon = (long)gga.longitude;
186 static void NORETURN ser_process(void)
190 nmea_poll(&nmea, &ser_port.fd);
191 kfile_clearerr(&ser_port.fd);
195 /* Target position screen */
196 static void target(Bitmap *bm)
198 const long STEP = 10000000,
199 MAX_LAT = 90000000, MIN_LAT = -90000000,
200 MAX_LON = 180000000, MIN_LON = -180000000;
201 long step = STEP, target;
202 int row = 0, pos = 1;
207 text_xprintf(bm, 0, 0,
208 STYLEF_UNDERLINE | TEXT_CENTER | TEXT_FILL,
214 text_xprintf(bm, 3, 0, TEXT_FILL,
215 "Lat: %02ld.%06ld %c %s",
216 ABS(target_lat) / 1000000L,
217 ABS(target_lat) % 1000000,
218 target_lat >= 0 ? 'N' : 'S',
219 row == 0 ? "<-" : "");
220 text_xprintf(bm, row ? 4 : 6, 0,
221 TEXT_FILL | TEXT_CENTER, " ");
222 text_xprintf(bm, row ? 6 : 4, 0,
225 pos + 7 : pos + 6, '^');
226 text_xprintf(bm, 5, 0, TEXT_FILL,
227 "Lon: %03ld.%06ld %c %s",
228 ABS(target_lon) / 1000000L,
229 ABS(target_lon) % 1000000L,
230 target_lon >= 0 ? 'E' : 'W',
231 row == 1 ? "<-" : "");
239 target = target_lat + step;
240 if (target <= MAX_LAT)
242 if (target_lat < 0 && target > 0)
243 target_lat = ABS(target_lat) +
251 target = target_lon + step;
252 if (target <= MAX_LON)
254 if (target_lon < 0 && target > 0)
255 target_lon = ABS(target_lon) +
262 else if (key & K_DOWN)
266 target = target_lat - step;
267 if (target >= MIN_LAT)
269 if (target_lat > 0 && target < 0)
270 target_lat = -ABS(target_lat) -
278 target = target_lon - step;
279 if (target >= MIN_LON)
281 if (target_lon > 0 && target < 0)
282 target_lon = -ABS(target_lon) -
289 else if (key & K_LEFT)
297 else if (key & K_RIGHT)
309 /* Move to longigude */
316 /* Compass management */
317 static void draw_compass(Bitmap *bm)
319 const int R = LCD_HEIGHT / 3, R_SMALL = 10;
320 long x, y, x1, y1, x2, y2, d;
323 d = distance(lat / 1E6, lon / 1E6, target_lat / 1E6, target_lon / 1E6);
325 x = (long)((float)R * (1 + sin(deg2rad((float)prev_b))));
326 y = (long)((float)R * (1 - cos(deg2rad((float)prev_b))));
328 x1 = R - R_SMALL + (long)((float)R_SMALL *
329 (1 + sin(deg2rad((float)prev_b - 120.0))));
330 y1 = R - R_SMALL + (long)((float)R_SMALL *
331 (1 - cos(deg2rad((float)prev_b - 120.0))));
332 x2 = R - R_SMALL + (long)((float)R_SMALL *
333 (1 + sin(deg2rad((float)prev_b + 120.0))));
334 y2 = R - R_SMALL + (long)((float)R_SMALL *
335 (1 - cos(deg2rad((float)prev_b + 120.0))));
338 /* Print direction heading and degrees */
339 text_xprintf(bm, 0, 5, 0, "%s", "N");
340 text_xprintf(bm, 0, 0, TEXT_RIGHT, "%s", compass_heading(prev_b));
341 text_xprintf(bm, 1, 0, TEXT_RIGHT, "%ld deg.", prev_b);
343 text_xprintf(bm, 2, 0, TEXT_RIGHT, "%ld %s",
344 d >= 1000 ? d / 1000 : d,
345 d >= 1000 ? "Km" : "m");
346 /* Print current and target position */
347 text_xprintf(bm, 6, 0, TEXT_FILL, "%02ld.%06ld%c%03ld.%06ld%c",
350 lat >= 0 ? 'N' : 'S',
353 lon >= 0 ? 'E' : 'W');
354 text_xprintf(bm, 7, 0, TEXT_FILL, "%02ld.%06ld%c%03ld.%06ld%c",
355 ABS(target_lat) / 1000000L,
356 ABS(target_lat) % 1000000,
357 target_lat >= 0 ? 'N' : 'S',
358 ABS(target_lon) / 1000000L,
359 ABS(target_lon) % 1000000L,
360 target_lon >= 0 ? 'E' : 'W');
361 /* Draw the circle */
362 for (i = 0; i < 360; i++)
364 (long)((float)R * (1 + sin(deg2rad((float)i)))),
365 (long)((float)R * (1 - cos(deg2rad((float)i)))));
366 /* Draw the needle */
367 gfx_rectFill(bm, R - 2, R - 2, R + 2, R + 2);
368 gfx_line(bm, R, R, x1, y1);
369 gfx_line(bm, R, R, x2, y2);
370 gfx_line(bm, x1, y1, x, y);
371 gfx_line(bm, x2, y2, x, y);
376 static void compass(Bitmap *bm)
385 b = bearing(lat / 1E6, lon / 1E6,
386 target_lat / 1E6, target_lon / 1E6);
387 inc = ABS(b - prev_b) < 360 - ABS(b - prev_b) ? 1 : -1;
388 /* Compass animation */
393 prev_b = (prev_b + inc) % 360;
398 if (keypad_peek() & KEY_MASK)
404 if (keypad_peek() & KEY_MASK)
409 /* GPS receiver status */
410 static const char *gps_fix[] =
423 static void gps_data(Bitmap *bm)
436 text_xprintf(bm, 3, 0,
437 TEXT_CENTER | TEXT_FILL, "No GPS data");
441 gmtime_r(&rmc.time, &tm);
443 text_xprintf(bm, 0, 0, TEXT_FILL,
447 lat >= 0 ? 'N' : 'S');
448 text_xprintf(bm, 1, 0, TEXT_FILL,
452 lon >= 0 ? 'E' : 'W');
453 text_xprintf(bm, 2, 0, TEXT_FILL,
454 "Alt. %d", gga.altitude);
455 text_xprintf(bm, 3, 0, TEXT_FILL,
456 "Speed: %d", vtg.km_speed);
457 if (gga.quality < countof(gps_fix))
458 text_xprintf(bm, 4, 0, TEXT_FILL,
460 gps_fix[gga.quality]);
462 text_xprintf(bm, 4, 0, TEXT_FILL,
463 "Fix: %d", gga.quality);
464 text_xprintf(bm, 5, 0, TEXT_FILL,
467 strftime(buf, sizeof(buf),
468 "Date: %Y-%m-%d %a", &tm);
469 text_xprintf(bm, 6, 0, TEXT_FILL, "%s", buf);
470 strftime(buf, sizeof(buf),
471 "Time: %H:%M:%S (UTC)", &tm);
472 text_xprintf(bm, 7, 0, TEXT_FILL, "%s", buf);
476 if (keypad_peek() & KEY_MASK)
483 static void about(Bitmap *bm)
487 text_xprintf(bm, 7, 0,
488 STYLEF_UNDERLINE | TEXT_CENTER | TEXT_FILL,
489 "http://www.bertos.org");
493 const uint8_t *p = &logo[BITMAP_HEADER_SIZE];
494 uint8_t h = logo[BITMAP_HEIGHT_OFFSET];
495 uint8_t w = logo[BITMAP_WIDTH_OFFSET];
498 for (r = 0; r < h; r++)
501 (128 - w) / 2, 70 - r, w, 1);
505 while (!(keypad_peek() & KEY_MASK))
509 static struct MenuItem main_items[] =
511 {(const_iptr_t)"Target", 0, (MenuHook)target, (iptr_t)&lcd_bitmap},
512 {(const_iptr_t)"Compass", 0, (MenuHook)compass, (iptr_t)&lcd_bitmap},
513 {(const_iptr_t)"GPS data", 0, (MenuHook)gps_data, (iptr_t)&lcd_bitmap},
514 {(const_iptr_t)"About...", 0, (MenuHook)about, (iptr_t)&lcd_bitmap},
515 {(const_iptr_t)0, 0, NULL, (iptr_t)NULL}
518 static struct Menu main_menu =
520 main_items, "DevelGPS v0.1", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0
523 static void init(void)
531 scrsvr_timestamp = ticks_to_ms(timer_clock_unlocked());
537 ser_init(&ser_port, SER_UART1);
538 ser_setbaudrate(&ser_port, 38400);
540 nmeap_init(&nmea, NULL);
541 nmeap_addParser(&nmea, "GPGGA", nmea_gpgga, nmea_callback, &gga);
542 nmeap_addParser(&nmea, "GPRMC", nmea_gprmc, nmea_callback, &rmc);
543 nmeap_addParser(&nmea, "GPVTG", nmea_gpvtg, nmea_callback, &vtg);
546 gfx_bitmapInit(&lcd_bitmap, raster, LCD_WIDTH, LCD_HEIGHT);
547 gfx_setFont(&lcd_bitmap, &font_gohu);
548 repaint(&lcd_bitmap);
557 proc_new(led_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
558 proc_new(ser_process, NULL, KERN_MINSTACKSIZE * 2, NULL);
562 iptr_t res = menu_handle(&main_menu);
563 if (res != MENU_TIMEOUT)