* Removed svn:executable property from non-executable files
[fw/sdcc] / device / examples / ds390 / ow390 / swtloop.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 //  swtloop.C - Goes through the testing of the DS2406(DS2407) switch
28 //  Version 2.00 
29
30
31 // Include files
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "ownet.h"
35 #include "swt12.h"
36
37 // Constant definition
38 #define MAXDEVICES         15
39
40 // tini hack
41 int argc=2;
42 char *argv[]={__FILE__, "exow"};
43 #define getkeystroke getchar
44
45 //--------------------------------------------------------------------------
46 // This is the begining of the program that tests the different Channels
47 int main() //short argc, char **argv)
48 {
49    char return_msg[128];           //returned message from 1-wire operations
50    int i,j,k,n;                    //loop counters
51    short test=0;                   //info byte data
52    short clear=0;                  //used to clear the button
53    SwitchProps sw;                 //used to set Channel A and B
54    uchar SwitchSN[MAXDEVICES][8];  //the serial numbers for the devices
55    int num;                        //for the number of devices present
56    int ch;                         //inputed character from user
57    char out[140];                  //used for output of the info byte data
58    int portnum=0;
59
60    //----------------------------------------
61    // Introduction header
62    printf("\n/---------------------------------------------\n");
63    printf("  swtest - V2.00\n"
64           "  The following is a test to excersize the\n"
65           "  different channels on the DS2406.\n");
66    printf("  Press any CTRL-C to stop this program.\n\n");
67
68    // check for required port name
69    if (argc != 2)
70    {
71       printf("1-Wire Net name required on command line!\n"
72              " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
73              "(Linux DS2480),\"1\" (Win32 TMEX)\n");
74       exit(1);
75    }
76
77    // attempt to acquire the 1-Wire Net
78    if (!owAcquire(portnum, argv[1], return_msg))
79    {  
80       printf("%s",return_msg);
81       exit(1);
82    }
83
84    // success
85    printf("%s",return_msg);
86    
87    // this is to get the number of the devices and the serial numbers
88    num = FindDevices(portnum, &SwitchSN[0], SWITCH_FAMILY, MAXDEVICES);
89
90    // setting up the first print out for the frist device
91    owSerialNum(portnum, SwitchSN[0], FALSE);
92    
93    j=1;
94    n=0;
95    do
96    {
97       // This is for after the different combinations of channels
98       // have been tested to reset to a different device to be tested.
99       if( ((test & 0x40) && (j==5)) ||
100          ((!(test & 0x40)) && (j==3)) )
101       {
102          printf("\n\n");
103          for(k=0; k < num; k++)
104          {
105             printf("%d  ", k+1);
106             for(i=7; i>=0; i--)
107             {
108                printf("%02X", SwitchSN[k][i]);
109             }
110             printf("\n");
111          }
112          printf("%d To quit or any other key.\n", k+1);
113
114          printf("\n");
115          printf("Pick a device\n");
116
117          ch = getkeystroke();
118          n = 0;
119          n = (10*n + (ch - '0')) - 1;
120
121          if( (n>num-1) || (n<0) )
122          {
123             n = 0;        //used to finish off the loop
124             break;
125          }
126
127          owSerialNum(portnum, SwitchSN[n], FALSE);
128          j = 1;
129       }
130       printf("\n");
131       
132       test = ReadSwitch12(portnum,clear);
133
134       // This looks at the info byte to determine if it is a
135       // two or one channel device.
136       if(test & 0x40)
137       {
138
139          switch(j)
140          {
141             case 1: 
142                sw.Chan_A = 0;
143                sw.Chan_B = 0;
144                break;
145             case 2:
146                sw.Chan_A = 0;
147                sw.Chan_B = 1;
148                break;
149             case 3:
150                sw.Chan_A = 1;
151                sw.Chan_B = 0;
152                break;
153             case 4:
154                sw.Chan_A = 1;
155                sw.Chan_B = 1;
156                break;
157             default:
158                sw.Chan_A = 1;
159                sw.Chan_B = 1;
160                j=0;
161                break;
162             }
163       }
164       else
165       {
166          switch(j)
167          {
168             case 1:
169                sw.Chan_B = 0;
170                sw.Chan_A = 0;
171                break;
172             case 2:
173                sw.Chan_B = 0;
174                sw.Chan_A = 1;
175                break;
176             default:
177                sw.Chan_B = 0;
178                sw.Chan_A = 1;
179                j = 0;
180                break;
181          }
182       }
183
184       if(!SetSwitch12(portnum, SwitchSN[n], &sw))
185       {
186          msDelay(50);
187          if(SetSwitch12(portnum, SwitchSN[n], &sw))
188             msDelay(50);
189          else
190             printf("Switch not set\n");
191       }
192
193       test = ReadSwitch12(portnum,clear);
194
195       printf("\n");
196
197       for(i=7; i>=0; i--)
198       {
199          printf("%02X", SwitchSN[n][i]);
200       }
201       printf("\n");
202
203       SwitchStateToString12(test, out);
204       printf("%s", out);
205
206       j++;
207
208    }
209    while(1);
210
211    // release the 1-Wire Net
212    owRelease(portnum,return_msg);
213    printf("%s",return_msg);
214    exit(0);
215
216    return 0;
217 }
218