예제 #1
0
int syncTime(int identifier,int fd, int cmd, char *data, int vallen)
{
	DATE_TIME_INFO dtinfo;
	int ret = 0;
	unsigned char sdata[256] = {0};
	int totallen = 0;
	int cmdlen = sizeof(int);
	int retlen = sizeof(int);
	memcpy(&dtinfo, data, vallen);
	PRINTF("dtinfo.year:%d,dtinfo.month:%d,dtinfo.mday:%d,dtinfo.hours:%d,dtinfo.min:%d,dtinfo.sec:%d\n",
	       dtinfo.year, dtinfo.month, dtinfo.mday, dtinfo.hours, dtinfo.min, dtinfo.sec);
	PRINTF("1 the run time =%d=%lld\n",get_run_time(),getCurrentTime());
	setRtcTime(dtinfo.year, dtinfo.month, dtinfo.mday, dtinfo.hours, dtinfo.min, dtinfo.sec);
	PRINTF("2 the run time =%d=%lld\n",get_run_time(),getCurrentTime());
#if 0  //del by zhangmin
	ReInitTimeTick();
#endif
	totallen = MSGINFOHEAD + cmdlen + vallen + retlen;
	msgPacket(identifier,sdata, STRUCT_TYPE, totallen, cmd, ret);
	memcpy(sdata + totallen - vallen, &dtinfo, vallen);
	DEBUG(DL_DEBUG, "syncTime...cmd =%d dtinfo:%d,%d,%d,%d,%d,%d\n",
	      cmd, dtinfo.year, dtinfo.month, dtinfo.mday, dtinfo.hours, dtinfo.min, dtinfo.sec);
	ret = send(fd, sdata, totallen, 0);

	if(ret < 0) {
		PRINTF("send failed ret:%d,totallen : %d\n", ret, totallen);
	}

	/*if time is change ,i must do some thing*/
	//app_build_reset_time();
	return 0;
}
예제 #2
0
파일: main.c 프로젝트: arantius/vfd-clock
/// Handle GPS data containing well synchronized time.
void handleGpsLine() {
  if (memcmp("$GPZDA,", gGpsLine, 7)) {
    trace_puts("Ignoring unknown GPS line:");
    trace_puts(gGpsLine);
    gGpsLine[0] = 0x00;
    return;
  }

  char *chkStart = strstr(gGpsLine, "*") + 1;
  uint8_t theirChk = strtol(chkStart, 0, 16);

  uint8_t chk = 0;
  for (uint8_t i = 1; ; i++) {
    if (gGpsLine[i] == 0x00) break;
    if (gGpsLine[i] == '*') break;
    chk ^= gGpsLine[i];
  }

  if (theirChk != chk) {
    trace_printf("Ignoring checksum mismatch: %02x / %02x\n", theirChk, chk);
    trace_puts(gGpsLine);
    gGpsLine[0] = 0x00;
    return;
  }

  // It just so happens that for the one line (ZDA) we want to handle, just
  // to get the time, it's always fixed length for all fields:
  //
  // 0         1         2         3
  // $GPZDA,,,,,00,00*48
  // $GPZDA,222544.00,24,08,2015,00,00*69
  // $GPZDA,231207.00,24,08,2015,00,00*6B
  //
  // Namely:
  //  - hhmmss.ss time
  //  - dd day
  //  - mm month
  //  - yyyy year
  //  - xx unsupported local hours
  //  - xx unsupported local minutes
  // So we can use very simple parsing techniques, just grab the characters
  // by fixed index!  We only have to prepare by discerning the empty from
  // the full format.

  if (gGpsLine[7] == ',') {
    trace_puts("Ignoring ZDA line with no fix.");
    trace_puts(gGpsLine);
    gGpsLine[0] = 0x00;
    return;
  }

  trace_puts("Setting time from GPS line:");
  trace_puts(gGpsLine);

  struct tm *t = gmtime(&gSeconds);
  uint32_t hms = strtol(gGpsLine + 7, 0, 10);
  t->tm_hour = (hms / 10000);
  t->tm_min = (hms / 100) % 100;
  t->tm_sec = hms % 100;
  t->tm_mday = strtol(gGpsLine + 17, 0, 10);
  t->tm_mon = strtol(gGpsLine + 20, 0, 10);
  t->tm_year = strtol(gGpsLine + 23, 0, 10) - 1900;

  setRtcTime(mktime(t));
  gpsSetPushFreq("ZDA", 0);
  gGpsNextPoll = gSeconds + 3593;
  gGpsAvailable = 1;

  // Empty the string to indicate we've processed it.
  gGpsLine[0] = 0x00;
}
예제 #3
0
파일: main.c 프로젝트: arantius/vfd-clock
/// Given gButtonPressed, update state accordingly.
void handleButtonPress() {
  switch (gButtonPressed) {
  case BTN_NONE:
    break;
  case BTN_MAPLE:
    trace_puts("Maple button!");
    gGpsAvailable = !gGpsAvailable;
    break;
  case BTN_SET:
    if (gGpsAvailable) {
      gSettingUtcOffset = !gSettingUtcOffset;
      trace_printf("Set button; setting utc offset=%d\n", gSettingUtcOffset);
      gSecondFlag |= !gSettingUtcOffset;
    } else {
      gBlinkPos += 1;
      if (gBlinkPos > 5) {
        gBlinkPos = 0;
        gSecondFlag = 1;  // Force logic loop to display digits.
      }
      trace_printf("Set button; new pos %d\n", gBlinkPos);
    }
    break;
  case BTN_DIM:
    gGridOc.TIM_Pulse += 10;
    gGridOc.TIM_Pulse %= 80;
    TIM_OC1Init(TIM2, &gGridOc);
    trace_printf("Dim button; pulse now %d of 80.\n", gGridOc.TIM_Pulse);
    backupSave();
    break;
  case BTN_UP:
    trace_puts("Up button!");
    if (gSettingUtcOffset) {
      gUtcOffset += UTC_OFFSET_ADJUST;
      if (gUtcOffset > (14 * 3600)) gUtcOffset = -12 * 3600;
      trace_printf("utc offset now=%d\n", gUtcOffset);
      backupSave();
    } else {
      gSeconds = RTC_GetCounter() + gSettingChange[gBlinkPos];
      setRtcTime(gSeconds);
    }
    gSecondFlag = 1;
    break;
  case BTN_DOWN:
    trace_puts("Down button!");
    if (gSettingUtcOffset) {
      gUtcOffset -= UTC_OFFSET_ADJUST;
      if (gUtcOffset < (-12 * 3600)) gUtcOffset = 14 * 3600;
      trace_printf("utc offset now=%d\n", gUtcOffset);
      backupSave();
    } else {
      gSeconds = RTC_GetCounter() - gSettingChange[gBlinkPos];
      setRtcTime(gSeconds);
    }
    gSecondFlag = 1;
    break;
  default:
    // No button pressed, ignore.
    break;
  }
  gButtonPressed = BTN_NONE;
}