forked from etwmc/Personal-HomeKit-HAP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PHKArduinoLightInterface.c
64 lines (49 loc) · 1.32 KB
/
PHKArduinoLightInterface.c
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
//
// PHKArduinoLightInterface.c
// Workbench
//
// Created by Wai Man Chan on 11/3/14.
//
//
#include "PHKArduinoLightInterface.h"
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#define SerialPortAddr "/dev/ttyUSB0"
int serialPort = -1;
void setupPort() {
serialPort = open(SerialPortAddr, O_WRONLY|O_NOCTTY|O_NDELAY);
printf("Serial Port: %d\n", serialPort);
if (serialPort >= 0) {
struct termios options;
/*
* Get the current options for the port...
*/
tcgetattr(serialPort, &options);
/*
* Set the baud rates to 19200...
*/
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
/*
* Enable the receiver and set local mode...
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Set the new options for the port...
*/
tcsetattr(serialPort, TCSANOW, &options);
}
}
void startIdentify() {
write(serialPort, "-1", 3);
}
void setLightStrength(int strengthLevel) {
strengthLevel = strengthLevel < 0? 0: strengthLevel;
strengthLevel = strengthLevel > 255? 255: strengthLevel;
char temp[6];
int len = snprintf(temp, 6, "+%d", strengthLevel);
write(serialPort, temp, len);
}
void setFanSpeed(int strengthLevel) {}