📄 thermo21.c
字号:
// mission start time
if (mstatus->start_delay == 0)
{
SecondsToDate(&td,mstatus->mission_start_time);
if (mstatus->mission_start_time == 0)
cnt += sprintf(&str[cnt],"Mission Start time: not started yet\n");
else
cnt += sprintf(&str[cnt],"Mission Start time: %02d/%02d/%04d %02d:%02d:%02d\n",
td.month,td.day,td.year,td.hour,td.minute,td.second);
}
else
cnt += sprintf(&str[cnt],"Mission Start time: na\n");
// mission start delay
cnt += sprintf(&str[cnt],"Mission Start delay: %d minute(s)\n",mstatus->start_delay);
// mission samples
cnt += sprintf(&str[cnt],"Mission Samples: %d\n",mstatus->mission_samples);
// device total samples
cnt += sprintf(&str[cnt],"Device total samples: %d\n",mstatus->samples_total);
// temperature display mode
cnt += sprintf(&str[cnt],"Temperature displayed in: ");
if (ConvertToF)
cnt += sprintf(&str[cnt],"(Fahrenheit)\n");
else
cnt += sprintf(&str[cnt],"(Celsius)\n");
// thresholds
cnt += sprintf(&str[cnt],"High Threshold: %6.1f\n",
TempToFloat(mstatus->high_threshold,ConvertToF));
cnt += sprintf(&str[cnt],"Low Threshold: %6.1f\n",
TempToFloat(mstatus->low_threshold,ConvertToF));
// time from D1921
SecondsToDate(&td,mstatus->current_time);
cnt += sprintf(&str[cnt],"Current Real-Time Clock from DS1921: %02d/%02d/%04d %02d:%02d:%02d\n",
td.month,td.day,td.year,td.hour,td.minute,td.second);
// current PC time
time(&tlong);
tstruct = localtime(&tlong);
cnt += sprintf(&str[cnt],"Current PC Time: %02d/%02d/%04d %02d:%02d:%02d\n",
tstruct->tm_mon + 1,tstruct->tm_mday,tstruct->tm_year + 1900,
tstruct->tm_hour,tstruct->tm_min,tstruct->tm_sec);
// zero terminate string
str[cnt] = 0;
}
//----------------------------------------------------------------------
// Interpret the Histogram by looking at the 'raw' portion of the
// Histogram structure. Store the temperature range values in Celsius.
//
void InterpretHistogram(Histogram *hist)
{
int i;
// loop through each bin value
for (i = 0; i < 126; i += 2) // (2.00)
{
// get the bin value
hist->bin_count[i / 2] = hist->hist_raw[i] | ((int)hist->hist_raw[i + 1] << 8);
// start value for this bin
hist->start_range[i / 2] = TempToFloat((uchar)((i / 2) << 2),FALSE);
// end value for this bin
hist->end_range[i / 2] = TempToFloat((uchar)(((i / 2) << 2) | 0x03),FALSE);
}
}
//--------------------------------------------------------------------------
// Take the Histogram structure and convert to string format
//
void HistogramToString(Histogram *hist, int ConvertToF, char *str)
{
int cnt=0,i;
// title
cnt += sprintf(&str[cnt],"Temperature Histogram\n---------------------\n"
"Format: [Temp Range, Count] ");
if (ConvertToF)
cnt += sprintf(&str[cnt],"(Fahrenheit)\n");
else
cnt += sprintf(&str[cnt],"(Celsius)\n");
// loop through bins
for (i = 0; i < 63; i++) // (2.00)
{
cnt += sprintf(&str[cnt],"%6.1f to %6.1f, %d\n",
(ConvertToF) ? CToF(hist->start_range[i]): hist->start_range[i],
(ConvertToF) ? CToF(hist->end_range[i]): hist->end_range[i],
hist->bin_count[i]);
}
// zero terminate string
str[cnt] = 0;
}
//----------------------------------------------------------------------
// Interpret the Temperature Alarm Event data by looking at the 'raw'
// portion of the TempAlarmEvents structure. Mission Status is needed
// to interpret the events.
//
void InterpretAlarms(TempAlarmEvents *alarm, MissionStatus *mstatus)
{
int i;
ulong event_mission_count;
uchar duration;
// low events
alarm->num_low = 0;
for (i = 0; i < 48; i += 4)
{
// get the mission start count of this event
event_mission_count = ((long)alarm->alarm_raw[i + 2] << 16) |
((int)alarm->alarm_raw[i + 1] << 8) |
alarm->alarm_raw[i];
// check if done with low events
if (!event_mission_count)
break;
// get the duration
duration = alarm->alarm_raw[i + 3];
// calculate the start time
alarm->low_start_time[alarm->num_low] =
mstatus->mission_start_time +
(event_mission_count - 1) * (mstatus->sample_rate * 60);
// calculate the end time
alarm->low_end_time[alarm->num_low] =
alarm->low_start_time[alarm->num_low] +
(duration - 1) * (mstatus->sample_rate * 60);
// increment number of low events
alarm->num_low++;
}
// high events
alarm->num_high = 0;
for (i = 48; i < 96; i += 4)
{
// get the mission start count of this event
event_mission_count = ((long)alarm->alarm_raw[i + 2] << 16) |
((int)alarm->alarm_raw[i + 1] << 8) |
alarm->alarm_raw[i];
// check if done with low events
if (!event_mission_count)
break;
// get the duration
duration = alarm->alarm_raw[i + 3];
// calculate the start time
alarm->high_start_time[alarm->num_high] =
mstatus->mission_start_time +
(event_mission_count - 1) * (mstatus->sample_rate * 60);
// calculate the end time
alarm->high_end_time[alarm->num_high] =
alarm->high_start_time[alarm->num_high] +
(duration - 1) * (mstatus->sample_rate * 60);
// increment number of low events
alarm->num_high++;
}
}
//--------------------------------------------------------------------------
// Take the Temperature Alarms Events structure and convert to string
// format
//
void AlarmsToString(TempAlarmEvents *alarm, char *str)
{
int i, cnt=0;
timedate td;
// title
cnt += sprintf(&str[cnt],"Temperature Alarms\n------------------\n"
"Format: [(HIGH/LOW), Time/Date Range]\n");
// loop through each low alarm
for (i = 0; i < alarm->num_low; i++)
{
cnt += sprintf(&str[cnt],"LOW , ");
// start time
SecondsToDate(&td,alarm->low_start_time[i]);
cnt += sprintf(&str[cnt]," %02d/%02d/%04d %02d:%02d to ",
td.month,td.day,td.year,td.hour,td.minute);
// end time
SecondsToDate(&td,alarm->low_end_time[i]);
cnt += sprintf(&str[cnt]," %02d/%02d/%04d %02d:%02d\n",
td.month,td.day,td.year,td.hour,td.minute);
}
// loop through each high alarm
for (i = 0; i < alarm->num_high; i++)
{
cnt += sprintf(&str[cnt],"HIGH , ");
// start time
SecondsToDate(&td,alarm->high_start_time[i]);
cnt += sprintf(&str[cnt]," %02d/%02d/%04d %02d:%02d to ",
td.month,td.day,td.year,td.hour,td.minute);
// end time
SecondsToDate(&td,alarm->high_end_time[i]);
cnt += sprintf(&str[cnt]," %02d/%02d/%04d %02d:%02d\n",
td.month,td.day,td.year,td.hour,td.minute);
}
// zero terminate string
str[cnt] = 0;
}
//----------------------------------------------------------------------
// Interpret the Log data by looking at the 'raw'
// portion of the Log structure. Mission Status is needed
// to interpret when the logs occurred.
//
void InterpretLog(Log *log, MissionStatus *mstatus)
{
ulong loops=0,overlap=0,lastlog=2048,i;
int logcnt=0;
// check if wrap occurred
if (mstatus->rollover_occurred)
{
// calculate the number loops
loops = (mstatus->mission_samples / 2048) - 1;
// calculate the number of overlap
overlap = mstatus->mission_samples % 2048;
log->num_log = 2048;
}
else
{
log->start_time = mstatus->mission_start_time;
if (mstatus->mission_samples > 2048) // (1.02)
lastlog = 2048;
else
lastlog = mstatus->mission_samples;
log->num_log = (int)lastlog;
}
// set the interval
log->interval = mstatus->sample_rate * 60;
// caluclate the start time of the first log value
log->start_time = mstatus->mission_start_time +
loops * 2048 * log->interval + overlap * log->interval;
// loop to fill in the remainder first
for (i = overlap; i < lastlog; i++)
log->temp[logcnt++] = TempToFloat(log->log_raw[i],FALSE);
// loop to get the overlap
for (i = 0; i < overlap; i++)
log->temp[logcnt++] = TempToFloat(log->log_raw[i],FALSE);
}
//--------------------------------------------------------------------------
// Take the Log structure and convert to string
// format
//
void LogToString(Log *log, int ConvertToF, char *str)
{
int i,cnt=0;
ulong logtime;
timedate td;
// title
cnt += sprintf(&str[cnt],"Log Data\n--------\n"
"Format: [Time/Date , Temperature] ");
if (ConvertToF)
cnt += sprintf(&str[cnt],"(Fahrenheit)\n");
else
cnt += sprintf(&str[cnt],"(Celsius)\n");
// loop through the logs
logtime = log->start_time;
for (i = 0; i < log->num_log; i++)
{
// time
SecondsToDate(&td,logtime);
cnt += sprintf(&str[cnt],"%02d/%02d/%04d %02d:%02d ,",
td.month,td.day,td.year,td.hour,td.minute);
// temp
cnt += sprintf(&str[cnt],"%6.1f\n",
(ConvertToF) ? CToF(log->temp[i]): log->temp[i]);
// increment the time
logtime += log->interval;
}
// zero terminate string
str[cnt] = 0;
}
//--------------------------------------------------------------------------
// Convert the raw debug data to a string
//
void DebugToString(MissionStatus *mstatus, TempAlarmEvents *alarm,
Histogram *hist, Log *log, char *str)
{
int i,cnt=0;
// title
cnt += sprintf(&str[cnt],"Debug Dump\n----------\nRegister Page:\n");
// reg
for (i = 0; i < 32; i++)
{
cnt += sprintf(&str[cnt],"%02X ",mstatus->status_raw[i]);
if (i && (((i + 1) % 16) == 0))
cnt += sprintf(&str[cnt],"\n");
}
// alarms
cnt += sprintf(&str[cnt],"Alarms:\n");
for (i = 0; i < 96; i++)
{
cnt += sprintf(&str[cnt],"%02X ",alarm->alarm_raw[i]);
if (i && (((i + 1) % 16) == 0))
cnt += sprintf(&str[cnt],"\n");
}
// histogram
cnt += sprintf(&str[cnt],"Histogram:\n");
for (i = 0; i < 128; i++)
{
cnt += sprintf(&str[cnt],"%02X ",hist->hist_raw[i]);
if (i && (((i + 1) % 16) == 0))
cnt += sprintf(&str[cnt],"\n");
}
// log
cnt += sprintf(&str[cnt],"Log:\n");
for (i = 0; i < ((log->num_log > 2048) ? 2048 : log->num_log); i++)
{
cnt += sprintf(&str[cnt],"%02X ",log->log_raw[i]);
if (i && (((i + 1) % 16) == 0))
cnt += sprintf(&str[cnt],"\n");
}
// zero terminate string
str[cnt] = 0;
}
//--------------------------------------------------------------------------
// Take one byte BCD value and return binary value
//
uchar BCDToBin(uchar bcd)
{
return (((bcd & 0xF0) >> 4) * 10) + (bcd & 0x0F);
}
//--------------------------------------------------------------------------
// Take a 4 byte long string and convert it into a timedata structure.
//
static int dm[] = { 0,0,31,59,90,120,151,181,212,243,273,304,334,365 };
void SecondsToDate(timedate *td, ulong x)
{
short tmp,i,j;
ulong y;
// check to make sure date is not over 2070 (sanity check)
if (x > 0xBBF81E00L)
x = 0;
y = x/60; td->second = (ushort)(x-60*y);
x = y/60; td->minute = (ushort)(y-60*x);
y = x/24; td->hour = (ushort)(x-24*y);
x = 4*(y+731); td->year = (ushort)(x/1461);
i = (int)((x-1461*(ulong)(td->year))/4); td->month = 13;
do
{
td->month -= 1;
tmp = (td->month > 2) && ((td->year & 3)==0) ? 1 : 0;
j = dm[td->month]+tmp;
} while (i < j);
td->day = i-j+1;
// slight adjustment to algorithm
if (td->day == 0)
td->day = 1;
td->year = (td->year < 32) ? td->year + 68 + 1900: td->year - 32 + 2000;
}
//--------------------------------------------------------------------------
// DateToSeconds takes a time/date structure and converts it into the
// number of seconds since 1970
//
ulong DateToSeconds(timedate *td)
{
ulong Sv,Bv,Xv;
// convert the date/time values into the 5 byte format used in the touch
if (td->year >= 2000)
Sv = td->year + 32 - 2000;
else
Sv = td->year - 68 - 1900;
if ((td->month > 2) && ( (Sv & 3) == 0))
Bv = 1;
else
Bv = 0;
Xv = 365 * (Sv-2) + (Sv-1)/4 + dm[td->month] + td->day + Bv - 1;
Xv = 86400 * Xv + (ulong)(td->second) + 60*((ulong)(td->minute) + 60*(ulong)(td->hour));
return Xv;
}
//--------------------------------------------------------------------------
// Convert from DS1921 termature format to a float
//
//
float TempToFloat(uchar tmp, int ConvertToF)
{
float tfloat;
tfloat = (float)((tmp / 2.0) - 40.0);
if (ConvertToF)
return (float)(tfloat * 9.0 / 5.0 + 32.0);
else
return tfloat;
}
//--------------------------------------------------------------------------
// Convert from Celsius to Fahrenheit
//
float CToF(float CVal)
{
return (float)(CVal * 9.0 / 5.0 + 32.0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -