|
| 1 | +/* |
| 2 | +***************************************************************************** |
| 3 | +@file Deneyap_DokunmatikTusTakimi.cpp |
| 4 | +@mainpage Deneyap Keypad Arduino library source file |
| 5 | +@maintainer RFtek Electronics <[email protected]> |
| 6 | +@version v1.0.0 |
| 7 | +@date June 23, 2022 |
| 8 | +@brief Includes functions to control Deneyap Keypad |
| 9 | + Arduino library |
| 10 | +
|
| 11 | +Library includes: |
| 12 | +--> Configuration functions |
| 13 | +--> Data manipulation functions |
| 14 | +--> I2C communication functions |
| 15 | +***************************************************************************** |
| 16 | +*/ |
| 17 | + |
| 18 | +#include "Deneyap_DokunmatikTusTakimi.h" |
| 19 | + |
| 20 | +/* Device Status Functions ---------------------------------------------------*/ |
| 21 | + |
| 22 | +/** |
| 23 | +* @brief Configures I2C connection with user defined address and port |
| 24 | +* @param address: I2C address of the device, port: I2C port number (0 or 1) |
| 25 | +* @retval connection status (1 --> connected, 0 --> not connected) |
| 26 | +*/ |
| 27 | +bool Keypad::begin(uint8_t address, TwoWire &port) |
| 28 | +{ |
| 29 | + Wire.begin(SDA_PIN,SCL_PIN); |
| 30 | + _i2cAddress = address; |
| 31 | + _i2cPort = &port; |
| 32 | + _dataPacket = {0}; |
| 33 | + |
| 34 | + return isConnected(); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | +* @brief Checks whether I2C connection established |
| 39 | +* @param None |
| 40 | +* @retval connection status (1 --> connected, 0 --> not connected) |
| 41 | +*/ |
| 42 | +bool Keypad::isConnected() |
| 43 | +{ |
| 44 | + _i2cPort->beginTransmission(_i2cAddress); |
| 45 | + |
| 46 | + if(_i2cPort->endTransmission() == 0) |
| 47 | + return true; |
| 48 | + return false; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | +* @brief Requests firmware version of the device |
| 53 | +* @param None |
| 54 | +* @retval Device firmware version |
| 55 | +*/ |
| 56 | +uint16_t Keypad::getFwVersion() |
| 57 | +{ |
| 58 | + _dataPacket.command = KEYPAD_REQUEST_FW_VERSION; |
| 59 | + _dataPacket.dataSize = 0; |
| 60 | + return I2C_ReadData16bit(&_dataPacket); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | +* @brief Sets (changes) I2C address of the device |
| 65 | +* @param address: I2C address to be set |
| 66 | +* @retval Transmission status (1 --> No error, Otherwise --> Transmission error) |
| 67 | +*/ |
| 68 | + |
| 69 | +bool Keypad::setI2Caddress(uint8_t newAddress) |
| 70 | +{ |
| 71 | + _dataPacket.command = KEYPAD_CHANGE_ADDR; |
| 72 | + _dataPacket.dataSize = 1; |
| 73 | + _dataPacket.data[0] = newAddress; |
| 74 | + |
| 75 | + bool status = I2C_SendDataPacket(&_dataPacket); |
| 76 | + |
| 77 | + if(status == true) |
| 78 | + { |
| 79 | + _i2cAddress = newAddress; |
| 80 | + return true; |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + return false; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/* I2C Data Manipulation Functions -------------------------------------------*/ |
| 89 | + |
| 90 | +/** |
| 91 | + * @brief Reads the keypad pins. |
| 92 | + * @param None |
| 93 | + * @retval uint8_t : Keypad pin. |
| 94 | + */ |
| 95 | +uint8_t Keypad::KeypadRead(void) |
| 96 | +{ |
| 97 | + _dataPacket.command = (uint8_t)KEYPAD_READ; |
| 98 | + _dataPacket.dataSize = 0x00; |
| 99 | + return I2C_ReadData8bit(&_dataPacket); |
| 100 | +} |
| 101 | + |
| 102 | +/* I2C Data Transaction Funstions --------------------------------------------*/ |
| 103 | + |
| 104 | +/** |
| 105 | +* @brief Reads 8bit data from I2C interface |
| 106 | +* @param dataPacket: includes protocol to request data |
| 107 | +* @retval I2C 8bit data |
| 108 | +*/ |
| 109 | +uint8_t Keypad::I2C_ReadData8bit(Keypad_DataPacket_TypeDef* dataPacket) |
| 110 | +{ |
| 111 | + _i2cPort->beginTransmission(_i2cAddress); |
| 112 | + _i2cPort->write(dataPacket->command); |
| 113 | + _i2cPort->endTransmission(); |
| 114 | + |
| 115 | + if(_i2cPort->requestFrom(_i2cAddress, static_cast<uint8_t>(1)) != 0) |
| 116 | + return _i2cPort->read(); |
| 117 | + return 0; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | +* @brief Reads 16bit data from I2C interface |
| 122 | +* @param dataPacket: includes protocol to request data |
| 123 | +* @retval I2C 16bit data |
| 124 | +*/ |
| 125 | +uint16_t Keypad::I2C_ReadData16bit(Keypad_DataPacket_TypeDef* dataPacket) |
| 126 | +{ |
| 127 | + _i2cPort->beginTransmission(_i2cAddress); |
| 128 | + _i2cPort->write(dataPacket->command); |
| 129 | + _i2cPort->endTransmission(); |
| 130 | + |
| 131 | + if(_i2cPort->requestFrom(_i2cAddress, static_cast<uint8_t>(2)) != 0) |
| 132 | + { |
| 133 | + uint16_t i2cData = _i2cPort->read(); |
| 134 | + i2cData |= (_i2cPort->read() << 8); |
| 135 | + return i2cData; |
| 136 | + } |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | +* @brief Sends data packet based on a custom defined protocol |
| 142 | +* @param dataPacket: includes all related data |
| 143 | +* @retval Transmission status (1 --> No error, Otherwise --> Transmission error) |
| 144 | +*/ |
| 145 | +bool Keypad::I2C_SendDataPacket(Keypad_DataPacket_TypeDef* dataPacket) |
| 146 | +{ |
| 147 | + _i2cPort->beginTransmission(_i2cAddress); |
| 148 | + _i2cPort->write(dataPacket->command); |
| 149 | + _i2cPort->write(dataPacket->dataSize); |
| 150 | + |
| 151 | + for(uint8_t i = 0; i < _dataPacket.dataSize; i++) |
| 152 | + _i2cPort->write(_dataPacket.data[i]); |
| 153 | + |
| 154 | + if(_i2cPort->endTransmission() == 0) |
| 155 | + return true; |
| 156 | + return false; |
| 157 | +} |
0 commit comments