whycyhl
2022-05-05 94861120b14313f81fc666e612c48c9e8a81f1e7
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/************************** Copyright (c) **********************************
**                 FUZHOU FUGUANG ELECTRONICS Co.,LTD.
**                        ¸£Öݸ£¹âµç×ÓÓÐÏÞ¹«Ë¾
**                       http://www.fuguang.com
**
**-------------- File Info -------------------------------------------------
** File name:            AD7843_Driver.c
** Last modified Date:  2008-11-16
** Last Version:        1.0
** Descriptions:        AD7843 Drivers lib
**
**--------------------------------------------------------------------------
** Created by:            mxpopstar
** Created date:        2008-11-16
** Version:                1.0
** Descriptions:        The original version
**
**--------------------------------------------------------------------------
** Modified by:            mxpopstar
** Modified date:        2008-11-20
** Version:
** Descriptions:        AD7843 Drivers lib
**
***************************************************************************/
//
#include "AD7843_Driver.h"
#include "Define.h"
#include "MainMenu.h"
#include "AdjustValuePage.h"
#include "KeyBoard_Driver.h"
#include "TFT_Driver.h"
#include "MessageBox.h"
#include "LogoPage.h"
#include "SYSSetPage.h"
#include "SelectForm.h"
 
extern SYS_State GB_SYS_State;
 
#if(SoftLanguage == CN)
const prog_uchar KeySelText[3][6] = 
{
    {213, 208, 114, 209, 210, 211},//°´¼ü+´¥ÃþÆÁ
    {213, 208, 101, 101, 101, 101},//°´¼ü
    {209, 210, 211, 101, 101, 101},//´¥ÃþÆÁ
};
//
#else
const prog_uchar KeySelText[3][16] = 
{
    {103,73,74,75,70,68,0,68,0,110,64,73,77,74,76},//°´¼ü+´¥Ãþ15
    {103,73,74,75,70,68,0,0,0,0,0,0,0,0,0},//°´¼ü
    {110,64,73,77,74,76,0,0,0,0,0,0,0,0,0},//´¥ÃþÆÁ
};
//
#endif
//
 
static unsigned char KeySelIndex = 2;
static unsigned char CustomInputOFF = 0;
 
void InitIo_AD7843(void)
{
    AD7843_CS_DDR |= (1<<AD7843_CS_PIN);
    AD7843_SCK_DDR |= (1<<AD7843_SCK_PIN);
    AD7843_MOSI_DDR |= (1<<AD7843_MOSI_PIN);
    AD7843_MISO_DDR &= ~(1<<AD7843_MISO_PIN);
    AD7843_PENIRQ_DDR &= ~(1<<AD7843_PENIRQ_PIN);
    AD7843_PENIRQ_1;
    
    AD7843_CS_1;
    AD7843_SCK_0;
}
//----------------------------------------------------------------------------------
//function SpiWrite();
/**************************************************************************/
void SpiWrite_AD7843(const unsigned char byte)
{
    unsigned char i;
    for (i=0;i<8;i++)            // Setup byte circulation bits
    {
        if (0x80 & (byte<<i))    // Put DATA_BUF.7 on data line
            AD7843_MOSI_1;
        else
            AD7843_MOSI_0;
        AD7843_SCK_1;            // Set clock line high
        AD7843_SCK_0;            // Set clock line low 
    }
    
    //_delay_us(1);
    AD7843_SCK_1;
    //_delay_us(1);
    AD7843_SCK_0;
}
/**************************************************************************/
 
//function SpiRead();
/**************************************************************************/
unsigned char SpiRead_AD7843(void)
{
    unsigned char i;
    unsigned char data = 0;
    for(i=0; i<8; i++)            // Setup byte circulation bits
    {
        AD7843_SCK_1;            // Set clock line high
        if (AD7843_MISO_1)
            data+=1;            // Read data
        if(i<7)
            data<<=1;
        AD7843_SCK_0;            // Set clock line low
    }
    
    for(i=0; i<7; i++)
    {
        AD7843_SCK_1;
        AD7843_SCK_0;
    }
    
    return data;                // Return function parameter
}
/**************************************************************************/
//----------------------------------------------------------------------------------
unsigned char CompareValueEqual(const unsigned char x1, const unsigned char y1, 
                                const unsigned char x2, const unsigned char y2)
{
    unsigned char resx,resy;
    if(x1 >= x2)
        resx = x1-x2;
    else resx = x2-x1;
    
    if(y1 >= y2)
        resy = y1-y2;
    else resy = y2-y1;
 
    if((resx <= 3) && (resy <= 3))
        return 1;
    else return 0;
}
//----------------------------------------------------------------------------------
 
void EnableCustomInput(void)
{
    CustomInputOFF = 0;
}
void DisableCustomInput(void)
{
    CustomInputOFF = 1;
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
void GetAndDSPKeyboardSel(void)
{
    if(GB_SYS_State.CurrentPage == DSPSYSSetPage)
    {
        //DSP16x24Str(KeySelText[KeySelIndex], 15, 14, 276, 144, blue, white);
    }
}
void SetKeyboardSel(const unsigned char settype)
{
    if(settype == 0)
    {
        if(KeySelIndex++ >= 2)
        {
            KeySelIndex = 0;
        }
    }
    else
    {
        if(KeySelIndex-- <= 0)
        {
            KeySelIndex = 2;
        }
    }
}
//----------------------------------------------------------------------------------
unsigned char GetTouchLocal(void)
{
    static unsigned char lastpoint = ButtonNull;
    
    unsigned int X1,Y1,X2,Y2;
    unsigned char point1 = ButtonNull;
    unsigned char point2 = ButtonNull;     
    unsigned char count = 0; 
    
    //-------------- ÊäÈ빦ÄܽûÓà---------------
    if( (CustomInputOFF==1) && (!(NO_AD7843_PENIRQ)) )
    {
        return ButtonNull;
    }
    else
    {
        EnableCustomInput();
    }
    
    //-------------- ÊäÈ뷽ʽѡÔñ ---------------
    if(KeySelIndex != 2)
    {
        point2 = GetKeyPress();
        if(point2 != ButtonNull)
            return point2;
    }
    if(KeySelIndex == 1)
    {
        return ButtonNull;
    }
    
    //---------- ¼ì²â´¥ÃþÆÁÊÇ·ñ±»´¥Ãþ -----------
    if(NO_AD7843_PENIRQ)
    {
        lastpoint = ButtonNull;
        return ButtonNull;
    }
    
    //========== ¿ªÊ¼²âÁ¿´¥ÃþÆÁ´¥µç,Í˳öÇÐÎðÍü¼Ç¹Ø±ÕAD7843Ƭѡ =============
    DDRD |= 0x02;
    PORTD &= (~0x02);    //PENirq¶Ë¿ÚÖõØ
    AD7843_CS_0;        //ѡͨAD7843
    
    _delay_us(5);
    
    //----------- ´ò¿ªXͨµÀ²¢²É¼¯XÖáÖµ ----------
    Y1 = 0;
    for(count=0; count<10; count++)
    {
        SpiWrite_AD7843(0x90);
        Y1 += SpiRead_AD7843();
    }
    Y1 /= 10;
    
    _delay_us(50);
    
    //----------- ´ò¿ªYͨµÀ²¢²É¼¯YÖáÖµ ----------
    X1 = 0; 
    for(count=0; count<10; count++)    
    {
        SpiWrite_AD7843(0xd0);
        X1 += SpiRead_AD7843();
    }
    X1 /= 10; 
    
    point1 = GetButtonDown(X1, Y1);
    
    if((point1 != ButtonNull) && (point1 != lastpoint))
    {
            lastpoint = point1;
            
            _delay_ms(30);    //ÑÓʱ30msÒÔÈ¥¶¶;
            
            //----------- ´ò¿ªXͨµÀ²¢²É¼¯XÖáÖµ ----------
            Y2 = 0;
            for(count=0; count<10; count++)    
            {
                SpiWrite_AD7843(0x90);
                Y2 += SpiRead_AD7843();    
            }
            Y2 /= 10;
            
            _delay_us(50);
            
            //----------- ´ò¿ªYͨµÀ²¢²É¼¯YÖáÖµ ----------
            X2 = 0; 
            for(count=0; count<10; count++)    
            {
                SpiWrite_AD7843(0xd0);
                X2 += SpiRead_AD7843();
            }
            X2 /= 10;
            
            point2 = GetButtonDown(X2, Y2);
            //----------- Èç¹û²ÉÑùÖµÕýÈ· -------------
            if(point2 != point1)
            {
                //AD7843_CS_1;//¹Ø±ÕAD7843
                point1 = ButtonNull;
            }
    }
 
    AD7843_CS_1;//¹Ø±ÕAD7843
    DDRD &= (~0x02);
    PORTD |= 0x02;
    //--------- È·±£Óû§µÄ´¥Ãþ»¹±£³Ö×Å ---------
    _delay_us(10);
    if(NO_AD7843_PENIRQ)
    {
        point1 = ButtonNull;
    }
    
    return point1;
}
//----------------------------------------------------------------------------------
unsigned char GetButtonDown(const unsigned char x, const unsigned char y)
{
    if(GB_SYS_State.CurrentPage == DSPAdjVPage)
    {
        PrintValue(2, 0, x, "X", 16, 200, 16, blue, gray2);//´¥ÃþÆÁXÖáÖµ
        PrintValue(2, 0, y, "Y", 16, 290, 16, blue, gray2);//´¥ÃþÆÁYÖáÖµ
    }
    
    if(GB_SYS_State.CurrentPage == DSPLogoPage)
    {
        if(((x>20)&&(x<220))&&((y>20)&&(y<220)))//
        {
            return MenuItem0;
        }
    }
    
    else if((GB_SYS_State.CurrentPage==DSPMainMenu)
            ||(GB_SYS_State.CurrentPage==DSPSelectForm))
    {
        if(((x>153)&&(x<216)) && ((y>175)&&(y<200)))        //·Åµç²âÊÔ
        {
            return MenuItem0;
        }
        
        else if(((x>42)&&(x<106))&&((y>175)&&(y<200)))        //³äµç²âÊÔ
        {
            return MenuItem1;
        }
        
        else if(((x>153)&&(x<216))&&((y>128)&&(y<152)))        //"Êý¾Ý¹ÜÀí"
        {
            return MenuItem2;
        }
        
        else if(((x>42)&&(x<106))&&((y>128)&&(y<152)))        //"²ÎÊýÉèÖÃ"
        {
            return MenuItem3;
        }
        
        else if(((x>153)&&(x<216))&&((y>79)&&(y<105)))        //"²é¿´µ¥Ìå"
        {
            return MenuItem4;
        }
        
        else if(((x>42)&&(x<106))&&((y>79)&&(y<105)))        //"²ÎÊýÔ¤ÖÃ"
        {
            return MenuItem5;
        }
        else if(((y>=25)&&(y<=57)) && ((x>=155)&&(x<=189)))    //Ñ¡Ôñ
        {
            return Button1;
        }
        else if(((y>=25)&&(y<=57)) && ((x>=62)&&(x<=98)))    //È·¶¨
        {
            return Button3;
        }
    }
    
    else if(GB_SYS_State.CurrentPage == DSPMessageBoxNull)
    {
        if(((y>=90)&&(y<=116)) && ((x>=156)&&(x<=190)))        //È·¶¨
        {
            return Button1;    
        }
        else if(((y>=90)&&(y<=116)) && ((x>=60)&&(x<=92)))    //È¡Ïû
        {
            return Button3;    
        }
        else if(((y>=90)&&(y<=116)) && ((x>=110)&&(x<=145)))//µ¥¸öÈ·¶¨
        {
            return Button2;
        }
    }
    
    else
    {
        if(((y>=25)&&(y<=57)) && ((x>=202)&&(x<=235)))        //·Åµç
        {
            return Button0;
        }
        else if(((y>=25)&&(y<=57)) && ((x>=156)&&(x<=189)))    //Í£Ö¹
        {
            return Button1;
        }
        else if(((y>=25)&&(y<=57)) && ((x>=110)&&(x<=144)))    //µ¥Ìå
        {
            return Button2;
        }
        else if(((y>=25)&&(y<=57)) && ((x>=64)&&(x<=97)))    //Ô¤ÖÃ
        {
            return Button3;
        }
        else if(((y>=25)&&(y<=57)) && ((x>=17)&&(x<=50)))    //Í˳ö
        {
            return Button4;
        }
        else if(((y>=78)&&(y<=100)) && ((x>=17)&&(x<=50)))    //È¡Ïû
        {
            return Button5;    
        }
        else if(((y>=118)&&(y<=141)) && ((x>=17)&&(x<=50)))    //-
        {
            return Button6;    
        }
        else if(((y>=160)&&(y<=181)) && ((x>=17)&&(x<=50)))    //+
        {
            return Button7;    
        }
        else if(((y>=198)&&(y<=221)) && ((x>=17)&&(x<=50)))//ÐÞ¸Ä
        {
            return Button8;
        }
    }
    
    return ButtonNull;
}
//