Subversion Repositories HomeAutomation

Rev

Rev 1274 | Rev 1279 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1274 Rev 1276
Line 1... Line 1...
1
 
1
 
2
#include "sns_Touch.h"
2
#include "sns_Touch.h"
-
 
3
point touchBuffer[sns_Touch_BUFFERSIZE];
-
 
4
uint8_t rxbufidx;
-
 
5
 
3
 
6
 
4
#ifdef sns_Touch_USEEEPROM
7
#ifdef sns_Touch_USEEEPROM
5
#include "sns_Touch_eeprom.h"
8
#include "sns_Touch_eeprom.h"
6
struct eeprom_sns_Touch EEMEM eeprom_sns_Touch =
9
struct eeprom_sns_Touch EEMEM eeprom_sns_Touch =
7
{
10
{
Line 33... Line 36...
33
 
36
 
34
    // to use PCINt lib, call this function: (the callback function look as a timer callback function)
37
    // to use PCINt lib, call this function: (the callback function look as a timer callback function)
35
    // Pcint_SetCallbackPin(sns_Touch_PCINT, EXP_C , &sns_Touch_pcint_callback);
38
    // Pcint_SetCallbackPin(sns_Touch_PCINT, EXP_C , &sns_Touch_pcint_callback);
36
 
39
 
37
    ADC_Init();
40
    ADC_Init();
38
    Timer_SetTimeout(sns_Touch_POLL_TIMER, sns_Touch_SEND_PERIOD , TimerTypeFreeRunning, 0);
41
    Timer_SetTimeout(sns_Touch_POLL_TIMER, sns_Touch_POLL_PERIOD , TimerTypeFreeRunning, 0);
-
 
42
   
-
 
43
    rxbufidx = 0;
-
 
44
}
-
 
45
 
-
 
46
 
-
 
47
/*
-
 
48
F1: Klar
-
 
49
F2: Klar
-
 
50
F3: Klar
-
 
51
F4: Klar
-
 
52
F5: Klar
-
 
53
F6: Klar
-
 
54
F7: Klar
-
 
55
F8: Klar
-
 
56
F9:
-
 
57
 
-
 
58
*/
-
 
59
 
-
 
60
 
-
 
61
 
-
 
62
uint8_t parseBuffer(uint8_t startIndex, uint8_t endIndex)
-
 
63
{
-
 
64
    int16_t f1_k = 0, f1_tmp = 0,f2_tmp = 0,f3_tmp = 0,f4_tmp = 0,f5_tmp = 0,f8_tmp = 0,f9_tmp = 0,int16temp;
-
 
65
    uint8_t f7_tmp = 0, f7_sign = 0, f1_sign = 0 ,f3_yref = 0, f1 = 0,f2 = 0,f3 = 0,f4 = 0,f5 = 0,f6 = 0,f7 = 0,f8 = 0,f9 = 0, xMin, xMax, yMin, yMax;
-
 
66
   
-
 
67
    if ((int16_t)touchBuffer[endIndex].y - (int16_t)touchBuffer[startIndex].y > 0)
-
 
68
        f6 = 1;
-
 
69
    else
-
 
70
        f6 = 0;
-
 
71
   
-
 
72
    xMin = touchBuffer[startIndex].x;
-
 
73
    xMax = touchBuffer[startIndex].x;
-
 
74
    yMin = touchBuffer[startIndex].y;
-
 
75
    yMax = touchBuffer[startIndex].y;
-
 
76
    f3_yref = (touchBuffer[startIndex].y + touchBuffer[endIndex].y)/2;
-
 
77
    f1_k = ((touchBuffer[endIndex].y-touchBuffer[startIndex].y)<<4)/(touchBuffer[endIndex].x-touchBuffer[startIndex].x);
-
 
78
    f1_sign = 2;
-
 
79
 
-
 
80
    for (uint8_t i = startIndex+1; i < endIndex-1; i++)
-
 
81
    {
-
 
82
        if (((f1_k*(touchBuffer[i].x-touchBuffer[0].x))>>4) + touchBuffer[0].y < touchBuffer[i].y) {
-
 
83
            if (f1_sign != 1)
-
 
84
                f1++;
-
 
85
            f1_sign = 1;
-
 
86
        }
-
 
87
        else {
-
 
88
            if (f1_sign != 0)
-
 
89
                f1++;
-
 
90
            f1_sign = 0;
-
 
91
        }
-
 
92
       
-
 
93
        if (touchBuffer[i].x > min(touchBuffer[startIndex].x, touchBuffer[endIndex].x) && touchBuffer[i].x < max(touchBuffer[startIndex].x, touchBuffer[endIndex].x))
-
 
94
        {
-
 
95
            f3_tmp += f3_yref - (touchBuffer[i].y + touchBuffer[i-1].y)/2;
-
 
96
        }
-
 
97
        if (xMin > touchBuffer[i].x)
-
 
98
            xMin = touchBuffer[i].x;
-
 
99
        if (xMax < touchBuffer[i].x)
-
 
100
            xMax = touchBuffer[i].x;
-
 
101
        if (yMin > touchBuffer[i].y)
-
 
102
            yMin = touchBuffer[i].y;
-
 
103
        if (yMax < touchBuffer[i].y)
-
 
104
            yMax = touchBuffer[i].y;
-
 
105
        f4_tmp += (int16_t)touchBuffer[i].x - (int16_t)touchBuffer[endIndex].x;
-
 
106
        f5_tmp += (int16_t)touchBuffer[i].x - (int16_t)touchBuffer[startIndex].x;
-
 
107
    }
-
 
108
   
-
 
109
    for (uint8_t j = yMin; j < yMax-1; j++)
-
 
110
    {
-
 
111
        f7_tmp = 0xff;
-
 
112
        f7_sign = 0;
-
 
113
        for (uint8_t i = startIndex+1; i < endIndex-1; i++)
-
 
114
        {
-
 
115
            if (j > touchBuffer[i].y) {
-
 
116
                if (f7_sign != '+')
-
 
117
                    f7_tmp++;
-
 
118
                f7_sign = '+';
-
 
119
            }
-
 
120
            else {
-
 
121
                if (f7_sign != '-')
-
 
122
                    f7_tmp++;
-
 
123
                f7_sign = '-';
-
 
124
            }
-
 
125
        }
-
 
126
        if (f7_tmp > f7)
-
 
127
            f7 = f7_tmp;
-
 
128
    }
-
 
129
   
-
 
130
    f1--;
-
 
131
    f2 = f1_sign;
-
 
132
    if (f1 == 0)
-
 
133
        f2 = 2;
-
 
134
   
-
 
135
    if (f3_tmp > 0)
-
 
136
        f3 = 0;
-
 
137
    else
-
 
138
        f3 = 1;
-
 
139
       
-
 
140
    //int16temp = (((int16_t)touchBuffer[startIndex].x - (int16_t)(xMax+xMin)/2)*((int16_t)touchBuffer[endIndex].x - (int16_t)(xMax+xMin)/2));
-
 
141
    //int16temp = ((touchBuffer[startIndex].x - (xMax+xMin)/2)*(touchBuffer[endIndex].x - (xMax+xMin)/2));
-
 
142
    int16temp = (touchBuffer[startIndex].x*touchBuffer[endIndex].x - (xMax+xMin)/2);
-
 
143
    if (int16temp > 0)
-
 
144
        f9 = 1;
-
 
145
    else
-
 
146
        f9 = 0;
-
 
147
    if (f5_tmp > 0)
-
 
148
        f5 = 1;
-
 
149
    else
-
 
150
        f5 = 0;
-
 
151
    if (f4_tmp > 0)
-
 
152
        f4 = 1;
-
 
153
    else
-
 
154
        f4 = 0;
-
 
155
    if (xMin < min(touchBuffer[startIndex].x,touchBuffer[endIndex].x) || xMax > max(touchBuffer[startIndex].x,touchBuffer[endIndex].x))
-
 
156
        f8 = 0;
-
 
157
    else
-
 
158
        f8 = 1;
-
 
159
       
-
 
160
    //printf("1%u2%c3%c4%c5%c6%c7%u8%c9%c\n", f1, f2, f3, f4, f5, f6, f7, f8, f9);
39
 
161
 
-
 
162
    StdCan_Msg_t txMsg;
-
 
163
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
-
 
164
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
-
 
165
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
-
 
166
    txMsg.Header.ModuleId = sns_Touch_ID;
-
 
167
    txMsg.Header.Command = CAN_MODULE_CMD_TOUCH_GESTURE;
-
 
168
    txMsg.Length = 3;
-
 
169
    txMsg.Data[0] = f1;
-
 
170
    txMsg.Data[1] = f7;
-
 
171
    txMsg.Data[2] = f3<<7|f4<<6|f5<<5|f6<<4|f8<<3|f9<<2|f2;
-
 
172
   
-
 
173
    StdCan_Put(&txMsg);
-
 
174
    return 0;
40
}
175
}
-
 
176
 
41
 
177
 
42
void sns_Touch_Process(void)
178
void sns_Touch_Process(void)
43
{
179
{
44
   
180
   
45
    if (Timer_Expired(sns_Touch_POLL_TIMER)) {
181
    if (Timer_Expired(sns_Touch_POLL_TIMER)) {
46
        //StdCan_Msg_t txMsg;
182
        //StdCan_Msg_t txMsg;
Line 53... Line 189...
53
        uint8_t adxval = ADC_Get(sns_Touch_TOUCHXAD)>>2;    //read x-
189
        uint8_t adxval = ADC_Get(sns_Touch_TOUCHXAD)>>2;    //read x-
54
        gpio_set_in(sns_Touch_YPLUS);
190
        gpio_set_in(sns_Touch_YPLUS);
55
        gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
191
        gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
56
        gpio_set_in(sns_Touch_YMINUS);
192
        gpio_set_in(sns_Touch_YMINUS);
57
        gpio_clr_pullup(sns_Touch_YMINUS);  //turn off pullup for y-
193
        gpio_clr_pullup(sns_Touch_YMINUS);  //turn off pullup for y-
58
        gpio_set_in(sns_Touch_XPLUS);
194
        gpio_set_in(sns_Touch_XPLUS);
59
        gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
195
        gpio_clr_pullup(sns_Touch_XPLUS);   //turn off pullup for x+
60
       
196
       
61
        gpio_set_in(sns_Touch_YPLUS);
197
        gpio_set_in(sns_Touch_YPLUS);
62
        gpio_set_pullup(sns_Touch_YPLUS);   //turn on pullup for y+
198
        gpio_set_pullup(sns_Touch_YPLUS);   //turn on pullup for y+
63
        gpio_set_out(sns_Touch_XPLUS);
199
        gpio_set_out(sns_Touch_XPLUS);
64
        gpio_set_pin(sns_Touch_XPLUS);      //turn on 1 on x+
200
        gpio_set_pin(sns_Touch_XPLUS);      //turn on 1 on x+
65
        gpio_set_out(sns_Touch_XMINUS);
201
        gpio_set_out(sns_Touch_XMINUS);
Line 70... Line 206...
70
        gpio_set_in(sns_Touch_XMINUS);
206
        gpio_set_in(sns_Touch_XMINUS);
71
        gpio_clr_pullup(sns_Touch_XMINUS);  //turn off pullup for x-
207
        gpio_clr_pullup(sns_Touch_XMINUS);  //turn off pullup for x-
72
        gpio_set_in(sns_Touch_YPLUS);
208
        gpio_set_in(sns_Touch_YPLUS);
73
        gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
209
        gpio_clr_pullup(sns_Touch_YPLUS);   //turn off pullup for y+
74
       
210
       
75
        if (adyval < 0xf0 && adxval < 0xf0 ) {
211
        if (adyval < 0xf0 && adxval < 0xf0 )
-
 
212
        {
-
 
213
            uint8_t xdiff=0;
-
 
214
            uint8_t ydiff=0;
-
 
215
            if (rxbufidx > 0)
-
 
216
            {
-
 
217
                xdiff = touchBuffer[rxbufidx-1].x - adxval;
-
 
218
                if (touchBuffer[rxbufidx-1].x < adxval)
-
 
219
                {
-
 
220
                    xdiff = adxval - touchBuffer[rxbufidx-1].x;
-
 
221
                }
-
 
222
                ydiff = touchBuffer[rxbufidx-1].y - adyval;
-
 
223
                if (touchBuffer[rxbufidx-1].y < adyval)
-
 
224
                {
-
 
225
                    ydiff = adyval - touchBuffer[rxbufidx-1].y;
-
 
226
                }
-
 
227
            }
-
 
228
            if (rxbufidx == 0 || (rxbufidx > 0 && xdiff+ydiff > 10))
-
 
229
            {
-
 
230
                touchBuffer[rxbufidx].x = adxval;
-
 
231
                touchBuffer[rxbufidx].y = adyval;
-
 
232
                rxbufidx++;
-
 
233
                if (rxbufidx==sns_Touch_BUFFERSIZE)
-
 
234
                {
-
 
235
                    rxbufidx=0;
-
 
236
                }
-
 
237
            }
76
            printf("Y: %d X: %d\n", adyval, adxval);
238
            //printf("Y: %d X: %d\n", adyval, adxval);
77
        }
239
        }
-
 
240
        else if (rxbufidx > 2)
-
 
241
        {
-
 
242
            //printf("Released %d\n", rxbufidx);
-
 
243
            uint8_t temp = parseBuffer(0, rxbufidx-1);
-
 
244
            /*for (uint8_t i = 0; i < rxbufidx; i++)
-
 
245
            {
-
 
246
                printf("%3d ", touchBuffer[i].x);
-
 
247
            }
-
 
248
            printf("\n");
-
 
249
            for (uint8_t i = 0; i < rxbufidx; i++)
-
 
250
            {
-
 
251
                printf("%3d ", touchBuffer[i].y);
-
 
252
            }
-
 
253
            printf("\n");
-
 
254
            */
-
 
255
           
-
 
256
            rxbufidx = 0;
78
 
257
        }
79
    }
258
    }
80
}
259
}
-
 
260
 
81
 
261
 
82
void sns_Touch_HandleMessage(StdCan_Msg_t *rxMsg)
262
void sns_Touch_HandleMessage(StdCan_Msg_t *rxMsg)
83
{
263
{
84
    /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
264
    /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
85
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
265
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
Line 99... Line 279...
99
{
279
{
100
    StdCan_Msg_t txMsg;
280
    StdCan_Msg_t txMsg;
101
   
281
   
102
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
282
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
103
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
283
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
104
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG;
284
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TOUCH;
105
    txMsg.Header.ModuleId = sns_Touch_ID;
285
    txMsg.Header.ModuleId = sns_Touch_ID;
106
    txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
286
    txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
107
    txMsg.Length = 6;
287
    txMsg.Length = 6;
108
 
288
 
109
    txMsg.Data[0] = NODE_HW_ID_BYTE0;
289
    txMsg.Data[0] = NODE_HW_ID_BYTE0;