SRK Version of mDot LoRa_Sensormode_SRK
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Sensornode by
SI1143.cpp
00001 /* 00002 * SI1143.cpp 00003 * 00004 * Created on: Jun 2, 2016 00005 * Author: Adrian 00006 */ 00007 00008 #include "SI1143.h " 00009 00010 SI1143::SI1143(I2C_RT* i2c) 00011 { 00012 this->i2c = i2c; 00013 this->config = new SI1143Config(); 00014 } 00015 00016 void SI1143::init(SI1143_MODE desiredMode){ 00017 config->build(desiredMode); 00018 restart(); 00019 } 00020 00021 void SI1143::restart() 00022 { 00023 command(SI1143_RESET); 00024 osDelay(30); 00025 uint8_t writeValue; 00026 00027 // Setting up LED Power to full 00028 writeValue = SI1143_HW_KEY_VAL0; 00029 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_HW_KEY,false,&writeValue,1); 00030 writeValue = config->getLed1Voltage(); 00031 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_PS_LED21,false,&writeValue,1); 00032 writeValue = 0x0A; 00033 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_PS_LED3,false,&writeValue,1); 00034 writeValue = SI1143_ALS_IR_TASK + SI1143_ALS_VIS_TASK + SI1143_PS1_TASK + 00035 SI1143_PS2_TASK + SI1143_PS3_TASK; 00036 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_PARAM_WR,false,&writeValue,1); 00037 command(SI1143_PARAM_SET + (SI1143_CHLIST & 0x1F)); 00038 writeValue = 0x04; 00039 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_PARAM_WR,false,&writeValue,1); 00040 command(SI1143_PARAM_SET + (SI1143_PS_ADC_MISC & 0x1F)); 00041 00042 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_INT_CFG,false,0,1); 00043 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_IRQ_ENABLE,false,0,1); 00044 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_IRQ_MODE1,false,0,1); 00045 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_IRQ_MODE2,false,0,1); 00046 } 00047 // 00048 void SI1143::command(uint8_t cmd) 00049 { 00050 uint8_t val; 00051 00052 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_RESPONSE,false,&val,1); 00053 osDelay(100); 00054 while(val!=0) 00055 { 00056 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_COMMAND,false,SI1143_NOP,1); 00057 osDelay(10); 00058 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_RESPONSE,false,&val,1); 00059 } 00060 do{ 00061 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_COMMAND,false,&cmd,1); 00062 osDelay(10); 00063 if(cmd==SI1143_RESET){ 00064 break; 00065 } 00066 osDelay(10); 00067 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_RESPONSE,false,&val,1); 00068 00069 }while(val==0); 00070 } 00071 00072 int SI1143::getProximity(int numberOfMeasurements) // Read the data for the first LED 00073 { 00074 uint8_t lowByte; 00075 uint8_t highByte; 00076 int stack = 0; 00077 int proximity = 0; 00078 00079 command(SI1143_PSALS_FORCE); 00080 00081 for(int r=numberOfMeasurements; r>0; r=r-1) 00082 { 00083 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_PS1_DATA0,false,&lowByte,1); 00084 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_PS1_DATA1,false,&highByte,1); 00085 stack = stack + (highByte * 256) + lowByte; 00086 } 00087 proximity = stack / numberOfMeasurements; 00088 00089 return proximity; 00090 } 00091 00092 int SI1143::getAmbientLight(int numberOfMeasurements) // Read the data for ambient light 00093 { 00094 00095 uint8_t lowByte; 00096 uint8_t highByte; 00097 int stack = 0; 00098 int ambientLight = 0; 00099 00100 command(SI1143_PSALS_FORCE); 00101 00102 for(int r=numberOfMeasurements; r>0; r=r-1) 00103 { 00104 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_ALS_VIS_DATA0,false,&lowByte,1); 00105 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_ALS_VIS_DATA1,false,&highByte,1); 00106 stack = stack + (highByte * 256) + lowByte; 00107 } 00108 ambientLight = stack / numberOfMeasurements; 00109 00110 return ambientLight; 00111 } 00112 00113 int SI1143::getInfraRedLight(int numberOfMeasurements) // Read the data for infrared light 00114 { 00115 uint8_t lowByte; 00116 uint8_t highByte; 00117 int stack = 0; 00118 int infraRedLight = 0; 00119 00120 command(SI1143_PSALS_FORCE); 00121 00122 for(int r=numberOfMeasurements; r>0; r=r-1) 00123 { 00124 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_ALS_IR_DATA0,false,&lowByte,1); 00125 i2c->read_RT((SI1143_IR_ADDRESS<<1),SI1143_ALS_IR_DATA1,false,&highByte,1); 00126 stack = stack + (highByte * 256) + lowByte; 00127 } 00128 infraRedLight = stack / numberOfMeasurements; 00129 00130 return infraRedLight; 00131 } 00132 00133 void SI1143::configureInterrupts(){ 00134 uint8_t interruptEnableValue = config->getInterruptEnable(); 00135 00136 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_IRQ_ENABLE,false,0,1); 00137 } 00138 00139 void SI1143::setProximitySensing1Threshold(){ 00140 uint8_t thresholdLowByte = (config->getProximitySensing1Threshold()) & 0xFF; 00141 uint8_t thresholdHighByte = (config->getProximitySensing1Threshold()) >> 8; 00142 00143 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_PS1_TH0,false,&thresholdLowByte,1); 00144 i2c->write_RT((SI1143_IR_ADDRESS<<1),SI1143_PS1_TH1,false,&thresholdHighByte,1); 00145 00146 } 00147 00148
Generated on Wed Jul 13 2022 09:23:47 by 1.7.2