-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledcontrolv3.c
159 lines (139 loc) · 3.65 KB
/
ledcontrolv3.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
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
153
154
155
156
157
158
#include <pthread.h>
#include <pigpio.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lirc/lirc_client.h>
#include <time.h>
#include <unistd.h>
#include <wiringPi.h>
void sLED (int red,int blue,int green,int mode);
#define RED 21
#define BLUE 16
#define GREEN 20
int MODE;
/* This is our thread function. It is like main(), but for a thread*/
void *threadFunc(void *arg)
{
struct lirc_config *config;
int buttonTimer = millis();
char *code;
char *c;
//Initiate LIRC. Exit on failure
if(lirc_init("lirc",1)==-1)
exit(EXIT_FAILURE);
//Read the default LIRC config at /etc/lirc/lircd.conf This is the config for your remot$
if(lirc_readconfig(NULL,&config,NULL)==0)
{
//Do stuff while LIRC socket is open 0=open -1=closed.
while(lirc_nextcode(&code)==0)
{
//If code = NULL, meaning nothing was returned from LIRC socket,
//then skip lines below and start while loop again.
if(code==NULL) continue;{
if (millis() - buttonTimer > 400){
//Check to see if the string "KEY_#" appears anywhere within the string $
if(strstr (code,"KEY_F")){
printf("MATCH on KEY_F\n");
sLED(0,200,0,0);
buttonTimer = millis();
}
else if(strstr (code,"KEY_G")){
printf("MATCH on KEY_G\n");
MODE = 2;
buttonTimer = millis();
}
else if(strstr (code,"KEY_H")){
printf("MATCH on KEY_H\n");
MODE = 1;
buttonTimer = millis();
}
else if(strstr (code,"KEY_E")){
printf("MATCH on KEY_E\n");
sLED(0,0,100,0);
buttonTimer = millis();
}
else if(strstr (code,"KEY_A")){
printf("MATCH on KEY_A\n");
sLED(0,0,0,0);
buttonTimer = millis();
}
}
}
//Need to free up code before the next loop
free(code);
}
//Frees the data structures associated with config.
lirc_freeconfig(config);
}
//lirc_deinit() closes the connection to lircd and does some internal clean-up stuff.
lirc_deinit();
//exit(EXIT_SUCCESS);
return NULL;
}
int main(void)
{
//initialise pigio and pins
if (gpioInitialise() < 0 )
{
printf("pigpio initialise failed\n");
return 1;
}
gpioSetMode(BLUE, PI_OUTPUT);
gpioSetMode(RED, PI_OUTPUT);
gpioSetMode(GREEN, PI_OUTPUT);
gpioSetPWMrange(BLUE, 200);
gpioSetPWMrange(RED, 200);
gpioSetPWMrange(GREEN, 200);
pthread_t pth; // this is our thread identifier
pthread_create(&pth,NULL,threadFunc,NULL);
int i;
while(1){
if(MODE != 0){
for (i=0;i<201;i++){
if (MODE == 1){
sLED(0,0,i,1);
usleep(20000);
}
else if(MODE == 2){
sLED(i,i/2,i/4,1);
usleep(20000);
}
else if(MODE == 0){
break;
}
}
sleep(1);
for(i=200;i>=0;i--){
if(MODE == 1){
sLED(0,0,i,1);
usleep(20000);
}
else if(MODE == 2){
sLED(i,i/2,i/4,1);
usleep(20000);
}
else if(MODE == 0){
break;
}
}
}
}
pthread_join(pth,NULL);
return 1;
}
void sLED (int red,int blue,int green,int mode)
{
if(mode == 1){
gpioPWM(RED,red);
gpioPWM(BLUE,blue);
gpioPWM(GREEN,green);
}
else if(mode == 0){
MODE = 0;
gpioPWM(RED,red);
gpioPWM(BLUE,blue);
gpioPWM(GREEN,green);
}
}