Lucas Citolin / Mbed OS Autoline
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCA9555.cpp Source File

PCA9555.cpp

00001 /* ICTS PLC Board Library
00002  *
00003  * PCA9555.cpp
00004  *
00005  *  Created on: 24 de mai de 2018
00006  *      Author: Claudio Pinheiro
00007  */
00008 
00009 #include "PCA9555.h"
00010 
00011 PCA9555::PCA9555(I2C *i2cBus, int address) : i2c_bus(i2cBus) {
00012     i2c_address = FIXED_ADDRESS | ((char)address << 1);
00013 }
00014 
00015 PCA9555::~PCA9555() {
00016     // TODO Auto-generated destructor stub
00017 }
00018 
00019 /** Configure the directions of the I/O pins for both
00020  * port 0 and 1.
00021  *
00022  *  @param configPort0  Configuration for Port0
00023  *  @param configPort1  Configuration for Port1
00024  */
00025 void PCA9555::configDirAll(char configPort0, char configPort1) {
00026     cmd[0] = COMMAND_CONFIGURATION_PORT0;
00027     cmd[1] = configPort0;
00028     cmd[2] = configPort1;
00029     i2c_bus->write(i2c_address, cmd, 3);
00030 }
00031 
00032 int PCA9555::write(PCA9555_Port_enum port, char value) {
00033     cmd[0] = COMMAND_OUTPUT_PORT0 + port;
00034     cmd[1] = value;
00035     return i2c_bus->write(i2c_address, cmd, 2, false);
00036 }
00037 
00038 char PCA9555::read(PCA9555_Port_enum port) {
00039     cmd[0] = COMMAND_INPUT_PORT0 + port;
00040     i2c_bus->write(i2c_address, cmd, 1, false);
00041     if (i2c_bus->read(i2c_address, cmd, 1, false)) {
00042         return 0xFF;
00043     }
00044     return cmd[0];
00045 }
00046 
00047 int PCA9555::read(PCA9555_Port_enum port, char *value) {
00048     int result;
00049     cmd[0] = COMMAND_INPUT_PORT0 + port;
00050     result = i2c_bus->write(i2c_address, cmd, 1, false);
00051     if (result) return result;
00052 
00053     return i2c_bus->read(i2c_address, value, 1, false);
00054 }
00055 
00056 int PCA9555::setBit(PCA9555_Port_enum port, char mask) {
00057     return this->write( port, this->read(port) | mask);
00058 }
00059 
00060 int PCA9555::clearBit(PCA9555_Port_enum port, char mask) {
00061     return this->write( port, this->read(port) & ~mask);
00062 }