1 | #include <avr/io.h>
|
2 | #define F_CPU 16000000UL
|
3 | #include <util/delay.h>
|
4 |
|
5 | #include <string.h>
|
6 | #include <stdio.h>
|
7 | #include <avr/interrupt.h>
|
8 | #include "i2cmaster.h"
|
9 | #include "eDIP-Touch.h"
|
10 | #include "gendefs.h"
|
11 |
|
12 |
|
13 |
|
14 | // command to display: ctd
|
15 | unsigned char ctddark[] = {0x04, 0x1B, 'Y', 'H', 0};
|
16 | unsigned char ctddark10[] = {0x04, 0x1B, 'Y', 'H', 10};
|
17 | unsigned char ctddark20[] = {0x04, 0x1B, 'Y', 'H', 20};
|
18 | unsigned char ctddark30[] = {0x04, 0x1B, 'Y', 'H', 30};
|
19 | unsigned char ctddark40[] = {0x04, 0x1B, 'Y', 'H', 40};
|
20 | unsigned char ctddark50[] = {0x04, 0x1B, 'Y', 'H', 50};
|
21 | unsigned char ctddark60[] = {0x04, 0x1B, 'Y', 'H', 60};
|
22 | unsigned char ctddark70[] = {0x04, 0x1B, 'Y', 'H', 70};
|
23 | unsigned char ctddark80[] = {0x04, 0x1B, 'Y', 'H', 80};
|
24 | unsigned char ctddark90[] = {0x04, 0x1B, 'Y', 'H', 90};
|
25 | unsigned char ctddark100[] = {0x04, 0x1B, 'Y', 'H', 100}; //100%
|
26 |
|
27 |
|
28 |
|
29 | unsigned char ctdcursoroff[] = {0x04, 0x1B, 'T', 'C', 0}; //cursor off ESC T C 0
|
30 | // disable touch:
|
31 | unsigned char ctdDisableTouch[] = {0x05, 0x1B, 'A', 'L', 0, 1};
|
32 |
|
33 |
|
34 | unsigned char ctdfont1[] = {0x04, 0x1b, 'Z','F',1}; //ESC Z F n1 Font 1: 4x6 monospaced
|
35 | unsigned char ctdfont2[] = {0x04, 0x1b, 'Z','F',2}; //ESC Z F n1 Font 2: 6x8 monospaced
|
36 | unsigned char ctdfont3[] = {0x04, 0x1b, 'Z','F',3}; //ESC Z F n1 Font 3: 7x12 monospaced
|
37 | unsigned char ctdfont4[] = {0x04, 0x1b, 'Z','F',4}; //ESC Z F n1 Font 4: GENEVA10 proportional
|
38 | unsigned char ctdfont5[] = {0x04, 0x1b, 'Z','F',5}; //ESC Z F n1 Font 5: CHICAGO14 proportional
|
39 | unsigned char ctdfont6[] = {0x04, 0x1b, 'Z','F',6}; //ESC Z F n1 Font 6: Swiss30 Bold proportional
|
40 | unsigned char ctdfont7[] = {0x04, 0x1b, 'Z','F',7}; //ESC Z F n1 Font 7: grosse Ziffern BigZif57
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | /************************************************************************/
|
55 | /* */
|
56 | /************************************************************************/
|
57 | unsigned char sendCommandToTouch (unsigned char i2cDestination , unsigned char* commandToDisplay) {
|
58 |
|
59 | unsigned char retvalue;
|
60 |
|
61 | i2c_start_wait(i2cDestination);
|
62 | SendCommand(commandToDisplay);
|
63 | i2c_start_wait(i2cDestination + 1);
|
64 | retvalue = i2c_readAck();
|
65 | i2c_stop();
|
66 |
|
67 | return retvalue;
|
68 |
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | /************************************************************************/
|
74 | /* */
|
75 | /************************************************************************/
|
76 | void SendCommand(unsigned char buf[])
|
77 | { unsigned char i, len, bcc;
|
78 | len = buf[0];
|
79 | i2c_write(0x11); // Send DC1
|
80 | bcc = 0x11;
|
81 | i2c_write(len); // Send data length
|
82 | bcc = bcc + len;
|
83 | for(i = 1; i < len + 1 ; i++) // Send buf
|
84 | {
|
85 | i2c_write(buf[i]);
|
86 | bcc = bcc + buf[i];
|
87 | };
|
88 | i2c_write(bcc); // Send checksum
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | /************************************************************************
|
96 | *
|
97 | * SendAsciiInFont (xpos, ypos, string)
|
98 | *
|
99 | ************************************************************************/
|
100 | void SendAsciiInFont(unsigned char i2cDestination, uint8_t x, uint8_t y, char *buf)
|
101 | {
|
102 | // example:
|
103 | // unsigned char ctdchars[] = {13, 0x1b, 'Z','L',1,1,'D','i','s','p','l','a','y', 0x10 }; //
|
104 |
|
105 | unsigned char i, j, bcc, test;
|
106 | j = strlen(buf) + 6 ; // 6 = esc + Z + L + xpos + ypos + NUL
|
107 |
|
108 | i2c_start_wait(i2cDestination + 0); //write a bunch of asciis
|
109 |
|
110 | //1.
|
111 | test = i2c_write(0x11); // send starter DC1
|
112 | bcc = 0x11;
|
113 |
|
114 | //2.
|
115 | test += i2c_write(j); // send data length
|
116 | bcc += j;
|
117 |
|
118 | //3.
|
119 | test += i2c_write(0x1b); // send esc
|
120 | bcc += 0x1b;
|
121 |
|
122 | //4.
|
123 | test += i2c_write('Z'); // send 'Z'
|
124 | bcc += 'Z';
|
125 |
|
126 | //5.
|
127 | test += i2c_write('L'); // send 'L'
|
128 | bcc += 'L';
|
129 |
|
130 | //6.
|
131 | test += i2c_write(x); // send xpos
|
132 | bcc += x;
|
133 |
|
134 | //7.
|
135 | test += i2c_write(y); // send ypos
|
136 | bcc += y;
|
137 |
|
138 | //8.
|
139 | for(i = 0; i < strlen(buf); i++) {// send buf
|
140 | test += i2c_write(buf[i]);
|
141 | bcc = bcc + buf[i];
|
142 | };
|
143 |
|
144 | //9.
|
145 | test += i2c_write(0x00); // send NUL for sequence end
|
146 | bcc += 0x0;
|
147 |
|
148 | test += i2c_write(bcc); // send checksum
|
149 |
|
150 | i2c_start_wait(i2cDestination + 1);
|
151 | i2c_readAck();
|
152 | i2c_stop();
|
153 |
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | /************************************************************************
|
161 | *
|
162 | * fetch Data from eDip Touch
|
163 | *
|
164 | ************************************************************************/
|
165 | unsigned char fetchdata (unsigned char i2cDestination, unsigned char *ubuf) { //buf = requestInfo2
|
166 | unsigned char test = 0,dc2 ,len ,sendbuf ,recbuf ,bcc;
|
167 |
|
168 | i2c_start_wait(i2cDestination + 0);
|
169 | SendcommandDC2(ubuf);
|
170 |
|
171 | i2c_start_wait(i2cDestination + 1);
|
172 | test = i2c_readAck(); // 06
|
173 | dc2 = i2c_readAck(); // 12
|
174 | len = i2c_readAck(); // 02
|
175 | sendbuf= i2c_readAck(); // 00
|
176 | recbuf = i2c_readAck(); // FA
|
177 | bcc = i2c_readNak(); // 0E
|
178 | i2c_stop();
|
179 |
|
180 |
|
181 | return 0; //... useless
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 | /************************************************************************
|
187 | * send DC2
|
188 |
|
189 | unsigned char dc2ReqSendBuf[] = {1, 'S'}; //sequence flush buffer
|
190 | unsigned char dc2ReqBufInfo[] = {1, 'I'}; //sequence
|
191 | unsigned char dc2ProtocolSettings[] = {3, 'D', 64, 200 }; //sequence
|
192 | unsigned char dc2ReqProtocolInfo[] = {1, 'P'}; //sequence
|
193 | unsigned char dc2RepLastPackage[] = { 1, 'R'}; //sequence
|
194 | unsigned char dc2ReqRS485Addr[] = {3, 'A', 'S',42}; //'S' = select, 'D' = desel., 42 = Addr
|
195 |
|
196 | ************************************************************************/
|
197 | void SendcommandDC2(unsigned char buf[])
|
198 | { unsigned char i, len, bcc;
|
199 |
|
200 | i2c_write(0x12); // Send DC2
|
201 | bcc = 0x12;
|
202 |
|
203 | len = buf[0];
|
204 | i2c_write(len); // Send data length
|
205 | bcc += len;
|
206 |
|
207 | for(i = 1; i < len + 1 ; i++) // Send buf
|
208 | {
|
209 | i2c_write(buf[i]);
|
210 | bcc += buf[i];
|
211 | };
|
212 |
|
213 | i2c_write(bcc); // Send checksum
|
214 | }
|
215 |
|
216 |
|
217 | /*******************************************************************
|
218 | *
|
219 | * initLcdImage & encoder startup
|
220 | *
|
221 | ********************************************************************/
|
222 | void initLcdImage(unsigned char i2cDestination) {
|
223 | PORTC |= (1 << reset); // preset LCD reset
|
224 | DDRC |= (1 << DDC2); // reset
|
225 |
|
226 | PORTC &= ~(1 << I2CMode); // preset I2C mode
|
227 | DDRC |= (1 << DDC3); // /I2C mode
|
228 |
|
229 | //PCINT7
|
230 | //Hardware: external LCD with built-in pullup, pulled low by open-drain.
|
231 | //if falling edge on PCint7 -> IRQ
|
232 |
|
233 | // PCIFR – Pin Change Interrupt Flag Register
|
234 |
|
235 | // PCMSK0 – Pin Change Mask Register 0
|
236 | PCMSK0 |= 0b10000000;
|
237 |
|
238 | //PCI0 Interrupt vector
|
239 | // PCICR – Pin Change Interrupt Control Register
|
240 | PCICR |=0b00000001; // Bit0: PCINT 0..7
|
241 |
|
242 |
|
243 | //init
|
244 | flagFetchData = 0;
|
245 |
|
246 | //LCD data transfer starts here
|
247 | i2c_init();
|
248 | sendCommandToTouch(i2cDestination, ctddark10);
|
249 | sendCommandToTouch(i2cDestination, ctdDisableTouch);
|
250 | sendCommandToTouch(i2cDestination, ctdcursoroff);
|
251 |
|
252 |
|
253 | if (i2cDestination == Touch128x64 ) sendCommandToTouch(i2cDestination, ctdcontrast);
|
254 | else if (i2cDestination == Touch160x104 ) sendCommandToTouch(i2cDestination, ctdcontrast160x104Blue);
|
255 | // else if (i2cDestination == Touch160x104 ) sendCommandToTouch(i2cDestination, ctdcontrast);
|
256 |
|
257 |
|
258 |
|
259 | sendCommandToTouch(i2cDestination, ctdDefineBargraph1);
|
260 | sendCommandToTouch(i2cDestination, ctdDefineBargraph2);
|
261 | sendCommandToTouch(i2cDestination, ctdDefineBargraph3);
|
262 |
|
263 | }
|