just an example of what SDCC can do for YOU
[fw/sdcc] / device / examples / ds390 / ow390 / thermodl.c
1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
3 // 
4 // Permission is hereby granted, free of charge, to any person obtaining a 
5 // copy of this software and associated documentation files (the "Software"), 
6 // to deal in the Software without restriction, including without limitation 
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8 // and/or sell copies of the Software, and to permit persons to whom the 
9 // Software is furnished to do so, subject to the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be included 
12 // in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
16 // MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES 
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
20 // OTHER DEALINGS IN THE SOFTWARE.
21 // 
22 // Except as contained in this notice, the name of Dallas Semiconductor 
23 // shall not be used except as stated in the Dallas Semiconductor 
24 // Branding Policy. 
25 //---------------------------------------------------------------------------
26 //
27 //  thermodl.c - This utility uses to download the results of the
28 //               current mission of a DS1921 Thermochron iButton.
29 //
30 //  Version: 2.00
31 //    
32 //    History:
33 //           1.03 -> 2.00  Reorganization of Public Domain Kit 
34 //                         Y2K update, display all histogram bins, debug
35 //                         dump.  Supports multiple thermochons.
36 //
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "ownet.h"   
41 #include "thermo21.h"
42
43 // TINI hack
44 #define ExitProg(msg,exit_code) {printf("%s\n",msg); exit(exit_code);}
45
46 // defines
47 #define MAXDEVICES   20
48
49 // local function prototypes
50 static void PrintResults(ThermoStateType *,FILE *,int);
51
52 //----------------------------------------------------------------------
53 //  This is the Main routine for thermodl.
54 //
55 int main()
56 {
57    int Fahrenheit=FALSE,num,i,j;
58    FILE *fp=stdout;
59    char return_msg[128];
60    ThermoStateType ThermoState;
61    uchar ThermoSN[MAXDEVICES][8]; //the serial numbers for the devices
62    int portnum=0;
63
64    // attempt to acquire the 1-Wire Net
65    if (!owAcquire(portnum,""))
66      return 0;
67
68    // success
69
70    //----------------------------------------
71    // Introduction
72    printf("\n/----------------------------------------------\n");
73    printf("  Find and download DS1921 Thermochron iButton(s)\n" 
74           "  Version 2.00\n\n");
75
76    // check arguments for temperature conversion and filename
77    Fahrenheit = FALSE;
78
79    // get list of Thermochron's 
80         num = FindDevices(portnum, &ThermoSN[0],THERMO_FAM, MAXDEVICES);
81
82    // check if not present or more then 1 present
83    if (num == 0)
84       ExitProg("Thermochron not present on 1-Wire\n",1);   
85
86    // loop to download each Thermochron
87    for (i = 0; i < num; i++)
88    {
89       // set the serial number portion in the thermo state
90       printf("\nDownloading: ");
91       for (j = 7; j >= 0; j--)
92       {
93          ThermoState.MissStat.serial_num[j] = ThermoSN[i][j];
94          printf("%02X",ThermoSN[i][j]);
95       }
96       printf("\n");
97
98       // download the Thermochron found
99       if (DownloadThermo(portnum,&ThermoSN[i][0],&ThermoState,stdout))
100       {
101          // interpret the results of the download
102          InterpretStatus(&ThermoState.MissStat);
103          InterpretAlarms(&ThermoState.AlarmData, &ThermoState.MissStat);
104          InterpretHistogram(&ThermoState.HistData);
105          InterpretLog(&ThermoState.LogData, &ThermoState.MissStat);
106
107          // print the output
108          PrintResults(&ThermoState,fp,Fahrenheit);
109       }
110       else
111       {
112          fprintf(fp,"\nError downloading device: ");
113          for (j = 0; j < 8; j++)
114             fprintf(fp,"%02X",ThermoSN[i][j]);
115          fprintf(fp,"\n");
116       }
117    }
118
119    // release the 1-Wire Net
120    owRelease(portnum);
121    printf("\n%s",return_msg);
122    ExitProg("End program normally\n",0);
123
124    return 0;
125 }
126
127 //--------------------------------------------------------------------------
128 //  Prints the mission data optionaly to a file or standard out
129 //
130 void PrintResults(ThermoStateType *ThermoState, FILE *fp, int ConvertToF)
131 {
132   //char *str;  
133   char str[80000];
134
135    // check if need to use standard out
136    if (fp == NULL)
137       fp = stdout;
138
139    // get big block to use as a buffer
140 #if 0
141    str = malloc(80000);   
142    if (str == NULL)
143    {
144       printf("Insufficient memory available to print!\n"); 
145       return;
146    }
147 #endif
148
149    // mission status 
150    MissionStatusToString(&ThermoState->MissStat, ConvertToF, &str[0]);
151    fprintf(fp,"\n%s\n",str);
152
153    // alarm events
154    AlarmsToString(&ThermoState->AlarmData, &str[0]);
155    fprintf(fp,"%s\n",str);
156
157    // histogram
158    HistogramToString(&ThermoState->HistData, ConvertToF, &str[0]);
159    fprintf(fp,"%s\n",str);
160
161    // log data
162    LogToString(&ThermoState->LogData, ConvertToF, &str[0]);
163    fprintf(fp,"%s\n",str);
164
165    // debug raw data
166    DebugToString(&ThermoState->MissStat, &ThermoState->AlarmData, 
167       &ThermoState->HistData, &ThermoState->LogData, &str[0]); 
168    fprintf(fp,"%s\n",str);
169
170    // free the memory block used
171    //free(str);
172 }
173