~foxtrotgps-team/foxtrotgps/trunk

« back to all changes in this revision

Viewing changes to src/hrm_functions.c

  • Committer: Joshua Judson Rosen
  • Author(s): Marcus Bauer
  • Date: 2010-08-08 22:28:45 UTC
  • Revision ID: rozzin@geekspace.com-20100808222845-86w25l3qq5nlf5ib
Perl scripts to convert track-logs from tangoGPS format to GPX and OSM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
 
3
 
#ifdef HAVE_CONFIG_H
4
 
#  include <config.h>
5
 
#endif
6
 
 
7
 
#include <glib.h>
8
 
 
9
 
#include <stdio.h>
10
 
#include <unistd.h>
11
 
 
12
 
#ifdef HAVE_BLUEZ
13
 
#include <sys/socket.h>
14
 
#include <bluetooth/bluetooth.h>
15
 
#include <bluetooth/rfcomm.h>
16
 
#endif /* HAVE_BLUEZ */
17
 
 
18
 
#include "hrm_functions.h"
19
 
#include "globals.h"
20
 
#include "support.h"
21
 
 
22
 
#define MAX_HEART_FREQ 160
23
 
 
24
 
static GIOChannel *hrm_io_channel = NULL;
25
 
 
26
 
static guint hrm_sid1,  hrm_sid3;
27
 
guint watchdog;
28
 
 
29
 
void * get_hrm_data_thread(void *ptr);
30
 
 
31
 
gboolean
32
 
reset_hrm_io()
33
 
{
34
 
#ifdef HAVE_BLUEZ
35
 
        g_free(hrmdata);
36
 
        hrmdata = NULL;
37
 
 
38
 
        global_reconnect_hrm = TRUE;
39
 
        g_source_remove(watchdog);
40
 
 
41
 
        g_source_remove(hrm_sid1);
42
 
        g_source_remove(hrm_sid3);
43
 
 
44
 
        osd_hrm(TRUE);
45
 
#endif /* HAVE_BLUEZ */
46
 
 
47
 
        return FALSE;
48
 
}
49
 
 
50
 
void
51
 
osd_hrm(gboolean force_redraw)
52
 
{
53
 
#ifdef HAVE_BLUEZ
54
 
        PangoContext            *context = NULL;
55
 
        PangoLayout             *layout  = NULL;
56
 
        PangoFontDescription    *desc    = NULL;
57
 
 
58
 
        GdkColor color;
59
 
        GdkGC *gc;
60
 
 
61
 
        gchar *buffer;
62
 
        static int x = 10, y = 310;
63
 
        static int width = 0, height = 0;
64
 
 
65
 
        static int hrm_tmp = 0;
66
 
        int hrm;
67
 
 
68
 
        if(!hrm_on)
69
 
                return;
70
 
 
71
 
 
72
 
        if(mouse_dx == 0 && mouse_dy == 0)
73
 
        {
74
 
                if(hrmdata)
75
 
                        buffer = g_strdup_printf("%u", hrmdata->freq);
76
 
                else
77
 
                        buffer = g_strdup_printf("--");
78
 
 
79
 
 
80
 
                context = gtk_widget_get_pango_context (map_drawable);
81
 
                layout  = pango_layout_new (context);
82
 
                desc    = pango_font_description_new();
83
 
 
84
 
                pango_font_description_set_absolute_size (desc, 50 * PANGO_SCALE);
85
 
                pango_layout_set_font_description (layout, desc);
86
 
                pango_layout_set_text (layout, buffer, strlen(buffer));
87
 
 
88
 
 
89
 
                gc = gdk_gc_new (map_drawable->window);
90
 
 
91
 
                if(hrmdata)
92
 
                        color.red = (hrmdata->freq > MAX_HEART_FREQ) ? 0xffff : 0;
93
 
                else
94
 
                        color.red = 0;
95
 
                color.green = 0;
96
 
                color.blue = 0;
97
 
 
98
 
                gdk_gc_set_rgb_fg_color (gc, &color);
99
 
 
100
 
                if(hrmdata)
101
 
                        hrm = hrmdata->freq;
102
 
                else
103
 
                        hrm = 0;
104
 
 
105
 
                if(hrm_tmp != hrm || force_redraw)
106
 
                {
107
 
 
108
 
                        gdk_draw_drawable (
109
 
                                map_drawable->window,
110
 
                                map_drawable->style->fg_gc[GTK_WIDGET_STATE (map_drawable)],
111
 
                                pixmap,
112
 
                                0,300,
113
 
                                0, 300,
114
 
                                width+10,height+10);
115
 
 
116
 
 
117
 
 
118
 
                                gdk_draw_layout(map_drawable->window,
119
 
                                                gc,
120
 
                                                x, y,
121
 
                                                layout);
122
 
 
123
 
 
124
 
                        pango_layout_get_pixel_size(layout, &width, &height);
125
 
                }
126
 
 
127
 
                hrm_tmp = hrm;
128
 
 
129
 
                g_free(buffer);
130
 
                pango_font_description_free (desc);
131
 
                g_object_unref (layout);
132
 
                g_object_unref (gc);
133
 
        }
134
 
#endif /* HAVE_BLUEZ */
135
 
}
136
 
 
137
 
void
138
 
set_hrm_labels()
139
 
{
140
 
#ifdef HAVE_BLUEZ
141
 
        static GtkWidget *widget1=NULL, *widget2=NULL;
142
 
        static gchar buffer[128];
143
 
 
144
 
        if(!widget1) {
145
 
                widget1 = lookup_widget(window1, "label206");
146
 
                widget2 = lookup_widget(window1, "label207");
147
 
        }
148
 
 
149
 
        g_snprintf (buffer, 128, _("<b><big>%d</big></b> <small>bpm</small>"),
150
 
                    hrmdata->freq);
151
 
        gtk_label_set_label(GTK_LABEL(widget1), buffer);
152
 
 
153
 
        g_snprintf (buffer, 128, _("<b><big>%d/%d</big></b>"),
154
 
                    hrmdata->min, hrmdata->max);
155
 
        gtk_label_set_label(GTK_LABEL(widget2), buffer);
156
 
#endif /* HAVE_BLUEZ */
157
 
}
158
 
 
159
 
 
160
 
static gboolean
161
 
cb_hrm_io_error(GIOChannel *src, GIOCondition condition, gpointer data)
162
 
{
163
 
#ifdef HAVE_BLUEZ
164
 
        g_free(hrmdata);
165
 
        hrmdata = NULL;
166
 
        g_source_remove(hrm_sid1);
167
 
        g_source_remove(hrm_sid3);
168
 
#endif /* HAVE_BLUEZ */
169
 
 
170
 
        return FALSE;
171
 
}
172
 
 
173
 
 
174
 
static gboolean
175
 
cb_hrm_data(GIOChannel *src, GIOCondition condition, gpointer data)
176
 
{
177
 
#ifdef HAVE_BLUEZ
178
 
        static int hrm_status = 0;
179
 
 
180
 
        gsize bytes_read;
181
 
        GError *error = NULL;
182
 
        gchar buf[1024];
183
 
        GIOStatus status;
184
 
 
185
 
        status =  g_io_channel_read_chars(
186
 
                                hrm_io_channel,
187
 
                                (char *) buf, sizeof(buf),
188
 
                                &bytes_read, &error);
189
 
 
190
 
        if(status == G_IO_STATUS_NORMAL)
191
 
        {
192
 
 
193
 
 
194
 
                if(hrm_status == 0)
195
 
                {
196
 
                        if((bytes_read == 60) && (buf[0] == 2))
197
 
                                hrmdata->freq = buf[12];
198
 
 
199
 
                        if((bytes_read == 1) && (buf[0] == 2))
200
 
                                hrm_status = 1;
201
 
                }
202
 
                else {
203
 
                        if(bytes_read == 59)
204
 
                                hrmdata->freq = buf[11];
205
 
 
206
 
                        hrm_status = 0;
207
 
                }
208
 
 
209
 
 
210
 
                g_source_remove(watchdog);
211
 
                watchdog = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE,60,reset_hrm_io,NULL,NULL);
212
 
        }
213
 
        else
214
 
        {
215
 
                if (error)
216
 
                        printf("gio: %s \n", error->message);
217
 
 
218
 
        }
219
 
 
220
 
 
221
 
        if(hrmdata->freq < hrmdata->min && hrmdata->freq > 50)
222
 
                hrmdata->min = hrmdata->freq;
223
 
        if(hrmdata->freq > hrmdata->max && hrmdata->freq < 220)
224
 
                hrmdata->max = hrmdata->freq;
225
 
#endif /* HAVE_BLUEZ */
226
 
 
227
 
        return TRUE;
228
 
}
229
 
 
230
 
 
231
 
 
232
 
void
233
 
get_hrm_data()
234
 
{
235
 
#ifdef HAVE_BLUEZ
236
 
#if GLIB_CHECK_VERSION(2,34,0)
237
 
        g_thread_new("get hrm thread", &get_hrm_data_thread, (gpointer) NULL);
238
 
#else
239
 
        g_thread_create(&get_hrm_data_thread, NULL, FALSE, NULL);
240
 
#endif
241
 
#endif /* HAVE_BLUEZ */
242
 
}
243
 
 
244
 
void *
245
 
get_hrm_data_thread(void *ptr)
246
 
{
247
 
#ifdef HAVE_BLUEZ
248
 
        static int hrm_sock = 0;
249
 
        struct sockaddr_rc addr = {0};
250
 
        int conn, len;
251
 
        unsigned char buf[501];
252
 
        static int cnt = 0;
253
 
        GError *error = NULL;
254
 
 
255
 
        if(!hrm_on)
256
 
                return NULL;
257
 
 
258
 
        if(!global_hrm_bt_addr)
259
 
                return NULL;
260
 
 
261
 
        if(!hrmdata  || global_reconnect_hrm)
262
 
        {
263
 
                if(cnt++ % 15 != 0)
264
 
                        return NULL;
265
 
 
266
 
                if (hrm_sock) hrm_sock = close(hrm_sock);
267
 
 
268
 
                hrm_sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
269
 
                if(hrm_sock < 0) {
270
 
                        printf("error opening hrm socket\n");
271
 
                        return NULL;
272
 
                }
273
 
 
274
 
                str2ba(global_hrm_bt_addr, &addr.rc_bdaddr);
275
 
 
276
 
                addr.rc_family = AF_BLUETOOTH;
277
 
                addr.rc_channel = 1;
278
 
 
279
 
                conn = connect(hrm_sock, (struct sockaddr *)&addr, sizeof(addr));
280
 
                if(conn < 0) {
281
 
                        printf("error connecting %s\n", global_hrm_bt_addr);
282
 
 
283
 
                        conn = 1;
284
 
                        shutdown(hrm_sock, SHUT_RDWR);
285
 
                }
286
 
                else {
287
 
                        global_reconnect_hrm = FALSE;
288
 
 
289
 
                        if(!hrmdata) {
290
 
                                hrmdata = g_new0(hrm_data_t,1);
291
 
                                hrmdata->min = 999;
292
 
                        }
293
 
 
294
 
                        len = read(hrm_sock, buf, 500);
295
 
 
296
 
                        fprintf(stderr, "HRM Rcvd %i bytes", len);
297
 
 
298
 
 
299
 
                        watchdog = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE,60,reset_hrm_io,NULL,NULL);
300
 
 
301
 
                        hrm_io_channel = g_io_channel_unix_new(hrm_sock);
302
 
                        g_io_channel_set_encoding(hrm_io_channel, NULL, &error);
303
 
                        g_io_channel_set_flags(hrm_io_channel, G_IO_FLAG_NONBLOCK, NULL);
304
 
 
305
 
                        hrm_sid1 = g_io_add_watch_full(hrm_io_channel, G_PRIORITY_HIGH_IDLE+200, G_IO_ERR | G_IO_HUP, cb_hrm_io_error, NULL, NULL);
306
 
                        hrm_sid3 = g_io_add_watch_full(hrm_io_channel, G_PRIORITY_HIGH_IDLE+200, G_IO_IN | G_IO_PRI, cb_hrm_data, NULL, NULL);
307
 
                }
308
 
        }
309
 
        return NULL;
310
 
#endif /* HAVE_BLUEZ */
311
 
}