* Removed svn:executable property from non-executable files
[fw/sdcc] / device / examples / ds390 / ow390 / counter.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 //  counter.c - Application to read the 1-Wire Net DS2423 - counter.
28 //               
29 //             This application uses the files from the 'Public Domain' 
30 //             1-Wire Net libraries ('general' and 'userial'). 
31 //
32 //  Version: 2.00
33 //
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include "ownet.h"
38 #include "cnt1d.h"
39
40 // defines 
41 #define MAXDEVICES           20
42
43 // local functions
44 void PrintSerialNum(uchar *SerialNum);
45
46 // local serial numbers 
47 static uchar FamilySN[MAXDEVICES][8];
48
49 // tini hack
50 int argc=2;
51 char *argv[]={__FILE__, "exow"};
52
53 //----------------------------------------------------------------------
54 //  Main Test for the DS2423 - counter
55 //
56 int main() //short argc, char **argv)
57 {
58    char return_msg[128];
59    int NumDevices=0;
60    int i;
61    int CounterPage;
62    ulong Count;
63    int portnum=0;
64       
65    //------------------------------------------------------
66    // Introduction header
67    printf("\n/---------------------------------------------\n");
68    printf("  Counter Application - V2.00\n"
69           "  The following is a test to excersize a\n"
70           "  DS2423 - counter \n\n");
71           
72    printf("  Press any CTRL-C to stop this program.\n\n");
73    printf("  Output   [Serial Number(s) ... Counter Value ... Counter Value ... " 
74                          "Counter Value ... Counter Value] \n\n");
75       
76    // check for required port name
77    if (argc != 2)
78    {
79       printf("1-Wire Net name required on command line!\n"
80              " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
81              "(Linux DS2480),\"1\" (Win32 TMEX)\n");
82       exit(1);
83    }
84
85    // attempt to acquire the 1-Wire Net
86    if (!owAcquire(portnum, argv[1], return_msg))
87    {  
88       printf("%s",return_msg);
89       exit(1);
90    }
91
92    // success
93    printf("%s",return_msg);
94
95    // Find the device(s)
96    NumDevices = FindDevices(portnum, &FamilySN[0], 0x1D, MAXDEVICES);
97    if (NumDevices>0)
98    {
99       printf("\n");
100       printf("Device(s) Found: \n");
101       for (i = 0; i < NumDevices; i++) 
102       {
103          PrintSerialNum(FamilySN[i]);
104          printf("\n");
105       }
106       printf("\n\n");
107
108       // (stops on CTRL-C)
109       do
110       {
111          // read the current counters
112          for (i = 0; i < NumDevices; i++)
113          {
114             printf("\n");
115             PrintSerialNum(FamilySN[i]);
116            
117             for (CounterPage = 12; CounterPage <= 15; CounterPage++)
118             {
119                if (ReadCounter(portnum, FamilySN[i], CounterPage, &Count))
120                {
121                    printf(" %10ld  ", Count);
122                }
123                else
124                   printf("\nError reading counter, verify device present:%d\n",
125                   owVerify(portnum,FALSE));
126             }
127          }
128          printf("\n\n");
129       }
130       while (!key_abort());
131    }
132    else 
133       printf("\n\n\n ERROR, device not found!\n");
134    
135    // release the 1-Wire Net
136    owRelease(portnum,return_msg);
137    printf("%s",return_msg);
138    exit(0);
139
140    return 0;
141 }
142
143 // -------------------------------------------------------------------------------
144 // Read and print the serial number
145 //
146 void PrintSerialNum(uchar *SerialNum)
147 {
148    int i;
149
150    for (i = 7; i>=0; i--)    
151       printf("%02X", SerialNum[i]);
152 }
153
154
155