fixed some issue with SoftI2C not working (missing delay)
added code for a waveshare eInk refactored some old code to match with the new SoftI2C
This commit is contained in:
@@ -73,7 +73,7 @@ private:
|
||||
|
||||
// read all 24 calibration bytes for temperature and pressure
|
||||
uint8_t b1[24];
|
||||
readRegister(REG_DIG_T1, b1, 24);
|
||||
i2c.readReg(ADDR7, REG_DIG_T1, 24, b1);
|
||||
|
||||
cal.dig_T1 = (b1[1] << 8) | b1[0];
|
||||
cal.dig_T2 = (b1[3] << 8) | b1[2];
|
||||
@@ -90,8 +90,8 @@ private:
|
||||
cal.dig_P9 = (b1[23] << 8) | b1[22];
|
||||
|
||||
// humidity
|
||||
readRegister(0xA1, &cal.dig_H1, 1);
|
||||
readRegister(0xE1, b1, 7);
|
||||
i2c.readReg(ADDR7, 0xA1, 1, &cal.dig_H1);
|
||||
i2c.readReg(ADDR7, 0xE1, 7, b1);
|
||||
cal.dig_H2 = (b1[1] << 8) | b1[0];
|
||||
cal.dig_H3 = b1[3];
|
||||
cal.dig_H4 = (b1[3] << 4) | (b1[4] & 0b000001111);
|
||||
@@ -101,9 +101,9 @@ private:
|
||||
//os_printf("calib temp: %d %d %d\n", cal.dig_T1, cal.dig_T2, cal.dig_T3);
|
||||
//os_printf("calib pres: %d %d %d %d %d %d %d %d %d\n", cal.dig_P1, cal.dig_P2, cal.dig_P3, cal.dig_P4, cal.dig_P5, cal.dig_P6, cal.dig_P7, cal.dig_P8, cal.dig_P9);
|
||||
//os_printf("calib humi: %d %d %d %d %d %d\n", cal.dig_H1, cal.dig_H2, cal.dig_H3, cal.dig_H4, cal.dig_H5, cal.dig_H6);
|
||||
debugMod3(NAME, "calTemp: %d %d %d", cal.dig_T1, cal.dig_T2, cal.dig_T3);
|
||||
debugMod9(NAME, "calPres: %d %d %d %d %d %d %d %d %d", cal.dig_P1, cal.dig_P2, cal.dig_P3, cal.dig_P4, cal.dig_P5, cal.dig_P6, cal.dig_P7, cal.dig_P8, cal.dig_P9);
|
||||
debugMod6(NAME, "calHumi: %d %d %d %d %d %d", cal.dig_H1, cal.dig_H2, cal.dig_H3, cal.dig_H4, cal.dig_H5, cal.dig_H6);
|
||||
//debugMod3(NAME, "calTemp: %d %d %d", cal.dig_T1, cal.dig_T2, cal.dig_T3);
|
||||
//debugMod9(NAME, "calPres: %d %d %d %d %d %d %d %d %d", cal.dig_P1, cal.dig_P2, cal.dig_P3, cal.dig_P4, cal.dig_P5, cal.dig_P6, cal.dig_P7, cal.dig_P8, cal.dig_P9);
|
||||
//debugMod6(NAME, "calHumi: %d %d %d %d %d %d", cal.dig_H1, cal.dig_H2, cal.dig_H3, cal.dig_H4, cal.dig_H5, cal.dig_H6);
|
||||
|
||||
}
|
||||
|
||||
@@ -115,8 +115,8 @@ private:
|
||||
const uint8_t cfgMode = 0b11;
|
||||
const uint8_t cfg1 = (cfgHumi << 1);
|
||||
const uint8_t cfg2 = (cfgTemp << 5) | (cfgPres << 2) | (cfgMode << 0);
|
||||
writeRegister(REG_CTRL1, &cfg1, 1);
|
||||
writeRegister(REG_CTRL2, &cfg2, 1);
|
||||
i2c.writeReg(ADDR7, REG_CTRL1, 1, &cfg1);
|
||||
i2c.writeReg(ADDR7, REG_CTRL2, 1, &cfg2);
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ public:
|
||||
|
||||
uint8_t getStatus() {
|
||||
uint8_t res[1];
|
||||
readRegister(REG_STATUS, res, 1);
|
||||
i2c.readReg(ADDR7, REG_STATUS, 1, res);
|
||||
//os_printf("Status: %d \n", res[0]);
|
||||
return 0;
|
||||
}
|
||||
@@ -141,58 +141,59 @@ public:
|
||||
/** get current pressure in hPa */
|
||||
float getPressure() {
|
||||
uint8_t res[3];
|
||||
readRegister(REG_PRESSURE, res, 3);
|
||||
i2c.readReg(ADDR7, REG_PRESSURE, 3, res);
|
||||
//os_printf("res: %d - %d - %d \n", res[0], res[1], res[2]);
|
||||
const uint32_t tmp = ((res[0] << 16) | (res[1] << 8) | (res[2] << 0)) >> 4;
|
||||
const uint32_t pres = BME280_compensate_P_int64(tmp);
|
||||
const float presF = pres / 256.0f / 100.0f; // convert from Q24.8 to float and from Pa to hPa
|
||||
const uint32_t p0 = pres / 256;
|
||||
const uint32_t p1 = (uint32_t) presF;
|
||||
const uint32_t p2 = (presF - p1) * 100000;
|
||||
debugMod4(NAME, "[pres] ADC: %d -> %d Pa | %d.%d hPa", tmp, p0, p1,p2);
|
||||
//const uint32_t p0 = pres / 256;
|
||||
//const uint32_t p1 = (uint32_t) presF;
|
||||
//const uint32_t p2 = (presF - p1) * 100000;
|
||||
//debugMod4(NAME, "[pres] ADC: %d -> %d Pa | %d.%d hPa", tmp, p0, p1,p2);
|
||||
return presF;
|
||||
}
|
||||
|
||||
float getTemperature() {
|
||||
uint8_t res[3];
|
||||
readRegister(REG_TEMPERATURE, res, 3);
|
||||
i2c.readReg(ADDR7, REG_TEMPERATURE, 3, res);
|
||||
//os_printf("res: %d - %d - %d \n", res[0], res[1], res[2]);
|
||||
const uint32_t tmp = ((res[0] << 16) | (res[1] << 8) | (res[2] << 0)) >> 4;
|
||||
const int32_t temp = BME280_compensate_T_int32(tmp);
|
||||
const float tempF = temp / 100.0f;
|
||||
debugMod2(NAME, "[temp] ADC: %d -> %d", tmp, temp);
|
||||
//debugMod2(NAME, "[temp] ADC: %d -> %d", tmp, temp);
|
||||
return tempF;
|
||||
}
|
||||
|
||||
float getHumidity() {
|
||||
uint8_t res[2];
|
||||
readRegister(REG_HUMIDITY, res, 2);
|
||||
i2c.readReg(ADDR7, REG_HUMIDITY, 2, res);
|
||||
//os_printf("res: %d - %d \n", res[0], res[1]);
|
||||
const uint32_t tmp = (res[0] << 8) | (res[1] << 0);
|
||||
const int32_t humi = bme280_compensate_H_int32(tmp);
|
||||
const float humiF = humi / 1024.0f;
|
||||
const uint16_t h0 = humi / 1024;
|
||||
const uint16_t h1 = (uint16_t) humiF;
|
||||
const uint16_t h2 = (humiF - humi) * 10000;
|
||||
debugMod4(NAME, "[humi] ADC: %d -> %d -> %d.%d %%", tmp, h0, h1,h2);
|
||||
//const uint16_t h0 = humi / 1024;
|
||||
//const uint16_t h1 = (uint16_t) humiF;
|
||||
//const uint16_t h2 = (humiF - humi) * 10000;
|
||||
//debugMod4(NAME, "[humi] ADC: %d -> %d -> %d.%d %%", tmp, h0, h1,h2);
|
||||
return humiF;
|
||||
}
|
||||
|
||||
/*
|
||||
bool readRegister(const uint8_t addr, uint8_t* dst, const uint8_t len) {
|
||||
|
||||
bool ok;
|
||||
|
||||
// address the slave in write mode and select the first register to read
|
||||
ok = i2c.startWrite(ADDR7);
|
||||
if (!ok) {os_printf("failed start write\n"); return false;}
|
||||
if (!ok) {printf("failed start write\n"); return false;}
|
||||
ok = i2c.writeByteAndCheck(addr);
|
||||
if (!ok) {os_printf("failed to select register %d\n", addr); return false;}
|
||||
if (!ok) {printf("failed to select register %d\n", addr); return false;}
|
||||
|
||||
//i2c::stop();
|
||||
|
||||
// address the slave in read mode and read [len] registers
|
||||
ok = i2c.startRead(ADDR7);
|
||||
if (!ok) {os_printf("failed start read\n"); return 0;}
|
||||
if (!ok) {printf("failed start read\n"); return 0;}
|
||||
i2c.readBytes(dst, len);
|
||||
|
||||
// done
|
||||
@@ -207,11 +208,11 @@ public:
|
||||
|
||||
// address the slave in write mode and select the first register to read
|
||||
ok = i2c.startWrite(ADDR7);
|
||||
if (!ok) {os_printf("failed start write\n"); return false;}
|
||||
if (!ok) {printf("failed start write\n"); return false;}
|
||||
ok = i2c.writeByteAndCheck(addr);
|
||||
if (!ok) {os_printf("failed to select register %d\n", addr); return false;}
|
||||
if (!ok) {printf("failed to select register %d\n", addr); return false;}
|
||||
ok = i2c.writeBytesAndCheck(src, len);
|
||||
if (!ok) {os_printf("failed to write register contents \n"); return false;}
|
||||
if (!ok) {printf("failed to write register contents \n"); return false;}
|
||||
|
||||
// done
|
||||
i2c.stop();
|
||||
@@ -219,7 +220,7 @@ public:
|
||||
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
private:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user