Ejemplo n.º 1
0
unsigned long SmSnDevice::beginLoop() {
    if (inputEnabled) {
        if (osc.receiveOSCBundle(*bundleIn)) {
            handleOSCBundleInternal(*bundleIn);
            bundleIn->empty();
            delete bundleIn;
            bundleIn = new OSCBundle();
        }
    }

    // no need for micros() here, and it's simpler to avoid overflow
    unsigned long now = millis();

    checkLedStatus(now);
#ifdef BOUNTIFUL_RAM
    checkConnection(now);
#endif
    onBeginLoop(now);

// TODO: never applies
#ifdef HEARTBEAT_MS
    if (now - lastHeartbeat > HEARTBEAT_MS) {
        sendHeartbeatMessage(now);
        lastHeartbeat = now;
    }
#endif // HEARTBEAT_MS

    // periodically adjust the loop time used in band-pass filtering (among potentially other applications)
    if (++loopCount >= loopThreshold) {
        loopTime = (now - loopStartTime)/1000.0/loopThreshold;

        onLoopTimeUpdated(loopTime);

        if (loopTimeHandler) {
            loopTimeHandler(loopTime);
        }

        loopCount = 0;
        loopStartTime = now;
    }

    return now;
}
Ejemplo n.º 2
0
uint16 CurrentDetectionT1_ProcessEvent(uint8 task_id, uint16 events)
{
   static uint16 timerCount = 0;
   static uint32 heartBitFailNum = 0;
   //uint8 deltaInfo[] = "delta = 1\n";

   afIncomingMSGPacket_t* MSGpkt;

   (void)task_id;  // Intentionally unreferenced parameter

   if (events & SYS_EVENT_MSG)
   {
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(CurrentDetectionT1_TaskID);
      while (MSGpkt)
      {
         switch (MSGpkt->hdr.event)
         {
            case CMD_SERIAL_MSG:
               CurrentDetectionT1_SerialCMD((mtOSALSerialData_t *)MSGpkt);
               break;

            case AF_INCOMING_MSG_CMD:
               CurrentDetectionT1_RfCMD(MSGpkt->cmd.DataLength, MSGpkt->cmd.Data);
               HalLedBlink(HAL_LED_2, 2, 20, 50);
               break;

            // Received whenever the device changes state in the network
            case ZDO_STATE_CHANGE:
               CurrentDetectionT1_NwkState = (devStates_t)(MSGpkt->hdr.status);
               if (CurrentDetectionT1_NwkState == DEV_END_DEVICE)
               {
                  HalLedSet(HAL_LED_2,  HAL_LED_MODE_ON);
                  HalUARTWrite(0, EndDeviceStatus, strlen((char *)EndDeviceStatus));
                  inNetwork = true;
               }
               break;

            default:
               break;
         }

         // Release the memory
         osal_msg_deallocate((uint8 *)MSGpkt);

         // Next - if one is available
         MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(CurrentDetectionT1_TaskID);
      }

      // return unprocessed events
      return (events ^ SYS_EVENT_MSG);
   }

   // Send a message out - This event is generated by a timer
   //  (setup in CurrentDetectionT1_Init()).
   if (events & DTCT_HEARTBEAT_MSG_EVT)
   {
      uint8 sendToG1Data[HEART_BIT_MSG_LEN];
      sendToG1Data[HEART_BIT_MSG_LEN - 1] = 0;

      memcpy(sendToG1Data, serialNumber, SN_LEN);

      reset_osal_SystemClock();
      if (lastNvTime_l + get_osal_SystemClock() < lastNvTime_l)
      {
         nv_write_config();
         HAL_SYSTEM_RESET();
      }

      if (CurrentDetectionT1_HandleSendRepairMessage(sendToG1Data))
      {
         CurrentDetectionT1_SampleCurrentAdcValue(true);
         osal_start_timerEx(
            CurrentDetectionT1_TaskID,
            DTCT_HEARTBEAT_MSG_EVT,
            DTCT_TIMER_MSG_TIMEOUT);

         // The repairing status, no need to send ADC value any more.
         return (events ^ DTCT_HEARTBEAT_MSG_EVT);
      }

      CurrentDetectionT1_SampleCurrentAdcValue(false);

      ++timerCount;

      if ((timerCount == (480 * heartbitRate))
          || (timerCount == (960 * heartbitRate))
          || (timerCount == (1440 * heartbitRate)))
      {
         CurrentDetectionT1_RecoverHeartBeatMessage(sendToG1Data);
      }

      // send heart beat every 1.92s.
      else if (timerCount == (HEARTBIT_TIME_UINT * heartbitRate))
      {
         CurrentDetectionT1_SetAverageCurrentAdcValue(sendToG1Data, CYCLE_NUM_IN_UINT * heartbitRate);
         SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_POWER_SUPPLY_MASK, 0);
         SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_BATTERY_CHARGING_MASK, 1);

         if (inNetwork)
         {
            // Send the Heartbeat Message
            if (!CurrentDetectionT1_SendHeartBeatMessage(sendToG1Data))
            {
               ++heartBitFailNum;
               CurrentDetectionT1_StoreHeartBeatMessage(sendToG1Data);
            }
            else
            {
               heartBitFailNum = 0;
            }
         }
         else
         {
            ++heartBitFailNum;
            if (CurrentDetectionT1_CheckDelta(sendToG1Data))
            {
               CurrentDetectionT1_StoreHeartBeatMessage(sendToG1Data);
            }
         }

         if (heartBitFailNum == FAIL_TIMES_TO_RESTART)
         {
            if (nv_mem_number())
            {
               nv_write_msg();
            }

            HAL_SYSTEM_RESET();
         }
         timerCount = 0;
      }

      osal_start_timerEx(
            CurrentDetectionT1_TaskID,
            DTCT_HEARTBEAT_MSG_EVT,
            DTCT_TIMER_MSG_TIMEOUT);

      // return unprocessed events
      return (events ^ DTCT_HEARTBEAT_MSG_EVT);
   }

   if (events & DTCT_LED_WTD_EVT)
   {
      //Feed WatchDog
      WDCTL = 0xa0;
      WDCTL = 0x50;

      if (P2_0)
      {
        HalLedSet(HAL_LED_1,  HAL_LED_MODE_ON);
        HalLedSet(HAL_LED_2,  HAL_LED_MODE_OFF);
      }
      else
      {
	checkLedStatus();
      }

      osal_start_timerEx(
        CurrentDetectionT1_TaskID,
        DTCT_LED_WTD_EVT,
        DTCT_LED_WTD_EVT_TIMEOUT);

      return (events ^ DTCT_LED_WTD_EVT);
   }
   // Discard unknown events
   return 0;
}