-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.cpp
205 lines (190 loc) · 3.73 KB
/
gpio.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "gpio.hpp"
int Gpio::init(int baud)
{
wiringPiSetup();
pinMode(0,OUTPUT);//RED LIGHT
digitalWrite(0,HIGH);
pinMode(2,OUTPUT);//GREEN LIGHT
digitalWrite(2,HIGH);
pinMode(3,OUTPUT);//BLUE LIGHT
digitalWrite(3,HIGH);
pinMode(7,OUTPUT);//BEEP
digitalWrite(7,HIGH);
pinMode(27,INPUT);//KEY 1
pullUpDnControl(27,PUD_UP);
pinMode(28,INPUT);//KEY 2
pullUpDnControl(28,PUD_UP);
pinMode(29,INPUT);//KEY 3
pullUpDnControl(29,PUD_UP);
fd=serialOpen("/dev/ttyAMA0",baud);
baud_rate=baud;
std::cout<<"gpio initialized!"<<std::endl;
show_baud_rate();
return 0;
}
int Gpio::setbit(int io,int action)
{
if (action==1)
{
digitalWrite(io,HIGH);
}
else
{
digitalWrite(io,LOW);
}
}
int Gpio::serial_receive()
{
int height;
char height_array[10]={0};
if(serialDataAvail(fd))
{
for(int i=0;i<4;i++)
{
height_array[i]=serialGetchar(fd)-48;
}
if(height_array[3]=='H'-48)
height=height_array[0]*100+height_array[1]*10+height_array[2];
}
serialFlush(fd);
return height;
}
int Gpio::serial_transmit(int status,float x,float y,float z)
{
status='M';//M/R/F not working
char x_p[4]={0};
char y_p[4]={0};
char z_p[4]={0};
x_p[0]= ((int)x)/100+48;
x_p[1]= ((int)x)/10%10+48;
x_p[2]= ((int)x)%10+48;
y_p[0]= ((int)y)/100+48;
y_p[1]= ((int)y)/10%10+48;
y_p[2]= ((int)y)%10+48;
z_p[0]= ((int)z)/100+48;
z_p[1]= ((int)z)/10%10+48;
z_p[2]= ((int)z)%10+48;
serialPrintf(fd,"A%cx%sy%sz%s",status,x_p,y_p,z_p);
return 0;
}
void Gpio::show_baud_rate()
{
std::cout<<"baud_rate: "<<baud_rate<<std::endl;
}
int Gpio::scan()
{
//std::cout<<"scanning"<<std::endl;
if(digitalRead(27)==LOW)
{
usleep(20000);
if(digitalRead(27)==LOW)
{
std::cout<<"task start"<<std::endl;
task_flag=0;
usleep(500000);
}
}
if(digitalRead(28)==LOW)
{
usleep(20000);
if(digitalRead(28)==LOW)
{
std::cout<<"28 pressed"<<std::endl;
usleep(500000);
}
}
if(digitalRead(29)==LOW)
{
usleep(20000);
if(digitalRead(29)==LOW)
{
std::cout<<"29 pressed"<<std::endl;
usleep(500000);
}
}
usleep(10000);
}
void Light::blink(int color, int speed)
{
int sleep_time;
if(speed==SLOW)
{
sleep_time=800000;
}
else
{
sleep_time=400000;
}
digitalWrite(color,LOW);
usleep(sleep_time);
digitalWrite(color,HIGH);
usleep(sleep_time);
}
void Light::turn_on(int color)
{
switch (color)
{
case RED:
{
if(RED_status==OFF)
{
digitalWrite(RED,LOW);
RED_status=ON;
}
break;
}
case GREEN:
{
if(GREEN_status==OFF)
{
digitalWrite(GREEN,LOW);
GREEN_status=ON;
}
break;
}
case BLUE:
{
if(BLUE_status==OFF)
{
digitalWrite(BLUE,LOW);
BLUE_status=ON;
}
break;
}
}
usleep(10000);
}
void Light::turn_off(int color)
{
switch (color)
{
case RED:
{
if(RED_status==ON)
{
digitalWrite(RED,HIGH);
RED_status=OFF;
}
break;
}
case GREEN:
{
if(GREEN_status==ON)
{
digitalWrite(GREEN,HIGH);
GREEN_status=OFF;
}
break;
}
case BLUE:
{
if(BLUE_status==ON)
{
digitalWrite(BLUE,HIGH);
BLUE_status=OFF;
}
break;
}
}
usleep(10000);
}