-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardware.cpp
81 lines (69 loc) · 1.5 KB
/
hardware.cpp
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
#include "hardware.h"
#include "arduino_cmd.h"
#include <pthread.h>
#include "slrov.h"
#include <errno.h>
#ifdef SLSERVER_WIN32
#include <windows.h>
#endif
#include <string.h>
#ifdef SLSERVER_LINUX
#include <unistd.h>
#endif
hardware::hardware() {
serial = NULL;
}
hardware::~hardware() {
running = false;
if(serial)
serial->close();
pthread_join(_thread, NULL);
pthread_mutex_destroy(&lock);
if(serial)
delete serial;
}
bool hardware::init(const char* p) {
serial = new Serial(p);
running = true;
if(!serial->touchForCDCReset()||serial->begin(_B115200) == false) {
printf("***ERROR*** init harware\n");
return false;
}
if (0 != pthread_create(&_thread, NULL, hardware::recv_thread, (void*)this)) {
printf("error when create pthread,%d\n", errno);
return false;
}
pthread_mutex_init(&lock,NULL);
return true;
}
void hardware::on(const char* p,ino_execute exec,void* user) {
arduino_cmd::add_command(p,exec,user);
}
int hardware::write(const char* p) {
int len = strlen(p);
pthread_mutex_lock(&lock);
if (-1 == serial->write(p, len)) {
pthread_mutex_unlock(&lock);
return -1;
}
pthread_mutex_unlock(&lock);
return len;
}
void* __cdecl hardware::recv_thread(void* data) {
hardware* t = (hardware*)data;
char buf[512];
while(t->running) {
if(t->serial->readline(buf) != -1 ) {
arduino_cmd::parse_command(buf);
}
else {
#ifdef SLSERVER_WIN32
Sleep(1);
#elif defined(SLSERVER_LINUX)
usleep(1);
#endif
}
}
LOGOUT("***INFO*** Hardware Return OK\n");
return NULL;
}