static int timerChargingControl(TimerHandle timer, void *context)
{
	Timer_Destroy(timer);

	// Get reading
	unsigned char BatteryCharging = BAT_CHARGE_IN;

	// Work out if external power is connected
	bool powerGood = !(BatteryCharging & BAT_CHARGE_PWR_GOOD);

	// Work out battery state
	BatteryCharging &= BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2;
	unsigned int powerState = BatteryCharging >> 3;

	if (powerState == BATTERY_CHARGE_OFF_FAULT_SLEEP)
	{
		BATTERY_CHARGE_DISABLE();
		BAT_CHARGE_OUT &= ~BAT_CHARGE_OPEN_DRAIN_BITS;
	}

	// Work out new state
	Battery_State newState = powerState;
	if (powerGood)
		newState |= Battery_Powered;

	// Launch ADC cycle
	Adc_Request(adcBatteryLevel, adcBattery, (void*)newState);

	return 0;
}
void TimeFace::OnHide(int type)
{
	WatchFace::OnHide(type);
	if ((!Visible()) && (m_timer != NULL))
	{
		Timer_Destroy(m_timer);
		m_timer = NULL;
	}
}
Exemple #3
0
void mexFunction(int nlhs, mxArray *plhs[], int	nrhs, const mxArray *prhs[])
{
  FILE *log;
  Options_Interface *o;
  Void *timer;
  MCP *m;
  MCP_Termination t;
  Information info;

  double *tempZ;
  double *status, *tot_time;
  double dnnz;
  int i, len;

  mat_install_error();
  mat_install_memory();

  log = fopen("logfile.tmp", "w");
  Output_SetLog(log);

  /* Options.
     1.  Get options associated with the algorithm
     2.  Set to default values
     3.  Read in an options file
     4.  Print out the options */

  o = Options_Create();
  Path_AddOptions(o);
  Options_Default(o);

  if (nrhs < 7) {
    Error("Not enough input arguments.\n");
  }

  if (nlhs < 2) {
    Error("Not enough output arguments.\n");
  }

  nMax = (int) mxGetScalar(prhs[0]);
  nnzMax = (int) mxGetScalar(prhs[1]);

  plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
  plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);

  if (nlhs > 2) {
    plhs[2] = mxCreateDoubleMatrix(nMax,1,mxREAL);
  }

  if (nlhs > 3) {
    plhs[3] = mxCreateDoubleMatrix(nMax,1,mxREAL);
  }

  /* Create a timer and start it. */
  timer = Timer_Create();
  Timer_Start(timer);

  /* Derefencing to get the status argument. */
  status = mxGetPr(plhs[0]);
  tot_time = mxGetPr(plhs[1]);

  /* First print out some version information.  */
  Output_Printf(Output_Log | Output_Status | Output_Listing,
		"%s: Matlab Link\n", Path_Version());

  /* Now check the arguments.

     If the dimension of the problem is 0, there is nothing to do.
  */

  if (nMax == 0) {
    Output_Printf(Output_Log | Output_Status,
		  "\n ** EXIT - solution found (degenerate model).\n");
    (*status) = 1.0;
    (*tot_time) = Timer_Query(timer);

    Timer_Destroy(timer);
    Options_Destroy(o);
    fclose(log);
    return;
  }

  /* Check size of the jacobian.  nnz < n*n.  Need to convert to double
     so we do not run out of integers.

     dnnz - max number of nonzeros as a double
  */

  dnnz = MIN(1.0*nnzMax, 1.0*nMax*nMax);
  if (dnnz > INT_MAX) {
    Output_Printf(Output_Log | Output_Status,
		  "\n ** EXIT - model too large.\n");
    (*status) = 0.0;
    (*tot_time) = Timer_Query(timer);

    Timer_Destroy(timer);
    Options_Destroy(o);
    fclose(log);
    return;
  }
  nnzMax = (int) dnnz;

  /* Have correct number of nonzeros.  Print some statistics.
     Print out the density.  */

  Output_Printf(Output_Log | Output_Status | Output_Listing,
		"%d row/cols, %d non-zeros, %3.2f%% dense.\n\n",
		nMax, nnzMax, 100.0*nnzMax/(1.0*nMax*nMax));

  if (nnzMax == 0) {
    nnzMax++;
  }

  zp = mxGetPr(prhs[2]);
  lp = mxGetPr(prhs[3]);
  up = mxGetPr(prhs[4]);

  m_start = mxGetJc(prhs[5]);
  m_row = mxGetIr(prhs[5]);
  m_data = mxGetPr(prhs[5]);
  m_len = (int *)Memory_Allocate(nMax*sizeof(int));

  for (i = 0; i < nMax; i++) {
    m_len[i] = m_start[i+1] - m_start[i];
    m_start[i]++;
  }
  actualNnz = m_start[nMax];

  for (i = 0; i < actualNnz; i++) {
    m_row[i]++;
  }

  q = mxGetPr(prhs[6]);
     
  /* Create a structure for the problem. */
  m = MCP_Create(nMax, nnzMax);
  MCP_Jacobian_Structure_Constant(m, 1);
  MCP_Jacobian_Data_Contiguous(m, 1);
  install_interface(m);

  Options_Read(o, "path.opt");
  Options_Display(o);

  info.generate_output = Output_Log | Output_Status | Output_Listing;
  info.use_start = True;
  info.use_basics = True;

  /* Solve the problem.  
     m - MCP to solve, info - information, t - termination */

  t = Path_Solve(m, &info);

  /* Read in the results.  First get the solution vector. The report. */

  tempZ = MCP_GetX(m);
  for (i = 0; i < nMax; i++) {
    zp[i] = tempZ[i];
  }

  /* If the final function evaluation is wanted, report it. */
  if (nlhs > 2) {
    double *fval = mxGetPr(plhs[2]);

    tempZ = MCP_GetF(m);
    for (i = 0; i < nMax; i++) {
      fval[i] = tempZ[i];
    }
  }

  /* If the final basis is wanted, report it. */
  if (nlhs > 3) {
    double *bval = mxGetPr(plhs[3]);
    int *tempB;

    tempB = MCP_GetB(m);
    for (i = 0; i < nMax; i++) {
      switch(tempB[i]) {
      case Basis_LowerBound:
	bval[i] = -1;
	break;

      case Basis_UpperBound:
	bval[i] = 1;
	break;

      default:
	bval[i] = 0;
	break;
      }
    }
  }

  switch(t) {
  case MCP_Solved:
    *status = 1.0;
    break;

  default:
    *status = 0.0;
    break;
  }
     
  /* Stop the timer and report the results. */
  (*tot_time) = Timer_Query(timer);
  Timer_Destroy(timer);

  /* Clean up.  Deallocate memory used and close the log. */
  MCP_Destroy(m);
  Memory_Free(m_len);
  Options_Destroy(o);

  fclose(log);
  return;
}
	static int DelayStartAdc(TimerHandle handle, void *context)
	{
		((LightSensor<input, channel>*)context)->StartADC();
		Timer_Destroy(handle);
		return 0;
	}
TimeFace::~TimeFace()
{
	if (m_timer != NULL)
		Timer_Destroy(m_timer);
}