1 | #include <DHT.h>
|
2 | #include <digitalWriteFast.h>
|
3 | #include "DHT.h"
|
4 | #include <cmd.h>
|
5 | #include <DisplayI2C.h>
|
6 | #include <digitalWriteFast.h>
|
7 | #include <fonts.h>
|
8 | #include <GraphicsLib.h>
|
9 | #include <DisplayI2C.h>
|
10 | #include <BMPheader.h>
|
11 | #include <Wire.h>
|
12 | #include "cmd.h"
|
13 |
|
14 | //#define RGB( r, g, b ) ( ( ( ( r ) & 248 ) | ( ( g ) >> 5 ) ) << 8 ) | ( ( ( ( g ) & 28 ) << 3 ) | ( ( b ) >> 3 ) )
|
15 | #define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //16bit: 5 red | 6 green | 5 blue
|
16 |
|
17 | #define COLOR_BLACK RGB( 0x00, 0x00, 0x00 )
|
18 | /** \def COLOR_WHITE
|
19 | * \brief Shorthand for RGB( 255, 255, 255 ).
|
20 | */
|
21 | #define COLOR_WHITE RGB( 0xFF, 0xFF, 0xFF )
|
22 | /** \def COLOR_RED
|
23 | * \brief Shorthand for RGB( 255, 0, 0 ).
|
24 | */
|
25 | #define COLOR_RED RGB( 0xFF, 0x00, 0x00 )
|
26 | /** \def COLOR_GREEN
|
27 | * \brief Shorthand for RGB( 0, 255, 0 ).
|
28 | */
|
29 | #define COLOR_GREEN RGB( 0x00, 0xFF, 0x00 )
|
30 | /** \def COLOR_BLUE
|
31 | * \brief Shorthand for RGB( 0, 0, 255 ).
|
32 | */
|
33 | #define COLOR_BLUE RGB( 0x00, 0x00, 0xFF )
|
34 |
|
35 | #define COLOR_YELLOW RGB( 0xFF, 0xFF, 0x00 )
|
36 |
|
37 | #define COLOR_ORANGE RGB( 0xFF, 0x88, 0x00 )
|
38 |
|
39 | #define COLOR_MAGENTA RGB( 0xFF, 0x00, 0xFF )
|
40 |
|
41 | #define I2C_ADDR 0x20
|
42 |
|
43 | #define DHTPIN 9
|
44 | #define DHTTYPE DHT22 //DHT11, DHT21, DHT22
|
45 | DHT dht(DHTPIN, DHTTYPE);
|
46 | #define SER
|
47 |
|
48 |
|
49 | void wait_for_input(void)
|
50 | {
|
51 | //#ifdef SER
|
52 | // Serial.println("Wait for input!");
|
53 | // while(Serial.available() == 0);
|
54 | // while(Serial.available() != 0){ Serial.read(); };
|
55 | //#endif
|
56 | }
|
57 |
|
58 | void write_word(uint16_t i)
|
59 | {
|
60 | Wire.write(i>>8);
|
61 | //Wire.write(i>>8); //for screens > 255 pixel, otherwise remove
|
62 | Wire.write(i);
|
63 | }
|
64 |
|
65 | void write_pos(uint16_t i)
|
66 | {
|
67 | //Wire.write(i>>8); //for screens > 255 pixel, otherwise remove
|
68 | Wire.write(i);
|
69 | }
|
70 |
|
71 | void setup()
|
72 | {
|
73 | uint16_t x=0, y=0, w=0, h=0;
|
74 |
|
75 | #ifdef SER
|
76 | //init Serial lib
|
77 | Serial.begin(9600);
|
78 | while(!Serial); //wait for serial port to connect - needed for Leonardo only
|
79 | wait_for_input();
|
80 | Serial.println("go...");
|
81 | #endif
|
82 |
|
83 | dht.begin();
|
84 | //init Wire lib
|
85 | Wire.begin();
|
86 | //get firmware version
|
87 | Serial.print("version: ");
|
88 |
|
89 | Wire.beginTransmission(I2C_ADDR);
|
90 | Wire.write(CMD_VERSION);
|
91 | Wire.endTransmission();
|
92 | Wire.requestFrom(I2C_ADDR, 4); //request 4 bytes
|
93 | while(Wire.available())
|
94 |
|
95 | {
|
96 | char c = Wire.read();
|
97 |
|
98 | #ifdef SER
|
99 | Serial.print(c);
|
100 | }
|
101 | Serial.println("");
|
102 | //get features
|
103 | Serial.print("features: ");
|
104 |
|
105 |
|
106 | Wire.beginTransmission(I2C_ADDR);
|
107 | Wire.write(CMD_FEATURES);
|
108 | Wire.endTransmission();
|
109 | Wire.requestFrom(I2C_ADDR, 1); //request 1 bytes
|
110 | while(Wire.available())
|
111 | {
|
112 | char c = Wire.read();
|
113 |
|
114 | if(c & FEATURE_LCD){ Serial.print("LCD "); }
|
115 | if(c & FEATURE_TP) { Serial.print("TP "); }
|
116 | if(c & FEATURE_ENC){ Serial.print("ENC "); }
|
117 | if(c & FEATURE_NAV){ Serial.print("NAV "); }
|
118 | if(c & FEATURE_LDR){ Serial.print("LDR "); }
|
119 |
|
120 | }
|
121 |
|
122 | #endif
|
123 |
|
124 |
|
125 | Serial.println(""); //save interface as defaul
|
126 | Serial.println("save interface...");
|
127 |
|
128 |
|
129 | Wire.beginTransmission(I2C_ADDR);
|
130 | Wire.write(CMD_CTRL);
|
131 | Wire.write(CMD_CTRL_SAVE);
|
132 | Wire.endTransmission();
|
133 |
|
134 | //get width and height
|
135 | #ifdef SER
|
136 | Serial.print("width/height: ");
|
137 |
|
138 |
|
139 |
|
140 | Wire.beginTransmission(I2C_ADDR);
|
141 | Wire.write(CMD_LCD_WIDTH);
|
142 | Wire.endTransmission();
|
143 | Wire.requestFrom(I2C_ADDR, 2); //request 2 bytes
|
144 | if(Wire.available())
|
145 |
|
146 | {
|
147 |
|
148 | x = Wire.read()<<8;
|
149 | x |= Wire.read()<<0;
|
150 | }
|
151 |
|
152 | Serial.print(x, DEC);
|
153 | Serial.print("/");
|
154 | Wire.beginTransmission(I2C_ADDR);
|
155 | Wire.write(CMD_LCD_HEIGHT);
|
156 | Wire.endTransmission();
|
157 | Wire.requestFrom(I2C_ADDR, 2); //request 2 bytes
|
158 | if(Wire.available())
|
159 | #endif
|
160 | #ifdef SER
|
161 | {
|
162 | y = Wire.read()<<8;
|
163 | y |= Wire.read()<<0;
|
164 | }
|
165 | Serial.print(y, DEC);
|
166 | Serial.println("");
|
167 | //Hintergrundbeleuchtung
|
168 | Serial.println("backlight...");
|
169 | Wire.beginTransmission(I2C_ADDR);
|
170 | Wire.write(CMD_LCD_LED);
|
171 | Wire.write(100); //0...100
|
172 | Wire.endTransmission();
|
173 | //clear screen
|
174 | Serial.println("clear...");
|
175 | Wire.beginTransmission(I2C_ADDR);
|
176 | Wire.write(CMD_LCD_CLEARBG);
|
177 | Wire.endTransmission();
|
178 | Wire.requestFrom(I2C_ADDR, 2);
|
179 |
|
180 | Wire.beginTransmission(I2C_ADDR);
|
181 | Wire.write(CMD_LCD_BGCOLOR); //Hintergrundfarbe für die Schrift
|
182 | // Wire.write(COLOR_RED>>8);
|
183 | // Wire.write(COLOR_RED);
|
184 | write_word(COLOR_WHITE);
|
185 | Wire.endTransmission();
|
186 | Wire.requestFrom(I2C_ADDR, 2);
|
187 |
|
188 |
|
189 | Wire.beginTransmission(I2C_ADDR);
|
190 | Wire.write(CMD_LCD_FGCOLOR);
|
191 | Wire.write(COLOR_GREEN>>8); //Schriftfarbe
|
192 | Wire.write(COLOR_GREEN);
|
193 | Wire.endTransmission();
|
194 | Wire.requestFrom(I2C_ADDR, 2);
|
195 |
|
196 | // x=90; y=90; h=95; w=95;
|
197 | // Wire.beginTransmission(I2C_ADDR);
|
198 | // Wire.write(CMD_LCD_DRAWLINE);
|
199 | // write_pos(x);
|
200 | // write_pos(y);
|
201 | // write_pos(h);
|
202 | // write_pos(w);
|
203 | // Wire.write(COLOR_BLUE>>8); //Zeichne Linie
|
204 | // Wire.write(COLOR_BLUE);
|
205 | // Wire.endTransmission();
|
206 | // Wire.requestFrom(I2C_ADDR, 2);
|
207 |
|
208 |
|
209 | #endif SER
|
210 |
|
211 | x=0; y=0; w=128; h=128;
|
212 | Wire.beginTransmission(I2C_ADDR); //Zeichne eine Rechteck
|
213 | Wire.write(CMD_LCD_DRAWRECTFG); //x0, y0, w, h
|
214 | write_pos(x);
|
215 | write_pos(y);
|
216 | write_pos(w);
|
217 | write_pos(h);
|
218 | Wire.write(COLOR_WHITE>>8);
|
219 | Wire.write(COLOR_WHITE);
|
220 | Wire.endTransmission();
|
221 |
|
222 | x=0; y=0; w=128; h=128;
|
223 | Wire.beginTransmission(I2C_ADDR); //Fülle das Rechteck
|
224 | Wire.write(CMD_LCD_FILLRECT); //x0, y0, w, h
|
225 | write_pos(x);
|
226 | write_pos(y);
|
227 | write_pos(w);
|
228 | write_pos(h);
|
229 | Wire.write(COLOR_WHITE>>8);
|
230 | Wire.write(COLOR_WHITE);
|
231 | Wire.endTransmission();
|
232 |
|
233 | //draw text
|
234 | //Serial.println("text...");
|
235 |
|
236 | //x=10; y=10;
|
237 | // Wire.beginTransmission(I2C_ADDR);
|
238 | // Wire.write(CMD_LCD_DRAWTEXTFG); //x0, y0, size, clear, length, text
|
239 | //write_pos(x);
|
240 | //write_pos(y);
|
241 | // Wire.write(1); //size + clear
|
242 | // Wire.write(8); //length
|
243 | ////Wire.write('1');
|
244 | ////Wire.write('3');
|
245 | ////Wire.write('5');
|
246 | // Wire.write("Abwesend");
|
247 | // Wire.endTransmission();
|
248 | ////Serial.println("...end");
|
249 | //
|
250 | //x=10; y=30;
|
251 | // Wire.beginTransmission(I2C_ADDR);
|
252 | // Wire.write(CMD_LCD_DRAWTEXTFG); //x0, y0, size, clear, length, text
|
253 | // write_pos(x);
|
254 | // write_pos(y);
|
255 | // Wire.write(1); //size + clear
|
256 | // Wire.write(13); //length
|
257 | //// Wire.write('1');
|
258 | //// Wire.write('3');
|
259 | //// Wire.write('5');
|
260 | // Wire.write("Gleich wieder");
|
261 | // Wire.endTransmission();
|
262 | ////Serial.println("...end");
|
263 | //
|
264 | //x=10; y=50;
|
265 | // Wire.beginTransmission(I2C_ADDR);
|
266 | // Wire.write(CMD_LCD_DRAWTEXTFG); //x0, y0, size, clear, length, text
|
267 | // write_pos(x);
|
268 | // write_pos(y);
|
269 | // Wire.write(1); //size + clear
|
270 | // Wire.write(2); //length
|
271 | //// Wire.write('1');
|
272 | //// Wire.write('3');
|
273 | //// Wire.write('5');
|
274 | // Wire.write("da");
|
275 | // Wire.endTransmission();
|
276 | Serial.println("...end");
|
277 |
|
278 | }
|
279 |
|
280 | //void myFto2I(float fInVal, uint8_t *iOutVorkomma, uint8_t *iOutNachkomma)
|
281 | //{
|
282 | // *iOutVorkomma = (uint8_t)fInVal;
|
283 | //
|
284 | // *iOutNachkomma = (fInVal*100 - (int16_t)(*iOutVorkomma*100));
|
285 |
|
286 |
|
287 |
|
288 | void loop()
|
289 | {
|
290 | float feuchte = dht.readHumidity(); // Luftfeuchte auslesen
|
291 | float temp = dht.readTemperature(); // Temperatur auslesen
|
292 | char strFeuchte[8];
|
293 | uint8_t vorkomma2, nachkomma2;
|
294 | uint8_t vorkomma, nachkomma;
|
295 | float var_float;
|
296 | float var_float2;
|
297 |
|
298 | var_float=feuchte * 100;
|
299 | var_float2=temp * 100;
|
300 | vorkomma2=(int)temp;
|
301 | nachkomma2=(int)var_float2;
|
302 | nachkomma2=(var_float2 - (vorkomma2*100));
|
303 |
|
304 | vorkomma=(int)feuchte;
|
305 | nachkomma=(int)var_float;
|
306 | nachkomma=(var_float - (vorkomma*100));
|
307 |
|
308 |
|
309 | // myFto2I(feuchte, &vorkomma, &nachkomma);
|
310 |
|
311 | uint16_t x=0, y=0, w=0, h=0; //Prüfen ob eine gültige Zahl zurückgegeben wird. Wenn NaN (not a number) zurückgegeben wird, dann Fehler ausgeben.
|
312 | if (isnan(temp) || isnan(feuchte))
|
313 | {
|
314 | Serial.println("DHT22 konnte nicht ausgelesen werden");
|
315 | }
|
316 | else
|
317 | {
|
318 | // Hier soll der Schrifzug Luftfeuchte aufs Display geschrieben werden
|
319 |
|
320 |
|
321 | x=10; y=10;
|
322 | Wire.beginTransmission(I2C_ADDR);
|
323 | Wire.write(CMD_LCD_DRAWTEXTFG); //x0, y0, size, clear, length, text
|
324 | write_pos(x);
|
325 | write_pos(y);
|
326 | Wire.write(1); //size _ clear
|
327 | Wire.write(12); //length
|
328 | Wire.write("Luftfeuchte:");
|
329 | Wire.endTransmission();
|
330 |
|
331 | x=10; y=30;
|
332 |
|
333 | Wire.beginTransmission(I2C_ADDR);
|
334 | Wire.write(CMD_LCD_DRAWTEXTFG); //x0, y0, size, clear, length, text
|
335 | write_pos(x);
|
336 | write_pos(y);
|
337 | Wire.write(2); //size + clear
|
338 | Wire.write(7); //length
|
339 | sprintf(strFeuchte,"%2d.%02d %%",vorkomma,nachkomma);
|
340 |
|
341 | Wire.write(strFeuchte);
|
342 | Wire.endTransmission();
|
343 |
|
344 | x=10; y=50;
|
345 | Wire.beginTransmission(I2C_ADDR);
|
346 | Wire.write(CMD_LCD_DRAWTEXTFG); //x0, y0, size, clear, length, text
|
347 | write_pos(x);
|
348 | write_pos(y);
|
349 | Wire.write(1); //size + clear
|
350 | Wire.write(11); //length
|
351 | Wire.write("Temperatur:");
|
352 | Wire.endTransmission();
|
353 |
|
354 | x=10; y=70;
|
355 |
|
356 | Wire.beginTransmission(I2C_ADDR);
|
357 | Wire.write(CMD_LCD_DRAWTEXTFG); //x0, y0, size, clear, length, text
|
358 | write_pos(x);
|
359 | write_pos(y);
|
360 | Wire.write(2); //size + clear
|
361 | Wire.write(7); //length
|
362 | sprintf(strFeuchte,"%2d.%02d C",vorkomma2,nachkomma2);
|
363 |
|
364 | Wire.write(strFeuchte);
|
365 | Wire.endTransmission();
|
366 |
|
367 | Serial.print("Luftfeuchte: ");
|
368 | Serial.print(feuchte);
|
369 | //Serial.print("----");
|
370 | //Serial.print(strFeuchte);
|
371 | Serial.print(" %\t");
|
372 | Serial.print("Temperatur: ");
|
373 | Serial.print(temp);
|
374 | Serial.println(" C");
|
375 | delay(500);
|
376 | }
|
377 | }
|