~ubuntu-branches/debian/wheezy/foxtrotgps/wheezy

« back to all changes in this revision

Viewing changes to src/hrm_functions.c

  • Committer: Package Import Robot
  • Author(s): Daniel Baumann
  • Date: 2012-02-14 06:13:28 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120214061328-zo4mu0rvgz06q5nw
Tags: 1.1.0-1
* Using compression level 9 also for binary packages.
* Merging upstream version 1.1.0:
  - includes osb2tango and poi2osm (Closes: #647986).
* Removing curl.patch, included upstream.
* Updating to debhelper version 9.
* Updating years in copyright.

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