Advertisement
RobertBerger

FreeRTOS PedestrianQ

Apr 27th, 2022
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. xQueueHandle xQPedestrian;
  2.  
  3. ...
  4.  
  5. main:
  6.  
  7. xQPedestrian = xQueueCreate(1, sizeof(int));
  8. if (xQPedestrian == NULL) {
  9. printf("Error creating xQPedestrian function: %s, line %d\n",
  10. __FUNCTION__, __LINE__);
  11. } else {
  12. /* Q successfully created
  13. * We disable the pedestrian functionality by default
  14. * and give xQueueSend 10 ticks to send it */
  15. if (xQueueSend( xQPedestrian, ( void * ) &DisallowPedestrianVar, ( portTickType ) 10 )
  16. != pdPASS) {
  17. printf("Error failed to send on xQPedestrian: %s, line %d\n",
  18. __FUNCTION__, __LINE__);
  19. }
  20. } /* xQPedestrian */
  21.  
  22. ...
  23.  
  24. /* shell task */
  25. void vTask1(void *pvParameters) {
  26. const char *pcTaskName = "Task 1 is running\n";
  27. char s[100];
  28. int delayVal;
  29. static int first_time = 1;
  30. int AllowPedestrianVar = 1;
  31. portBASE_TYPE QReceiveRetVal, QSendRetVal;
  32. int *pxPedestrianFlushMessage;
  33.  
  34. /* As per most tasks, this task is implemented in an infinite loop. */
  35. for (;;) {
  36. /* Delay for a random period. */
  37. delayVal = rand() % (RAND_HIGH - RAND_LOW + 1) + RAND_LOW;
  38. vTaskDelay(delayVal / portTICK_RATE_MS);
  39.  
  40. if (GetUseInputVal() == 1) {
  41. vPrintString("shell>");
  42. /* Wait for input
  43. * the brain dead library is blocking
  44. * also output, so we need to do some ugly tricks
  45. */
  46. scanf("%s", s);
  47. }
  48.  
  49. if (strcmp("c", s) == 0) {
  50. vPrintString("continue\n");
  51. }
  52. else if (strcmp("p", s) == 0) {
  53. vPrintString("let the pedestrian go\n");
  54. //pedestrian=1;
  55. QSendRetVal
  56. =xQueueSend( xQPedestrian, ( void * ) &AllowPedestrianVar, ( portTickType ) 10 );
  57. if (QSendRetVal == errQUEUE_FULL) {
  58. /* this can happen the first time, since we sent a 0 over
  59. * and the Q is only one element long
  60. * so let's try to empty the queue
  61. */
  62. printf(
  63. "Warning failed to send on xQPedestrian first time: %s, line %d\n",
  64. __FUNCTION__, __LINE__);
  65. QReceiveRetVal
  66. =xQueueReceive( xQPedestrian, &( pxPedestrianFlushMessage ), ( portTickType ) 0 );
  67. if (QReceiveRetVal != pdTRUE) {
  68. /* something more serious happened here */
  69. printf(
  70. "Error failed to receive on xQPedestrian: %s, line %d\n",
  71. __FUNCTION__, __LINE__);
  72. } else {
  73. /* retry to send after trying to empty Q */
  74. QSendRetVal
  75. =xQueueSend( xQPedestrian, ( void * ) &AllowPedestrianVar, ( portTickType ) 10 );
  76. }
  77. }
  78. if (QSendRetVal != pdTRUE) {
  79. /* something more serious happened here */
  80. printf("Error failed to send on xQPedestrian: %s, line %d\n",
  81. __FUNCTION__, __LINE__);
  82. }
  83.  
  84. } else if (strcmp("exit", s) == 0) {
  85. SetUseInputVal(0);
  86. if (first_time == 1) {
  87. vPrintString("killing shell\n");
  88. first_time = 0;
  89. }
  90. } else if (strcmp("help", s) == 0) {
  91. vPrintString("help:\n");
  92. vPrintString(" c to continue\n");
  93. vPrintString(" exit to kill the shell\n");
  94. vPrintString(" p let the pedestrian go\n");
  95. } else {
  96. vPrintString("unknown command\n");
  97. }
  98. }
  99. }
  100. -----
  101.  
  102. #if 1
  103. /* don't block on message */
  104. ...
  105.  
  106. ----
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement