Library for I2C 4 channel relay module
Dependents: K9_Head_Controller
MultiChannelRelay.h@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 | multi_channel_relay.cpp |
SomeRandomBloke | 0:661beaa91d83 | 3 | Seeed multi channel relay Arduino 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 | |
SomeRandomBloke | 0:661beaa91d83 | 26 | #pragma once |
SomeRandomBloke | 0:661beaa91d83 | 27 | |
SomeRandomBloke | 0:661beaa91d83 | 28 | #ifndef MULTI_CHANNEL_RELAY_H |
SomeRandomBloke | 0:661beaa91d83 | 29 | #define MULTI_CHANNEL_RELAY_H |
SomeRandomBloke | 0:661beaa91d83 | 30 | |
SomeRandomBloke | 0:661beaa91d83 | 31 | #include "mbed.h" |
SomeRandomBloke | 0:661beaa91d83 | 32 | #define DEBUG_PRINT pc.printf |
SomeRandomBloke | 0:661beaa91d83 | 33 | |
SomeRandomBloke | 0:661beaa91d83 | 34 | |
SomeRandomBloke | 0:661beaa91d83 | 35 | #define CHANNLE1_BIT 0x01 |
SomeRandomBloke | 0:661beaa91d83 | 36 | #define CHANNLE2_BIT 0x02 |
SomeRandomBloke | 0:661beaa91d83 | 37 | #define CHANNLE3_BIT 0x04 |
SomeRandomBloke | 0:661beaa91d83 | 38 | #define CHANNLE4_BIT 0x08 |
SomeRandomBloke | 0:661beaa91d83 | 39 | #define CHANNLE5_BIT 0x10 |
SomeRandomBloke | 0:661beaa91d83 | 40 | #define CHANNLE6_BIT 0x20 |
SomeRandomBloke | 0:661beaa91d83 | 41 | #define CHANNLE7_BIT 0x40 |
SomeRandomBloke | 0:661beaa91d83 | 42 | #define CHANNLE8_BIT 0x80 |
SomeRandomBloke | 0:661beaa91d83 | 43 | |
SomeRandomBloke | 0:661beaa91d83 | 44 | #define CMD_CHANNEL_CTRL 0x10 |
SomeRandomBloke | 0:661beaa91d83 | 45 | #define CMD_SAVE_I2C_ADDR 0x11 |
SomeRandomBloke | 0:661beaa91d83 | 46 | #define CMD_READ_I2C_ADDR 0x12 |
SomeRandomBloke | 0:661beaa91d83 | 47 | #define CMD_READ_FIRMWARE_VER 0x13 |
SomeRandomBloke | 0:661beaa91d83 | 48 | |
SomeRandomBloke | 0:661beaa91d83 | 49 | class MultiChannelRelay |
SomeRandomBloke | 0:661beaa91d83 | 50 | { |
SomeRandomBloke | 0:661beaa91d83 | 51 | public: |
SomeRandomBloke | 0:661beaa91d83 | 52 | MultiChannelRelay(I2C *i2c, uint8_t slave_adr = 0x11); |
SomeRandomBloke | 0:661beaa91d83 | 53 | |
SomeRandomBloke | 0:661beaa91d83 | 54 | /** |
SomeRandomBloke | 0:661beaa91d83 | 55 | @brief Change device address from old_addr to new_addr. |
SomeRandomBloke | 0:661beaa91d83 | 56 | @param new_addr, the address to use. |
SomeRandomBloke | 0:661beaa91d83 | 57 | old_addr, the original address |
SomeRandomBloke | 0:661beaa91d83 | 58 | @return None |
SomeRandomBloke | 0:661beaa91d83 | 59 | */ |
SomeRandomBloke | 0:661beaa91d83 | 60 | void changeI2CAddress(uint8_t new_addr, uint8_t old_addr); |
SomeRandomBloke | 0:661beaa91d83 | 61 | |
SomeRandomBloke | 0:661beaa91d83 | 62 | /** |
SomeRandomBloke | 0:661beaa91d83 | 63 | /brief Get channel state |
SomeRandomBloke | 0:661beaa91d83 | 64 | /return One byte value to indicate channel state |
SomeRandomBloke | 0:661beaa91d83 | 65 | the bits range from 0 to 7 represents channel 1 to 8 |
SomeRandomBloke | 0:661beaa91d83 | 66 | */ |
SomeRandomBloke | 0:661beaa91d83 | 67 | uint8_t getChannelState(void); |
SomeRandomBloke | 0:661beaa91d83 | 68 | |
SomeRandomBloke | 0:661beaa91d83 | 69 | /** |
SomeRandomBloke | 0:661beaa91d83 | 70 | @brief Read firmware version from on board MCU |
SomeRandomBloke | 0:661beaa91d83 | 71 | @param |
SomeRandomBloke | 0:661beaa91d83 | 72 | @return Firmware version in byte |
SomeRandomBloke | 0:661beaa91d83 | 73 | */ |
SomeRandomBloke | 0:661beaa91d83 | 74 | uint8_t getFirmwareVersion(void); |
SomeRandomBloke | 0:661beaa91d83 | 75 | |
SomeRandomBloke | 0:661beaa91d83 | 76 | /** |
SomeRandomBloke | 0:661beaa91d83 | 77 | @brief Control relay channels |
SomeRandomBloke | 0:661beaa91d83 | 78 | @param state, use one Byte to represent 8 channel |
SomeRandomBloke | 0:661beaa91d83 | 79 | @return None |
SomeRandomBloke | 0:661beaa91d83 | 80 | */ |
SomeRandomBloke | 0:661beaa91d83 | 81 | void channelCtrl(uint8_t state); |
SomeRandomBloke | 0:661beaa91d83 | 82 | |
SomeRandomBloke | 0:661beaa91d83 | 83 | /** |
SomeRandomBloke | 0:661beaa91d83 | 84 | @brief Turn on one of 8 channels |
SomeRandomBloke | 0:661beaa91d83 | 85 | @param channel, channel to control with (range form 1 to 8) |
SomeRandomBloke | 0:661beaa91d83 | 86 | @return None |
SomeRandomBloke | 0:661beaa91d83 | 87 | */ |
SomeRandomBloke | 0:661beaa91d83 | 88 | void turn_on_channel(uint8_t channel); |
SomeRandomBloke | 0:661beaa91d83 | 89 | |
SomeRandomBloke | 0:661beaa91d83 | 90 | /** |
SomeRandomBloke | 0:661beaa91d83 | 91 | @brief Turn off on of 8 channels |
SomeRandomBloke | 0:661beaa91d83 | 92 | @param channel, channel to control with (range form 1 to 8) |
SomeRandomBloke | 0:661beaa91d83 | 93 | @return None |
SomeRandomBloke | 0:661beaa91d83 | 94 | */ |
SomeRandomBloke | 0:661beaa91d83 | 95 | void turn_off_channel(uint8_t channel); |
SomeRandomBloke | 0:661beaa91d83 | 96 | |
SomeRandomBloke | 0:661beaa91d83 | 97 | bool sendToDevice( char *cmd, int reqSize, int respSize, long msWait ); |
SomeRandomBloke | 0:661beaa91d83 | 98 | |
SomeRandomBloke | 0:661beaa91d83 | 99 | private: |
SomeRandomBloke | 0:661beaa91d83 | 100 | I2C *_i2c; |
SomeRandomBloke | 0:661beaa91d83 | 101 | int _i2cAddr; // This is the I2C address you want to use |
SomeRandomBloke | 0:661beaa91d83 | 102 | int channel_state; // Value to save channel state |
SomeRandomBloke | 0:661beaa91d83 | 103 | }; |
SomeRandomBloke | 0:661beaa91d83 | 104 | |
SomeRandomBloke | 0:661beaa91d83 | 105 | |
SomeRandomBloke | 0:661beaa91d83 | 106 | |
SomeRandomBloke | 0:661beaa91d83 | 107 | #endif |