Fixes to speed alarm and poi alerts. Added flicker effect. Some new fields to text...
[jspeed] / src / poialerts.cpp
index dcc847e..2d8fe0d 100644 (file)
@@ -44,6 +44,16 @@ inline static double radToDeg(double rad)
     return rad * 180 / PI;
 }
 
+inline static double absValue(double value)
+{
+    if(value < 0.0)
+    {
+        return -value;
+    }
+
+    return value;
+}
+
 PoiAlerts::PoiAlerts(): QObject(0), enabled_(false), currentPoi_(0), loaded_(false)
 {
 }
@@ -104,7 +114,7 @@ bool PoiAlerts::loadConfig()
         return true;
     }
 
-    distance_ = Settings::instance().value("alert_distance", 300).toBool();
+    distance_ = Settings::instance().value("alert_distance", 300).toDouble();
     onlyOnRoute_ = Settings::instance().value("alert_only_on_route", true).toBool();
 
     if(distance_ < 0)
@@ -212,35 +222,23 @@ QString const& PoiAlerts::error() const
 
 void PoiAlerts::onDataUpdated()
 {
-    qDebug() << "Data update";
-
-    static int i = 0;
-    i++;
-
-    double travelled = Odometer::instance().getTotal();
     const Location::Fix* fix = &(Odometer::instance().getLatestFix());
 
-    if(fix->latitude < 0.01 || fix->longitude < 0.01 || fix->kmSpeed < 0.01)
+    if(fix->latitude < 0.01 || fix->longitude < 0.01 ||
+       fix->kmSpeed < 0.01 || isnan(fix->eph))
     {
         return;
     }
-    else
-    {
-        /*if(i < 5)
-        {
-            pois_[0].latitude = fix->latitude;
-            pois_[0].longitude = fix->longitude;
-        }*/
-    }
-
-    double distance;
-    double inRouteMargin = IN_ROUTE_MARGIN + (fix->eph / 1000.0);
 
-    qDebug() << "Eph: " << fix->eph;
-    qDebug() << "In route margin: " << inRouteMargin;
+    double travelled = Odometer::instance().getTotal();
 
-    if(1 == 1 || abs(travelled - travelled_) > 0.03)
+    if(absValue(travelled - travelled_) > 0.02)
     {
+        double distance;
+        double inRouteMargin = IN_ROUTE_MARGIN + (fix->eph / 1000.0);
+
+        bool found = false;
+
         travelled_ = travelled;
 
         for(int i = 0; i < pois_.size(); i++)
@@ -252,8 +250,8 @@ void PoiAlerts::onDataUpdated()
 
                 if(onlyOnRoute_)
                 {
-                    double track = abs(calculateTrack(fix->latitude, fix->longitude,
-                                                      pois_.at(i).latitude, pois_.at(i).longitude) - fix->track);
+                    double track = absValue(calculateTrack(fix->latitude, fix->longitude,
+                                                           pois_.at(i).latitude, pois_.at(i).longitude) - fix->track);
 
                     if(track > 180)
                     {
@@ -261,7 +259,6 @@ void PoiAlerts::onDataUpdated()
                     }
 
                     qDebug() << "Real track: " << track;
-                    qDebug() << "Epd: " << fix->epd;
 
                     double trackLimit;
 
@@ -271,55 +268,45 @@ void PoiAlerts::onDataUpdated()
                     }
                     else
                     {
-                        trackLimit = 90.0 - radToDeg(acos((inRouteMargin + (distance * 0.15)) / distance));
+                        trackLimit = 90.0 - radToDeg(acos((inRouteMargin + (distance * 0.16)) / distance));
                     }
 
                     qDebug() << "Tracklimit: " << trackLimit;
 
                     if(track < trackLimit)
                     {
+                        found = true;
                         currentPoi_ = &pois_[i];
                         currentDistance_ = distance;
+                        emit visibilityChanged(true);
                         playSound(i);
                     }
                     else
                     {
                         currentPoi_ = 0;
+                        emit visibilityChanged(false);
                     }
 
                 }
                 else
                 {
+                    found = true;
                     currentPoi_ = &pois_[i];
                     currentDistance_ = distance;
+                    emit visibilityChanged(true);
                     playSound(i);
                 }
 
                 break;
             }
-            else
-            {
-                currentPoi_ = 0;
-                qDebug() << "Distance: " << distance;
-            }
         }
-    }
 
-    qDebug() << '\n';
-}
-
-void PoiAlerts::startTest()
-{
-    Odometer::instance().start();
-    enabled_ = true;
-    distance_ = 100;
-    PoiReader::Poi poi;
-    poi.latitude = 0.0;
-    poi.longitude = 0.0;
-    poi.name = "Mokki";
-    pois_.push_back(poi);
-    onlyOnRoute_ = true;
-    start();
+        if(!found && currentPoi_)
+        {
+            currentPoi_ = 0;
+            emit visibilityChanged(false);
+        }
+    }
 }
 
 double PoiAlerts::calculateDistance(double latitude1, double longitude1,
@@ -377,4 +364,3 @@ QString PoiAlerts::getPoiDir()
 {
     return Settings::getDir() + "pois" + QDir::separator();
 }
-