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.
SX1509.cpp
00001 #include "SX1509.h" 00002 00003 const uint8_t onTReg[16] = {REGTON0, REGTON1, REGTON2, REGTON3, REGTON4, REGTON5, REGTON6, REGTON7, REGTON8, REGTON9, REGTON10, REGTON11, REGTON12, REGTON13, REGTON14, REGTON15}; 00004 00005 const uint8_t onIReg[16] = {REGION0, REGION1, REGION2, REGION3, REGION4, REGION5, REGION6, REGION7, REGION8, REGION9, REGION10, REGION11, REGION12, REGION13, REGION14, REGION15}; 00006 00007 const uint8_t offTReg[16] = {REGOFF0, REGOFF1, REGOFF2, REGOFF3, REGOFF4, REGOFF5, REGOFF6, REGOFF7, REGOFF8, REGOFF9, REGOFF10, REGOFF11, REGOFF12, REGOFF13, REGOFF14, REGOFF15}; 00008 00009 const uint8_t riseTReg[16] = {0,0,0,0, REGTRISE4, REGTRISE5, REGTRISE6, REGTRISE7, 0,0,0,0, REGTRISE12, REGTRISE13, REGTRISE14, REGTRISE15}; 00010 00011 const uint8_t fallTReg[16] = {0,0,0,0, REGTFALL4, REGTFALL5, REGTFALL6, REGTFALL7, 0,0,0,0, REGTFALL12, REGTFALL13, REGTFALL14, REGTFALL15}; 00012 00013 00014 SX1509::SX1509(PinName sda, PinName scl) : i2c(sda,scl) {} 00015 00016 void SX1509::i2cWrite8(char reg, char data){ 00017 char packet[2] = {reg ,data & 0xFF}; 00018 i2c.write(SX1509_ADDR,packet,2,false); 00019 wait(0.01); 00020 } 00021 00022 uint8_t SX1509::i2cRead8(char reg){ 00023 char packet[1] = {reg}; 00024 char data[1] = {0}; 00025 i2c.write(SX1509_ADDR,packet,1, true); 00026 i2c.read(SX1509_ADDR,data,1,false); 00027 wait(0.01); 00028 return (uint8_t)data[0]; 00029 } 00030 00031 uint16_t SX1509::i2cRead16(char reg){ 00032 char packet[1] = {reg}; 00033 char data[2] = {0,0}; 00034 i2c.write(SX1509_ADDR,packet,1, true); 00035 i2c.read(SX1509_ADDR,data,2, false); 00036 wait(0.01); 00037 return ((uint16_t)data[1] << 8) | (uint16_t)data[0]; 00038 } 00039 00040 bool SX1509::init(){ 00041 i2c.frequency(400000); 00042 uint8_t val = i2cRead8(REGINTERRUPTMASKA); //defaults to 0xFF 00043 if(val == 0xFF){ 00044 val = i2cRead8(REGSENSEHIGHB); //defaults to 0x00 00045 if(val == 0x00){ 00046 return 0; 00047 } 00048 } 00049 return 1; 00050 } 00051 00052 void SX1509::enableLEDDriver(){ 00053 enableLEDDriver(7, 0); 00054 } 00055 00056 void SX1509::enableLEDDriver(uint8_t div, bool mode){ 00057 if(div > 7)div=7; 00058 div_g = div; 00059 //REGCLOCK (0x1E) 00060 // 6:5 10 00061 // 01000000 = 0x40 00062 00063 //REGMISC (0x1F) 00064 // 7 0/1 00065 // 6:4 div 00066 // 3 0/1 00067 // 2 1 00068 // 1 1 00069 // 0 0 00070 //00000110 | (div<<4) 00071 //00000110 = 0x06 or 0x8E for log mode 00072 00073 i2cWrite8(REGCLOCK, 0x40); 00074 i2cWrite8(REGMISC, 0x06 | (div<<4) | (mode<<3) | (mode<<7)); 00075 } 00076 00077 void SX1509::setLEDMode(bool mode){ 00078 uint8_t buf = i2cRead8(REGMISC); 00079 if(mode==LINEAR){ 00080 buf &= ~(1<<3); 00081 buf &= ~(1<<7); 00082 } else { 00083 buf |= (1<<3); 00084 buf |= (1<<7); 00085 } 00086 i2cWrite8(REGMISC, buf); 00087 } 00088 00089 void SX1509::setMode(uint8_t pin, mode_t mode){ 00090 00091 // REGINPUTDISABLEA 1 0 0 00092 // REGPULLUPA 0 0 0 00093 // REGOPENDRAINA 0 1 1 00094 // REGDIRA 1 0 0 00095 00096 uint8_t inputReg, pullupReg, opendReg, dirReg, ledReg; 00097 00098 if(pin < 8){ 00099 inputReg = REGINPUTDISABLEA; 00100 pullupReg = REGPULLUPA; 00101 opendReg = REGOPENDRAINA; 00102 dirReg = REGDIRA; 00103 ledReg = REGLEDDRIVERENABLEA; 00104 } else { 00105 inputReg = REGINPUTDISABLEB; 00106 pullupReg = REGPULLUPB; 00107 opendReg = REGOPENDRAINB; 00108 dirReg = REGDIRB; 00109 ledReg = REGLEDDRIVERENABLEB; 00110 pin-=8; 00111 } 00112 00113 uint8_t buf; 00114 00115 switch(mode){ 00116 case INPUT: 00117 //TODO: Finish this 00118 break; 00119 case OUTPUT: 00120 //TODO: Finish this 00121 break; 00122 case LED: 00123 buf = i2cRead8(inputReg); 00124 buf &= ~(1<<pin); 00125 i2cWrite8(inputReg, buf); 00126 buf = i2cRead8(pullupReg); 00127 buf &= ~(1<<pin); 00128 i2cWrite8(pullupReg, buf); 00129 buf = i2cRead8(opendReg); 00130 buf |= (1<<pin); 00131 i2cWrite8(opendReg, buf); 00132 buf = i2cRead8(dirReg); 00133 buf &= ~(1<<pin); 00134 i2cWrite8(dirReg, buf); 00135 buf = i2cRead8(ledReg); 00136 buf |= (1<<pin); 00137 i2cWrite8(ledReg, buf); 00138 break; 00139 default: 00140 break; 00141 } 00142 } 00143 00144 void SX1509::setBreath(uint8_t pin, uint8_t fadeIn, uint8_t fadeOut){ 00145 // Clamp these 00146 fadeIn &= 0x1F; 00147 fadeOut &= 0x1F; 00148 i2cWrite8(riseTReg[pin],fadeIn); 00149 i2cWrite8(fallTReg[pin],fadeOut); 00150 } 00151 00152 void SX1509::setBlink(uint8_t pin, uint8_t tOn, uint8_t tOff, uint8_t iOff){ 00153 tOn &= 0x1F; 00154 tOff &= 0x1F; 00155 iOff &= 0x07; 00156 i2cWrite8(onTReg[pin],tOn); 00157 uint8_t buf = i2cRead8(offTReg[pin]) & 0x07; 00158 i2cWrite8(offTReg[pin],tOff<<3 | buf | iOff); 00159 } 00160 00161 void SX1509::writePin(uint8_t pin, uint8_t intensity){ 00162 i2cWrite8(onIReg[pin], intensity&0xFF); 00163 uint8_t dataReg; 00164 if(pin < 8){ 00165 dataReg = REGDATAA; 00166 } else { 00167 dataReg = REGDATAB; 00168 pin-=8; 00169 } 00170 uint8_t buf = i2cRead8(dataReg); 00171 if(intensity == 0) 00172 i2cWrite8(dataReg, buf | (1<<pin)); 00173 else 00174 i2cWrite8(dataReg, buf & ~(1<<pin)); 00175 } 00176 00177 void SX1509::setReset(){ 00178 uint8_t buf = i2cRead8(REGMISC); 00179 buf |= (1<<2); 00180 i2cWrite8(REGMISC, buf); 00181 } 00182 00183 void SX1509::fullReset(){ 00184 i2cWrite8(REGRESET, 0x12); 00185 i2cWrite8(REGRESET, 0x34); 00186 }
Generated on Sat Jul 16 2022 02:19:27 by
1.7.2