along with this program. If not, see <http://www.gnu.org/licenses/>.
****/
+#include <math.h>
+
#include "callbacks.h"
#include "appdata.h"
#include "util.h"
void location_changed(LocationGPSDevice* device, gpointer data) {
//print_location(device);
+ g_assert(data);
+ g_assert(device);
AppData* appdata = (AppData*) data;
+ // check for NaN before passing values
if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
- g_print("Speed is %.2f km/h\n", device->fix->speed);
- interpret_speed_from_gps(appdata, device->fix->speed);
+ if(!isnan(device->fix->speed)) {
+ interpret_speed_from_gps(appdata, device->fix->speed);
+ }
}
}
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
****/
-
+#include <math.h>
#include <location/location-gps-device.h>
#include <location/location-gpsd-control.h>
#include <gconf/gconf-client.h>
void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
g_assert(appdata);
+ g_assert(!isnan(speed));
- // if speed is below one then it's zero
- if(speed < 1) {
- set_nth_digit(appdata, 0, 0);
- set_nth_digit(appdata, 1, 0);
- set_nth_digit(appdata, 2, 0);
- return;
- }
-
- // speed => 1
- // first let's convert the number to string
- gchar* cspeed = g_malloc(30); // alloc
- g_sprintf(cspeed, "%f", speed);
-
- // split the string using comma as delimiter
- gchar** splitted = g_strsplit(cspeed, ",", 2); // alloc
-
- gchar* ckm = splitted[0]; // contains the km/h part e.g 123 assuming speed was 123,034510
- gchar* cm = splitted[1]; // contains the m/h part e.g 034510 assuming speed was 123,034510
+ // convert float to a 6 digits (including dot) wide string with leading zeros
+ gchar* charspeed = g_malloc(10); // alloc
+ g_sprintf(charspeed, "%0*.2f", 6, speed);
- g_print("Original speed %c, splitted speed %c and %c\n", cspeed, ckm, cm);
+ g_print("Speed is %s km/h\n", charspeed);
- // we need to pad km part in order to ensure it is *atleast* 3 digits long
- gchar* padding = "00";
- gchar* padded = g_strconcat(padding, cm, NULL); // alloc
-
- g_print("Original speed %c, padded speed %c (km) and %c (m)\n", cspeed, padded, cm);
-
-
- guint i = 2;
- guint pspeedl = strlen(padded);
-
- while(i+1) {
- guint value = g_ascii_digit_value(padded[pspeedl]);
- set_nth_digit(appdata, i, value);
+ guint i = 3;
+ while(i) {
i--;
+ set_nth_digit(appdata, i, g_ascii_digit_value(charspeed[i]));
}
repaint_all_digits(appdata);
- g_free(padded);
- g_free(cspeed);
- g_strfreev(splitted);
+ g_free(charspeed);
}
+
static show_dialog() {
GtkWidget *dialog = gtk_message_dialog_new(
NULL,