path:
/right_initiator.cpp
1.17 KB | plain
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
#include <Bluetooth.hpp>
#include <GPIO.hpp>
#include <LED.hpp>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <signal.h>
#define BUF_SIZE 256
using namespace std;
using namespace gpio;
xs_SOCKET x;
void deviceConnected(xs_SOCKET s)
{
x = s;
if(s == -1){
fmt::print("Error creation of socket\n");
return;
}
fmt::print("Device connected\n");
int pin = 17;
GPIO *g = GPIO::getGPIO(pin);
if (g == nullptr)
{
fmt::print("Cannot open GPIO {}\n", pin);
return;
}
fmt::print("led going to create\n");
LED led(g);
int bytesRead = 0;
char *msg = (char *)calloc(sizeof(char), BUF_SIZE);
fmt::print("going to read\n");
while ((bytesRead = read(s, msg, BUF_SIZE - 1)) > 0)
{
msg[bytesRead] = '\0';
if (strncmp("rmk on", msg, 6) == 0)
{
led.on();
}
else
{
led.off();
}
printf("%s", msg);
}
fmt::print("EOF\n");
}
void exit_handler(int sig){
close(x);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
signal(SIGINT, exit_handler);
Bluetooth b;
b.setOnDeviceConnected(deviceConnected);
if (b.startSlave())
{
fmt::print("Slave running\n");
}
else
{
fmt::print("Cannot start slave\n");
}
while (1)
;
return 0;
}