13
#include <sys/socket.h>
14
#include <bluetooth/bluetooth.h>
15
#include <bluetooth/rfcomm.h>
16
#endif /* HAVE_BLUEZ */
18
#include "hrm_functions.h"
22
#define MAX_HEART_FREQ 160
24
static GIOChannel *hrm_io_channel = NULL;
26
static guint hrm_sid1, hrm_sid3;
29
void * get_hrm_data_thread(void *ptr);
35
printf("*** %s(): \n",__PRETTY_FUNCTION__);
40
global_reconnect_hrm = TRUE;
41
g_source_remove(watchdog);
43
g_source_remove(hrm_sid1);
44
g_source_remove(hrm_sid3);
47
#endif /* HAVE_BLUEZ */
53
osd_hrm(gboolean force_redraw)
56
PangoContext *context = NULL;
57
PangoLayout *layout = NULL;
58
PangoFontDescription *desc = NULL;
64
static int x = 10, y = 310;
65
static int width = 0, height = 0;
67
static int hrm_tmp = 0;
74
if(mouse_dx == 0 && mouse_dy == 0)
77
buffer = g_strdup_printf("%u", hrmdata->freq);
79
buffer = g_strdup_printf("--");
82
context = gtk_widget_get_pango_context (map_drawable);
83
layout = pango_layout_new (context);
84
desc = pango_font_description_new();
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));
91
gc = gdk_gc_new (map_drawable->window);
94
color.red = (hrmdata->freq > MAX_HEART_FREQ) ? 0xffff : 0;
100
gdk_gc_set_rgb_fg_color (gc, &color);
107
if(hrm_tmp != hrm || force_redraw)
111
map_drawable->window,
112
map_drawable->style->fg_gc[GTK_WIDGET_STATE (map_drawable)],
120
gdk_draw_layout(map_drawable->window,
126
pango_layout_get_pixel_size(layout, &width, &height);
132
pango_font_description_free (desc);
133
g_object_unref (layout);
136
#endif /* HAVE_BLUEZ */
143
static GtkWidget *widget1=NULL, *widget2=NULL;
144
static gchar buffer[128];
147
widget1 = lookup_widget(window1, "label206");
148
widget2 = lookup_widget(window1, "label207");
151
g_snprintf (buffer, 128, _("<b><big>%d</big></b> <small>bpm</small>"),
153
gtk_label_set_label(GTK_LABEL(widget1), buffer);
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 */
163
cb_hrm_io_error(GIOChannel *src, GIOCondition condition, gpointer data)
166
printf("*** %s(): \n",__PRETTY_FUNCTION__);
169
g_source_remove(hrm_sid1);
170
g_source_remove(hrm_sid3);
171
#endif /* HAVE_BLUEZ */
178
cb_hrm_data(GIOChannel *src, GIOCondition condition, gpointer data)
181
static int hrm_status = 0;
184
GError *error = NULL;
188
status = g_io_channel_read_chars(
190
(char *) buf, sizeof(buf),
191
&bytes_read, &error);
193
if(status == G_IO_STATUS_NORMAL)
199
if((bytes_read == 60) && (buf[0] == 2))
200
hrmdata->freq = buf[12];
202
if((bytes_read == 1) && (buf[0] == 2))
207
hrmdata->freq = buf[11];
213
g_source_remove(watchdog);
214
watchdog = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE,60,reset_hrm_io,NULL,NULL);
219
printf("gio: %s \n", error->message);
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 */
239
g_thread_create(&get_hrm_data_thread, NULL, FALSE, NULL);
240
#endif /* HAVE_BLUEZ */
244
get_hrm_data_thread(void *ptr)
247
static int hrm_sock = 0;
248
struct sockaddr_rc addr = {0};
250
unsigned char buf[501];
252
GError *error = NULL;
257
if(!global_hrm_bt_addr)
260
if(!hrmdata || global_reconnect_hrm)
265
if (hrm_sock) hrm_sock = close(hrm_sock);
267
hrm_sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
269
printf("error opening hrm socket\n");
273
str2ba(global_hrm_bt_addr, &addr.rc_bdaddr);
275
addr.rc_family = AF_BLUETOOTH;
278
conn = connect(hrm_sock, (struct sockaddr *)&addr, sizeof(addr));
280
printf("error connecting %s\n", global_hrm_bt_addr);
283
shutdown(hrm_sock, SHUT_RDWR);
286
printf("connection done %s\n", global_hrm_bt_addr);
288
global_reconnect_hrm = FALSE;
291
hrmdata = g_new0(hrm_data_t,1);
295
len = read(hrm_sock, buf, 500);
297
fprintf(stderr, "HRM Rcvd %u bytes", len);
300
watchdog = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT_IDLE,60,reset_hrm_io,NULL,NULL);
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);
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);
311
#endif /* HAVE_BLUEZ */