1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 // Except as contained in this notice, the name of Dallas Semiconductor
23 // shall not be used except as stated in the Dallas Semiconductor
25 //--------------------------------------------------------------------------
27 // swt12.c - Modifies Channel A and B and returns info byte data for
28 // the DS2406 and DS2407.
37 //----------------------------------------------------------------------
38 // SUBROUTINE - ReadSwitch12
40 // This routine gets the Channel Info Byte and returns it.
42 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
43 // OpenCOM to indicate the port number.
44 // 'ClearActivity' - To reset the button
46 // Returns: (-1) If the Channel Info Byte could not be read.
47 // (Info Byte) If the Channel Info Byte could be read.
49 int ReadSwitch12(int portnum, int ClearActivity)
51 int rt=-1; //this is the return value depending if the byte was read
52 int trans_cnt=0; //this is the counter for the number of bytes to send
53 uchar transfer[30]; //this is the whole block of byte info
55 // access and verify it is there
56 if (owAccess(portnum))
61 // channel access command
62 transfer[trans_cnt++] = 0xF5;
63 docrc16(portnum,0xF5);
68 transfer[trans_cnt++] = 0xD5;
69 docrc16(portnum,0xD5);
73 transfer[trans_cnt++] = 0x55;
74 docrc16(portnum,0x55);
77 transfer[trans_cnt++] = 0xFF;
78 docrc16(portnum,0xFF);
81 transfer[trans_cnt++] = 0xFF;
84 transfer[trans_cnt++] = 0xFF;
85 transfer[trans_cnt++] = 0xFF;
86 transfer[trans_cnt++] = 0xFF;
88 if (owBlock(portnum,FALSE,transfer,trans_cnt))
91 // read a dummy read byte and CRC16
92 docrc16(portnum,transfer[trans_cnt-4]);
93 docrc16(portnum,transfer[trans_cnt-3]);
94 docrc16(portnum,transfer[trans_cnt-2]);
95 if(docrc16(portnum,transfer[trans_cnt-1]) != 0xB001)
105 //----------------------------------------------------------------------
106 // SUBROUTINE - SetSwitch12
108 // This routine sets the channel state of the specified DS2406
110 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
111 // OpenCOM to indicate the port number.
112 // 'SerialNum' - Serial Number of DS2406 to set the switch state
113 // 'State' - Is a type containing what to set A and/or B to. It
114 // also contains the other fields that maybe written later
116 // Returns: TRUE(1) State of DS2406 set and verified
117 // FALSE(0) could not set the DS2406, perhaps device is not
120 int SetSwitch12(int portnum, uchar *SerialNum, SwitchProps *State)
124 uchar send_block[30];
129 // set the device serial number to the counter device
130 owSerialNum(portnum,SerialNum,FALSE);
133 if (owAccess(portnum))
135 // create a block to send that reads the counter
136 // write status command
137 send_block[send_cnt++] = 0x55;
138 docrc16(portnum,0x55);
140 // address of switch state
141 send_block[send_cnt++] = 0x07;
142 docrc16(portnum,0x07);
143 send_block[send_cnt++] = 0x00;
144 docrc16(portnum,0x00);
148 if(!State->Chan_B) st |= 0x40;
149 if(!State->Chan_A) st |= 0x20;
150 // more ifs can be added here for the other fields.
152 send_block[send_cnt++] = (uchar)st;
156 send_block[send_cnt++] = 0xFF;
157 send_block[send_cnt++] = 0xFF;
159 // now send the block
160 if (owBlock(portnum,FALSE,send_block,send_cnt))
162 // perform the CRC16 on the last 2 bytes of packet
163 docrc16(portnum,send_block[send_cnt-2]);
165 // verify crc16 is correct
166 if(docrc16(portnum,send_block[send_cnt-1]) == 0xB001)
171 // return the result flag rt
176 //----------------------------------------------------------------------
177 // SUBROUTINE - SwitchStateToString12
179 // This routine uses the info byte to return a string with all the data.
181 // 'infobyte' - This is the information byte data from the hardware.
182 // 'outstr' - This will be the output string. It gets set in the
185 int SwitchStateToString12(int infobyte, char *outstr)
192 cnt += sprintf(outstr+cnt, "%s", "Channel A and B\n");
195 cnt += sprintf(outstr+cnt, "%s", "Supply\n");
197 cnt += sprintf(outstr+cnt, "%s", "No Supply\n");
200 cnt += sprintf(outstr+cnt, "%s", "Activity on PIO-B\n");
202 cnt += sprintf(outstr+cnt, "%s", "No activity on PIO-B\n");
205 cnt += sprintf(outstr+cnt, "%s", "Activity on PIO-A\n");
207 cnt += sprintf(outstr+cnt, "%s", "No activity on PIO-A\n");
210 cnt += sprintf(outstr+cnt, "%s", "Hi level on PIO B\n");
212 cnt += sprintf(outstr+cnt, "%s", "Lo level on PIO B\n");
215 cnt += sprintf(outstr+cnt, "%s", "Hi level on PIO A\n");
217 cnt += sprintf(outstr+cnt, "%s", "Lo level on PIO A\n");
220 cnt += sprintf(outstr+cnt, "%s", "Channel B off\n");
222 cnt += sprintf(outstr+cnt, "%s", "Channel B on\n");
225 cnt += sprintf(outstr+cnt, "%s", "Channel A off\n");
227 cnt += sprintf(outstr+cnt, "%s", "Channel A on\n");
231 cnt += sprintf(outstr+cnt, "%s", "Channel A\n");
234 cnt += sprintf(outstr+cnt, "%s", "Supply\n");
236 cnt += sprintf(outstr+cnt, "%s", "No Supply\n");
239 cnt += sprintf(outstr+cnt, "%s", "Activity on PIO-A\n");
241 cnt += sprintf(outstr+cnt, "%s", "No activity on PIO-A\n");
244 cnt += sprintf(outstr+cnt, "%s", "Hi level on PIO A\n");
246 cnt += sprintf(outstr+cnt, "%s", "Lo level on PIO A\n");
249 cnt += sprintf(outstr+cnt, "%s", "Channel A off\n");
251 cnt += sprintf(outstr+cnt, "%s", "Channel A on\n");