Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Digitalio.h
00001 /* 00002 * Digitalio.h 00003 * 00004 * Created on: Jul 9, 2018 00005 * Author: Claudio Pinheiro 00006 */ 00007 00008 #ifndef PLC_DIGITALIO_H_ 00009 #define PLC_DIGITALIO_H_ 00010 00011 #include <bitset> 00012 #include "PCA9555.h" 00013 #include "mbed.h" 00014 00015 #define SAMPLER_PERIOD_MS 20 00016 00017 struct IO { 00018 PCA9555 PCA_A; 00019 PCA9555 PCA_B; 00020 IO() {}; 00021 IO(I2C *i2cBus, int address1, int address2) : PCA_A(i2cBus, address1), PCA_B(i2cBus, address2) { 00022 PCA_A.configDirAll(0xFF, 0x00); 00023 PCA_B.configDirAll(0xFF, 0x00); 00024 } 00025 int writeWord(short value) { 00026 if ( PCA_A.write(PCA9555_PORT1, ~value & 0xFF) ) return 51;; 00027 if ( PCA_B.write(PCA9555_PORT1, (~value >> 8) & 0xFF) ) return 52; 00028 return 0; 00029 } 00030 int readWord(short *value) { 00031 char m, l; 00032 if (PCA_A.read(PCA9555_PORT0, &l)) return 51; 00033 if (PCA_B.read(PCA9555_PORT0, &m)) return 52; 00034 *value = ~(l | (m << 8)); 00035 return 0; 00036 } 00037 }; 00038 00039 class DigitalIO { 00040 public: 00041 DigitalIO(); 00042 virtual ~DigitalIO(); 00043 00044 std::bitset<16>::reference operator[] (std::size_t idx) { 00045 _mutex.lock(); 00046 return ioBuffer[idx/16][idx & 0xF]; 00047 _mutex.unlock(); 00048 } 00049 std::bitset<16> const operator[] (std::size_t idx) const { 00050 return ioBuffer[idx/16][idx & 0xF]; 00051 } 00052 00053 bool get(std::size_t idx) { 00054 return ioBuffer[idx/16][idx & 0xF]; 00055 } 00056 00057 void set(std::size_t idx, bool value = true) { 00058 ioBuffer[idx/16][idx & 0xF] = value; 00059 } 00060 00061 void clear(std::size_t idx, bool value = false) { 00062 ioBuffer[idx/16][idx & 0xF] = value; 00063 } 00064 00065 std::string toString() { 00066 return ioBuffer[5].to_string<char,std::string::traits_type,std::string::allocator_type>(); 00067 } 00068 00069 unsigned long get_ulong(std::size_t idx) { 00070 return ioBuffer[idx].to_ulong(); 00071 } 00072 00073 00074 private: 00075 I2C i2cBus1; 00076 I2C i2cBus2; 00077 IO *boards[5]; 00078 IO PLC_IO; 00079 IO EXT0_IO; 00080 IO EXT1_IO; 00081 IO EXT2_IO; 00082 IO EXT3_IO; 00083 00084 Serial pc; 00085 00086 std::bitset<16> inBuffer_[5]; 00087 // short inBuffer[5]; 00088 std::bitset<16> outBuffer[5]; 00089 00090 std::bitset<16> ioBuffer[10]; 00091 00092 private: 00093 Ticker inputTicker; 00094 void inputReader(void); 00095 Ticker outputTicker; 00096 void outputWriter(void); 00097 00098 Mutex _mutex; 00099 00100 }; 00101 00102 #endif /* PLC_DIGITALIO_H_ */
Generated on Mon Jul 25 2022 15:13:17 by
1.7.2