Example #1
0
void Schedule(void){
  static Bool solarPanelInserted=FALSE;
  xTaskHandle solarPanelHandle=NULL, keypadHandle=NULL;
  xTaskCreate( vOLEDTask, ( signed portCHAR * ) "OLED", mainOLED_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  xTaskCreate(DisplayManage, "DisplayManage", 100,&(myDisplay), 6,NULL);
  xTaskCreate(PowerManage, "PowerManage", 100,&(myPower), 1,NULL);
  xTaskCreate(ThrusterManage, "ThrusterManage", 100,&(myThruster), 2,NULL);
  xTaskCreate(ComsManage, "ComsManage", 100,&(myComs), 3,NULL);
  xTaskCreate(WarningManage, "WarningManage", 100,&(myWarning), 4,NULL);
  xTaskCreate(VComsManage, "VComsManage", 100,&(myVehComs), 5,NULL);
  xTaskCreate(Distance, "Distance", 100,&(myVehComs), 9,NULL);
  while(1){
    if(g_ulGPIOb){
      if(!solarPanelInserted){
        xTaskCreate(SolarPanel, "SolarPanel", 100,&(mySolarPanel), 7,solarPanelHandle);
        xTaskCreate(Keypad,"KeyPad",100,&(myKeypad),8,keypadHandle);
      } else{
        xTaskDelete(solarPanelHandle);
        xTaskDelete(keypadHandle);
      }
      g_ulGPIOb=0;
    }
    vTaskDelay(50);
  }
}
/**
 * Destroys a mailbox.
 *
 * @param mbox is the mailbox to be destroyed.
 */
void
sys_mbox_free(sys_mbox_t mbox)
{
  unsigned portBASE_TYPE count;

  /* There should not be any messages waiting (if there are it is a bug).  If
     any are waiting, increment the mailbox error count. */
#if RTOS_SAFERTOS
  if((xQueueMessagesWaiting(mbox->queue, &count) != pdPASS) || (count != 0)) {
#elif RTOS_FREERTOS
  if(uxQueueMessagesWaiting(mbox->queue) != 0) {
#endif /* RTOS_SAFERTOS */

#if SYS_STATS
    STATS_INC(sys.mbox.err);
#endif /* SYS_STATS */
  }

  /* Clear the queue handle. */
  mbox->queue = 0;

  /* Update the mailbox statistics. */
#if SYS_STATS
   STATS_DEC(sys.mbox.used);
#endif /* SYS_STATS */
}

/**
 * The routine for a thread.  This handles some housekeeping around the
 * applications's thread routine.
 *
 * @param arg is the index into the thread structure for this thread
 */
static void
sys_arch_thread(void *arg)
{
  u32_t i;

  /* Get this threads index. */
  i = (u32_t)arg;

  /* Call the application's thread routine. */
  threads[i].thread(threads[i].arg);

  /* Free the memory used by this thread's stack. */
  mem_free(threads[i].stackstart);

  /* Clear the stack from the thread structure. */
  threads[i].stackstart = NULL;
  threads[i].stackend = NULL;

  /* Delete this task. */
#if RTOS_SAFERTOS
  xTaskDelete(NULL);
#elif RTOS_FREERTOS
  vTaskDelete(NULL);
#endif
}
Example #3
0
/**
 * The routine for a thread.  This handles some housekeeping around the
 * applications's thread routine.
 *
 * @param arg is the index into the thread structure for this thread
 */
static void
sys_arch_thread(void *arg)
{
  u32_t i;

  /* Get this threads index. */
  i = (u32_t)arg;

  /* Call the application's thread routine. */
  threads[i].thread(threads[i].arg);

  /* Free the memory used by this thread's stack. */
  mem_free(threads[i].stackstart);

  /* Clear the stack from the thread structure. */
  threads[i].stackstart = NULL;
  threads[i].stackend = NULL;

  /* Delete this task. */
  xTaskDelete(NULL);
}
//*****************************************************************************
//
// This task manages the scurring about of a spider.
//
//*****************************************************************************
static void
SpiderTask(void *pvParameters)
{
    unsigned long ulDir, ulImage, ulTemp;
    long lX, lY, lSpider;

    //
    // Get the spider number from the parameter.
    //
    lSpider = (long)pvParameters;

    //
    // Add the current tick count to the random entropy pool.
    //
    RandomAddEntropy(xTaskGetTickCount());

    //
    // Reseed the random number generator.
    //
    RandomSeed();

    //
    // Indicate that this spider is alive.
    //
    HWREGBITW(&g_ulSpiderAlive, lSpider) = 1;

    //
    // Indicate that this spider is not dead yet.
    //
    HWREGBITW(&g_ulSpiderDead, lSpider) = 0;

    //
    // Get a local copy of the spider's starting position.
    //
    lX = g_plSpiderX[lSpider];
    lY = g_plSpiderY[lSpider];

    //
    // Choose a random starting direction for the spider.
    //
    ulDir = RandomNumber() >> 29;

    //
    // Start by displaying the first of the two spider animation images.
    //
    ulImage = 0;

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // See if this spider has been killed.
        //
        if(HWREGBITW(&g_ulSpiderDead, lSpider) == 1)
        {
            //
            // Wait for 2 seconds.
            //
            xTaskDelay((1000 / portTICK_RATE_MS) * 2);

            //
            // Clear the spider from the display.
            //
            DisplayImage(lX - (SPIDER_WIDTH / 2), lY - (SPIDER_HEIGHT / 2),
                         g_pucSpiderBlankImage);

            //
            // Indicate that this spider is not alive.
            //
            HWREGBITW(&g_ulSpiderAlive, lSpider) = 0;

            //
            // Delete the current task.  This should never return.
            //
            xTaskDelete(NULL);

            //
            // In case it does return, loop forever.
            //
            while(1)
            {
            }
        }

        //
        // Enter a critical section while the next move for the spider is
        // determined.  Having more than one spider trying to move at a time
        // (via preemption) would make the collision detection check fail.
        //
        taskENTER_CRITICAL();

        //
        // Move the spider.
        //
        lX += g_plSpiderStepX[ulDir];
        lY += g_plSpiderStepY[ulDir];

        //
        // See if the spider has cross the boundary of its area, if it has
        // collided with another spider, or if random chance says that the
        // spider should turn despite not having collided with anything.
        //
        if((lX < SPIDER_MIN_X) || (lX > SPIDER_MAX_X) ||
           (lY < SPIDER_MIN_Y) || (lY > SPIDER_MAX_Y) ||
           (SpiderCollide(lSpider, lX, lY) != -1) ||
           (RandomNumber() < 0x08000000))
        {
            //
            // Undo the previous movement of the spider.
            //
            lX -= g_plSpiderStepX[ulDir];
            lY -= g_plSpiderStepY[ulDir];

            //
            // Get a random number to determine the turn to be made.
            //
            ulTemp = RandomNumber();

            //
            // Determine how to turn the spider based on the random number.
            // Half the time the spider turns to the left and half the time it
            // turns to the right.  Of each half, it turns a quarter of a turn
            // 12.5% of the time and an eighth of a turn 87.5% of the time.
            //
            if(ulTemp < 0x10000000)
            {
                ulDir = (ulDir + 2) & 7;
            }
            else if(ulTemp < 0x80000000)
            {
                ulDir = (ulDir + 1) & 7;
            }
            else if(ulTemp < 0xf0000000)
            {
                ulDir = (ulDir - 1) & 7;
            }
            else
            {
                ulDir = (ulDir - 2) & 7;
            }
        }

        //
        // Update the position of the spider.
        //
        g_plSpiderX[lSpider] = lX;
        g_plSpiderY[lSpider] = lY;

        //
        // Exit the critical section now that the spider has been moved.
        //
        taskEXIT_CRITICAL();

        //
        // Have the display task draw the spider at the new position.  Since
        // there is a one pixel empty border around all the images, and the
        // position of the spider is incremented by only one pixel, this also
        // erases any traces of the spider in its previous position.
        //
        DisplayImage(lX - (SPIDER_WIDTH / 2), lY - (SPIDER_HEIGHT / 2),
                     g_ppucSpiderImage[(ulDir * 2) + ulImage]);

        //
        // Toggle the spider animation index.
        //
        ulImage ^= 1;

        //
        // Delay this task for an amount of time based on the direction the
        // spider is moving.
        //
        xTaskDelay(g_pulSpiderDelay[ulDir & 1]);

        //
        // Add the new tick count to the random entropy pool.
        //
        RandomAddEntropy(xTaskGetTickCount());

        //
        // Reseed the random number generator.
        //
        RandomSeed();
    }
}