Navi9500 - map, address search and navigation for the Nokia 9500 (Series 80 v2.0)
UID3 0x01000A21, app name "Navi9500".

Shows a map with the Bluetooth GPS position on it: a vector map of all of
Finland (.NVM, drawn on the phone) or a raster map pack (.NMP). It searches streets / house numbers / places, and navigates with turn
instructions, using the .NAV file made by ..\pc\navbuild.py.

FILES TO COPY
  All of Finland:
    ..\pc\out_finland\finland_v2.nvm (118 MB)            map with forests, or
    ..\pc\out_finland\finland_v2_noforest.nvm (82 MB)    without
    ..\pc\out_finland\finland.nvr (66 MB)                roads + search + emergency data
                                                         (made by navfin.py, then emgbuild.py)
  (a .nvr with the map's name is used, else any .nvr in the same folder)
  Uusikaupunki, raster, with search and navigation:
    ..\pc\out_uusikaupunki\uusikaupunki.nmp (~49 MB)   map (zlib tiles)
    ..\pc\out_uusikaupunki\uusikaupunki.nav (~2.4 MB)  roads + search
  to E:\Navi9500\ (memory card). D:\Navi9500\ and C:\Navi9500\ are also
  searched, in that order; in each folder the first .nvm (by name) wins, then
  the first .nmp. The .nav with the same name next to the map is loaded too.
  Without it the map still works, search doesn't. Keep one map in the folder:
  a .nvm hides a .nmp.

GPS
  Uses the GPS picked in GPSTest (C:\System\Data\GPSTest.dat), so run GPSTest
  once first. Navi9500 connects to it at start-up.

KEYS / BUTTONS (map)
  CBA: Hae / Lahenna / Loitonna / Sulje (Lopeta reitti while a route is set;
  Navigoi instead of Hae while a flag from Nayta is shown: routes to it)
  S                    search (same as Hae)
  E                    emergency data (Nayta > Hatatiedot)
  arrows, rocker       pan (stops following)
  + / -  (also i / o)  zoom in / out (turns auto zoom off)
  Enter, rocker centre, F   start following the GPS position (connects if
                       needed, auto zoom on); pressed again while following:
                       north-up -> heading-up -> 3D -> north-up
  H                    heading-up on/off (2D)
  D, 3                 3D on/off (starts following)
  V                    voice guidance on / off (a sample prompt plays)
  A                    auto zoom on/off
  G                    reconnect the GPS
  Esc                  end the route; with no route: exit
  Menu key: Tiedosto (Asetukset, Sulje), Nayta (2D-tila, 3D-tila, Hatatiedot), Ohje
  (Tietoja). Asetukset = voice guidance on / off and its volume 1-10 (default
  5; 10 = the stream's maximum, which was too loud); a sample plays on OK.
  The standard Cancel/OK set showed only Vaihda and Sulje on the 9500, so the
  dialog relabels its buttons when it opens: second OK, bottom Peruuta (a CBA
  resource as the dialog's buttons gave BAFL 4). Nayta ticks the view in use.
  The last view (zoom, centre, heading-up, auto zoom) and the last GPS
  position are kept in
  C:\System\Data\Navi9500.dat.

SEARCH
  Three searches: Katu (streets, always first), Kaupunki (towns, villages, suburbs)
  and Paikka (shops, restaurants, fuel stations ...). The left and right arrows or
  the third button change between them; the typed text stays. They are shown at the
  end of the query line. A place's result has its street address when OSM has one,
  so "K-Supermarket" lists each shop by address, nearest first. A word later in the
  name, or OSM's other names for the place, find it too ("supermarket").
  The municipality can follow the name: "keskuskatu laitila", "keskuskatu 5 laitila",
  "keskuskatu laitila 5", "k-super laitila". While it is being typed, every municipality
  starting with what there is counts. The names are read once, at the first query of
  two words. A house number OSM doesn't have gives the street without a number.

  Type a name; results update on every key. In Katu a last word that
  starts with a digit is a house number: "alinenkatu 5". Case and a/o umlauts
  as typed (the 9500 keyboard has them). Exact names first, then names that
  start with the text, then names with a word that does ("katu" doesn't
  match the middle of a word); in Kaupunki towns before the rest.
  Up/Down choose, Enter or Navigoi = route there from the GPS position
  (waits for a fix if there is none yet), Nayta = show it on the map with a
  flag, Esc or Peruuta = back.
  With Finland's .nvr: streets are per municipality ("Koulukatu 5, Uusikaupunki"),
  names that start with the text, exact names first, each nearest the GPS
  position (or the map's centre) first; a later word of a name also finds it
  ("turuntie" finds "Vanha Turuntie"). Cities show their kind (kaupunki, kyla...).
  Under 4 letters in Katu, 3 in the others, only whole names are found, so the
  first letters don't scan thousands of names. The typed text is drawn before
  the search runs. The page index and the municipality names (~150 KB) are read
  once, when the search screen first opens; a query then reads ~1-60 small pages.

EMERGENCY DATA (Nayta > Hatatiedot, or E)
  A screen like the search: the position in degrees and minutes (N 60 52,788') and
  as decimal WGS84, where it comes from (GPS now; red when it is the last fix, the one
  saved at exit, or only the map's centre), the nearest house address within 5 km
  with its distance and direction, and the 6 nearest hospitals as the crow flies
  (not those OSM tags emergency=no), plus the nearest emergency=yes one if it isn't
  among them, "(paivystys)" after those. Up/Down choose a hospital,
  Navigoi (Enter) routes there, Nayta shows it, Sulje (Esc) goes back. Every new GPS
  fix works it all out again. Data: the .nvr's emergency section (..\pc\emgbuild.py:
  an address grid of ~570 m cells + OSM's amenity=hospital places); an older .nvr
  says so on the address line.

STREET NAMES
  At the closest zoom (18) in 2D the map shows street names too, upright like the
  place names and below them in importance: one per street near the view's centre,
  at the road's point nearest it. Further out, and in 3D, only place names.

PANE IMAGE
  A picture at the bottom of the pane, the speed and clock above it: pane.bmp from
  \Navi9500\ on E:, D: or C: (your own, read at start), else the one in the app's folder
  (installed by the SIS, the car). A Windows BMP, 24 or 32 bits, uncompressed, at most 136
  wide and 60 high; 132 x 34 is the size it was made for. Magenta (255, 0, 255) shows the
  pane's colour through. Without a usable file the speed and clock are at the very bottom.

SCREEN
  Full screen but the softkeys on the right (the view covers the title bar).
  Pane on the left (dark): the next turn arrow and its distance, what to do +
  the road name, the distance and time left and the arrival time; without a
  route the flag's name. At the bottom the speed in big digits and the clock;
  above them a GPS problem, or "Enter: seuraa" when the map isn't following.
  Asetukset > Diagnostiikka shows instead (and the clock at the top without a
  route): the speed, the GPS state / satellites + zoom ("auto" = auto zoom), and
  "kuva N ms" = average compose time over the last 8 composes (the phone clock
  ticks in ~15.6 ms steps), "laatta N" = average time to draw one vector tile.
  It is saved, off by default. The map fills the rest, with the
  OpenStreetMap credit (required by the map licence) bottom right.
  Each frame (map, names, route, car, pane) is drawn into an offscreen bitmap
  and copied to the screen in one blit, so the route line doesn't flicker.

NAVIGATION
  Routing memory: a 4 MB workspace (3 or 2 if 4 isn't free) is reserved at start in an
  RChunk of its own and kept; Navi9500.log says how much ("route workspace: N KB").
  A route takes ~42 bytes per junction searched: Uusikaupunki - Lappeenranta 1.8 MB.
  Before, the 4 MB (or 2) was asked for from the heap at each route, and after hours
  of map tiles it often wasn't there: "Muisti ei riita" on long trips.
  Blue line from the car to the destination (the driven part disappears), red
  flag at the destination; the turn and what is left are in the pane.
  Without a GPS fix (or no connection) the route starts from the last known
  position: this run's last fix, or the one saved at exit (shown as a grey car
  until a fix comes). On the first real
  fix, if the car is more than 40 m off that route, it is routed again at once.
  Only before the very first fix ever is there "Odotetaan GPS-paikkaa".
  While a route is set the screen stays on (the inactivity timer is reset every
  4 s), until Lopeta reitti / Esc.
  Off the route by more than 40 m for three fixes in a row while moving: a new
  route is computed from where the car is. Within 25 m of the destination:
  "Olet perilla". Islands without a road connection give "Ei reittia".
  Across Finland (.nvr, navfin.c): the road network is cut into ~5 km cells read
  from the card when needed (2.5 MB of them kept). All roads are searched in the
  start's and the goal's cells, tertiary and up within 4 cells (~20 km) of them,
  and further away the secondary-and-up roads and car ferries, along shortcuts
  between their real crossings; roads linking ferry landings count as main
  roads. If that finds nothing, it tries wider (1 and 12 cells), then all roads.
  The time estimate assumes 80 km/h in a straight line, so the search stays
  narrow; on 200 random pairs across Finland half the routes were the fastest
  possible and 90 % within 2.5 %. A route takes a 4 MB workspace (2 MB if 4 MB
  isn't free) only while it is computed. Forest tracks aren't routed.
  Banner messages: "Reitin haku vei kaiken muistin" = the workspace filled up,
  "Muisti ei riita" = no memory for it.

SKY (sky.c, the 3D view)
  The sky follows the sun's height, worked out every 20 s from the position
  and the phone's clock (set its time and time zone right; the GPS date isn't
  used, old receivers' can be years off). Day from 8 degrees up, warm low sun,
  orange sunset, red and violet dusk, dark blue with the first stars from -6
  degrees, full night with all the stars from -18. Everything is blended
  evenly. The ground dims with it to half, a little blue, and place names
  turn light on dark. The stars stay put on the compass as the car turns.
  Host check: tests\skytest.c (the sun against the NOAA formulas);
  SKY_SUN=-18 p3test ... draws a frame at that height.

VOICE (navvoice.c + NaviVoice.cpp)
  Finnish prompts from the clips in ..\Voice, joined into sentences:
  "Kahden kilometrin paasta kaanny vasemmalle" (80+ km/h) or "Kilometrin
  paasta ..." (50-80), then "Seitsemansadan / Viidensadan / ... metrin paasta
  ..." (700/500/300/200 m by speed, the nearest distance clip), then at the turn
  (~6 s before it, at least 45 m) "Nyt kaanny vasemmalle". Roundabouts: "Liikenneympyrassa ota
  toinen ulosmeno". A turn close after the one being said is added: "ja sen
  jalkeen ..." / "ja sitten valittomasti ..." (and isn't said again). Arrival
  "Olet perilla"; during a route "Yhteys GPS-laitteeseen on katkennut" when the
  GPS link drops, "GPS ei anna luotettavaa sijaintia" after 10 s without a fix.
  A sentence is read into memory whole before it starts.
  Prompts come only while on the route (not while off it waiting to re-route).
  The clips are in voice.vox (installed with the app; else \Navi9500\ on E:,
  D:, C:), 16 kHz 16-bit, played through one CMdaAudioOutputStream in a thread
  of its own (in the UI thread it was fed only between map frames and broke
  up); a sentence ends in at least 640 ms of silence (the stream drops what is
  still in its buffer when it runs out); a prompt asked for during another
  waits for it. On / off and the volume are saved.
  Host check: tests\voicetest.c drives a real route and prints every prompt
  with the words from ..\Voice\files.txt.

3D VIEW (persp.c)
  A camera behind and above the car looks along the course: sky at the top,
  the ground below it fading into haze, the car at 82 % of the height. Each
  screen row is a straight line on the ground, so it is sampled from the tiles
  like the heading-up view (16.16 steps along the row). Near ground uses the
  car's zoom, further ground the next zoom out, the furthest two zooms out
  (a row switches when a screen pixel spans 2 / 4 ground pixels), so detail
  drops with distance and one frame needs ~20-26 tiles (at most 38; the
  ground is drawn less far if more would be needed). The tile cache is 48 tiles
  (6 MB). Tiles not drawn yet show as stand-ins and are drawn when idle
  and for up to 40 ms before each frame, nearest zoom first, nearest the car first.
  The route line, the flag and the place names (only on the nearer half of the
  ground) are put through the same projection; the car arrow lies flattened
  on the ground. Zoom keys and auto zoom work as in 2D (the zoom is the car's).
  Host check: tests\p3test.c renders a 3D frame of the real map and checks that
  every ground pixel projects back onto itself.

DRIVING VIEW
  The GPS gives a fix once a second. Between fixes the position is predicted
  from speed and course (up to 1.5 s) at 20 frames a second, and the shown
  position closes 35 % of the gap to the prediction each frame, so a new fix
  that disagrees is eased in instead of jumped to. The course eases the same
  way (25 %). Below 3 km/h the position stands still and the course is kept
  (the GPS course is noise when standing).
  Heading-up: the map turns so the course points up; the car sits at 72 % of
  the screen height so more of the road ahead shows. Each frame samples the
  tile cache directly in 16.16 fixed point (nearest pixel).
  Auto zoom: under 30 km/h z16, under 65 z15, under 100 z14, faster z13, with
  5 km/h of hysteresis so it doesn't flip at a boundary. One zoom closer in
  busy streets: 40+ roads within 300 m of the car (counted every 3 s in the
  vector map's blocks; off again under 28). Town centres have 35-450
  (Uusikaupunki 50, Helsinki 440), main roads and the country 1-25
  (tests\denstest.c). One more while navigating, from 10 s (at least 200 m)
  before the next manoeuvre until 3 s after it. At most z18.
  A new frame is composed only when the view moved a pixel or turned half a
  degree.

HOW IT WORKS
  Vector maps (NvmMap + vecdraw.c): a tile is drawn the first time the view
  needs it and then kept in a 40-tile cache (5 MB), so panning, heading-up and
  the driving view work the same way as with raster tiles. Composing never
  waits for drawing: a tile that isn't ready is shown at once as a stand-in
  (up to 3 zooms out enlarged, with what there is up to 2 zooms in reduced on
  top, or plain land), and before each driving frame (up to 40 ms: frames can
  outrun the timer, and then idle time never comes) and when the phone is idle
  (CIdle, one tile per step) the real tiles are drawn, those on screen first, nearest the centre first, each
  shown as soon as it is done; then the tiles around the screen are drawn
  ahead: a ring around it, or in heading-up one more row/column on the side
  the car is heading to. Place names are fetched from the map once per area
  (256 px around the screen) and only placed per frame.
  Drawing thins points nearer than 3/4 px to the last one kept, skips rings
  that are wholly off the tile and merges runs of edges left of the tile. Drawing a tile:
  the map blocks under it (z12 blocks for zoom 13-18, z9 for 10-12, z6 for
  5-9) are read and decoded (bit stream, see pc\nvm2.py; 16 decoded blocks
  are cached), areas are filled with a scanline filler (even-odd, pixel
  centres), roads are drawn as quads with round-ish joins, casings first and
  fills over them, with colours and widths from vecstyle.h (made from
  pc\style.py by pc\mkvecstyle.py). Place names are not in the tiles: each
  frame the view asks for the places on screen and draws them upright, the
  most important first, skipping any that would overlap (zoom 5-9 towns,
  10-12 villages, 13-14 suburbs, 15+ everything). Zooms 5-18.
  Raster maps: NmpMap inflates a tile (EZLIB; version 1 packs: RLE) and turns the indices
  straight into screen-format 16-bit pixels through
  a 256-entry palette table (RGB565, or 0x0RGB on a 12-bit screen) and keeps
  28 decoded tiles (3.5 MB) in an LRU cache. NaviView copies the visible rows of
  each tile into an offscreen CFbsBitmap only when the map moves, and every
  redraw is one BitBlt plus route, marker, banner and info line.
  navcore.c (plain C89, no library calls) does the rest: snapping a point to
  the nearest road, A* over the road graph (drive time; private roads cost x5
  so they are only used at the ends), the route line, turn instructions
  (angle + name changes, roundabout exit numbers), following the route, and
  the search. The whole .nav file is loaded into RAM.

FILES
  NaviApp.cpp     app UI, document, application
  NaviView.h/cpp  the view: map, search screen, keys, GPS, navigation, drawing
  MapSource.h     what the view needs from a map (raster or vector)
  NmpMap.h/cpp    .NMP reader, RLE decoder, tile cache
  NvmMap.h/cpp    .NVM reader: block cache, tiles drawn by vecdraw.c, places
  navfin.h/c      routing + search over Finland's .NVR, read on demand (plain C89,
                  also built on the PC); routes come out as navcore's NavRoute
  vecdraw.h/c     NVM decoding and tile drawing (plain C89, also built on the PC)
  vecstyle.h      colours and widths, generated: edit pc\style.py instead
  persp.h/c       the 3D view: rows, zoom levels, tiles, projection (plain C89)
  navvoice.h/c    when to say what (plain C89)
  NaviVoice.h/cpp plays the prompts from voice.vox
  NaviAif.rss, icon_*.bmp  the Desk icon (road to the horizon), drawn by make_icon.py
  voice.vox       the voice clips, made by ..\pc\mkvoice.py (in the SIS)
  pane.bmp        the picture at the bottom of the pane (in the SIS)
  navcore.h/c     routing, search, route following (also built on the PC)
  GpsLink.*, Nmea.*   copied from ..\..\GPSTest (keep them in step)
  GpsThread.*     runs GpsLink in its own thread: a hung Bluetooth link can't hold up
                  the UI. 8 s without an RMC line = link lost; the thread is killed
                  (never asked to close) and a new one tried every 10 s
  tests\          host checks:
                  compose_test.c + compare.py / compare_rot.py - map compose
                  navtest.c - navcore.c compiled unchanged: search, route,
                  drive (route following), batch (cases.txt from
                  make_cases.py: costs must equal pc\navtool.py's Dijkstra)
                  vectest.c + compare_vec.py - vecdraw.c against pc\vecview.py
                  on the real Finland map, whole view or tile by tile (--tile 256)
                  nftest.c - navfin.c: check (every cell), nodes, route, search
                  p3test.c - persp.c: a 3D frame of the Finland map to a PPM
                  voicetest.c - navvoice.c: the prompts along a driven route
                  skytest.c - sky.c: the sun's height against the NOAA formulas
                  nfcompare.py - navfin.c's routes against the true optimum
                  (scipy) and pc\navfinread.py (must agree cost for cost)

BUILD (on the Win2000 VM, with the Series 80 v2.0 SDK)
  bldmake bldfiles
  abld build armi urel
  makesis Navi.pkg

LOGS
  C:\System\Data\Navi9500.log   GPS link changes, slow frames and routes, a stalled UI
                                thread, each step of closing the app
  C:\System\Data\NaviVoice.log  sentences that didn't end normally
  Both are short and start again when full (64 KB, 16 KB).
