Library for I2C 4 channel relay module
Dependents: K9_Head_Controller
MultiChannelRelay.cpp@0:661beaa91d83, 2020-09-08 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Tue Sep 08 16:20:02 2020 +0000
- Revision:
- 0:661beaa91d83
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:661beaa91d83 | 1 | /* |
SomeRandomBloke | 0:661beaa91d83 | 2 | MultiChannelRelay.h |
SomeRandomBloke | 0:661beaa91d83 | 3 | Seeed multi channel relay library |
SomeRandomBloke | 0:661beaa91d83 | 4 | |
SomeRandomBloke | 0:661beaa91d83 | 5 | Copyright (c) 2018 Seeed Technology Co., Ltd. |
SomeRandomBloke | 0:661beaa91d83 | 6 | Author : lambor |
SomeRandomBloke | 0:661beaa91d83 | 7 | Create Time : June 2018 |
SomeRandomBloke | 0:661beaa91d83 | 8 | Change Log : |
SomeRandomBloke | 0:661beaa91d83 | 9 | |
SomeRandomBloke | 0:661beaa91d83 | 10 | This library is free software; you can redistribute it and/or |
SomeRandomBloke | 0:661beaa91d83 | 11 | modify it under the terms of the GNU Lesser General Public |
SomeRandomBloke | 0:661beaa91d83 | 12 | License as published by the Free Software Foundation; either |
SomeRandomBloke | 0:661beaa91d83 | 13 | version 2.1 of the License, or (at your option) any later version. |
SomeRandomBloke | 0:661beaa91d83 | 14 | |
SomeRandomBloke | 0:661beaa91d83 | 15 | This library is distributed in the hope that it will be useful, |
SomeRandomBloke | 0:661beaa91d83 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
SomeRandomBloke | 0:661beaa91d83 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
SomeRandomBloke | 0:661beaa91d83 | 18 | Lesser General Public License for more details. |
SomeRandomBloke | 0:661beaa91d83 | 19 | |
SomeRandomBloke | 0:661beaa91d83 | 20 | You should have received a copy of the GNU Lesser General Public |
SomeRandomBloke | 0:661beaa91d83 | 21 | License along with this library; if not, write to the Free Software |
SomeRandomBloke | 0:661beaa91d83 | 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
SomeRandomBloke | 0:661beaa91d83 | 23 | */ |
SomeRandomBloke | 0:661beaa91d83 | 24 | |
SomeRandomBloke | 0:661beaa91d83 | 25 | #include "MultiChannelRelay.h" |
SomeRandomBloke | 0:661beaa91d83 | 26 | |
SomeRandomBloke | 0:661beaa91d83 | 27 | MultiChannelRelay::MultiChannelRelay(I2C *i2c, uint8_t slave_adr) |
SomeRandomBloke | 0:661beaa91d83 | 28 | : _i2c(i2c) |
SomeRandomBloke | 0:661beaa91d83 | 29 | //, _i2cAddr(slave_adr) |
SomeRandomBloke | 0:661beaa91d83 | 30 | { |
SomeRandomBloke | 0:661beaa91d83 | 31 | channel_state = 0; |
SomeRandomBloke | 0:661beaa91d83 | 32 | _i2cAddr = slave_adr << 1; |
SomeRandomBloke | 0:661beaa91d83 | 33 | } |
SomeRandomBloke | 0:661beaa91d83 | 34 | |
SomeRandomBloke | 0:661beaa91d83 | 35 | |
SomeRandomBloke | 0:661beaa91d83 | 36 | uint8_t MultiChannelRelay::getFirmwareVersion(void) |
SomeRandomBloke | 0:661beaa91d83 | 37 | { |
SomeRandomBloke | 0:661beaa91d83 | 38 | char cmd[2]; |
SomeRandomBloke | 0:661beaa91d83 | 39 | cmd[0] = CMD_READ_FIRMWARE_VER; |
SomeRandomBloke | 0:661beaa91d83 | 40 | |
SomeRandomBloke | 0:661beaa91d83 | 41 | sendToDevice( cmd, 1, 1, 10 ); |
SomeRandomBloke | 0:661beaa91d83 | 42 | |
SomeRandomBloke | 0:661beaa91d83 | 43 | return cmd[0]; |
SomeRandomBloke | 0:661beaa91d83 | 44 | } |
SomeRandomBloke | 0:661beaa91d83 | 45 | |
SomeRandomBloke | 0:661beaa91d83 | 46 | void MultiChannelRelay::changeI2CAddress(uint8_t old_addr, uint8_t new_addr) |
SomeRandomBloke | 0:661beaa91d83 | 47 | { |
SomeRandomBloke | 0:661beaa91d83 | 48 | char cmd[2]; |
SomeRandomBloke | 0:661beaa91d83 | 49 | cmd[0] = CMD_SAVE_I2C_ADDR; |
SomeRandomBloke | 0:661beaa91d83 | 50 | cmd[1] = new_addr; |
SomeRandomBloke | 0:661beaa91d83 | 51 | |
SomeRandomBloke | 0:661beaa91d83 | 52 | sendToDevice( cmd, 2, 0, 10 ); |
SomeRandomBloke | 0:661beaa91d83 | 53 | _i2cAddr = new_addr; |
SomeRandomBloke | 0:661beaa91d83 | 54 | } |
SomeRandomBloke | 0:661beaa91d83 | 55 | |
SomeRandomBloke | 0:661beaa91d83 | 56 | uint8_t MultiChannelRelay::getChannelState(void) |
SomeRandomBloke | 0:661beaa91d83 | 57 | { |
SomeRandomBloke | 0:661beaa91d83 | 58 | return channel_state; |
SomeRandomBloke | 0:661beaa91d83 | 59 | } |
SomeRandomBloke | 0:661beaa91d83 | 60 | |
SomeRandomBloke | 0:661beaa91d83 | 61 | void MultiChannelRelay::channelCtrl(uint8_t state) |
SomeRandomBloke | 0:661beaa91d83 | 62 | { |
SomeRandomBloke | 0:661beaa91d83 | 63 | channel_state = state; |
SomeRandomBloke | 0:661beaa91d83 | 64 | char cmd[2]; |
SomeRandomBloke | 0:661beaa91d83 | 65 | cmd[0] = CMD_CHANNEL_CTRL; |
SomeRandomBloke | 0:661beaa91d83 | 66 | cmd[1] = channel_state; |
SomeRandomBloke | 0:661beaa91d83 | 67 | |
SomeRandomBloke | 0:661beaa91d83 | 68 | sendToDevice( cmd, 2, 0, 10 ); |
SomeRandomBloke | 0:661beaa91d83 | 69 | } |
SomeRandomBloke | 0:661beaa91d83 | 70 | |
SomeRandomBloke | 0:661beaa91d83 | 71 | void MultiChannelRelay::turn_on_channel(uint8_t channel) |
SomeRandomBloke | 0:661beaa91d83 | 72 | { |
SomeRandomBloke | 0:661beaa91d83 | 73 | channel_state |= (1 << (channel - 1)); |
SomeRandomBloke | 0:661beaa91d83 | 74 | char cmd[2]; |
SomeRandomBloke | 0:661beaa91d83 | 75 | cmd[0] = CMD_CHANNEL_CTRL; |
SomeRandomBloke | 0:661beaa91d83 | 76 | cmd[1] = channel_state; |
SomeRandomBloke | 0:661beaa91d83 | 77 | |
SomeRandomBloke | 0:661beaa91d83 | 78 | sendToDevice( cmd, 2, 0, 10 ); |
SomeRandomBloke | 0:661beaa91d83 | 79 | } |
SomeRandomBloke | 0:661beaa91d83 | 80 | |
SomeRandomBloke | 0:661beaa91d83 | 81 | void MultiChannelRelay::turn_off_channel(uint8_t channel) |
SomeRandomBloke | 0:661beaa91d83 | 82 | { |
SomeRandomBloke | 0:661beaa91d83 | 83 | channel_state &= ~(1 << (channel - 1)); |
SomeRandomBloke | 0:661beaa91d83 | 84 | char cmd[2]; |
SomeRandomBloke | 0:661beaa91d83 | 85 | cmd[0] = CMD_CHANNEL_CTRL; |
SomeRandomBloke | 0:661beaa91d83 | 86 | cmd[1] = channel_state; |
SomeRandomBloke | 0:661beaa91d83 | 87 | |
SomeRandomBloke | 0:661beaa91d83 | 88 | sendToDevice( cmd, 2, 0, 10 ); |
SomeRandomBloke | 0:661beaa91d83 | 89 | } |
SomeRandomBloke | 0:661beaa91d83 | 90 | |
SomeRandomBloke | 0:661beaa91d83 | 91 | |
SomeRandomBloke | 0:661beaa91d83 | 92 | /** Send a request to the chip, perform a wait and get response |
SomeRandomBloke | 0:661beaa91d83 | 93 | * @param cmd Pointer to character array containing request, no line endings needed |
SomeRandomBloke | 0:661beaa91d83 | 94 | * @param reqSize Size of the request in characters |
SomeRandomBloke | 0:661beaa91d83 | 95 | * @param respSize Size of the response in bytes, must be at least 1 character |
SomeRandomBloke | 0:661beaa91d83 | 96 | * @param msWait Delay between sending request and reading response in mS |
SomeRandomBloke | 0:661beaa91d83 | 97 | * @return true/false to indicate command was processed sucessfully or not, true success, false failure |
SomeRandomBloke | 0:661beaa91d83 | 98 | */ |
SomeRandomBloke | 0:661beaa91d83 | 99 | bool MultiChannelRelay::sendToDevice( char *cmd, int reqSize, int respSize, long msWait ) |
SomeRandomBloke | 0:661beaa91d83 | 100 | { |
SomeRandomBloke | 0:661beaa91d83 | 101 | bool retstat = false; |
SomeRandomBloke | 0:661beaa91d83 | 102 | int result = -10; |
SomeRandomBloke | 0:661beaa91d83 | 103 | _i2c->frequency(100000); |
SomeRandomBloke | 0:661beaa91d83 | 104 | _i2c->lock(); |
SomeRandomBloke | 0:661beaa91d83 | 105 | result = _i2c->write(_i2cAddr, cmd, reqSize); |
SomeRandomBloke | 0:661beaa91d83 | 106 | if ( result ) { |
SomeRandomBloke | 0:661beaa91d83 | 107 | _i2c->unlock(); |
SomeRandomBloke | 0:661beaa91d83 | 108 | return retstat; |
SomeRandomBloke | 0:661beaa91d83 | 109 | } |
SomeRandomBloke | 0:661beaa91d83 | 110 | |
SomeRandomBloke | 0:661beaa91d83 | 111 | wait_us( msWait * 100 ); |
SomeRandomBloke | 0:661beaa91d83 | 112 | if( respSize == 0 ) { |
SomeRandomBloke | 0:661beaa91d83 | 113 | _i2c->unlock(); |
SomeRandomBloke | 0:661beaa91d83 | 114 | return true; |
SomeRandomBloke | 0:661beaa91d83 | 115 | } |
SomeRandomBloke | 0:661beaa91d83 | 116 | |
SomeRandomBloke | 0:661beaa91d83 | 117 | result = _i2c->read(_i2cAddr, cmd, respSize); |
SomeRandomBloke | 0:661beaa91d83 | 118 | _i2c->unlock(); |
SomeRandomBloke | 0:661beaa91d83 | 119 | if ( result ) { |
SomeRandomBloke | 0:661beaa91d83 | 120 | } else |
SomeRandomBloke | 0:661beaa91d83 | 121 | retstat = true; |
SomeRandomBloke | 0:661beaa91d83 | 122 | |
SomeRandomBloke | 0:661beaa91d83 | 123 | return retstat; |
SomeRandomBloke | 0:661beaa91d83 | 124 | } |
SomeRandomBloke | 0:661beaa91d83 | 125 | |
SomeRandomBloke | 0:661beaa91d83 | 126 |