Exemple #1
0
void CheckBattery(void)
{
  BatterySenseCycle();

  if (!ClipOn())
  {
    ChargeStatus = CHARGE_STATUS_OFF;
    CheckBatteryLow();
  }
  else if (ChargeEnable) ChargingControl();
  
  PrintDecimal(Read(BATTERY)); PrintString(CR);
}
/*! Handle the messages routed to the background task */
static void BackgroundMessageHandler(tMessage* pMsg)
{
  tMessage OutgoingMsg;

  switch(pMsg->Type)
  {
  case SetCallbackTimerMsg:
    SetCallbackTimerHandler(pMsg);
    break;

  case GetDeviceType:
    SetupMessageAndAllocateBuffer(&OutgoingMsg,
                                  GetDeviceTypeResponse,
                                  NO_MSG_OPTIONS);

    OutgoingMsg.pBuffer[0] = BOARD_TYPE;
    OutgoingMsg.Length = 1;
    RouteMsg(&OutgoingMsg);
    break;

  case AdvanceWatchHandsMsg:
    AdvanceWatchHandsHandler(pMsg);
    break;

  case SetVibrateMode:
    SetVibrateModeHandler(pMsg);
    break;

  case SetRealTimeClock:
    halRtcSet((tRtcHostMsgPayload*)pMsg->pBuffer);

#ifdef DIGITAL
    SetupMessage(&OutgoingMsg,IdleUpdate,NO_MSG_OPTIONS);
    RouteMsg(&OutgoingMsg);
#endif
    break;

  case GetRealTimeClock:
    SetupMessageAndAllocateBuffer(&OutgoingMsg,
                                  GetRealTimeClockResponse,
                                  NO_MSG_OPTIONS);

    halRtcGet((tRtcHostMsgPayload*)OutgoingMsg.pBuffer);
    OutgoingMsg.Length = sizeof(tRtcHostMsgPayload);
    RouteMsg(&OutgoingMsg);
    break;

  case EnableButtonMsg:
    EnableButtonMsgHandler(pMsg);
    break;

  case DisableButtonMsg:
    DisableButtonMsgHandler(pMsg);
    break;

  case ReadButtonConfigMsg:
    ReadButtonConfigHandler(pMsg);
    break;

  case BatteryChargeControl:

#ifdef DIGITAL
    /* update the screen if there has been a change in charging status */
    if ( BatteryChargingControl() )
    {
      SetupMessage(&OutgoingMsg, IdleUpdate, NO_MSG_OPTIONS);
      RouteMsg(&OutgoingMsg);
    }
#endif

    BatterySenseCycle();
    LowBatteryMonitor();

#ifdef TASK_DEBUG
    UTL_FreeRtosTaskStackCheck();
#endif

#if 0
    LightSenseCycle();
#endif

    break;

  case LedChange:
    LedChangeHandler(pMsg);
    break;

  case BatteryConfigMsg:
    SetBatteryLevels(pMsg->pBuffer);
    break;

  case ReadBatteryVoltageMsg:
    ReadBatteryVoltageHandler();
    break;

  case ReadLightSensorMsg:
    ReadLightSensorHandler();
    break;

  case SoftwareResetMsg:
    SoftwareResetHandler(pMsg);
    break;

  case NvalOperationMsg:
    NvalOperationHandler(pMsg);
    break;

  case GeneralPurposeWatchMsg:
    /* insert handler here */
    break;

  case ButtonStateMsg:
    ButtonStateHandler();
    break;

  /*
   * Accelerometer Messages
   */
  case AccelerometerEnableMsg:
    AccelerometerEnable();
    break;

  case AccelerometerDisableMsg:
    AccelerometerDisable();
    break;

  case AccelerometerSendDataMsg:
    AccelerometerSendDataHandler();
    break;

  case AccelerometerAccessMsg:
    AccelerometerAccessHandler(pMsg);
    break;

  case AccelerometerSetupMsg:
    AccelerometerSetupHandler(pMsg);
    break;

  /*
   *
   */
  case RateTestMsg:
    SetupMessageAndAllocateBuffer(&OutgoingMsg,DiagnosticLoopback,NO_MSG_OPTIONS);
    /* don't care what data is */
    OutgoingMsg.Length = 10;
    RouteMsg(&OutgoingMsg);
    break;

  /*
   *
   */
  default:
    PrintStringAndHex("<<Unhandled Message>> in Background Task: Type 0x", pMsg->Type);
    break;
  }

}
/*! Function to implement the BackgroundTask loop
 *
 * \param pvParameters not used
 *
 */
static void BackgroundTask(void *pvParameters)
{
  if ( QueueHandles[BACKGROUND_QINDEX] == 0 )
  {
    PrintString("Background Queue not created!\r\n");
  }

  PrintString(SPP_DEVICE_NAME);
  PrintString2("\r\nSoftware Version ",VERSION_STRING);
  PrintString("\r\n\r\n");

  InitializeRstNmiConfiguration();

  /*
   * check on the battery
   */
  ConfigureBatteryPins();
  BatteryChargingControl();
  BatterySenseCycle();

  /*
   * now set up a timer that will cause the battery to be checked at
   * a regular frequency.
   */
  BatteryMonitorTimerId = AllocateOneSecondTimer();

  InitializeBatteryMonitorInterval();

  SetupOneSecondTimer(BatteryMonitorTimerId,
                      nvBatteryMonitorIntervalInSeconds,
                      REPEAT_FOREVER,
                      BACKGROUND_QINDEX,
                      BatteryChargeControl,
                      NO_MSG_OPTIONS);

  StartOneSecondTimer(BatteryMonitorTimerId);

  /*
   * Setup a timer to use with the LED for the LCD.
   */
  LedTimerId = AllocateOneSecondTimer();

  SetupOneSecondTimer(LedTimerId,
                      ONE_SECOND*3,
                      NO_REPEAT,
                      BACKGROUND_QINDEX,
                      LedChange,
                      LED_OFF_OPTION);

  // Allocate a timer for wake-up iOS background BLE app
  CallbackTimerId = AllocateOneSecondTimer();

  /****************************************************************************/

#ifdef RAM_TEST

  RamTestTimerId = AllocateOneSecondTimer();

  SetupOneSecondTimer(RamTestTimerId,
                      ONE_SECOND*20,
                      NO_REPEAT,
                      DISPLAY_QINDEX,
                      RamTestMsg,
                      NO_MSG_OPTIONS);

  StartOneSecondTimer(RamTestTimerId);

#endif

  /****************************************************************************/

  InitializeAccelerometer();

#ifdef ACCELEROMETER_DEBUG

  SetupMessageAndAllocateBuffer(&BackgroundMsg,
                                AccelerometerSetupMsg,
                                ACCELEROMETER_SETUP_INTERRUPT_CONTROL_OPTION);

  BackgroundMsg.pBuffer[0] = INTERRUPT_CONTROL_ENABLE_INTERRUPT;
  BackgroundMsg.Length = 1;
  RouteMsg(&BackgroundMsg);

  /* don't call AccelerometerEnable() directly use a message*/
  SetupMessage(&BackgroundMsg,AccelerometerEnableMsg,NO_MSG_OPTIONS);
  RouteMsg(&BackgroundMsg);

#endif

  /****************************************************************************/

#ifdef RATE_TEST

  StartCrystalTimer(CRYSTAL_TIMER_ID3,RateTestCallback,RATE_TEST_INTERVAL_MS);

#endif

  /****************************************************************************/

  for(;;)
  {
    if( pdTRUE == xQueueReceive(QueueHandles[BACKGROUND_QINDEX],
                                &BackgroundMsg, portMAX_DELAY ) )
    {
      PrintMessageType(&BackgroundMsg);

      BackgroundMessageHandler(&BackgroundMsg);

      SendToFreeQueue(&BackgroundMsg);

      CheckStackUsage(xBkgTaskHandle,"Background Task");

      CheckQueueUsage(QueueHandles[BACKGROUND_QINDEX]);

    }

  }

}