Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / ow390 / swtoper.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 //  swtoper.C - Menu-driven test of DS2406(DS2407) 1-Wire switch
28 //  version 2.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 MAXDEVICES         15
40
41 // tini hack
42 int argc=2;
43 char *argv[]={__FILE__, "exow"};
44 #define getkeystroke getchar
45
46 //---------------------------------------------------------------------------
47 // The main program that performs the operations on switches
48 //
49 int main() //short argc, char **argv)
50 {
51    char return_msg[128];           //returned message from 1-wire operations
52    short test;                     //info byte data
53    short clear=0;                  //used to clear the button
54    short done;                     //to tell when the user is done
55    SwitchProps sw;                 //used to set Channel A and B
56    uchar SwitchSN[MAXDEVICES][8];  //the serial number for the devices
57    int num;                        //for the number of devices present
58    int ch;                         //inputed character from user
59    int i,j,n;                      //loop counters and indexes
60    char out[140];                  //used for output of the info byte data
61    int count;                      //the number of characters in the info byte data
62    int portnum=0;
63
64    //----------------------------------------
65    // Introduction header
66    printf("\n/---------------------------------------------\n");
67    printf("  Switch - V2.00\n"
68           "  The following is a test to excersize the \n"
69           "  setting of the state in a DS2406.\n");
70
71    printf("  Press any CTRL-C to stop this program.\n\n");
72
73    // check for required port name
74    if (argc != 2)
75    {
76       printf("1-Wire Net name required on command line!\n"
77              " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
78              "(Linux DS2480),\"1\" (Win32 TMEX)\n");
79       exit(1);
80    }
81
82    // attempt to acquire the 1-Wire Net
83    if (!owAcquire(portnum, argv[1], return_msg))
84    {
85       printf("%s",return_msg);
86       exit(1);
87    }
88
89    // success
90    printf("%s",return_msg);
91
92    // this is to get the number of the devices and the serial numbers
93    num = FindDevices(portnum, &SwitchSN[0], SWITCH_FAMILY, MAXDEVICES);
94
95    // setting up the first print out for the frist device
96    owSerialNum(portnum, SwitchSN[0], FALSE);
97
98    printf("\n");
99    n=0;
100    if(owAccess(portnum))
101    {
102       // loop while not done
103       do
104       {
105          test = ReadSwitch12(portnum, clear);
106
107          for(i=7; i>=0; i--)
108          printf("%02X", SwitchSN[n][i]);
109
110          printf("\n");
111
112          count = SwitchStateToString12(test, out);
113          printf("%s", out);
114
115          // print menu
116          printf("\n\n(1) Display the switch Info\n"
117               "(2) Clear activity Latches\n"
118               "(3) Set Flip Flop(s) on switch\n"
119               "(4) Select different device\n"
120               "(5) Quit\n"
121               "Select a Number:");
122          ch = getkeystroke();
123          printf("\n\n");
124
125          // do something from the menu selection
126          clear = FALSE;
127          switch(ch)
128          {
129             case '1': // Display the switch Info
130                done = FALSE;
131                break;
132             case '2': // Clear activity Latches
133                clear = TRUE;
134                done = FALSE;
135                break;
136             case '3': // Set Flip Flop(s) on switch
137                printf("Channel %c Flip Flop (1 set, 0 clear):",'A');
138                ch = getkeystroke();
139                if (ch == '0')
140                   sw.Chan_A = 0;
141                else
142                   sw.Chan_A = 1;
143                printf("\n");
144
145                if(test & 0x40)
146                {
147                  printf("Channel %c Flip Flop (1 set, 0 clear):",'B');
148                  ch = getkeystroke();
149                  if (ch == '0')
150                      sw.Chan_B = 0;
151                  else
152                      sw.Chan_B = 1;
153                  printf("\n");
154                }
155                else
156                {
157                  printf("\n");
158                  sw.Chan_B = 0;
159                }
160                printf("\n");
161
162                if(!SetSwitch12(portnum, SwitchSN[n], &sw))
163                {
164                   msDelay(50);
165                   if(SetSwitch12(portnum, SwitchSN[n], &sw))
166                      msDelay(50);
167                   else
168                      printf("Switch not set\n");
169                }
170                done = FALSE;
171                break;
172             case '4': // Switch Devices
173                for(j=0; j < num; j++)
174                {
175                   printf("%d  ", j+1);
176                   for(i=7; i>=0; i--)
177                   {
178                      printf("%02X", SwitchSN[j][i]);
179                   }
180                   printf("\n");
181                }
182                printf("\n");
183
184                do
185                {
186                   printf("Pick a device\n");
187                   ch = getkeystroke();
188                   n = 0;
189                   n = (10*n + (ch - '0')) - 1;
190                   printf("\n");
191                }
192                while((!isalnum(ch)) || (n>num-1));
193                printf("\n");
194
195                n = 0;
196                n = (10*n + (ch - '0')) - 1;
197
198                owSerialNum(portnum, SwitchSN[n], FALSE);
199
200                done = FALSE;
201                break;
202
203             case '5': case 'q': case 'Q': // Done
204                done = TRUE;
205                break;
206                default:
207             break;
208          }
209       }
210       while (!done);
211    }
212    //One Wire Access
213    owRelease(portnum,return_msg);
214    printf("%s",return_msg);
215    exit(0);
216
217    return 0;
218 }
219