-
Notifications
You must be signed in to change notification settings - Fork 0
/
owon_device.vala
87 lines (76 loc) · 2.87 KB
/
owon_device.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**********************************************************************
owon-tools - Copyright (C) 2017 - Andreas Kemnade
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
public class OwonDevice : GLib.Object {
org.bluez.GattCharacteristic1 [] chars;
org.bluez.Device1 device;
public enum Button {
SELECT = 1,
RANGE = 2,
HOLD_LIGHT = 3,
DELTA_BT = 4,
HZ_DUTY = 5,
MAX_MIN = 6
}
public const string uuid = "0000fff0-0000-1000-8000-00805f9b34fb";
public void press_button(Button b, int times) throws IOError
{
uint8[] packet = {(uint8) b, (uint8)times};
chars[2].write_value(packet, new HashTable<string, Variant>(str_hash, str_equal));
}
public void start_measure() throws IOError {
chars[3].start_notify();
}
public void stop_measure() throws IOError {
chars[3].stop_notify();
}
public bool is_connected() {
return device.connected;
}
public signal void got_measurement(MeasurementResult res);
void got_measurement_noti(Variant changed_properties,
string[] invalidated_properties) {
// stdout.printf("changed props: %s\n", changed_properties.print(true));
Variant val = changed_properties.lookup_value("Value", VariantType.BYTESTRING);
if (val != null) {
int i;
uint8 [] value = new uint8[val.n_children()];
// stdout.printf("changed value: %s\n", val.print(true));
for(i = 0; i < value.length; i++) {
value[i]=val.get_child_value(i).get_byte();
}
MeasurementResult res = new MeasurementResult(value);
if (res.valid)
got_measurement(res);
}
}
public static OwonDevice? connect_via_pathlist(GLib.DBusConnection conn, GLib.ObjectPath servicepath, GLib.ObjectPath [] charpaths) throws IOError {
int i;
org.bluez.GattCharacteristic1 [] chars = new org.bluez.GattCharacteristic1[charpaths.length];
for(i = 0; i < charpaths.length; i++) {
chars[i] = conn.get_proxy_sync("org.bluez",
charpaths[i]);
}
org.bluez.GattService1 service = conn.get_proxy_sync("org.bluez",
servicepath);
if (service.u_u_i_d != uuid)
return null;
org.bluez.Device1 device = conn.get_proxy_sync("org.bluez",
service.device);
return new OwonDevice(device, chars);
}
public class OwonDevice(org.bluez.Device1 device,
org.bluez.GattCharacteristic1 [] chars) {
this.chars = chars;
this.device = device;
(chars[3] as DBusProxy).g_properties_changed.connect(got_measurement_noti);
}
}