FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tinyb/examples/notifications.cpp at master · intel-iot-devkit/tinyb · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Jan 4, 2023. It is now read-only.
intel-iot-devkit
/
tinyb
Public archive
Notifications
You must be signed in to change notification settings
Fork
120
Star
270
Code
Issues
83
Pull requests
5
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
tinyb
/
examples
/
notifications.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
152 lines (127 loc) · 5.4 KB
Breadcrumbs
tinyb
/
examples
/
notifications.cpp
Copy path
File metadata and controls
152 lines (127 loc) · 5.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Author: Petre Eftime <petre.p.eftime@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#
include
<
tinyb.hpp
>
#
include
<
vector
>
#
include
<
iostream
>
#
include
<
thread
>
#
include
<
csignal
>
#
include
<
condition_variable
>
using
namespace
tinyb
;
std::condition_variable cv;
/*
* Converts a raw temperature read from the sensor to a Celsius value.
* @param[in] raw_temp The temperature read from the sensor (two bytes)
* @return The Celsius value of the temperature
*/
static
float
celsius_temp
(
uint16_t
raw_temp)
{
const
float
SCALE_LSB
=
0.03125
;
return
((
float
)(raw_temp >>
2
)) *
SCALE_LSB
;
}
void
data_callback
(BluetoothGattCharacteristic &c, std::vector<
unsigned
char
> &data,
void
*userdata)
{
/*
Read temperature data and display it
*/
unsigned
char
*data_c;
unsigned
int
size = data.
size
();
if
(size >
0
) {
data_c = data.
data
();
std::cout <<
"
Raw data=[
"
;
for
(
unsigned
i =
0
; i < size; i++)
std::cout << std::hex <<
static_cast
<
int
>(data_c[i]) <<
"
,
"
;
std::cout <<
"
]
"
;
uint16_t
ambient_temp, object_temp;
object_temp = data_c[
0
] | (data_c[
1
] <<
8
);
ambient_temp = data_c[
2
] | (data_c[
3
] <<
8
);
std::cout <<
"
Ambient temp:
"
<<
celsius_temp
(ambient_temp) <<
"
C
"
;
std::cout <<
"
Object temp:
"
<<
celsius_temp
(object_temp) <<
"
C
"
;
std::cout << std::endl;
}
}
void
signal_handler
(
int
signum)
{
if
(signum ==
SIGINT
) {
cv.
notify_all
();
}
}
/*
* This program reads the temperature from a
* TI Sensor Tag(http://www.ti.com/ww/en/wireless_connectivity/sensortag2015/?INTC=SensorTag&HQS=sensortag)
* Pass the MAC address of the sensor as the first parameter of the program.
*/
int
main
(
int
argc,
char
**argv)
{
if
(argc <
2
) {
std::cerr <<
"
Run as:
"
<< argv[
0
] <<
"
<device_address>
"
<< std::endl;
exit
(
1
);
}
BluetoothManager *manager =
nullptr
;
try
{
manager =
BluetoothManager::get_bluetooth_manager
();
}
catch
(
const
std::runtime_error& e) {
std::cerr <<
"
Error while initializing libtinyb:
"
<< e.
what
() << std::endl;
exit
(
1
);
}
/*
Start the discovery of devices
*/
bool
ret = manager->
start_discovery
();
std::cout <<
"
Started =
"
<< (ret ?
"
true
"
:
"
false
"
) << std::endl;
std::unique_ptr<BluetoothGattService> temperature_service;
std::string
device_mac
(argv[
1
]);
auto
sensor_tag = manager->
find
<BluetoothDevice>(
nullptr
, &device_mac,
nullptr
,
std::chrono::seconds
(
10
));
if
(sensor_tag ==
nullptr
) {
std::cout <<
"
Device not found
"
<< std::endl;
return
1
;
}
sensor_tag->
enable_connected_notifications
([] (BluetoothDevice &d,
bool
connected,
void
*usedata)
{
if
(connected) std::cout <<
"
Connected
"
<< d.
get_name
() << std::endl; },
NULL
);
if
(sensor_tag !=
nullptr
) {
/*
Connect to the device and get the list of services exposed by it
*/
sensor_tag->
connect
();
std::string
service_uuid
(
"
f000aa00-0451-4000-b000-000000000000
"
);
std::cout <<
"
Waiting for service
"
<< service_uuid <<
"
to be discovered
"
<< std::endl;
temperature_service = sensor_tag->
find
(&service_uuid);
}
else
{
ret = manager->
stop_discovery
();
std::cerr <<
"
SensorTag not found after 30 seconds, exiting
"
<< std::endl;
return
1
;
}
/*
Stop the discovery (the device was found or timeout was over)
*/
ret = manager->
stop_discovery
();
std::cout <<
"
Stopped =
"
<< (ret ?
"
true
"
:
"
false
"
) << std::endl;
auto
value_uuid =
std::string
(
"
f000aa01-0451-4000-b000-000000000000
"
);
auto
temp_value = temperature_service->
find
(&value_uuid);
auto
config_uuid =
std::string
(
"
f000aa02-0451-4000-b000-000000000000
"
);
auto
temp_config = temperature_service->
find
(&config_uuid);
auto
period_uuid =
std::string
(
"
f000aa03-0451-4000-b000-000000000000
"
);
auto
temp_period = temperature_service->
find
(&period_uuid);
/*
Activate the temperature measurements
*/
std::vector<
unsigned
char
> config_on {
0x01
};
temp_config->
write_value
(config_on);
temp_period->
write_value
({
100
});
temp_value->
enable_value_notifications
(data_callback,
nullptr
);
std::mutex m;
std::unique_lock<std::mutex>
lock
(m);
std::signal
(
SIGINT
, signal_handler);
cv.
wait
(lock);
/*
Disconnect from the device
*/
if
(sensor_tag !=
nullptr
)
sensor_tag->
disconnect
();
}
Back
|
FazBrowse Home
|
New Git URL