Vladimir N. Shilov vor 2 Jahren
Ursprung
Commit
47ec8991f5
6 geänderte Dateien mit 70 neuen und 63 gelöschten Zeilen
  1. 12 21
      app/AHTxx.cpp
  2. 6 7
      app/application.cpp
  3. 24 9
      app/tm1650.cpp
  4. 0 1
      include/AHTxx.h
  5. 24 24
      include/configuration.h
  6. 4 1
      include/tm1650.h

+ 12 - 21
app/AHTxx.cpp

@@ -25,7 +25,6 @@ uint8_t Calc_CRC8(uint8_t *message, uint8_t Num);
 ahtxx_st_t JH_Reset_REG(uint8_t addr);
 */
 static bool ReadyToRequest;
-static bool ReadyData;
 static ahtxx_t Data;
 
 /**
@@ -35,12 +34,11 @@ AHTxx::AHTxx(void)
 {
   // Just powered on, it takes time for the product chip to be ready internally,
   // the delay is 100~500ms, and 500ms is recommended
-	auto timer = new AutoDeleteTimer;
-	timer->initializeMs<500>(Init);
-	timer->startOnce();
+  auto timer = new AutoDeleteTimer;
+  timer->initializeMs<500>(Init);
+  timer->startOnce();
 
   ReadyToRequest = false;
-  ReadyData = false;
 }
 
 void AHTxx::Init(void) {
@@ -49,7 +47,7 @@ void AHTxx::Init(void) {
 
   // When power on, send 0x71 to read the status word for the first time,
   // and judge whether the status word is 0x18.
-	Wire.beginTransmission(AHT_I2C_ADDR);
+  Wire.beginTransmission(AHT_I2C_ADDR);
   Wire.write(CMD_GET_STATUS);
   res = (ahtxx_st_t)Wire.endTransmission();
   if (res != St_OK) {
@@ -58,7 +56,7 @@ void AHTxx::Init(void) {
   }
 
   Wire.requestFrom(AHT_I2C_ADDR, 1);
-	data = Wire.read();
+  data = Wire.read();
 
   if ((data & STATUS_CALB) != STATUS_CALB) {
     //reinitialize registers
@@ -105,7 +103,7 @@ void AHTxx::StartMeasure(void) {
   ahtxx_st_t res;
   Data.Error = St_OK;
 
-	Wire.beginTransmission(AHT_I2C_ADDR);
+  Wire.beginTransmission(AHT_I2C_ADDR);
   Wire.write(CMD_MEASURE);
   Wire.write(CMD_MEASURE_CTL);
   Wire.write(CMD_MEASURE_NOP);
@@ -114,8 +112,6 @@ void AHTxx::StartMeasure(void) {
     Data.Error = res;
     return;
   }
-
-  ReadyData = false;
 }
 
 void AHTxx::GetData(ahtxx_t * data) {
@@ -178,7 +174,6 @@ void AHTxx::GetData(ahtxx_t * data) {
   data->Error = Data.Error;
   data->Humidity = Data.Humidity;
   data->Temperature = Data.Temperature;
-  ReadyData = true;
 }
 
 /**
@@ -212,7 +207,7 @@ ahtxx_st_t AHTxx::JH_Reset_REG(uint8_t addr) {
   ahtxx_st_t res;
   uint8_t Byte_first, Byte_second, Byte_third;
 
-	Wire.beginTransmission(AHT_I2C_ADDR);
+  Wire.beginTransmission(AHT_I2C_ADDR);
   Wire.write((uint8_t)0x70);
   Wire.write(addr);
   Wire.write((uint8_t)0x00);
@@ -223,7 +218,7 @@ ahtxx_st_t AHTxx::JH_Reset_REG(uint8_t addr) {
   }
 
   delay(5); //Delay about 5ms
-	Wire.beginTransmission(AHT_I2C_ADDR);
+  Wire.beginTransmission(AHT_I2C_ADDR);
   Wire.write((uint8_t)0x71);
   res = (ahtxx_st_t)Wire.endTransmission();
   if (res != St_OK) {
@@ -235,7 +230,7 @@ ahtxx_st_t AHTxx::JH_Reset_REG(uint8_t addr) {
   Byte_third = Wire.read();
 
   delay(10); //Delay about 10ms
-	Wire.beginTransmission(AHT_I2C_ADDR);
+  Wire.beginTransmission(AHT_I2C_ADDR);
   Wire.write((uint8_t)0x70);
   Wire.write((uint8_t)(0xB0|addr)); //register command
   Wire.write(Byte_second);
@@ -249,15 +244,11 @@ ahtxx_st_t AHTxx::JH_Reset_REG(uint8_t addr) {
 }
 
 ahtxx_st_t AHTxx::I2CWriteTo(uint8_t DataToSend) {
-	Wire.beginTransmission(AHT_I2C_ADDR);
-	Wire.write(DataToSend);
-	return (ahtxx_st_t)Wire.endTransmission();
+  Wire.beginTransmission(AHT_I2C_ADDR);
+  Wire.write(DataToSend);
+  return (ahtxx_st_t)Wire.endTransmission();
 }
 
 bool AHTxx::IsReadyToRequest(void) {
   return ReadyToRequest;
 }
-
-bool AHTxx::IsDataReady(void) {
-   return ReadyData;
-}

+ 6 - 7
app/application.cpp

@@ -58,6 +58,10 @@ void init(void) {
 	WifiEvents.onStationDisconnect(connectFail);
 	WifiEvents.onStationGotIP(gotIP);
 
+	// initialize I2C
+	Wire.pins(4, 5);
+	Wire.begin();
+
 	// Sensors start. Possible infinity loop...
 //	Serial.println("Wait for Sensor...");
 //	while (sensor.IsReadyToRequest() == false);
@@ -156,7 +160,7 @@ void setBright(void) {
 void RequestData(void) {
 	if (sensor.IsReadyToRequest()) {
 		sensor.StartMeasure();
-		tmpTimer.initializeMs(1000, GetData).startOnce();
+		tmpTimer.initializeMs(600, GetData).startOnce();
 	} else {
 		Serial.println("Sensor: not ready to request.");
 	}
@@ -166,11 +170,6 @@ void RequestData(void) {
  * @brief Get data from Temperature/Humidity Sensor.
  */
 void GetData(void) {
-	if (sensor.IsDataReady() == false) {
-		Serial.println("Sensor: Data not ready!");
-		return;
-	}
-
 	ahtxx_t data;
 
 	sensor.GetData(&data);
@@ -186,7 +185,7 @@ void GetData(void) {
 	Serial.print(SensorH);
 	Serial.print("%. Temperature: ");
 	Serial.print(SensorT);
-	Serial.print("*C");
+	Serial.println("*C");
 }
 
 void connectOk(const String& SSID, MacAddress bssid, uint8_t channel)

+ 24 - 9
app/tm1650.cpp

@@ -4,12 +4,12 @@
 #include "Wire.h"
 
 /* Private Defines */
-#define TM1650_ADDR_SYS  0x48
-#define TM1650_ADDR_BTN  0x4F
-#define TM1650_ADDR_DIG1 0x68
-#define TM1650_ADDR_DIG2 0x6A
-#define TM1650_ADDR_DIG3 0x6C
-#define TM1650_ADDR_DIG4 0x6E
+#define TM1650_ADDR_SYS  (uint8_t)(0x48 >> 1)
+#define TM1650_ADDR_BTN  (uint8_t)(0x4F >> 1)
+#define TM1650_ADDR_DIG1 (uint8_t)(0x68 >> 1)
+#define TM1650_ADDR_DIG2 (uint8_t)(0x6A >> 1)
+#define TM1650_ADDR_DIG3 (uint8_t)(0x6C >> 1)
+#define TM1650_ADDR_DIG4 (uint8_t)(0x6E >> 1)
 
 /* Private Variables */
 static const uint8_t Digits[16] = {
@@ -19,16 +19,22 @@ static const uint8_t Digits[16] = {
 static const uint8_t Addr[LED_NUM] = {
   TM1650_ADDR_DIG1, TM1650_ADDR_DIG2, TM1650_ADDR_DIG3, TM1650_ADDR_DIG4
 };
+static bool isPresent;
 static uint8 Buffer[LED_NUM] = {0};
 
 /* Private Fuctions */ 
-bool I2CWriteTo(uint8_t addr, uint8_t data);
+static bool I2CWriteTo(uint8_t addr, uint8_t data);
 
 /**
  * @brief Initialization
  */
 void TM1650_Init(void) {
-  I2CWriteTo(TM1650_ADDR_SYS, 0x79);
+  if (I2CWriteTo(TM1650_ADDR_SYS, 0x79)) {
+    Serial.println("TM1650 not respond!");
+    isPresent = false;
+    return;
+  }
+  isPresent = true;
   TM1650_Out(Sym_Minus, Sym_Minus, Sym_Minus, Sym_Minus);
 }
 
@@ -41,6 +47,8 @@ void TM1650_Init(void) {
  * @param i4 Value in range 0..F
  */
 void TM1650_Out(uint8_t i1, uint8_t i2, uint8_t i3, uint8_t i4) {
+  if (!isPresent) { return; }
+
   Buffer[0] = Digits[i1];
   Buffer[1] = Digits[i2];
   Buffer[2] = Digits[i3];
@@ -58,6 +66,7 @@ void TM1650_Out(uint8_t i1, uint8_t i2, uint8_t i3, uint8_t i4) {
  * @param value Segment code.
  */
 void TM1650_Out1(tm1650_sym_t value) {
+  if (!isPresent) { return; }
   Buffer[0] = value;
   I2CWriteTo(TM1650_ADDR_DIG1, value);
 }
@@ -68,6 +77,7 @@ void TM1650_Out1(tm1650_sym_t value) {
  * @param value Segment code.
  */
 void TM1650_Out2(tm1650_sym_t value) {
+  if (!isPresent) { return; }
   Buffer[1] = value;
   I2CWriteTo(TM1650_ADDR_DIG2, value);
 }
@@ -78,6 +88,7 @@ void TM1650_Out2(tm1650_sym_t value) {
  * @param value Segment code.
  */
 void TM1650_Out3(tm1650_sym_t value) {
+  if (!isPresent) { return; }
   Buffer[2] = value;
   I2CWriteTo(TM1650_ADDR_DIG3, value);
 }
@@ -88,6 +99,7 @@ void TM1650_Out3(tm1650_sym_t value) {
  * @param value Segment code.
  */
 void TM1650_Out4(tm1650_sym_t value) {
+  if (!isPresent) { return; }
   Buffer[3] = value;
   I2CWriteTo(TM1650_ADDR_DIG4, value);
 }
@@ -98,6 +110,7 @@ void TM1650_Out4(tm1650_sym_t value) {
  * @param pos Led 0..3
  */
 void TM1650_DotSet(tm1650_pos_t pos) {
+  if (!isPresent) { return; }
   if (pos > LED_NUM-1) {
     return;
   }
@@ -110,6 +123,7 @@ void TM1650_DotSet(tm1650_pos_t pos) {
  * @param pos Led 0..3
  */
 void TM1650_DotRes(tm1650_pos_t pos) {
+  if (!isPresent) { return; }
   if (pos > LED_NUM-1) {
     return;
   }
@@ -122,6 +136,7 @@ void TM1650_DotRes(tm1650_pos_t pos) {
  * @param bright Brgiht level 0..7
  */
 void TM1650_Bright(uint8_t bright) {
+  if (!isPresent) { return; }
   if (bright > LedBrightMax) {
     bright = LedBrightMax;
   }
@@ -130,7 +145,7 @@ void TM1650_Bright(uint8_t bright) {
   I2CWriteTo(TM1650_ADDR_SYS, bright);
 }
 
-bool I2CWriteTo(uint8_t addr, uint8_t data) {
+static bool I2CWriteTo(uint8_t addr, uint8_t data) {
 	Wire.beginTransmission(addr);
 	Wire.write(data);
 	return Wire.endTransmission();

+ 0 - 1
include/AHTxx.h

@@ -29,7 +29,6 @@ public:
   void SoftReset(void);
   void GetData(ahtxx_t * data);
   bool IsReadyToRequest(void);
-  bool IsDataReady(void);
 
 private:
   static void Init(void);

+ 24 - 24
include/configuration.h

@@ -10,35 +10,35 @@
 #define WIFI_PWD "Heaven-32847"
 #endif
 
-#define MinLightThreshold		0
-#define MaxLightThreshold		1023
+#define MinLightThreshold    0
+#define MaxLightThreshold    1023
 
-#define LedBrightMin				1
-#define LedBrightMiddl			3
-#define LedBrightMax				7
+#define LedBrightMin         0
+#define LedBrightMiddl       3
+#define LedBrightMax         7
 
 #define CLOCK_CONFIG_FILE ".clock.conf" // leading point for security reasons :)
 
 struct ClockConfig {
-	ClockConfig()
-	{
-		AddTZ = 2;
-		LightTrhLow = 341;
-		LightTrhHigh = 683;
-		BrightnessLow = 1;
-		BrightnessMiddle = 7;
-		BrightnessHigh = 15;
-	}
-
-	String NetworkSSID;
-	String NetworkPassword;
-
-	float AddTZ; // TimeZone - local time offset
-	uint16_t LightTrhLow; // Low Light level
-	uint16_t LightTrhHigh; // High Light level
-	int8_t BrightnessLow; // Low LED brightness level
-	int8_t BrightnessMiddle; // Middle LED brightness level
-	int8_t BrightnessHigh; // High LED brightness level
+  ClockConfig()
+  {
+    AddTZ = 2;
+    LightTrhLow = 341;
+    LightTrhHigh = 683;
+    BrightnessLow = 0;
+    BrightnessMiddle = 3;
+    BrightnessHigh = 7;
+  }
+
+  String NetworkSSID;
+  String NetworkPassword;
+
+  float AddTZ; // TimeZone - local time offset
+  uint16_t LightTrhLow; // Low Light level
+  uint16_t LightTrhHigh; // High Light level
+  int8_t BrightnessLow; // Low LED brightness level
+  int8_t BrightnessMiddle; // Middle LED brightness level
+  int8_t BrightnessHigh; // High LED brightness level
 };
 
 ClockConfig loadConfig();

+ 4 - 1
include/tm1650.h

@@ -23,7 +23,10 @@ typedef enum {
   Sym_E = 0x79,
   Sym_F = 0x71,
   Sym_Minus = 0x40,
-  Sym_Dot = 0x80
+  Sym_Dot = 0x80,
+  Sym_Plus = 0x46,
+  Sym_H = 0x76,
+  Sym_Off = 0x00
 } tm1650_sym_t;
 
 typedef enum {