// Return the number of milliseconds to the start of the next frame.
static uint32_t irGetDelayToNextFrameInMs(uint32_t intervalInMs)
{
  uint32_t now;
  uint32_t elapsed;

  now = halCommonGetInt32uMillisecondTick();
  elapsed = elapsedTimeInt32u(irStartTimeInMs, now);
  return (elapsed >= intervalInMs) ? 1 : (intervalInMs - elapsed);
}
Esempio n. 2
0
void emberAfPluginHeartbeatTickCallback(void)
{
  static uint32_t lastMs = 0;
  uint32_t nowMs = halCommonGetInt32uMillisecondTick();
  if (EMBER_AF_PLUGIN_HEARTBEAT_PERIOD_QS * MILLISECOND_TICKS_PER_QUARTERSECOND
      < elapsedTimeInt32u(lastMs, nowMs)) {
    halToggleLed(BOARD_HEARTBEAT_LED);
    lastMs = nowMs;
  }
}
static void emRestartMessageTimer()
{
  uint32_t smallestTimeoutIndex = SLEEPY_MSG_QUEUE_NUM_ENTRIES;
  uint32_t smallestTimeoutMSec = 0xFFFFFFFF;
  uint32_t timeNowMs;
  uint32_t remainingMs;
  uint32_t delayQs;
  uint8_t x;

  timeNowMs = halCommonGetInt32uMillisecondTick();
   
  for( x=0; x<SLEEPY_MSG_QUEUE_NUM_ENTRIES; x++ )
  {
    if( SleepyMessageQueue[x].status == SLEEPY_MSG_QUEUE_STATUS_USED )
    {
      if( timeGTorEqualInt32u( timeNowMs, SleepyMessageQueue[x].timeoutMSec ) ){
        // Timeout already expired - break out of loop - process immediately.
        smallestTimeoutIndex = x;
        smallestTimeoutMSec = 0;
        break;
      }
      else{
        remainingMs = elapsedTimeInt32u( timeNowMs, SleepyMessageQueue[x].timeoutMSec );
        if( remainingMs < smallestTimeoutMSec ){
          smallestTimeoutMSec = remainingMs;
          smallestTimeoutIndex = x;
        }
      }
    }
  }
  // Now know the smallest timeout index, and the smallest timeout value.
  if( smallestTimeoutIndex < SLEEPY_MSG_QUEUE_NUM_ENTRIES )
  {
    // Run the actual timer as a QS timer since that allows us to delay longer
    // than a u16 MS timer would.
    delayQs = smallestTimeoutMSec >> 8;
    delayQs++;    // Round up to the next quarter second tick.
    if( delayQs > MAX_DELAY_QS ){
      delayQs = MAX_DELAY_QS;
    }

    emberEventControlSetDelayQS( msgTimeoutEvent, delayQs );
    emberAfAppPrintln("Restarting sleepy message timer for %d Qsec.", delayQs );
  }
Esempio n. 4
0
// Tunnel event handler used to retry previously attempted tunnel creations
void emberAfPluginCommsHubFunctionTunnelEventHandler(void)
{
  uint8_t tunnelIndex;
  uint32_t timeNowMs;
  uint32_t nearestEventTimeoutDelayMs = UINT32_MAX;
  uint32_t currentTunnelTimeoutMs = 0;

  emberEventControlSetInactive(emberAfPluginCommsHubFunctionTunnelEventControl);
  timeNowMs = halCommonGetInt32uMillisecondTick();

  emberAfPluginCommsHubFunctionPrintln("CHF: emberAfPluginCommsHubFunctionTunnelEventHandler");

  // If we're no longer waiting for a tunnel to come up then find which tunnel
  // (or tunnels) are ready for another attempt at tunnel creation
  if (responsePendingIndex == EM_AF_PLUGIN_COMMS_HUB_FUNCTION_NULL_TUNNEL_INDEX) {
    for (tunnelIndex=0; tunnelIndex<EMBER_AF_PLUGIN_COMMS_HUB_FUNCTION_TUNNEL_LIMIT; tunnelIndex++) {
      if (tunnels[tunnelIndex].type == CLIENT_TUNNEL
	        && tunnels[tunnelIndex].state == REQUEST_PENDING_TUNNEL) {
        // JIRA EMAPPFWKV2-1392: Event handler was not registered and was not working properly
        // if timeout time has passed, then request a tunnel, else retry after least time remaining
        if (timeGTorEqualInt32u(timeNowMs, tunnels[tunnelIndex].timeoutMSec)) {
          emberAfPluginCommsHubFunctionPrintln("Retrying tunnel creation to node ID 0x%2x",
                                                tunnels[tunnelIndex].remoteNodeId);
          if (requestTunnel(tunnelIndex)) {
            emberEventControlSetDelayMS(emberAfPluginCommsHubFunctionTunnelEventControl,
                                        MILLISECOND_TICKS_PER_SECOND);
            return;
          }
        } else {
          currentTunnelTimeoutMs = elapsedTimeInt32u(timeNowMs, tunnels[tunnelIndex].timeoutMSec);
          if (currentTunnelTimeoutMs < nearestEventTimeoutDelayMs) {
            nearestEventTimeoutDelayMs = currentTunnelTimeoutMs;
          }
        }
      }
    }
  }

  if (nearestEventTimeoutDelayMs != MAX_INT32U_VALUE) {
    emberEventControlSetDelayMS(emberAfPluginCommsHubFunctionTunnelEventControl, nearestEventTimeoutDelayMs);
  }
}
Esempio n. 5
0
static bool buttonPress(uint8_t button, uint8_t state)
{
  // ISR CONTEXT!!!
  static uint32_t timeMs;
  EmberEventControl* event;
	
  if (button == BUTTON0) {
    event = &buttonEvent0;
  } else if (button == BUTTON1) {
		event = &buttonEvent1;
  } else {
		return false;
	}
	if (state == BUTTON_PRESSED) {
		buttonPressDurationMs = 0;
		timeMs = halCommonGetInt32uMillisecondTick();
  } else {
		buttonPressDurationMs = elapsedTimeInt32u(timeMs, halCommonGetInt32uMillisecondTick());
		emberEventControlSetActive(*event);
	}

  return true;
}