1 //---------------------------------------------------------------------------
2 // Copyright (C) 2001 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 //  swtoper.C - Menu-driven test of DS2406(DS2407) 1-Wire switch
28 //  version 3.00
29 
30 
31 // Include files
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include "ownet.h"
36 #include "swt12.h"
37 
38 // Constant definition
39 #define SWITCH_FAMILY      0x12
40 #define MAXDEVICES         15
41 
42 // External subroutines
43 extern short ReadSwitch12(int,int);
44 extern int SetSwitch12(int,uchar *,SwitchProps);
45 extern int SwitchStateToString12(int,char *);
46 extern SMALLINT owAccess(int);
47 extern SMALLINT owAcquire(int,char *);
48 extern void owRelease(int);
49 extern void owSerialNum(int,uchar *,SMALLINT);
50 extern SMALLINT FindDevices(int,uchar FamilySN[][8],SMALLINT,SMALLINT);
51 extern void msDelay(int);
52 extern int EnterNum(char *,int,long *,long,long);
53 
54 //---------------------------------------------------------------------------
55 // The main program that performs the operations on switches
56 //
main(int argc,char ** argv)57 int main(int argc, char **argv)
58 {
59    short test;                     //info byte data
60    short clear=0;                  //used to clear the button
61    short done;                     //to tell when the user is done
62    SwitchProps sw;                 //used to set Channel A and B
63    uchar SwitchSN[MAXDEVICES][8];  //the serial number for the devices
64    int num;                        //for the number of devices present
65    int i,j,n,count,result;         //loop counters and indexes
66    char out[140];                  //used for output of the info byte data
67    int portnum=0;
68    long select;                    //inputed number from user
69 
70    //----------------------------------------
71    // Introduction header
72    printf("\n/---------------------------------------------\n");
73    printf("  Switch - V3.00\n"
74           "  The following is a test to excersize the \n"
75           "  setting of the state in a DS2406.\n");
76 
77    printf("  Press any CTRL-C to stop this program.\n\n");
78 
79    // check for required port name
80    if (argc != 2)
81    {
82       printf("1-Wire Net name required on command line!\n"
83              " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
84              "(Linux DS2480),\"1\" (Win32 TMEX)\n");
85       exit(1);
86    }
87 
88    // attempt to acquire the 1-Wire Net
89    if (!owAcquire(portnum,argv[1]))
90    {
91       OWERROR_DUMP(stdout);
92       exit(1);
93    }
94 
95    // success
96    printf("Port opened: %s\n",argv[1]);
97 
98    // this is to get the number of the devices and the serial numbers
99    num = FindDevices(portnum, &SwitchSN[0], SWITCH_FAMILY, MAXDEVICES);
100 
101    // setting up the first print out for the frist device
102    owSerialNum(portnum, SwitchSN[0], FALSE);
103 
104    printf("\n");
105    n=0;
106    if(owAccess(portnum))
107    {
108       // loop while not done
109       do
110       {
111          test = ReadSwitch12(portnum, clear);
112 
113          for(i=7; i>=0; i--)
114             printf("%02X", SwitchSN[n][i]);
115 
116          printf("\n");
117 
118          count = SwitchStateToString12(test, out);
119          printf("%s", out);
120 
121          // print menu
122          select = 1;
123          if (!EnterNum("\n\n(1) Display the switch Info\n"
124               "(2) Clear activity Latches\n"
125               "(3) Set Flip Flop(s) on switch\n"
126               "(4) Select different device\n"
127               "(5) Quit\n"
128               "Select a Number", 1, &select, 1, 5))
129               break;
130          printf("\n\n");
131 
132          // do something from the menu selection
133          clear = FALSE;
134          switch(select)
135          {
136             case 1: // Display the switch Info
137                done = FALSE;
138                break;
139             case 2: // Clear activity Latches
140                clear = TRUE;
141                done = FALSE;
142                break;
143             case 3: // Set Flip Flop(s) on switch
144                select = 0;
145                if (EnterNum("Channel A Flip Flop (1 set, 0 clear)",
146                     1, &select, 0, 1))
147                {
148                   sw.Chan_A = (uchar)select;
149 
150                   if(test & 0x40)
151                   {
152                      if (EnterNum("Channel B Flip Flop (1 set, 0 clear)",
153                           1, &select, 0, 1))
154                      {
155                         sw.Chan_B = (uchar)select;
156                         done = FALSE;
157                      }
158                   }
159                   else
160                   {
161                     sw.Chan_B = 0;
162                     done = FALSE;
163                   }
164                   printf("\n");
165                }
166 
167                // proceed to setting switch state if not done
168                if (!done)
169                {
170                   // loop to attempt to set the switch (try up to 5 times)
171                   count = 0;
172                   do
173                   {
174                      result = SetSwitch12(portnum, SwitchSN[n], sw);
175 
176                      // if failed then delay to let things settle
177                      if (!result)
178                         msDelay(50);
179                   }
180                   while ((count++ < 5) && (result != TRUE));
181 
182                   // if could not set then show error
183                   if (!result)
184                      printf("Could not set Switch!\n");
185                }
186                break;
187             case 4: // Switch Devices
188                // print the device list
189                for(j=0; j < num; j++)
190                {
191                   printf("%d  ", j+1);
192                   for(i=7; i>=0; i--)
193                   {
194                      printf("%02X", SwitchSN[j][i]);
195                   }
196                   printf("\n");
197                }
198                printf("\n");
199 
200                // select the device
201                select = 0;
202                if (EnterNum("Select Device",1, &select, 1, num))
203                {
204                   n = (int)(select - 1);
205                   owSerialNum(portnum, SwitchSN[n], FALSE);
206                   done = FALSE;
207                }
208                break;
209 
210             case 5: // Done
211                done = TRUE;
212                break;
213                default:
214             break;
215          }
216       }
217       while (!done);
218    }
219    //One Wire Access
220    owRelease(portnum);
221    printf("Closing port %s.\n", argv[1]);
222    exit(0);
223 
224    return 0;
225 }
226 
227