Skip to content
This repository
Browse code

made touch task into own task again

  • Loading branch information...
commit b63f24517cda731c3ef91dfbabda53e28b367c1c 1 parent 0fec915
maso0237 maso0237 authored
491 project/main.c
... ... @@ -1,245 +1,246 @@
1   -/**
2   - * Program skeleton for the course "Programming embedded systems"
3   - */
4   -
5   -#include <stdio.h>
6   -
7   -/* Scheduler includes. */
8   -#include "FreeRTOS.h"
9   -#include "task.h"
10   -#include "semphr.h"
11   -#include "queue.h"
12   -#include "setup.h"
13   -#include "assert.h"
14   -#include "GLCD.h"
15   -#include "stm3210c_eval_ioe.h"
16   -
17   -/*-----------------------------------------------------------*/
18   -
19   -#include "task_input_gpio.h"
20   -#include "task_input_touch.h"
21   -#include "task_controller.h"
22   -#include "task_watchdog.h"
23   -#include "api_controller.h"
24   -#include "api_watchdog.h"
25   -#include "task_measure.h"
26   -#include "task_display.h"
27   -#define WIDTH 320
28   -
29   -xSemaphoreHandle lcdLock;
30   -
31   -static void initDisplay () {
32   - /* LCD Module init */
33   -
34   - lcdLock = xSemaphoreCreateMutex();
35   -
36   - GLCD_init();
37   - GLCD_clear(White);
38   - GLCD_setTextColor(Blue);
39   - GLCD_displayStringLn(Line2, " Programming");
40   - GLCD_displayStringLn(Line3, " Embedded");
41   - GLCD_displayStringLn(Line4, " Systems");
42   -}
43   -/*
44   -static void lcdTask(void *params) {
45   - unsigned short col1 = Blue, col2 = Red, col3 = Green;
46   - unsigned short t;
47   -
48   - for (;;) {
49   - xSemaphoreTake(lcdLock, portMAX_DELAY);
50   - GLCD_setTextColor(col1);
51   - GLCD_displayChar(Line7, WIDTH - 40, '1');
52   - GLCD_setTextColor(col2);
53   - GLCD_displayChar(Line7, WIDTH - 60, '2');
54   - GLCD_setTextColor(col3);
55   - GLCD_displayChar(Line7, WIDTH - 80, '3');
56   - xSemaphoreGive(lcdLock);
57   - t = col1; col1 = col2; col2 = col3; col3 = t;
58   - vTaskDelay(300 / portTICK_RATE_MS);
59   - }
60   -}
61   - */
62   -/*-----------------------------------------------------------*/
63   -
64   -/**
65   - * Display stdout on the display
66   - */
67   -
68   -xQueueHandle printQueue;
69   -
70   -static void printTask(void *params) {
71   - unsigned char str[21] = " ";
72   - portTickType lastWakeTime = xTaskGetTickCount();
73   - int i;
74   -
75   - for (;;) {
76   - xSemaphoreTake(lcdLock, portMAX_DELAY);
77   - GLCD_setTextColor(Black);
78   - GLCD_displayStringLn(Line9, str);
79   - xSemaphoreGive(lcdLock);
80   -
81   - for (i = 0; i < 19; ++i)
82   - str[i] = str[i+1];
83   -
84   - if (!xQueueReceive(printQueue, str + 19, 0))
85   - str[19] = ' ';
86   -
87   - vTaskDelayUntil(&lastWakeTime, 100 / portTICK_RATE_MS);
88   - }
89   -}
90   -
91   -/* Retarget printing to the serial port 1 */
92   -int fputc(int ch, FILE *f) {
93   - unsigned char c = ch;
94   - xQueueSend(printQueue, &c, 0);
95   - return ch;
96   -}
97   -
98   -/*-----------------------------------------------------------*/
99   -
100   -/**
101   - * Register a callback that will be invoked when a touch screen
102   - * event occurs within a given rectangle
103   - *
104   - * NB: the callback function should have a short execution time,
105   - * since long-running callbacks will prevent further events from
106   - * being generated
107   - */
108   -
109   -typedef struct {
110   - u16 lower, upper, left, right;
111   - void *data;
112   - void (*callback)(u16 x, u16 y, u16 pressure, void *data);
113   -} TSCallback;
114   -
115   -static TSCallback callbacks[16];
116   -static u8 callbackNum = 0;
117   -
118   -void registerTSCallback(u16 left, u16 right, u16 lower, u16 upper,
119   - void (*callback)(u16 x, u16 y, u16 pressure, void *data),
120   - void *data) {
121   - callbacks[callbackNum].lower = lower;
122   - callbacks[callbackNum].upper = upper;
123   - callbacks[callbackNum].left = left;
124   - callbacks[callbackNum].right = right;
125   - callbacks[callbackNum].callback = callback;
126   - callbacks[callbackNum].data = data;
127   - callbackNum++;
128   -}
129   -
130   -static void touchScreenTask(void *params) {
131   - portTickType lastWakeTime = xTaskGetTickCount();
132   - TS_STATE *ts_state;
133   - u8 pressed = 0;
134   - u8 i;
135   -
136   - for (;;) {
137   - ts_state = IOE_TS_GetState();
138   -
139   - if (pressed) {
140   - if (!ts_state->TouchDetected)
141   - pressed = 0;
142   - } else if (ts_state->TouchDetected) {
143   - for (i = 0; i < callbackNum; ++i) {
144   - if (callbacks[i].left <= ts_state->X &&
145   - callbacks[i].right >= ts_state->X &&
146   - callbacks[i].lower >= ts_state->Y &&
147   - callbacks[i].upper <= ts_state->Y)
148   - callbacks[i].callback(ts_state->X, ts_state->Y, ts_state->Z,
149   - callbacks[i].data);
150   - }
151   - pressed = 1;
152   - }
153   -
154   - if (ts_state->TouchDetected) {
155   - printf("%d,%d,%d ", ts_state->X, ts_state->Y, ts_state->Z);
156   - }
157   -
158   - vTaskDelayUntil(&lastWakeTime, 100 / portTICK_RATE_MS);
159   - }
160   -}
161   -
162   -/*-----------------------------------------------------------*/
163   -
164   -xQueueHandle buttonQueue;
165   -
166   -static void highlightButton(u16 x, u16 y, u16 pressure, void *data) {
167   - u16 d = (int)data;
168   - xQueueSend(buttonQueue, &d, 0);
169   -}
170   -
171   -static void setupButtons(void) {
172   - u16 i;
173   - buttonQueue = xQueueCreate(4, sizeof(u16));
174   -
175   - for (i = 0; i < 3; ++i) {
176   - GLCD_drawRect(30 + 60*i, 30, 40, 40);
177   - registerTSCallback(WIDTH - 30 - 40, WIDTH - 30, 30 + 60*i + 40, 30 + 60*i,
178   - &highlightButton, (void*)i);
179   - }
180   -}
181   -
182   -static void highlightButtonsTask(void *params) {
183   - u16 d;
184   -
185   - for (;;) {
186   - xQueueReceive(buttonQueue, &d, portMAX_DELAY);
187   -
188   - xSemaphoreTake(lcdLock, portMAX_DELAY);
189   - GLCD_setTextColor(Red);
190   - GLCD_fillRect(31 + 60*d, 31, 38, 38);
191   - GLCD_setTextColor(Blue);
192   - xSemaphoreGive(lcdLock);
193   -
194   - vTaskDelay(500 / portTICK_RATE_MS);
195   -
196   - xSemaphoreTake(lcdLock, portMAX_DELAY);
197   - GLCD_setTextColor(White);
198   - GLCD_fillRect(31 + 60*d, 31, 38, 38);
199   - GLCD_setTextColor(Blue);
200   - xSemaphoreGive(lcdLock);
201   - }
202   -}
203   -
204   -/*-----------------------------------------------------------*/
205   -
206   -/*
207   - * Entry point of program execution
208   - */
209   -int main( void )
210   -{
211   - prvSetupHardware();
212   - IOE_Config();
213   -
214   - printQueue = xQueueCreate(128, 1);
215   -
216   - initDisplay();
217   - setupButtons();
218   - measureInit();
219   -
220   - if(pdFALSE == ipc_init())
221   - {
222   - /* TODO: Output error mesg? */
223   - task_watchdog_signal_error();
224   - }
225   -
226   - //xTaskCreate(lcdTask, "lcd", 100, NULL, 1, NULL);
227   - xTaskCreate(printTask, "print", 100, NULL, 1, NULL);
228   - xTaskCreate(touchScreenTask, "touchScreen", 100, NULL, 1, NULL);
229   - xTaskCreate(highlightButtonsTask, "highlighter", 100, NULL, 1, NULL);
230   - xTaskCreate(task_controller, "Controller", 100, NULL, 1, NULL);
231   - xTaskCreate(task_watchdog, "Watchdog driver", 100, NULL, 1, NULL);
232   - xTaskCreate(task_input_gpio, "Input driver for GPIO", 100, NULL, 1, NULL);
233   - xTaskCreate(task_input_touch, "Input driver for touchscreen", 100, NULL, 1, NULL);
234   - xTaskCreate(task_display, "Display", 100, NULL, 1, NULL);
235   - xTaskCreate( measureTask , "Measure", 100, NULL, 1, NULL);
236   -
237   - printf("Setup complete "); // this is redirected to the display
238   -
239   - vTaskStartScheduler();
240   -
241   - assert(0);
242   - return 0; // not reachable
243   -}
244   -
245   -/*-----------------------------------------------------------*/
  1 +/**
  2 + * Program skeleton for the course "Programming embedded systems"
  3 + */
  4 +
  5 +#include <stdio.h>
  6 +
  7 +/* Scheduler includes. */
  8 +#include "FreeRTOS.h"
  9 +#include "task.h"
  10 +#include "semphr.h"
  11 +#include "queue.h"
  12 +#include "setup.h"
  13 +#include "assert.h"
  14 +#include "GLCD.h"
  15 +#include "stm3210c_eval_ioe.h"
  16 +
  17 +/*-----------------------------------------------------------*/
  18 +
  19 +#include "task_input_gpio.h"
  20 +#include "task_input_touch.h"
  21 +#include "task_controller.h"
  22 +#include "task_watchdog.h"
  23 +#include "api_controller.h"
  24 +#include "api_watchdog.h"
  25 +#include "task_measure.h"
  26 +#include "task_display.h"
  27 +#define WIDTH 320
  28 +
  29 +xSemaphoreHandle lcdLock;
  30 +
  31 +static void initDisplay () {
  32 + /* LCD Module init */
  33 +
  34 + lcdLock = xSemaphoreCreateMutex();
  35 +
  36 + GLCD_init();
  37 + GLCD_clear(White);
  38 + GLCD_setTextColor(Blue);
  39 + GLCD_displayStringLn(Line2, " Programming");
  40 + GLCD_displayStringLn(Line3, " Embedded");
  41 + GLCD_displayStringLn(Line4, " Systems");
  42 +}
  43 +/*
  44 +static void lcdTask(void *params) {
  45 + unsigned short col1 = Blue, col2 = Red, col3 = Green;
  46 + unsigned short t;
  47 +
  48 + for (;;) {
  49 + xSemaphoreTake(lcdLock, portMAX_DELAY);
  50 + GLCD_setTextColor(col1);
  51 + GLCD_displayChar(Line7, WIDTH - 40, '1');
  52 + GLCD_setTextColor(col2);
  53 + GLCD_displayChar(Line7, WIDTH - 60, '2');
  54 + GLCD_setTextColor(col3);
  55 + GLCD_displayChar(Line7, WIDTH - 80, '3');
  56 + xSemaphoreGive(lcdLock);
  57 + t = col1; col1 = col2; col2 = col3; col3 = t;
  58 + vTaskDelay(300 / portTICK_RATE_MS);
  59 + }
  60 +}
  61 + */
  62 +/*-----------------------------------------------------------*/
  63 +
  64 +/**
  65 + * Display stdout on the display
  66 + */
  67 +
  68 +xQueueHandle printQueue;
  69 +
  70 +static void printTask(void *params) {
  71 + unsigned char str[21] = " ";
  72 + portTickType lastWakeTime = xTaskGetTickCount();
  73 + int i;
  74 +
  75 + for (;;) {
  76 + xSemaphoreTake(lcdLock, portMAX_DELAY);
  77 + GLCD_setTextColor(Black);
  78 + GLCD_displayStringLn(Line9, str);
  79 + xSemaphoreGive(lcdLock);
  80 +
  81 + for (i = 0; i < 19; ++i)
  82 + str[i] = str[i+1];
  83 +
  84 + if (!xQueueReceive(printQueue, str + 19, 0))
  85 + str[19] = ' ';
  86 +
  87 + vTaskDelayUntil(&lastWakeTime, 100 / portTICK_RATE_MS);
  88 + }
  89 +}
  90 +
  91 +/* Retarget printing to the serial port 1 */
  92 +int fputc(int ch, FILE *f) {
  93 + unsigned char c = ch;
  94 + xQueueSend(printQueue, &c, 0);
  95 + return ch;
  96 +}
  97 +
  98 +/*-----------------------------------------------------------*/
  99 +
  100 +/**
  101 + * Register a callback that will be invoked when a touch screen
  102 + * event occurs within a given rectangle
  103 + *
  104 + * NB: the callback function should have a short execution time,
  105 + * since long-running callbacks will prevent further events from
  106 + * being generated
  107 + */
  108 +
  109 +typedef struct {
  110 + u16 lower, upper, left, right;
  111 + void *data;
  112 + void (*callback)(u16 x, u16 y, u16 pressure, void *data);
  113 +} TSCallback;
  114 +
  115 +//static TSCallback callbacks[16];
  116 +//static u8 callbackNum = 0;
  117 +/*
  118 +void registerTSCallback(u16 left, u16 right, u16 lower, u16 upper,
  119 + void (*callback)(u16 x, u16 y, u16 pressure, void *data),
  120 + void *data) {
  121 + callbacks[callbackNum].lower = lower;
  122 + callbacks[callbackNum].upper = upper;
  123 + callbacks[callbackNum].left = left;
  124 + callbacks[callbackNum].right = right;
  125 + callbacks[callbackNum].callback = callback;
  126 + callbacks[callbackNum].data = data;
  127 + callbackNum++;
  128 +}
  129 +
  130 +static void touchScreenTask(void *params) {
  131 + portTickType lastWakeTime = xTaskGetTickCount();
  132 + TS_STATE *ts_state;
  133 + u8 pressed = 0;
  134 + u8 i;
  135 +
  136 + for (;;) {
  137 + ts_state = IOE_TS_GetState();
  138 +
  139 + if (pressed) {
  140 + if (!ts_state->TouchDetected)
  141 + pressed = 0;
  142 + } else if (ts_state->TouchDetected) {
  143 + for (i = 0; i < callbackNum; ++i) {
  144 + if (callbacks[i].left <= ts_state->X &&
  145 + callbacks[i].right >= ts_state->X &&
  146 + callbacks[i].lower >= ts_state->Y &&
  147 + callbacks[i].upper <= ts_state->Y)
  148 + callbacks[i].callback(ts_state->X, ts_state->Y, ts_state->Z,
  149 + callbacks[i].data);
  150 + }
  151 + pressed = 1;
  152 + }
  153 +
  154 + if (ts_state->TouchDetected) {
  155 + printf("%d,%d,%d ", ts_state->X, ts_state->Y, ts_state->Z);
  156 + }
  157 +
  158 + vTaskDelayUntil(&lastWakeTime, 100 / portTICK_RATE_MS);
  159 + }
  160 +}
  161 + */
  162 +/*-----------------------------------------------------------*/
  163 + /*
  164 +xQueueHandle buttonQueue;
  165 +
  166 +static void highlightButton(u16 x, u16 y, u16 pressure, void *data) {
  167 + u16 d = (int)data;
  168 + xQueueSend(buttonQueue, &d, 0);
  169 +}
  170 +
  171 +static void setupButtons(void) {
  172 + u16 i;
  173 + buttonQueue = xQueueCreate(4, sizeof(u16));
  174 +
  175 + for (i = 0; i < 3; ++i) {
  176 + GLCD_drawRect(30 + 60*i, 30, 40, 40);
  177 + registerTSCallback(WIDTH - 30 - 40, WIDTH - 30, 30 + 60*i + 40, 30 + 60*i,
  178 + &highlightButton, (void*)i);
  179 + }
  180 +}
  181 +
  182 +static void highlightButtonsTask(void *params) {
  183 + u16 d;
  184 +
  185 + for (;;) {
  186 + xQueueReceive(buttonQueue, &d, portMAX_DELAY);
  187 +
  188 + xSemaphoreTake(lcdLock, portMAX_DELAY);
  189 + GLCD_setTextColor(Red);
  190 + GLCD_fillRect(31 + 60*d, 31, 38, 38);
  191 + GLCD_setTextColor(Blue);
  192 + xSemaphoreGive(lcdLock);
  193 +
  194 + vTaskDelay(500 / portTICK_RATE_MS);
  195 +
  196 + xSemaphoreTake(lcdLock, portMAX_DELAY);
  197 + GLCD_setTextColor(White);
  198 + GLCD_fillRect(31 + 60*d, 31, 38, 38);
  199 + GLCD_setTextColor(Blue);
  200 + xSemaphoreGive(lcdLock);
  201 + }
  202 +}
  203 + */
  204 +/*-----------------------------------------------------------*/
  205 +
  206 +/*
  207 + * Entry point of program execution
  208 + */
  209 +int main( void )
  210 +{
  211 + prvSetupHardware();
  212 + IOE_Config();
  213 +
  214 + printQueue = xQueueCreate(128, 1);
  215 +
  216 + initDisplay();
  217 + //setupButtons();
  218 + measureInit();
  219 +
  220 +
  221 + if(pdFALSE == ipc_init())
  222 + {
  223 + /* TODO: Output error mesg? */
  224 + task_watchdog_signal_error();
  225 + }
  226 +
  227 + //xTaskCreate(lcdTask, "lcd", 100, NULL, 1, NULL);
  228 + xTaskCreate(printTask, "print", 100, NULL, 1, NULL);
  229 + //xTaskCreate(touchScreenTask, "touchScreen", 100, NULL, 1, NULL);
  230 + //xTaskCreate(highlightButtonsTask, "highlighter", 100, NULL, 1, NULL);
  231 + xTaskCreate(task_controller, "Controller", 100, NULL, 1, NULL);
  232 + xTaskCreate(task_watchdog, "Watchdog driver", 100, NULL, 1, NULL);
  233 + xTaskCreate(task_input_gpio, "Input driver for GPIO", 100, NULL, 1, NULL);
  234 + xTaskCreate(task_input_touch, "Input driver for touchscreen", 100, NULL, 1, NULL);
  235 + xTaskCreate(task_display, "Display", 100, NULL, 1, NULL);
  236 + xTaskCreate( measureTask , "Measure", 100, NULL, 1, NULL);
  237 +
  238 + printf("Setup complete "); // this is redirected to the display
  239 +
  240 + vTaskStartScheduler();
  241 +
  242 + assert(0);
  243 + return 0; // not reachable
  244 +}
  245 +
  246 +/*-----------------------------------------------------------*/
62 project/task_input_touch.c
@@ -9,6 +9,8 @@
9 9
10 10 /* FreeRTOS */
11 11 #include "FreeRTOS.h"
  12 +#include "GLCD.h"
  13 +#include "stm3210c_eval_ioe.h"
12 14 #include "task.h"
13 15
14 16 #include "api_input_touch.h"
@@ -21,10 +23,44 @@
21 23 /* private variables */
22 24
23 25 /* private functions */
  26 +/*-----------------------------------------------------------*/
  27 +
  28 +/**
  29 + * Register a callback that will be invoked when a touch screen
  30 + * event occurs within a given rectangle
  31 + *
  32 + * NB: the callback function should have a short execution time,
  33 + * since long-running callbacks will prevent further events from
  34 + * being generated
  35 + */
  36 +
  37 +typedef struct {
  38 + u16 lower, upper, left, right;
  39 + void *data;
  40 + void (*callback)(u16 x, u16 y, u16 pressure, void *data);
  41 +} TSCallback;
  42 +
  43 +static TSCallback callbacks[16];
  44 +static u8 callbackNum = 0;
  45 +
  46 +void registerTSCallback(u16 left, u16 right, u16 lower, u16 upper,
  47 + void (*callback)(u16 x, u16 y, u16 pressure, void *data),
  48 + void *data) {
  49 + callbacks[callbackNum].lower = lower;
  50 + callbacks[callbackNum].upper = upper;
  51 + callbacks[callbackNum].left = left;
  52 + callbacks[callbackNum].right = right;
  53 + callbacks[callbackNum].callback = callback;
  54 + callbacks[callbackNum].data = data;
  55 + callbackNum++;
  56 +}
24 57
25 58 /* public functions */
26 59 void task_input_touch(void *p)
27   -{
  60 +{
  61 + TS_STATE *ts_state;
  62 + u8 pressed = 0;
  63 + u8 i;
28 64 portTickType timeout = CFG_TASK_INPUT_TOUCH__POLLING_PERIOD;
29 65
30 66 /* subscribe to mode variable in the controller, returns pd(TRUE|FALSE) */
@@ -40,7 +76,29 @@ void task_input_touch(void *p)
40 76 assert(ipc_input_touch);
41 77 if(pdFALSE == xQueueReceive(ipc_input_touch, &msg, timeout))
42 78 {
43   - /* timeout work */
  79 + /* timeout work */
  80 + ts_state = IOE_TS_GetState();
  81 +
  82 + if (pressed) {
  83 + if (!ts_state->TouchDetected)
  84 + pressed = 0;
  85 + } else if (ts_state->TouchDetected) {
  86 + for (i = 0; i < callbackNum; ++i) {
  87 + if (callbacks[i].left <= ts_state->X &&
  88 + callbacks[i].right >= ts_state->X &&
  89 + callbacks[i].lower >= ts_state->Y &&
  90 + callbacks[i].upper <= ts_state->Y)
  91 + callbacks[i].callback(ts_state->X, ts_state->Y, ts_state->Z,
  92 + callbacks[i].data);
  93 + }
  94 + pressed = 1;
  95 + }
  96 +
  97 + if (ts_state->TouchDetected) {
  98 + printf("%d,%d,%d ", ts_state->X, ts_state->Y, ts_state->Z);
  99 + }
  100 +
  101 +
44 102 }
45 103 else
46 104 {
2  project/task_watchdog.c
@@ -4,7 +4,7 @@
4 4 * TASK responsible for status leds and HW-watchdog reset circuts.
5 5 * (For now a pure SW that toogles a LED.)
6 6 */
7   -
  7 +#include <stdio.h>
8 8 /* Firmware */
9 9 #include "assert.h"
10 10

0 comments on commit b63f245

Please sign in to comment.
Something went wrong with that request. Please try again.