1 | #include "bcm/bcm2835.h" |
---|
2 | #include "spi/iSPI.h" |
---|
3 | #include "lcd/lcd.h" |
---|
4 | #include "i2c/i2c8Bit.h" |
---|
5 | #include <iostream> |
---|
6 | #include <sstream> |
---|
7 | #include <string> |
---|
8 | #include <math.h> |
---|
9 | #include <stdio.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #include <unistd.h> |
---|
12 | #include <sys/types.h> |
---|
13 | #include <sys/socket.h> |
---|
14 | #include <netinet/in.h> |
---|
15 | #include <limits> |
---|
16 | using namespace std; |
---|
17 | |
---|
18 | unsigned int myDATA[8]; |
---|
19 | uint8_t ibut = 25;//button |
---|
20 | uint8_t pinA = RPI_V2_GPIO_P1_11, |
---|
21 | pinB = RPI_V2_GPIO_P1_12, |
---|
22 | pinRI = RPI_V2_GPIO_P1_13, |
---|
23 | A=0, B=0, RI=0, |
---|
24 | LAST=0; |
---|
25 | //int8_t VAL[16]={0,1,-1,0,-1,0,0,1,1,0,0,-1,0,-1,1,0}; |
---|
26 | int8_t VAL[16]={0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};//same direction as NP30 |
---|
27 | int C=0, Cp=0; |
---|
28 | double Cout=0,errLE=0; |
---|
29 | double tpr[4];//temperature,C |
---|
30 | double hum[2];//humidity,% |
---|
31 | double Rx; |
---|
32 | double Voltage=4783.;//measure voltage on RPi! |
---|
33 | double PotPosition;//mm |
---|
34 | double current;//mA |
---|
35 | double pre[4]; |
---|
36 | void error(const char *msg) |
---|
37 | { |
---|
38 | perror(msg); |
---|
39 | exit(1); |
---|
40 | } |
---|
41 | |
---|
42 | int main(int argc, char **argv) |
---|
43 | { int imax = numeric_limits<int>::max(); |
---|
44 | iSPI adc; |
---|
45 | lcd LCD; |
---|
46 | //button |
---|
47 | bcm2835_init(); |
---|
48 | bcm2835_gpio_set_pud(ibut, BCM2835_GPIO_PUD_OFF); |
---|
49 | |
---|
50 | |
---|
51 | /*INITIALIZATION*/ |
---|
52 | //Variable init |
---|
53 | unsigned char button = 0; |
---|
54 | int c=0; |
---|
55 | bool sw[4], LE, rst;//LE - linear encoder |
---|
56 | //GPIO expander init |
---|
57 | i2c8Bit mcp23017(0x20,string("/dev/i2c-1")); |
---|
58 | //0x20 - i2c address |
---|
59 | mcp23017.writeReg(0x01,0b11111110); |
---|
60 | //0x01- GPIOB (0x00 GPIOA); GPIOB0 - out (0), GPIOB1-7 - in (1) |
---|
61 | /*Socket init*/ |
---|
62 | int sockfd, newsockfd, portno=80; |
---|
63 | socklen_t clilen; |
---|
64 | |
---|
65 | struct sockaddr_in serv_addr, cli_addr; |
---|
66 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
67 | fcntl(sockfd, F_SETFL, O_NONBLOCK); |
---|
68 | if (sockfd < 0) error("ERROR opening socket"); |
---|
69 | |
---|
70 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
71 | serv_addr.sin_family = AF_INET; |
---|
72 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
73 | serv_addr.sin_port = htons(portno); |
---|
74 | if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); |
---|
75 | listen(sockfd,5); |
---|
76 | clilen = sizeof(cli_addr); |
---|
77 | /*end of init*/ |
---|
78 | |
---|
79 | //Button check |
---|
80 | mcp23017.writeReg(0x15,0x01); |
---|
81 | usleep(10); |
---|
82 | mcp23017.readReg(0x13,button); |
---|
83 | //printf("Checking for switch and LE connections:\n1 - is connected, 0 - is unconnected\n"); |
---|
84 | for (int i=1;i<5;i++) |
---|
85 | {//printf("Switch N%i = %i\n",i,(button>>i)&0x01); |
---|
86 | pre[i-1]=((button>>i)&0x01);}//1 -is connect, 0 -not; |
---|
87 | //swiches are called one by one, from 1 to 4 |
---|
88 | mcp23017.writeReg(0x15,0x00); |
---|
89 | |
---|
90 | mcp23017.readReg(0x13,button); |
---|
91 | LE=((button>>5)&0x01); |
---|
92 | //printf("Linear encoder = %i\n",LE);//will work except it is on zero position |
---|
93 | |
---|
94 | ostringstream present; |
---|
95 | string pr; |
---|
96 | present<<"Presence: LE="<<LE<<" Switches: "<<pre[0]<<pre[1]<<pre[2]<<pre[3]<<"<br>"; |
---|
97 | pr=present.str(); |
---|
98 | string theNumberString; |
---|
99 | string theNumberStringS; |
---|
100 | ostringstream ostr ; |
---|
101 | ostr<<"LE="<< Cout<<" mm"; //use the string stream just like cout, |
---|
102 | theNumberStringS = ostr.str(); |
---|
103 | LCD.write(theNumberStringS,1); |
---|
104 | |
---|
105 | |
---|
106 | while(1) |
---|
107 | { |
---|
108 | if (c==imax) c=0; |
---|
109 | c++; |
---|
110 | |
---|
111 | A=bcm2835_gpio_lev(pinA); |
---|
112 | B=bcm2835_gpio_lev(pinB); |
---|
113 | RI=bcm2835_gpio_lev(pinRI);//check for 1 if You want to know zero position |
---|
114 | LAST=((LAST<<2)+(A<<1)+B)&0xf; |
---|
115 | C=C+VAL[LAST]; |
---|
116 | if (VAL[LAST]==0) {errLE++;} |
---|
117 | if (C!=Cp) |
---|
118 | { |
---|
119 | Cout=C/100.; |
---|
120 | //printf("Position: %.2f mm\n", Cout); |
---|
121 | } |
---|
122 | Cp=C; |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | if ((c%500000)==0) |
---|
127 | { |
---|
128 | ostringstream ostr ; |
---|
129 | ostr<<"LE="<< Cout<<" mm"; |
---|
130 | theNumberStringS = ostr.str(); |
---|
131 | string str= "HTTP/1.1 200 OK\r\n" |
---|
132 | "Content-Type: text/html; charset=UTF-8\r\n\r\n" |
---|
133 | "<!DOCTYPE html><html><head><title>Ugly board</title>" |
---|
134 | "<body>"; |
---|
135 | str.append(pr); |
---|
136 | str.append(theNumberStringS); |
---|
137 | LCD.write(theNumberStringS,1); |
---|
138 | |
---|
139 | mcp23017.readReg(0x13,button); |
---|
140 | //1 is pressed, 0 is not pressed |
---|
141 | sw[0]=((button>>1)&0x01); |
---|
142 | sw[1]=((button>>2)&0x01); |
---|
143 | sw[2]=((button>>3)&0x01); |
---|
144 | sw[3]=((button>>4)&0x01); |
---|
145 | rst=((button>>6)&0x01); |
---|
146 | //###################################### |
---|
147 | /*put there if condition for switches */ |
---|
148 | //###################################### |
---|
149 | if (rst) {C=0;} |
---|
150 | |
---|
151 | adc.Read(myDATA); |
---|
152 | |
---|
153 | if (myDATA[0]>80.) { |
---|
154 | hum[0]=(myDATA[0]-823.)/30.167; |
---|
155 | //printf("%lf\t",(myDATA[0]-823.)/30.167); |
---|
156 | } |
---|
157 | else { hum[0]=-1; |
---|
158 | //printf("no HS N1\t"); |
---|
159 | } |
---|
160 | |
---|
161 | if (myDATA[1]>80.) { |
---|
162 | hum[1]=(myDATA[1]-823.)/30.167; |
---|
163 | //printf("%lf\t",(myDATA[1]-823.)/30.167); |
---|
164 | } |
---|
165 | else { hum[1]=-1; |
---|
166 | //printf("no HS N2\t"); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | if (myDATA[4]>0.98*4095) |
---|
172 | { tpr[0]=666;//If no nts connected - write 666 value |
---|
173 | //printf("no NTC N1\t"); |
---|
174 | } |
---|
175 | else{ |
---|
176 | Rx=10.*Voltage/(Voltage-myDATA[4]); |
---|
177 | tpr[0]=1./(1./298.3+log(Rx/22.)/4300.)-273.; |
---|
178 | //printf("%lf\t",tpr[0]); |
---|
179 | } |
---|
180 | |
---|
181 | if (myDATA[5]>0.98*4095) |
---|
182 | { tpr[1]=666; |
---|
183 | //printf("no NTC N2\t"); |
---|
184 | } |
---|
185 | else{ |
---|
186 | Rx=10.*Voltage/(Voltage-myDATA[5]); |
---|
187 | tpr[1]=1./(1./298.3+log(Rx/22.)/4300.)-273.; |
---|
188 | //printf("%lf\t",tpr[1]); |
---|
189 | } |
---|
190 | if (myDATA[6]>0.98*4095) |
---|
191 | { tpr[2]=666; |
---|
192 | //printf("no NTC N3\t"); |
---|
193 | } |
---|
194 | else{ |
---|
195 | Rx=10.*Voltage/(Voltage-myDATA[6]); |
---|
196 | tpr[2]=1./(1./298.3+log(Rx/22.)/4300.)-273.; |
---|
197 | //printf("%lf\t",tpr[2]); |
---|
198 | } |
---|
199 | if (myDATA[7]>0.98*4095) |
---|
200 | { tpr[3]=666; |
---|
201 | //printf("no NTC N4\t"); |
---|
202 | } |
---|
203 | else{ |
---|
204 | Rx=10.*Voltage/(Voltage-myDATA[7]); |
---|
205 | tpr[3]=1./(1./298.3+log(Rx/22.)/4300.)-273.; |
---|
206 | //printf("%lf\t",tpr[3]); |
---|
207 | } |
---|
208 | |
---|
209 | PotPosition=(1.-myDATA[2]/3953.)*27.;//change 2.7 for correct value (this is maximum path length of slider of potentiometer) |
---|
210 | current=myDATA[3]/1899.31; |
---|
211 | //printf("%.2lf mm\t",PotPosition); |
---|
212 | //printf("%lf mA\t",current); |
---|
213 | ostringstream ostr2 ; |
---|
214 | ostr2.setf(ios_base::fixed, ios_base::floatfield); |
---|
215 | ostr2.precision(1); |
---|
216 | ostr2<< sw[0]<<sw[1]<<sw[2]<<sw[3]<<" "<<hum[0]<<" "<<hum[1]; |
---|
217 | theNumberString = ostr2.str(); |
---|
218 | LCD.write(theNumberString,2); |
---|
219 | str.append("<br>"); |
---|
220 | str.append(theNumberString); |
---|
221 | ostringstream ostr3 ; |
---|
222 | ostr3.setf(ios_base::fixed, ios_base::floatfield); |
---|
223 | ostr3.precision(0); |
---|
224 | ostr3<<tpr[0]<<" "<<tpr[1]<<" "<<tpr[2]<<" "<<tpr[3]; |
---|
225 | theNumberString = ostr3.str(); |
---|
226 | string lcds22=theNumberString; |
---|
227 | str.append("<br>"); |
---|
228 | str.append(theNumberString); |
---|
229 | ostringstream ostr4 ; |
---|
230 | ostr4.setf(ios_base::fixed, ios_base::floatfield); |
---|
231 | ostr4.precision(2); |
---|
232 | ostr4<<PotPosition<<" mm "<<current<<" mA"; |
---|
233 | theNumberString = ostr4.str(); |
---|
234 | string lcds21=theNumberString; |
---|
235 | str.append("<br>"); |
---|
236 | str.append(theNumberString); |
---|
237 | if (bcm2835_gpio_lev(ibut)==1) |
---|
238 | {LCD.write(lcds22,2); |
---|
239 | LCD.write(lcds21,1); |
---|
240 | } |
---|
241 | //printf("\n"); |
---|
242 | newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); |
---|
243 | if (newsockfd > 0) { // error("ERROR on accept"); |
---|
244 | printf("got connection\n"); |
---|
245 | str.append("<br></body></html>\r\n"); |
---|
246 | write(newsockfd, str.c_str(), str.length()); /*-1:'\0'*/} |
---|
247 | |
---|
248 | //printf("%i\n",c); |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | } |
---|
253 | |
---|
254 | printf("LE errors =%d\n",errLE); |
---|
255 | //LCD.write("213",1); |
---|
256 | //LCD.write("123abc",2); |
---|
257 | close(newsockfd); |
---|
258 | close(sockfd); |
---|
259 | bcm2835_close(); |
---|
260 | return 0; |
---|
261 | } |
---|
262 | |
---|
263 | |
---|