Library for I2C 4 channel relay module
Dependents: K9_Head_Controller
Revision 0:661beaa91d83, committed 2020-09-08
- Comitter:
- SomeRandomBloke
- Date:
- Tue Sep 08 16:20:02 2020 +0000
- Commit message:
- Initial commit
Changed in this revision
MultiChannelRelay.cpp | Show annotated file Show diff for this revision Revisions of this file |
MultiChannelRelay.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 661beaa91d83 MultiChannelRelay.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MultiChannelRelay.cpp Tue Sep 08 16:20:02 2020 +0000 @@ -0,0 +1,126 @@ +/* + MultiChannelRelay.h + Seeed multi channel relay library + + Copyright (c) 2018 Seeed Technology Co., Ltd. + Author : lambor + Create Time : June 2018 + Change Log : + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "MultiChannelRelay.h" + +MultiChannelRelay::MultiChannelRelay(I2C *i2c, uint8_t slave_adr) + : _i2c(i2c) + //, _i2cAddr(slave_adr) +{ + channel_state = 0; + _i2cAddr = slave_adr << 1; +} + + +uint8_t MultiChannelRelay::getFirmwareVersion(void) +{ + char cmd[2]; + cmd[0] = CMD_READ_FIRMWARE_VER; + + sendToDevice( cmd, 1, 1, 10 ); + + return cmd[0]; +} + +void MultiChannelRelay::changeI2CAddress(uint8_t old_addr, uint8_t new_addr) +{ + char cmd[2]; + cmd[0] = CMD_SAVE_I2C_ADDR; + cmd[1] = new_addr; + + sendToDevice( cmd, 2, 0, 10 ); + _i2cAddr = new_addr; +} + +uint8_t MultiChannelRelay::getChannelState(void) +{ + return channel_state; +} + +void MultiChannelRelay::channelCtrl(uint8_t state) +{ + channel_state = state; + char cmd[2]; + cmd[0] = CMD_CHANNEL_CTRL; + cmd[1] = channel_state; + + sendToDevice( cmd, 2, 0, 10 ); +} + +void MultiChannelRelay::turn_on_channel(uint8_t channel) +{ + channel_state |= (1 << (channel - 1)); + char cmd[2]; + cmd[0] = CMD_CHANNEL_CTRL; + cmd[1] = channel_state; + + sendToDevice( cmd, 2, 0, 10 ); +} + +void MultiChannelRelay::turn_off_channel(uint8_t channel) +{ + channel_state &= ~(1 << (channel - 1)); + char cmd[2]; + cmd[0] = CMD_CHANNEL_CTRL; + cmd[1] = channel_state; + + sendToDevice( cmd, 2, 0, 10 ); +} + + +/** Send a request to the chip, perform a wait and get response + * @param cmd Pointer to character array containing request, no line endings needed + * @param reqSize Size of the request in characters + * @param respSize Size of the response in bytes, must be at least 1 character + * @param msWait Delay between sending request and reading response in mS + * @return true/false to indicate command was processed sucessfully or not, true success, false failure + */ +bool MultiChannelRelay::sendToDevice( char *cmd, int reqSize, int respSize, long msWait ) +{ + bool retstat = false; + int result = -10; + _i2c->frequency(100000); + _i2c->lock(); + result = _i2c->write(_i2cAddr, cmd, reqSize); + if ( result ) { + _i2c->unlock(); + return retstat; + } + + wait_us( msWait * 100 ); + if( respSize == 0 ) { + _i2c->unlock(); + return true; + } + + result = _i2c->read(_i2cAddr, cmd, respSize); + _i2c->unlock(); + if ( result ) { + } else + retstat = true; + + return retstat; +} + +
diff -r 000000000000 -r 661beaa91d83 MultiChannelRelay.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MultiChannelRelay.h Tue Sep 08 16:20:02 2020 +0000 @@ -0,0 +1,107 @@ +/* + multi_channel_relay.cpp + Seeed multi channel relay Arduino library + + Copyright (c) 2018 Seeed Technology Co., Ltd. + Author : lambor + Create Time : June 2018 + Change Log : + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + + +#pragma once + +#ifndef MULTI_CHANNEL_RELAY_H +#define MULTI_CHANNEL_RELAY_H + +#include "mbed.h" +#define DEBUG_PRINT pc.printf + + +#define CHANNLE1_BIT 0x01 +#define CHANNLE2_BIT 0x02 +#define CHANNLE3_BIT 0x04 +#define CHANNLE4_BIT 0x08 +#define CHANNLE5_BIT 0x10 +#define CHANNLE6_BIT 0x20 +#define CHANNLE7_BIT 0x40 +#define CHANNLE8_BIT 0x80 + +#define CMD_CHANNEL_CTRL 0x10 +#define CMD_SAVE_I2C_ADDR 0x11 +#define CMD_READ_I2C_ADDR 0x12 +#define CMD_READ_FIRMWARE_VER 0x13 + +class MultiChannelRelay +{ +public: + MultiChannelRelay(I2C *i2c, uint8_t slave_adr = 0x11); + + /** + @brief Change device address from old_addr to new_addr. + @param new_addr, the address to use. + old_addr, the original address + @return None + */ + void changeI2CAddress(uint8_t new_addr, uint8_t old_addr); + + /** + /brief Get channel state + /return One byte value to indicate channel state + the bits range from 0 to 7 represents channel 1 to 8 + */ + uint8_t getChannelState(void); + + /** + @brief Read firmware version from on board MCU + @param + @return Firmware version in byte + */ + uint8_t getFirmwareVersion(void); + + /** + @brief Control relay channels + @param state, use one Byte to represent 8 channel + @return None + */ + void channelCtrl(uint8_t state); + + /** + @brief Turn on one of 8 channels + @param channel, channel to control with (range form 1 to 8) + @return None + */ + void turn_on_channel(uint8_t channel); + + /** + @brief Turn off on of 8 channels + @param channel, channel to control with (range form 1 to 8) + @return None + */ + void turn_off_channel(uint8_t channel); + + bool sendToDevice( char *cmd, int reqSize, int respSize, long msWait ); + +private: + I2C *_i2c; + int _i2cAddr; // This is the I2C address you want to use + int channel_state; // Value to save channel state +}; + + + +#endif \ No newline at end of file