Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.32 KB | None | 0 0
  1. /**
  2. * This is the main file of the ESPLaboratory Demo project.
  3. * It implements simple sample functions for the usage of UART,
  4. * writing to the display and processing user inputs.
  5. *
  6. * @author: Alex Hoffman alex.hoffman@tum.de (RCS, TUM)
  7. * Jonathan M��ller-Boruttau,
  8. * Tobias Fuchs tobias.fuchs@tum.de
  9. * Nadja Peters nadja.peters@tum.de (RCS, TUM)
  10. *
  11. */
  12. #include "includes.h"
  13. #include <math.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19.  
  20. #define DISPLAY_SIZE_X 320
  21. #define DISPLAY_SIZE_Y 240
  22.  
  23. #define TEXT_X(TEXT) DISPLAY_SIZE_X / 2 - (gdispGetStringWidth(TEXT, font1) / 2)
  24. #define TEXT_Y(LINE) DISPLAY_SIZE_Y / 2 - (gdispGetFontMetric(font1, fontHeight) * -(LINE + 0.5)) + 65
  25.  
  26. #define SLIDING_TEXT_X(TEXT) DISPLAY_SIZE_X / 2 - (gdispGetStringWidth(TEXT, font1) / 2) + sliderCounter() - 100
  27. #define SLIDING_TEXT_Y(LINE) DISPLAY_SIZE_Y / 2 - (gdispGetFontMetric(font1, fontHeight) * -(LINE + 0.5)) - 100
  28.  
  29. #define PI 3.14159265
  30. #define RADIAN_CONVERSION PI / 180
  31. #define ROTATION_RADIUS_CIRCLE 40
  32. #define ROTATING_CIRCLE_X DISPLAY_SIZE_X / 2 + ROTATION_RADIUS_CIRCLE * cos(circleRotate() * RADIAN_CONVERSION)
  33. #define ROTATING_CIRCLE_Y DISPLAY_SIZE_Y / 2 - 5 + ROTATION_RADIUS_CIRCLE * sin(circleRotate() * RADIAN_CONVERSION)
  34.  
  35. #define ROTATION_RADIUS_SQUARE 40
  36. #define ROTATING_SQUARE_X DISPLAY_SIZE_X / 2 + ROTATION_RADIUS_SQUARE * cos((circleRotate() + 180) * RADIAN_CONVERSION)
  37. #define ROTATING_SQUARE_Y DISPLAY_SIZE_Y / 2 + ROTATION_RADIUS_SQUARE * sin((circleRotate() + 180) * RADIAN_CONVERSION)
  38.  
  39. #define SCREEN_SLIDE_X buttonStatus.joystick.x / 8
  40. #define SCREEN_SLIDE_Y buttonStatus.joystick.y / 8
  41.  
  42. #define SCREEN_SLIDE_X_UART joystickStatusUART.joystick.x / 8
  43. #define SCREEN_SLIDE_Y_UART joystickStatusUART.joystick.y / 8
  44.  
  45. #define STATUS_JOYSTICK_X (uint8_t)(ADC_GetConversionValue(ESPL_ADC_Joystick_2) >> 4)
  46. #define STATUS_JOYSTICK_Y (uint8_t) 255 - (ADC_GetConversionValue(ESPL_ADC_Joystick_1) >> 4)
  47.  
  48.  
  49. #define BUTTON_QUEUE_LENGTH 20
  50. #define STATE_QUEUE_LENGTH 1
  51. #define FPS_QUEUE_LENGTH 20
  52.  
  53. #define STATE_COUNT 3
  54. #define STATE_ONE 1
  55. #define STATE_TWO 2
  56. #define STATE_THREE 3
  57.  
  58. #define NEXT_TASK 1
  59. #define PREV_TASK 2
  60. #define NUM_POINTS (sizeof(triangle)/sizeof(triangle[0]))
  61.  
  62. #define STACK_SIZE 200
  63. #define RESET_TIMER_EX_3 15 //In seconds
  64.  
  65. // Start and stop bytes for the UART protocol (READ-ONLY)
  66. static const uint8_t startByte = 0xAA, stopByte = 0x55;
  67.  
  68. // Coordinates for the triangle we are drawing in exercise 1 (READ-ONLY)
  69. static const point triangle[] = {
  70. { -15, 0 },
  71. { 0 , -20 },
  72. { 15, 0 },
  73. };
  74.  
  75. // Load font for ugfx
  76. font_t font1;
  77.  
  78. // Function prototypes
  79. // General
  80. void frameSwapTask(void * params);
  81. void basicStateMachine(void * params);
  82. void checkButtons(void * params);
  83.  
  84. void exercise1Display(void * params);
  85. void exercise2Display(void * params);
  86. void exercise3Display(void * params);
  87.  
  88. // Exercise 3.2.2
  89. void circleBlinkDynamic(void *params);
  90. void circleBlinkStatic(void * params);
  91.  
  92. // Exercise 3.2.3
  93. void timesButtonAPressed(void * params);
  94. void timesButtonBPressed(void * params);
  95. void resetTimer(void * params);
  96.  
  97. // Exercise 3.2.4
  98. void increaseVariable(void * params);
  99.  
  100. // Exercise 3.2.5
  101. void sendPositionUART(void * params);
  102. void receivePositionUART(void * params);
  103.  
  104. // Exercise 3.3
  105. void ex4task1(void * params);
  106. void ex4task2(void * params);
  107. void ex4task3(void * params);
  108. void ex4task4(void * params);
  109.  
  110. // General
  111. QueueHandle_t ESPL_RxQueue; // Already defined in ESPL_Functions.h
  112. SemaphoreHandle_t ESPL_DisplayReady;
  113. SemaphoreHandle_t DrawReady; // After swapping buffer calll drawing
  114.  
  115. // Exercise 3.2.2
  116. SemaphoreHandle_t BlinkCircle2Hz;
  117. SemaphoreHandle_t BlinkCircle1Hz;
  118.  
  119. // Exercise 3.2.3
  120. SemaphoreHandle_t ButtonAPressed;
  121. SemaphoreHandle_t ButtonBPressed;
  122. SemaphoreHandle_t ResetTimer;
  123.  
  124. // Exercise 3.2.4
  125. SemaphoreHandle_t increaseVariable_Semaphore;
  126.  
  127. // General
  128. QueueHandle_t ButtonQueue;
  129. QueueHandle_t StateQueue;
  130. TaskHandle_t frameSwapHandle;
  131. TaskHandle_t exercise1DisplayHandle;
  132. TaskHandle_t exercise2DisplayHandle;
  133. TaskHandle_t exercise3DisplayHandle;
  134. TaskHandle_t stateMachineHandle;
  135.  
  136. //Exercise 3.2.2
  137. TaskHandle_t circleBlinkDynamicHandle;
  138. StaticTask_t xTaskBuffer;
  139. StackType_t xStack[ STACK_SIZE ];
  140. QueueHandle_t FPSQueue;
  141.  
  142. //Exercise 3.2.3
  143. TaskHandle_t timesButtonAPressedHandle;
  144. TaskHandle_t timesButtonBPressedHandle;
  145. TaskHandle_t resetTimerHandle;
  146. QueueHandle_t resetTimerQueue;
  147.  
  148. //Exercise 3.2.4
  149. TaskHandle_t increaseVariableHandle;
  150.  
  151. // Exercise 3.2.5
  152. TaskHandle_t sendPositionUARTHandle;
  153. TaskHandle_t receivePositionUARTHandle;
  154. QueueHandle_t UARTjoystickQueue;
  155. locked_buttons_t buttonCounts;
  156.  
  157. // Exercise 3.3
  158. TaskHandle_t ex4task1Handle;
  159. TaskHandle_t ex4task2Handle;
  160. TaskHandle_t ex4task3Handle;
  161. TaskHandle_t ex4task4Handle;
  162. QueueHandle_t ex4TaskQueue;
  163. //locked_ex4output_t ex4Output;
  164.  
  165. char print[15][70] = { "Tick 1: ", "Tick 2: ", "Tick 3: ", "Tick 4: ", "Tick 5: ",
  166. "Tick 6: ", "Tick 7: ", "Tick 8: ", "Tick 9: ", "Tick 10: ",
  167. "Tick 11: ", "Tick 12: ", "Tick 13: ", "Tick 14: ", "Tick 15: " };
  168.  
  169.  
  170. int main(void){
  171.  
  172.  
  173. // Initialize Board functions and graphics
  174. ESPL_SystemInit();
  175. font1 = gdispOpenFont("DejaVuSans24*");
  176. // General
  177. ButtonQueue = xQueueCreate(BUTTON_QUEUE_LENGTH, sizeof(struct buttons));
  178. StateQueue = xQueueCreate(STATE_QUEUE_LENGTH, sizeof(unsigned char));
  179.  
  180. ESPL_DisplayReady = xSemaphoreCreateBinary();
  181. DrawReady = xSemaphoreCreateBinary();
  182.  
  183. // UART buttons counting for 3.2.5
  184. buttonCounts = initButtons();
  185.  
  186. // Exercise 3.2.2
  187. BlinkCircle2Hz = xSemaphoreCreateBinary();
  188. BlinkCircle1Hz = xSemaphoreCreateBinary();
  189. FPSQueue = xQueueCreate(FPS_QUEUE_LENGTH, sizeof(uint8_t));
  190.  
  191. // Exercise 3.2.3
  192. ButtonAPressed = xSemaphoreCreateBinary();
  193. ButtonBPressed = xSemaphoreCreateBinary();
  194. ResetTimer = xSemaphoreCreateBinary();
  195. resetTimerQueue = xQueueCreate(FPS_QUEUE_LENGTH, sizeof(unsigned char));
  196.  
  197. // Exercise 3.2.4
  198. increaseVariable_Semaphore = xSemaphoreCreateBinary();
  199.  
  200. // Initializes Tasks with their respective priority
  201. // Core tasks
  202. xTaskCreate(frameSwapTask, "frameSwapper", 1000, NULL, 5, &frameSwapHandle);
  203. xTaskCreate(basicStateMachine, "StateMachine", 1000, NULL, 3, &stateMachineHandle);
  204. xTaskCreate(checkButtons, "checkButtons", 1000, NULL, 4, NULL);
  205.  
  206. // display tasks for the different exercises
  207. xTaskCreate(exercise1Display, "exercise1Display", 1000, NULL, 2, &exercise1DisplayHandle);
  208. xTaskCreate(exercise2Display, "exercise2Display", 1000, NULL, 2, &exercise2DisplayHandle);
  209. xTaskCreate(exercise3Display, "exercise3Display", 1000, NULL, 2, &exercise3DisplayHandle);
  210.  
  211. // Exercise 3.2.2
  212. xTaskCreate(circleBlinkDynamic, "circleBlinkDynamic", 1000, NULL, 1, &circleBlinkDynamicHandle);
  213. xTaskCreateStatic(circleBlinkStatic, "circleBlinkStatic", 200, NULL, 2, xStack, &xTaskBuffer);
  214.  
  215.  
  216. // Exercise 3.2.3
  217. xTaskCreate(timesButtonAPressed, "timesButtonAPressed", 1000, NULL, 1, &timesButtonAPressedHandle);
  218. xTaskCreate(timesButtonBPressed, "timesButtonBPressed", 1000, NULL, 1, &timesButtonBPressedHandle);
  219. xTaskCreate(resetTimer, "resetTimer", 1000, NULL, 2, &resetTimerHandle);
  220.  
  221. // Exercise 3.2.4
  222. xTaskCreate(increaseVariable, "increaseVariable", 1000, NULL, 2, &increaseVariableHandle);
  223.  
  224. // Exercise 3.2.5
  225. xTaskCreate(sendPositionUART, "sendPositionUART", 1000, NULL, 4, &sendPositionUARTHandle);
  226. xTaskCreate(receivePositionUART, "receivePositionUART", 1000, NULL, 4, &receivePositionUARTHandle);
  227. UARTjoystickQueue = xQueueCreate(BUTTON_QUEUE_LENGTH, sizeof(struct buttons));
  228.  
  229. // Exercise 3.3
  230. xTaskCreate(ex4task1, "ex4task1", 1000, NULL, 1, &ex4task1Handle);
  231. xTaskCreate(ex4task2, "ex4task2", 1000, NULL, 2, &ex4task2Handle);
  232. xTaskCreate(ex4task3, "ex4task3", 1000, NULL, 3, &ex4task3Handle);
  233. xTaskCreate(ex4task4, "ex4task4", 1000, NULL, 4, &ex4task4Handle);
  234. // ex4TaskQueue = xQueueCreate(100, sizeof(ex4output_t));
  235. // ex4Output = initEx4Output();
  236.  
  237. // Initial task suspends
  238. vTaskSuspend(exercise1DisplayHandle);
  239. vTaskSuspend(exercise2DisplayHandle);
  240. vTaskSuspend(exercise3DisplayHandle);
  241. vTaskSuspend(timesButtonAPressedHandle);
  242. vTaskSuspend(timesButtonBPressedHandle);
  243. vTaskSuspend(receivePositionUARTHandle);
  244. vTaskSuspend(sendPositionUARTHandle);
  245. vTaskSuspend(ex4task1Handle);
  246. vTaskSuspend(ex4task2Handle);
  247. vTaskSuspend(ex4task3Handle);
  248. vTaskSuspend(ex4task4Handle);
  249.  
  250. // Start FreeRTOS Scheduler
  251. vTaskStartScheduler();
  252. }
  253.  
  254. void frameSwapTask(void * params) {
  255. TickType_t xLastWakeTime;
  256. xLastWakeTime = xTaskGetTickCount();
  257. TickType_t xOldWakeTime;
  258. xOldWakeTime = 0;
  259. const TickType_t frameratePeriod = 20;
  260.  
  261. while (1) {
  262.  
  263. // Draw next frame
  264. xSemaphoreGive(DrawReady);
  265. // Wait for display to stop writing
  266. xSemaphoreTake(ESPL_DisplayReady, portMAX_DELAY);
  267. // Swap buffers
  268. ESPL_DrawLayer();
  269. xOldWakeTime = xLastWakeTime;
  270. vTaskDelayUntil(&xLastWakeTime, frameratePeriod);
  271. TickType_t xDifference;
  272. xDifference = xLastWakeTime - xOldWakeTime;
  273. uint8_t xFPS = 1000 / xDifference;
  274. xQueueSend(FPSQueue, &xFPS, 0);
  275. }
  276. }
  277.  
  278. void changeState(volatile unsigned char *state, unsigned char forwards) {
  279.  
  280. switch (forwards) {
  281. case 0:
  282. if (*state == 0)
  283. *state = STATE_COUNT;
  284. else
  285. (*state)--;
  286. break;
  287. case 1:
  288. if (*state == STATE_COUNT)
  289. *state = 0;
  290. else
  291. (*state)++;
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297.  
  298. void basicStateMachine(void * params) {
  299. unsigned char current_state = 1; // Default state
  300. unsigned char state_changed = 1; // Only re-evaluate state if it has changed
  301. unsigned char input = 0;
  302.  
  303. while (1) {
  304.  
  305. if (state_changed)
  306. goto initial_state;
  307.  
  308. // Handle state machine input
  309. if (xQueueReceive(StateQueue, &input, portMAX_DELAY) == pdTRUE) {
  310. if (input == NEXT_TASK) {
  311. changeState(&current_state, 1);
  312. state_changed = 1;
  313. }
  314. else if (input == PREV_TASK) {
  315. changeState(&current_state, 0);
  316. changeState(&current_state, 0);
  317. state_changed = 1;
  318. }
  319. }
  320.  
  321.  
  322. initial_state:
  323. // Handle current state
  324. if (state_changed) {
  325. switch (current_state) {
  326. case STATE_ONE:
  327. vTaskSuspend(timesButtonAPressedHandle);
  328. vTaskSuspend(timesButtonBPressedHandle);
  329. vTaskSuspend(receivePositionUARTHandle);
  330. vTaskSuspend(sendPositionUARTHandle);
  331. vTaskSuspend(ex4task1Handle);
  332. vTaskSuspend(ex4task2Handle);
  333. vTaskSuspend(ex4task3Handle);
  334. vTaskSuspend(ex4task4Handle);
  335. vTaskSuspend(exercise2DisplayHandle);
  336. vTaskSuspend(exercise3DisplayHandle);
  337. vTaskResume(exercise1DisplayHandle);
  338. state_changed = 0;
  339. break;
  340. case STATE_TWO:
  341. vTaskSuspend(ex4task1Handle);
  342. vTaskSuspend(ex4task2Handle);
  343. vTaskSuspend(ex4task3Handle);
  344. vTaskSuspend(ex4task4Handle);
  345. vTaskSuspend(exercise1DisplayHandle);
  346. vTaskSuspend(exercise3DisplayHandle);
  347. vTaskResume(exercise2DisplayHandle);
  348. vTaskResume(timesButtonAPressedHandle);
  349. vTaskResume(timesButtonBPressedHandle);
  350. vTaskResume(sendPositionUARTHandle);
  351. vTaskResume(receivePositionUARTHandle);
  352. state_changed = 0;
  353. break;
  354. case STATE_THREE:
  355. vTaskSuspend(timesButtonAPressedHandle);
  356. vTaskSuspend(timesButtonBPressedHandle);
  357. vTaskSuspend(receivePositionUARTHandle);
  358. vTaskSuspend(sendPositionUARTHandle);
  359. vTaskSuspend(exercise1DisplayHandle);
  360. vTaskSuspend(exercise2DisplayHandle);
  361. vTaskResume(exercise3DisplayHandle);
  362. vTaskResume(ex4task1Handle);
  363. vTaskResume(ex4task2Handle);
  364. // vTaskResume(ex4task3Handle);
  365. vTaskResume(ex4task4Handle);
  366. state_changed = 0;
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372.  
  373. }
  374. }
  375.  
  376. void exercise1Display(void * params) {
  377. char str[100]; // buffer for messages to draw to display
  378. struct buttons buttonStatus; // joystick queue input buffer
  379. const unsigned char next_state_signal = NEXT_TASK;
  380.  
  381.  
  382. int press_count_A = 0, press_count_B = 0, press_count_C = 0, press_count_D = 0;
  383. int sliding_text_position = 0, sliding_text_direction = 1;
  384.  
  385. int sliderCounter(){
  386. int m = sliding_text_position;
  387. if(sliding_text_position == 0){
  388. sliding_text_position++;
  389. sliding_text_direction = 0;
  390. return m;
  391. }
  392. if (sliding_text_position < 200){
  393. if(!sliding_text_direction){
  394. sliding_text_position++;
  395. return m;
  396. }
  397. else{
  398. sliding_text_position--;
  399. return m;
  400. }
  401. }
  402. if (sliding_text_position == 200){
  403. sliding_text_position--;
  404. sliding_text_direction = 1;
  405. return m;
  406. }
  407. }
  408.  
  409.  
  410. int circle_angle = 0;
  411. int circleRotate(){
  412. int internal_angle = circle_angle;
  413. if(circle_angle < 360){
  414. circle_angle++;
  415. return internal_angle;
  416. }
  417. if(circle_angle == 360){
  418. circle_angle = 0;
  419. return internal_angle;
  420. }
  421. }
  422.  
  423. // Start endless loop
  424. while (1) {
  425.  
  426. if (xSemaphoreTake(DrawReady, portMAX_DELAY) == pdTRUE) { // Block until screen is ready
  427. while (xQueueReceive(ButtonQueue, &buttonStatus, 0) == pdTRUE);
  428.  
  429. // State machine input
  430. if (buttonCount(BUT_E)){
  431. xQueueSend(StateQueue, &next_state_signal, 100);
  432. }
  433.  
  434. // Clear background
  435. gdispClear(White);
  436.  
  437. // Generate string with current joystick values
  438. sprintf(str, "X from left: %5d|Y from top: %5d|VBat: %5d",
  439. buttonStatus.joystick.x, buttonStatus.joystick.y,
  440. ADC_GetConversionValue(ESPL_ADC_VBat));
  441. // Print string of joystick values
  442. gdispDrawString(SCREEN_SLIDE_X, SCREEN_SLIDE_Y, str, font1, Black);
  443.  
  444.  
  445. // Count number of button presses
  446. if(buttonStatus.A){
  447. press_count_A++;
  448. }
  449. if(buttonStatus.B){
  450. press_count_B++;
  451. }
  452. if(buttonStatus.C){
  453. press_count_C++;
  454. }
  455. if(buttonStatus.D){
  456. press_count_D++;
  457. }
  458. if(buttonStatus.K){
  459. press_count_A = 0;
  460. press_count_B = 0;
  461. press_count_C = 0;
  462. press_count_D = 0;
  463. }
  464.  
  465. // Generate string with number of times buttons have been pressed
  466. sprintf(str, "A: %d|B: %d|C: %d|D: %d", press_count_A,
  467. press_count_B, press_count_C, press_count_D);
  468.  
  469. // Print string of number of presses
  470. gdispDrawString(SCREEN_SLIDE_X, 11 + SCREEN_SLIDE_Y, str, font1, Black);
  471.  
  472.  
  473. //Draw small teal triangle in the center
  474. gdispFillConvexPoly(DISPLAY_SIZE_X / 2 + SCREEN_SLIDE_X,
  475. DISPLAY_SIZE_Y / 2 + SCREEN_SLIDE_Y, triangle, NUM_POINTS, Teal);
  476.  
  477. //Draw rotating orange square
  478. gdispFillArea(ROTATING_SQUARE_X + SCREEN_SLIDE_X, ROTATING_SQUARE_Y + SCREEN_SLIDE_Y, 12, 12, Orange);
  479.  
  480. //Draw rotating olive circle
  481. gdispFillCircle(ROTATING_CIRCLE_X + SCREEN_SLIDE_X, ROTATING_CIRCLE_Y + SCREEN_SLIDE_Y, 12, Olive);
  482.  
  483.  
  484. //displaying sliding text above figures
  485. char str_slide[1][70] = { "ESPL LAB"};
  486. for (unsigned char j = 0; j < 1; j++)
  487. gdispDrawString(SLIDING_TEXT_X(str_slide[j]) + SCREEN_SLIDE_X, SLIDING_TEXT_Y(j) + SCREEN_SLIDE_Y, str_slide[j],
  488. font1, Black);
  489. //displaying text below figures
  490. char str[1][70] = {"EXERCISE 2"};
  491. for (unsigned char i = 0; i < 1; i++)
  492. gdispDrawString(TEXT_X(str[i]) + SCREEN_SLIDE_X, TEXT_Y(i) + SCREEN_SLIDE_Y, str[i],
  493. font1, Black);
  494. }
  495. }
  496. }
  497.  
  498. void exercise2Display(void * params) {
  499. char str[1][70] = { "EXERCISE 3" };
  500. char print_string[1][70] = {{0}};
  501. struct buttons buttonStatus; // joystick queue input buffer
  502. struct buttons joystickStatusUART;
  503. const unsigned char next_state_signal = NEXT_TASK;
  504. unsigned int button_A_count = 0;
  505. unsigned int button_B_count = 0;
  506. uint8_t fps_num;
  507. int counter324 = 0;
  508. int task_switch = 1;
  509. int blink2Hz_toggle = 1;
  510. int blink1Hz_toggle = 1;
  511. struct buttons buttons_UART;
  512.  
  513. while (1) {
  514. if (xSemaphoreTake(DrawReady, portMAX_DELAY) == pdTRUE) {
  515. while (xQueueReceive(ButtonQueue, &buttonStatus, 0) == pdTRUE)
  516. ;
  517. while (xQueueReceive(UARTjoystickQueue, &joystickStatusUART, 0) == pdTRUE)
  518. ;
  519.  
  520. // State machine input
  521. if (buttonCount(BUT_E)){
  522. xQueueSend(StateQueue, &next_state_signal, 100);
  523. }
  524.  
  525. // Clear background
  526. gdispClear(White);
  527.  
  528. //Exercise 3.2.2
  529. if(xSemaphoreTake(BlinkCircle2Hz, 0) == pdTRUE){
  530. blink2Hz_toggle = !blink2Hz_toggle;
  531. }
  532. if(xSemaphoreTake(BlinkCircle1Hz, 0) == pdTRUE){
  533. blink1Hz_toggle = !blink1Hz_toggle;
  534. }
  535.  
  536. if(blink2Hz_toggle == 1){
  537. gdispFillCircle(DISPLAY_SIZE_X / 2 - 20 + SCREEN_SLIDE_X_UART,
  538. DISPLAY_SIZE_Y / 2 + SCREEN_SLIDE_Y_UART, 12, Blue);
  539. }
  540. if(blink1Hz_toggle == 1){
  541. gdispFillCircle(DISPLAY_SIZE_X / 2 + 20 + SCREEN_SLIDE_X_UART,
  542. DISPLAY_SIZE_Y / 2 + SCREEN_SLIDE_Y_UART, 12, Red);
  543. }
  544.  
  545. //Exercise 3.2.3
  546. if(xSemaphoreTake(ButtonAPressed, 0) == pdTRUE){
  547. button_A_count++;
  548. }
  549. if(xSemaphoreTake(ButtonBPressed, 0) == pdTRUE){
  550. button_B_count++;
  551. }
  552. if(xSemaphoreTake(ResetTimer, 0) == pdTRUE){
  553. button_A_count = 0;
  554. button_B_count = 0;
  555. }
  556. sprintf(print_string, "Times pressed: A: %d|B: %d", button_A_count, button_B_count);
  557. for (unsigned char i = 0; i < 1; i++){
  558. gdispDrawString(TEXT_X(print_string[i]) + SCREEN_SLIDE_X_UART,
  559. TEXT_Y(i) - 5 + SCREEN_SLIDE_Y_UART, print_string[i], font1, Black);
  560. }
  561.  
  562. for (unsigned char i = 0; i < 1; i++)
  563. gdispDrawString(TEXT_X(str[i]) + SCREEN_SLIDE_X_UART,
  564. TEXT_Y(i) + 15 + SCREEN_SLIDE_Y_UART, str[i], font1, Black);
  565.  
  566.  
  567. // FPS from 3.2.2.6
  568. xQueueReceive(FPSQueue, &fps_num, 0);
  569. sprintf(print_string, "FPS: %d", fps_num);
  570. for (unsigned char i = 0; i < 1; i++)
  571. gdispDrawString(TEXT_X(print_string[i]) + SCREEN_SLIDE_X_UART,
  572. TEXT_Y(i) + 5 + SCREEN_SLIDE_Y_UART, print_string[i], font1, Black);
  573.  
  574. // Exercise 3.2.4
  575. if (buttonCount(BUT_C)){
  576. task_switch = !task_switch;
  577. }
  578. if(task_switch){
  579. if(xSemaphoreTake(increaseVariable_Semaphore, 0) == pdTRUE){
  580. counter324++;
  581. }
  582. }
  583. sprintf(print_string, "Second Counter: %d", counter324);
  584. for (unsigned char i = 0; i < 1; i++)
  585. gdispDrawString(TEXT_X(print_string[i]) + SCREEN_SLIDE_X_UART,
  586. TEXT_Y(i) - 15 + SCREEN_SLIDE_Y_UART, print_string[i], font1, Black);
  587.  
  588. if(xSemaphoreTake(buttonCounts.lock, portMAX_DELAY) == pdTRUE){
  589. sprintf(print_string, "From connected board: A: %d|B: %d|C: %d|D: %d",
  590. buttonCounts.buttons.A,
  591. buttonCounts.buttons.B,
  592. buttonCounts.buttons.C,
  593. buttonCounts.buttons.D);
  594. xSemaphoreGive(buttonCounts.lock);
  595. for (unsigned char i = 0; i < 1; i++)
  596. gdispDrawString(TEXT_X(print_string[i]) + SCREEN_SLIDE_X_UART,
  597. TEXT_Y(i) - 25 + SCREEN_SLIDE_Y_UART, print_string[i], font1, Black);
  598. }
  599.  
  600. }
  601. }
  602. }
  603.  
  604. void exercise3Display(void * params){
  605. struct buttons buttonStatus;
  606.  
  607. // char print[15][70] = { "Tick 1: ", "Tick 2: ", "Tick 3: ", "Tick 4: ", "Tick 5: ",
  608. // "Tick 6: ", "Tick 7: ", "Tick 8: ", "Tick 9: ", "Tick 10: ",
  609. // "Tick 11: ", "Tick 12: ", "Tick 13: ", "Tick 14: ", "Tick 15: " };
  610.  
  611. const unsigned char next_state_signal = PREV_TASK;
  612. TickType_t xLastTickTime;
  613. xLastTickTime = xTaskGetTickCount();
  614. while (1) {
  615. if (xSemaphoreTake(DrawReady, portMAX_DELAY) == pdTRUE) { // Block until screen is ready
  616. while(xQueueReceive(ButtonQueue, &buttonStatus,0) == pdTRUE);
  617. if (buttonCount(BUT_E)){
  618. xQueueSend(StateQueue, &next_state_signal, 100);
  619. }
  620. // Clear background
  621. gdispClear(White);
  622.  
  623. // if(xSemaphoreTake(ex4Output.lock, portMAX_DELAY) == pdTRUE){
  624. // for(int i = 0; i < 15; i++){
  625. // strcat(print[i], ex4Output.ex4output.print[i]);
  626. // }
  627. // xSemaphoreGive(ex4Output.lock);
  628. // }
  629. for(unsigned char i = 0; i < 15; i++){
  630. gdispDrawString(TEXT_X(print[i]) - 20, TEXT_Y(i) - 150, print[i], font1, Black);
  631. }
  632.  
  633. }
  634. }
  635. }
  636.  
  637. //Exercise 3.2.2
  638. void circleBlinkDynamic(void *params){
  639. TickType_t xLastTickTime;
  640. xLastTickTime = xTaskGetTickCount();
  641. const TickType_t delayPeriod = 500;
  642. while (1) {
  643. xSemaphoreGive(BlinkCircle2Hz);
  644. vTaskDelayUntil(&xLastTickTime, delayPeriod);
  645. }
  646. }
  647.  
  648. void circleBlinkStatic(void *params){
  649. TickType_t xLastTickTime;
  650. xLastTickTime = xTaskGetTickCount();
  651. const TickType_t delayPeriod = 1000;
  652. while (1) {
  653. xSemaphoreGive(BlinkCircle1Hz);
  654. vTaskDelayUntil(&xLastTickTime, delayPeriod);
  655. }
  656. }
  657.  
  658. //Exercise 3.2.3
  659. void timesButtonAPressed(void * params){
  660. TickType_t xLastTickTime;
  661. xLastTickTime = xTaskGetTickCount();
  662. const TickType_t pollingRate = 20;
  663. //struct buttons buttonStatus;
  664. while(1){
  665. //xQueueReceive(ButtonQueue, &buttonStatus, 0);
  666. if(buttonCount(BUT_A)){
  667. xSemaphoreGive(ButtonAPressed);
  668. }
  669. vTaskDelayUntil(&xLastTickTime, pollingRate);
  670. }
  671. }
  672.  
  673. void timesButtonBPressed(void * params){
  674. TickType_t xLastTickTime;
  675. xLastTickTime = xTaskGetTickCount();
  676. const TickType_t pollingRate = 20;
  677. //struct buttons buttonStatus;
  678. while(1){
  679. //xQueueReceive(ButtonQueue, &buttonStatus, 0);
  680. if(buttonCount(BUT_B)){
  681. xSemaphoreGive(ButtonBPressed);
  682. }
  683. vTaskDelayUntil(&xLastTickTime, pollingRate);
  684. }
  685. }
  686.  
  687. void resetTimer(void * params){
  688. TickType_t xLastTickTime;
  689. xLastTickTime = xTaskGetTickCount();
  690. const TickType_t delayTime = RESET_TIMER_EX_3 * 1000;
  691. while(1){
  692. xSemaphoreGive(ResetTimer);
  693. vTaskDelayUntil(&xLastTickTime, delayTime);
  694. }
  695. }
  696.  
  697. // Exercise 3.2.4
  698. void increaseVariable(void * params){
  699. TickType_t xLastTickTime;
  700. xLastTickTime = xTaskGetTickCount();
  701. const TickType_t delayTime = 1000; // 1 second
  702. while(1){
  703. xSemaphoreGive(increaseVariable_Semaphore);
  704. vTaskDelayUntil(&xLastTickTime, delayTime);
  705. }
  706. }
  707.  
  708. // Exercise 3.2.5
  709. void sendPositionUART(void * params){
  710. TickType_t xLastTickTime;
  711. xLastTickTime = xTaskGetTickCount();
  712. const TickType_t pollingRate = 20;
  713. struct buttons buttonStatus;
  714. struct coord prev_joystick, abs_joystick;
  715. const int joystick_threshold = 20;
  716. unsigned char send=0;
  717. while(1){
  718. /*
  719. * Implementation with buttton queue and checkButtons -- causes data corruption
  720. * */
  721. xQueueReceive(ButtonQueue, &buttonStatus, 100);
  722. const uint8_t checksum = buttonStatus.joystick.x ^ buttonStatus.joystick.y
  723. ^ buttonStatus.A ^ buttonStatus.B ^ buttonStatus.C ^ buttonStatus.D
  724. ^ buttonStatus.E ^ buttonStatus.K;
  725.  
  726. if(buttonStatus.A)
  727. send = 1;
  728. if(buttonStatus.B)
  729. send = 1;
  730. if(buttonStatus.C)
  731. send = 1;
  732. if(buttonStatus.D)
  733. send = 1;
  734. if(buttonStatus.K)
  735. send = 1;
  736. abs_joystick.x = abs(buttonStatus.joystick.x - prev_joystick.x);
  737. abs_joystick.y = abs(buttonStatus.joystick.y - prev_joystick.y);
  738. if(abs_joystick.x > joystick_threshold)
  739. send = 1;
  740. if(abs_joystick.y > joystick_threshold)
  741. send = 1;
  742.  
  743. if(send){
  744. UART_SendData(startByte);
  745. UART_SendData(buttonStatus.joystick.x);
  746. UART_SendData(buttonStatus.joystick.y);
  747. UART_SendData(buttonStatus.A);
  748. UART_SendData(buttonStatus.B);
  749. UART_SendData(buttonStatus.C);
  750. UART_SendData(buttonStatus.D);
  751. UART_SendData(buttonStatus.E);
  752. UART_SendData(buttonStatus.K);
  753. UART_SendData(checksum);
  754. UART_SendData(stopByte);
  755. memcpy(&prev_joystick, &buttonStatus.joystick, sizeof(struct coord));
  756. }
  757.  
  758.  
  759. send=0;
  760. vTaskDelayUntil(&xLastTickTime, pollingRate);
  761. }
  762. }
  763.  
  764. void receivePositionUART(void * params){
  765. TickType_t xLastTickTime;
  766. xLastTickTime = xTaskGetTickCount();
  767. const TickType_t pollingRate = 20;
  768. char input;
  769. uint8_t pos = 0;
  770. char checksum;
  771. char buffer[11] = { { 0 } };
  772. struct buttons buttonStatus_internal = { { 0 } };
  773. while (1) {
  774. // wait for data in queue
  775. xQueueReceive(ESPL_RxQueue, &input, portMAX_DELAY);
  776.  
  777. // decode package by buffer position
  778. switch (pos) {
  779. // start byte
  780. case 0:
  781. if (input != startByte)
  782. break;
  783. buffer[pos] = input;
  784. pos++;
  785. break;
  786. case 1:
  787. case 2:
  788. case 3:
  789. case 4:
  790. case 5:
  791. case 6:
  792. case 7:
  793. case 8:
  794. case 9:
  795. // read received data in buffer
  796. buffer[pos] = input;
  797. pos++;
  798. break;
  799. case 10:
  800. // Check if package is corrupted
  801. checksum = buffer[1] ^ buffer[2] ^ buffer[3] ^ buffer[4] ^ buffer[5]
  802. ^ buffer[6] ^ buffer[7] ^ buffer[8];
  803. if (input == stopByte && checksum == buffer[9]) {
  804. buttonStatus_internal.joystick.x = buffer[1];
  805. buttonStatus_internal.joystick.y = buffer[2];
  806. buttonStatus_internal.A = buffer[3];
  807. buttonStatus_internal.B = buffer[4];
  808. buttonStatus_internal.C = buffer[5];
  809. buttonStatus_internal.D = buffer[6];
  810. buttonStatus_internal.E = buffer[7];
  811. buttonStatus_internal.K = buffer[8];
  812.  
  813. xSemaphoreTake(buttonCounts.lock, portMAX_DELAY);
  814. if(buttonStatus_internal.A)
  815. buttonCounts.buttons.A++;
  816. if(buttonStatus_internal.B)
  817. buttonCounts.buttons.B++;
  818. if(buttonStatus_internal.C)
  819. buttonCounts.buttons.C++;
  820. if(buttonStatus_internal.D)
  821. buttonCounts.buttons.D++;
  822. if(buttonStatus_internal.joystick.x){
  823. xQueueSend(UARTjoystickQueue, &buttonStatus_internal.joystick.x, portMAX_DELAY);
  824. }
  825. if(buttonStatus_internal.joystick.y){
  826. xQueueSend(UARTjoystickQueue, &buttonStatus_internal.joystick.y, portMAX_DELAY);
  827. }
  828. xSemaphoreGive(buttonCounts.lock);
  829. }
  830. pos = 0;
  831. break;
  832. }
  833. vTaskDelayUntil(&xLastTickTime, pollingRate);
  834. }
  835. }
  836.  
  837. // Exercise 3.3
  838. void ex4task1(void * params){
  839. TickType_t xLastTickTime;
  840. xLastTickTime = xTaskGetTickCount();
  841. TickType_t first_exec;
  842. first_exec = xLastTickTime;
  843. const TickType_t delay = 1;
  844. while(1){
  845. if((xTaskGetTickCount() - first_exec) < 15){
  846. // xSemaphoreTake(ex4Output.lock, portMAX_DELAY);
  847. // strcat(ex4Output.ex4output.print[xTaskGetTickCount() - first_exec], "1 ");
  848. // xSemaphoreGive(ex4Output.lock);
  849. strcat(print[xTaskGetTickCount() - first_exec], "1 ");
  850. }
  851. vTaskDelayUntil(&xLastTickTime, delay);
  852. }
  853. }
  854. void ex4task2(void * params){
  855. TickType_t xLastTickTime;
  856. xLastTickTime = xTaskGetTickCount();
  857. TickType_t first_exec;
  858. first_exec = xLastTickTime;
  859. const TickType_t delay = 2;
  860. struct ex4output output;
  861. const char str_out = "2";
  862. while(1){
  863. if((xTaskGetTickCount() - first_exec) < 15){
  864. // xSemaphoreTake(ex4Output.lock, portMAX_DELAY);
  865. // strcat(ex4Output.ex4output.print[xTaskGetTickCount() - first_exec], "2 ");
  866. // xSemaphoreGive(ex4Output.lock);
  867. strcat(print[xTaskGetTickCount() - first_exec], "2 ");
  868. vTaskResume(ex4task3Handle);
  869. }
  870. vTaskDelayUntil(&xLastTickTime, delay);
  871. }
  872. }
  873. void ex4task3(void * params){
  874. TickType_t xLastTickTime;
  875. xLastTickTime = xTaskGetTickCount();
  876. TickType_t first_exec;
  877. first_exec = xLastTickTime;
  878. const TickType_t delay = 1;
  879. struct ex4output output;
  880. const char str_out = "3";
  881. while(1){
  882. if((xTaskGetTickCount() - first_exec) < 15){
  883. strcat(print[xTaskGetTickCount() - first_exec], "3 ");
  884. // xSemaphoreTake(ex4Output.lock, portMAX_DELAY);
  885. // strcat(ex4Output.ex4output.print[xTaskGetTickCount() - first_exec], "3 ");
  886. // xSemaphoreGive(ex4Output.lock);
  887. }
  888. vTaskDelayUntil(&xLastTickTime, delay);
  889. }
  890. }
  891. void ex4task4(void * params){
  892. TickType_t xLastTickTime;
  893. xLastTickTime = xTaskGetTickCount();
  894. TickType_t first_exec;
  895. first_exec = xLastTickTime;
  896. const TickType_t delay = 4;
  897. struct ex4output output;
  898. const char str_out = "4";
  899. while(1){
  900. if((xTaskGetTickCount() - first_exec) < 15){
  901. strcat(print[xTaskGetTickCount() - first_exec], "4 ");
  902. // xSemaphoreTake(ex4Output.lock, portMAX_DELAY);
  903. // strcat(ex4Output.ex4output.print[xTaskGetTickCount() - first_exec], "4 ");
  904. // xSemaphoreGive(ex4Output.lock);
  905. }
  906. vTaskDelayUntil(&xLastTickTime, delay);
  907. }
  908. }
  909.  
  910. void checkButtons(void * params) {
  911. TickType_t xLastWakeTime;
  912. xLastWakeTime = xTaskGetTickCount();
  913. struct buttons buttonStatus = { { 0 } };
  914. const TickType_t PollingRate = 20;
  915.  
  916. while (TRUE) {
  917. // Remember last joystick values
  918. buttonStatus.joystick.x = (uint8_t)(
  919. ADC_GetConversionValue(ESPL_ADC_Joystick_2) >> 4);
  920. buttonStatus.joystick.y = (uint8_t) 255
  921. - (ADC_GetConversionValue(ESPL_ADC_Joystick_1) >> 4);
  922. buttonStatus.joystick_direct.x = (uint8_t)(
  923. ADC_GetConversionValue(ESPL_ADC_Joystick_2) >> 4);
  924. buttonStatus.joystick_direct.y = (uint8_t) 255
  925. - (ADC_GetConversionValue(ESPL_ADC_Joystick_1) >> 4);
  926. buttonStatus.A = buttonCount(BUT_A);
  927. buttonStatus.B = buttonCount(BUT_B);
  928. buttonStatus.C = buttonCount(BUT_C);
  929. buttonStatus.D = buttonCount(BUT_D);
  930. buttonStatus.E = 0;
  931. buttonStatus.K = buttonCount(BUT_K);
  932. xQueueSend(ButtonQueue, &buttonStatus, 0);
  933. vTaskDelayUntil(&xLastWakeTime, PollingRate);
  934. }
  935. }
  936.  
  937. void vApplicationIdleHook() {
  938. while (TRUE) {
  939. };
  940. }
  941.  
  942. void vApplicationMallocFailedHook() {
  943. while (TRUE) {
  944. };
  945. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement