diff --git a/README.md b/README.md index 036ba6ae4..445642754 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ People helping to keep the show on the road: - Pete Ba for more user friendly energy monitor calibration - Lobradov providing compile optimization tips - Flexiti for his initial timer implementation -- reloxx13 for his [SonWeb](https://github.com/reloxx13/SonWEB) management tool +- reloxx13 for his [TasmoAdmin](https://github.com/reloxx13/TasmoAdmin) management tool - Joachim Banzhaf for his TSL2561 library and driver - Gijs Noorlander for his MHZ19 and SenseAir drivers - Emontnemery for his HomeAssistant Discovery concept and many code tuning tips diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 59cada7e3..71007cc50 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,4 +1,10 @@ -/* 6.0.0a +/* 6.0.0b + * Add experimental (untested) TM1638 switch support (#2226) + * Change number of switches from 4 to 8 (#2885, #3086) + * + * 6.0.0a + * Add support for APDS9960 proximity sensor (#3051) + * Add increment and decrement value to command Counter (#2838) * Add option 0 to command Timers disarming all timers (#2962) * Add time in minutes to rule Time#Initialized, Time#set and Time#Minute (#2669) * Add rule variables %time% for minutes since midnight, %uptime%, %sunrise% and %sunset% giving time in minutes (#2669) @@ -17,6 +23,7 @@ * Fix Pzem004T checksum error * Fix KNX bug when doing reply of sensors values * Fix rules induced LWT message + * Fix possible wifi connection problem (#1366) * * 5.14.0b * Add Console Commands to send KNX Commands diff --git a/sonoff/language/bg-BG.h b/sonoff/language/bg-BG.h index 8f397bb82..6d82a843c 100644 --- a/sonoff/language/bg-BG.h +++ b/sonoff/language/bg-BG.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/cs-CZ.h b/sonoff/language/cs-CZ.h index eec72fc31..cb93ad553 100644 --- a/sonoff/language/cs-CZ.h +++ b/sonoff/language/cs-CZ.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/de-DE.h b/sonoff/language/de-DE.h index 91a2bcfef..4c0fde470 100644 --- a/sonoff/language/de-DE.h +++ b/sonoff/language/de-DE.h @@ -321,10 +321,10 @@ #define D_UPLOAD_ERR_7 "Upload abgebrochen" #define D_UPLOAD_ERR_8 "Datei ungültig" #define D_UPLOAD_ERR_9 "Datei zu groß" -#define D_UPLOAD_ERR_10 "Failed to init RF chip" -#define D_UPLOAD_ERR_11 "Failed to erase RF chip" -#define D_UPLOAD_ERR_12 "Failed to write to RF chip" -#define D_UPLOAD_ERR_13 "Failed to decode RF firmware" +#define D_UPLOAD_ERR_10 "RF Chip init fehlgeschlagen" +#define D_UPLOAD_ERR_11 "RF Chip löschen fehlgeschlagen" +#define D_UPLOAD_ERR_12 "RF Chip beschreiben fehlgeschlagen" +#define D_UPLOAD_ERR_13 "RF Firmware ungültig" #define D_UPLOAD_ERROR_CODE "Upload Fehler Nummer" #define D_ENTER_COMMAND "Befehl eingeben" @@ -387,25 +387,25 @@ #define D_TIMER_ACTION "Aktion" // xdrv_10_knx.ino -#define D_CONFIGURE_KNX "Configure KNX" -#define D_KNX_PARAMETERS "KNX Parameters" -#define D_KNX_GENERAL_CONFIG "General" -#define D_KNX_PHYSICAL_ADDRESS "Physical Address" -#define D_KNX_PHYSICAL_ADDRESS_NOTE "( Must be unique on the KNX network )" -#define D_KNX_ENABLE "Enable KNX" -#define D_KNX_GROUP_ADDRESS_TO_WRITE "Data to Send to Group Addresses" -#define D_ADD "Add" -#define D_DELETE "Delete" -#define D_REPLY "Reply" -#define D_KNX_GROUP_ADDRESS_TO_READ "Group Addresses to Receive Data from" +#define D_CONFIGURE_KNX "KNX konfigurieren" +#define D_KNX_PARAMETERS "KNX-Parameter" +#define D_KNX_GENERAL_CONFIG "Allgemein" +#define D_KNX_PHYSICAL_ADDRESS "Physikalische Adresse" +#define D_KNX_PHYSICAL_ADDRESS_NOTE "( Muss einmalig im KNX-Netzwerk sein )" +#define D_KNX_ENABLE "KNX aktivieren" +#define D_KNX_GROUP_ADDRESS_TO_WRITE "Daten zum Senden an Gruppenadresse" +#define D_ADD "Hinzufügen" +#define D_DELETE "Löschen" +#define D_REPLY "Antworten" +#define D_KNX_GROUP_ADDRESS_TO_READ "Gruppenadresse zum Emfang von Daten" #define D_LOG_KNX "KNX: " -#define D_RECEIVED_FROM "Received from" -#define D_KNX_COMMAND_WRITE "Write" -#define D_KNX_COMMAND_READ "Read" -#define D_KNX_COMMAND_OTHER "Other" -#define D_SENT_TO "sent to" -#define D_KNX_WARNING "The group address ( 0 / 0 / 0 ) is reserved and can not be used." -#define D_KNX_ENHANCEMENT "Communication Enhancement" +#define D_RECEIVED_FROM "Empfangen von" +#define D_KNX_COMMAND_WRITE "Schreiben" +#define D_KNX_COMMAND_READ "Lesen" +#define D_KNX_COMMAND_OTHER "Andere" +#define D_SENT_TO "gesendet an" +#define D_KNX_WARNING "Die Gruppenadresse ( 0 / 0 / 0 ) ist reserviert und kann nicht verwendet werden." +#define D_KNX_ENHANCEMENT "Erweiterte Kommunikation" #define D_KNX_TX_SLOT "KNX TX" #define D_KNX_RX_SLOT "KNX RX" @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/el-GR.h b/sonoff/language/el-GR.h index 2281e2faf..702381ab1 100644 --- a/sonoff/language/el-GR.h +++ b/sonoff/language/el-GR.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/en-GB.h b/sonoff/language/en-GB.h index f5f0d6e22..8858eea2f 100644 --- a/sonoff/language/en-GB.h +++ b/sonoff/language/en-GB.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/es-AR.h b/sonoff/language/es-AR.h index 37fa89589..cb50b7377 100644 --- a/sonoff/language/es-AR.h +++ b/sonoff/language/es-AR.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/fr-FR.h b/sonoff/language/fr-FR.h index 09f9ffe0f..ce1485513 100644 --- a/sonoff/language/fr-FR.h +++ b/sonoff/language/fr-FR.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/hu-HU.h b/sonoff/language/hu-HU.h index 986b18fdf..14fb2a08b 100644 --- a/sonoff/language/hu-HU.h +++ b/sonoff/language/hu-HU.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/it-IT.h b/sonoff/language/it-IT.h index 1e7ed00b5..6f1c4db0f 100644 --- a/sonoff/language/it-IT.h +++ b/sonoff/language/it-IT.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/nl-NL.h b/sonoff/language/nl-NL.h index a764cb667..cf1c2b3b0 100644 --- a/sonoff/language/nl-NL.h +++ b/sonoff/language/nl-NL.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pl-PL.h b/sonoff/language/pl-PL.h index a039cf333..e250e4c4d 100644 --- a/sonoff/language/pl-PL.h +++ b/sonoff/language/pl-PL.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pt-BR.h b/sonoff/language/pt-BR.h index 4bcf8efa2..a32a7b5c3 100644 --- a/sonoff/language/pt-BR.h +++ b/sonoff/language/pt-BR.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/pt-PT.h b/sonoff/language/pt-PT.h index 4eeeb3965..393a74a87 100644 --- a/sonoff/language/pt-PT.h +++ b/sonoff/language/pt-PT.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "A" diff --git a/sonoff/language/ru-RU.h b/sonoff/language/ru-RU.h index cda108cdc..79df5b2d4 100644 --- a/sonoff/language/ru-RU.h +++ b/sonoff/language/ru-RU.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "А" diff --git a/sonoff/language/uk-UK.h b/sonoff/language/uk-UK.h index c0339b9e8..d925d13fb 100644 --- a/sonoff/language/uk-UK.h +++ b/sonoff/language/uk-UK.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "А" diff --git a/sonoff/language/zh-CN.h b/sonoff/language/zh-CN.h index 4310111ee..9e5149125 100644 --- a/sonoff/language/zh-CN.h +++ b/sonoff/language/zh-CN.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "安" diff --git a/sonoff/language/zh-TW.h b/sonoff/language/zh-TW.h index e6adb4b0e..6d4154cdf 100644 --- a/sonoff/language/zh-TW.h +++ b/sonoff/language/zh-TW.h @@ -471,6 +471,9 @@ #define D_SENSOR_SDM120_RX "SDM120 Rx" #define D_SENSOR_SDM630_TX "SDM630 Tx" #define D_SENSOR_SDM630_RX "SDM630 Rx" +#define D_SENSOR_TM1638_CLK "TM16 CLK" +#define D_SENSOR_TM1638_DIO "TM16 DIO" +#define D_SENSOR_TM1638_STB "TM16 STB" // Units #define D_UNIT_AMPERE "安" diff --git a/sonoff/settings.h b/sonoff/settings.h index 31d139b44..6489b05c5 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -216,8 +216,9 @@ struct SYSCFG { uint16_t blinkcount; // 39C uint16_t light_rotation; // 39E - byte free_3A0[12]; // 3A9 + byte free_3A0[4]; // 3A0 + uint8_t switchmode[MAX_SWITCHES]; // 3A4 (6.0.0b - moved from 0x4CA) char friendlyname[MAX_FRIENDLYNAMES][33]; // 3AC char switch_topic[33]; // 430 char serial_delimiter; // 451 @@ -243,7 +244,9 @@ struct SYSCFG { uint16_t light_wakeup; // 4A6 byte knx_CB_registered; // 4A8 Number of Group Address to write char web_password[33]; // 4A9 - uint8_t switchmode[MAX_SWITCHES]; // 4CA + + uint8_t ex_switchmode[4]; // 4CA Free since 6.0.0a + char ntp_server[3][33]; // 4CE byte ina219_mode; // 531 uint16_t pulse_timer[MAX_PULSETIMERS]; // 532 diff --git a/sonoff/settings.ino b/sonoff/settings.ino index 2b7803304..f472e2685 100644 --- a/sonoff/settings.ino +++ b/sonoff/settings.ino @@ -763,6 +763,20 @@ void SettingsDelta() Settings.cfg_size = sizeof(SYSCFG); Settings.cfg_crc = GetSettingsCrc(); } + if (Settings.version < 0x06000002) { + for (byte i = 0; i < MAX_SWITCHES; i++) { + if (i < 4) { + Settings.switchmode[i] = Settings.ex_switchmode[i]; + } else { + Settings.switchmode[i] = SWITCH_MODE; + } + } + for (byte i = 0; i < MAX_GPIO_PIN; i++) { + if (Settings.my_gp.io[i] >= GPIO_SWT5) { // Move up from GPIO_SWT5 to GPIO_KEY1 + Settings.my_gp.io[i] += 4; + } + } + } Settings.version = VERSION; SettingsSave(1); diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index 3ad6ee096..1c1f24532 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -37,10 +37,10 @@ typedef unsigned long power_t; // Power (Relay) type \*********************************************************************************************/ // Changes to the following MAX_ defines will impact settings layout +#define MAX_SWITCHES 8 // Max number of switches #define MAX_RELAYS 8 // Max number of relays #define MAX_LEDS 4 // Max number of leds #define MAX_KEYS 4 // Max number of keys or buttons -#define MAX_SWITCHES 4 // Max number of switches #define MAX_PWMS 5 // Max number of PWM channels #define MAX_COUNTERS 4 // Max number of counter sensors #define MAX_TIMERS 16 // Max number of Timers diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index fec987722..3a09d446c 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -25,7 +25,7 @@ - Select IDE Tools - Flash Size: "1M (no SPIFFS)" ====================================================*/ -#define VERSION 0x06000001 // 6.0.0a +#define VERSION 0x06000002 // 6.0.0b // Location specific includes #include // Arduino_Esp8266 version information (ARDUINO_ESP8266_RELEASE and ARDUINO_ESP8266_RELEASE_2_3_0) @@ -167,6 +167,7 @@ uint8_t multiwindow[MAX_KEYS] = { 0 }; // Max time between button presses t uint8_t multipress[MAX_KEYS] = { 0 }; // Number of button presses within multiwindow uint8_t lastwallswitch[MAX_SWITCHES]; // Last wall switch states uint8_t holdwallswitch[MAX_SWITCHES] = { 0 }; // Timer for wallswitch push button hold +uint8_t virtualswitch[MAX_SWITCHES] = { 0 }; // Virtual switch states mytmplt my_module; // Active copy of Module name and GPIOs uint8_t pin[GPIO_MAX]; // Possible pin configurations @@ -797,8 +798,13 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len) } else if ((CMND_COUNTER == command_code) && (index > 0) && (index <= MAX_COUNTERS)) { if ((data_len > 0) && (pin[GPIO_CNTR1 + index -1] < 99)) { - RtcSettings.pulse_counter[index -1] = payload16; - Settings.pulse_counter[index -1] = payload16; + if ((dataBuf[0] == '-') || (dataBuf[0] == '+')) { + RtcSettings.pulse_counter[index -1] += payload32; + Settings.pulse_counter[index -1] += payload32; + } else { + RtcSettings.pulse_counter[index -1] = payload32; + Settings.pulse_counter[index -1] = payload32; + } } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_LVALUE, command, index, RtcSettings.pulse_counter[index -1]); } @@ -1141,8 +1147,8 @@ boolean SendKey(byte key, byte device, byte state) char *tmp = (key) ? Settings.switch_topic : Settings.button_topic; Format(key_topic, tmp, sizeof(key_topic)); if (Settings.flag.mqtt_enabled && MqttIsConnected() && (strlen(key_topic) != 0) && strcmp(key_topic, "0")) { - if (!key && (device > devices_present)) device = 1; - GetTopic_P(stopic, CMND, key_topic, GetPowerDevice(scommand, device, sizeof(scommand), key)); + if (!key && (device > devices_present)) device = 1; // Only allow number of buttons up to number of devices + GetTopic_P(stopic, CMND, key_topic, GetPowerDevice(scommand, device, sizeof(scommand), key)); // cmnd/switchtopic/POWERx if (9 == state) { mqtt_data[0] = '\0'; } else { @@ -1426,7 +1432,11 @@ boolean MqttShowSensor() snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{\"" D_JSON_TIME "\":\"%s\""), mqtt_data, GetDateAndTime(DT_LOCAL).c_str()); int json_data_start = strlen(mqtt_data); for (byte i = 0; i < MAX_SWITCHES; i++) { +#ifdef USE_TM1638 + if ((pin[GPIO_SWT1 +i] < 99) || ((pin[GPIO_TM16CLK] < 99) && (pin[GPIO_TM16DIO] < 99) && (pin[GPIO_TM16STB] < 99))) { +#else if (pin[GPIO_SWT1 +i] < 99) { +#endif // USE_TM1638 boolean swm = ((FOLLOW_INV == Settings.switchmode[i]) || (PUSHBUTTON_INV == Settings.switchmode[i]) || (PUSHBUTTONHOLD_INV == Settings.switchmode[i])); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_SWITCH "%d\":\"%s\""), mqtt_data, i +1, GetStateText(swm ^ lastwallswitch[i])); } @@ -1659,7 +1669,7 @@ void ButtonHandler() * Switch handler \*********************************************************************************************/ -void SwitchHandler() +void SwitchHandler(byte mode) { uint8_t button = NOT_PRESSED; uint8_t switchflag; @@ -1674,7 +1684,12 @@ void SwitchHandler() } } - button = digitalRead(pin[GPIO_SWT1 +i]); + if (mode) { + button = virtualswitch[i]; + } else { + button = digitalRead(pin[GPIO_SWT1 +i]); + } + if (button != lastwallswitch[i]) { switchflag = 3; switch (Settings.switchmode[i]) { @@ -1808,7 +1823,7 @@ void StateLoop() \*-------------------------------------------------------------------------------------------*/ ButtonHandler(); - SwitchHandler(); + SwitchHandler(0); XdrvCall(FUNC_EVERY_50_MSECOND); XsnsCall(FUNC_EVERY_50_MSECOND); diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index f97c32203..38fadf086 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -35,6 +35,10 @@ enum UserSelectablePins { GPIO_SWT2, GPIO_SWT3, GPIO_SWT4, + GPIO_SWT5, + GPIO_SWT6, + GPIO_SWT7, + GPIO_SWT8, GPIO_KEY1, // Button usually connected to GPIO0 GPIO_KEY2, GPIO_KEY3, @@ -97,6 +101,9 @@ enum UserSelectablePins { GPIO_SDM120_RX, // SDM120 Serial interface GPIO_SDM630_TX, // SDM630 Serial interface GPIO_SDM630_RX, // SDM630 Serial interface + GPIO_TM16CLK, // TM1638 Clock + GPIO_TM16DIO, // TM1638 Data I/O + GPIO_TM16STB, // TM1638 Strobe GPIO_SENSOR_END }; // Programmer selectable GPIO functionality offset by user selectable GPIOs @@ -124,7 +131,7 @@ const char kSensorNames[] PROGMEM = D_SENSOR_I2C_SCL "|" D_SENSOR_I2C_SDA "|" D_SENSOR_WS2812 "|" D_SENSOR_IRSEND "|" - D_SENSOR_SWITCH "1|" D_SENSOR_SWITCH "2|" D_SENSOR_SWITCH "3|" D_SENSOR_SWITCH "4|" + D_SENSOR_SWITCH "1|" D_SENSOR_SWITCH "2|" D_SENSOR_SWITCH "3|" D_SENSOR_SWITCH "4|" D_SENSOR_SWITCH "5|" D_SENSOR_SWITCH "6|" D_SENSOR_SWITCH "7|" D_SENSOR_SWITCH "8|" D_SENSOR_BUTTON "1|" D_SENSOR_BUTTON "2|" D_SENSOR_BUTTON "3|" D_SENSOR_BUTTON "4|" D_SENSOR_RELAY "1|" D_SENSOR_RELAY "2|" D_SENSOR_RELAY "3|" D_SENSOR_RELAY "4|" D_SENSOR_RELAY "5|" D_SENSOR_RELAY "6|" D_SENSOR_RELAY "7|" D_SENSOR_RELAY "8|" D_SENSOR_RELAY "1i|" D_SENSOR_RELAY "2i|" D_SENSOR_RELAY "3i|" D_SENSOR_RELAY "4i|" D_SENSOR_RELAY "5i|" D_SENSOR_RELAY "6i|" D_SENSOR_RELAY "7i|" D_SENSOR_RELAY "8i|" @@ -142,7 +149,8 @@ const char kSensorNames[] PROGMEM = D_SENSOR_SBR_TX "|" D_SENSOR_SBR_RX "|" D_SENSOR_SR04_TRIG "|" D_SENSOR_SR04_ECHO "|" D_SENSOR_SDM120_TX "|" D_SENSOR_SDM120_RX "|" - D_SENSOR_SDM630_TX "|" D_SENSOR_SDM630_RX; + D_SENSOR_SDM630_TX "|" D_SENSOR_SDM630_RX "|" + D_SENSOR_TM1638_CLK "|" D_SENSOR_TM1638_DIO "|" D_SENSOR_TM1638_STB; /********************************************************************************************/ diff --git a/sonoff/support.ino b/sonoff/support.ino index 067c8c291..98be71f8b 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -368,10 +368,10 @@ char* GetPowerDevice(char* dest, uint8_t idx, size_t size, uint8_t option) { char sidx[8]; - strncpy_P(dest, S_RSLT_POWER, size); + strncpy_P(dest, S_RSLT_POWER, size); // POWER if ((devices_present + option) > 1) { - snprintf_P(sidx, sizeof(sidx), PSTR("%d"), idx); - strncat(dest, sidx, size); + snprintf_P(sidx, sizeof(sidx), PSTR("%d"), idx); // x + strncat(dest, sidx, size); // POWERx } return dest; } @@ -567,6 +567,11 @@ boolean GetUsedInModule(byte val, uint8_t *arr) #ifndef USE_SDM630 if (GPIO_SDM630_TX == val) { return true; } if (GPIO_SDM630_RX == val) { return true; } +#endif +#ifndef USE_TM1638 + if (GPIO_TM16CLK == val) { return true; } + if (GPIO_TM16DIO == val) { return true; } + if (GPIO_TM16STB == val) { return true; } #endif if ((val >= GPIO_REL1) && (val < GPIO_REL1 + MAX_RELAYS)) { offset = (GPIO_REL1_INV - GPIO_REL1); @@ -1009,7 +1014,6 @@ void WifiBegin(uint8_t flag) WiFi.mode(WIFI_OFF); // See https://github.com/esp8266/Arduino/issues/2186 #endif - WiFi.persistent(false); // Solve possible wifi init errors WiFi.disconnect(true); // Delete SDK wifi config delay(200); WiFi.mode(WIFI_STA); // Disable AP mode diff --git a/sonoff/user_config.h b/sonoff/user_config.h index 5e178aa60..0c4aa66f5 100644 --- a/sonoff/user_config.h +++ b/sonoff/user_config.h @@ -276,6 +276,7 @@ // #define USE_INA219 // Add I2C code for INA219 Low voltage and current sensor (+1k code) // #define USE_MGS // Add I2C code for Xadow and Grove Mutichannel Gas sensor using library Multichannel_Gas_Sensor (+10k code) #define MGS_SENSOR_ADDR 0x04 // Default Mutichannel Gas sensor i2c address +// #define USE_APDS9960 // Add I2C code for APDS9960 Proximity Sensor. Disables SHT and VEML6070 (+4k7 code) #endif // USE_I2C // -- SPI sensors --------------------------------- @@ -311,6 +312,8 @@ #define USE_SR04 // Add support for HC-SR04 ultrasonic devices (+1k code) +//#define USE_TM1638 // Add support for TM1638 switches copying Switch1 .. Switch8 (+1k code) + #define USE_RF_FLASH // Add support for flashing the EFM8BB1 chip on the Sonoff RF Bridge. C2CK must be connected to GPIO4, C2D to GPIO5 on the PCB /*********************************************************************************************\ diff --git a/sonoff/xdrv_02_webserver.ino b/sonoff/xdrv_02_webserver.ino index 1f699cfb0..da47b2223 100644 --- a/sonoff/xdrv_02_webserver.ino +++ b/sonoff/xdrv_02_webserver.ino @@ -488,6 +488,9 @@ void ShowPage(String &page, bool auth) page += FPSTR(HTTP_END); page.replace(F("{mv"), my_version); SetHeader(); + + ShowFreeMem(PSTR("ShowPage")); + WebServer->send(200, FPSTR(HDR_CTYPE_HTML), page); } diff --git a/sonoff/xdrv_10_rules.ino b/sonoff/xdrv_10_rules.ino index 28037de8b..1218b9854 100644 --- a/sonoff/xdrv_10_rules.ino +++ b/sonoff/xdrv_10_rules.ino @@ -357,6 +357,9 @@ bool RulesProcessEvent(char *json_event) String event_saved = json_event; event_saved.toUpperCase(); +//snprintf_P(log_data, sizeof(log_data), PSTR("RUL: Event %s"), event_saved.c_str()); +//AddLog(LOG_LEVEL_DEBUG); + for (byte i = 0; i < MAX_RULE_SETS; i++) { if (strlen(Settings.rules[i]) && bitRead(Settings.rule_enabled, i)) { if (RuleSetProcess(i, event_saved)) { serviced = true; } @@ -554,55 +557,44 @@ boolean RulesCommand() snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.mems[index -1]); } else if ((CMND_ADD == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) { - if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){ - int16_t tempvar = atol(vars[index -1]) + XdrvMailbox.payload; - snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar ); + if ( XdrvMailbox.data_len > 0 ) { + double tempvar = CharToDouble(vars[index -1]) + CharToDouble(XdrvMailbox.data); + dtostrfd(tempvar,2,vars[index -1]); } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]); } else if ((CMND_SUB == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) { - if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){ - int16_t tempvar = atol(vars[index -1]) - XdrvMailbox.payload; - snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar ); + if ( XdrvMailbox.data_len > 0 ){ + double tempvar = CharToDouble(vars[index -1]) - CharToDouble(XdrvMailbox.data); + dtostrfd(tempvar,2,vars[index -1]); } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]); } else if ((CMND_MULT == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) { - if ( (XdrvMailbox.data_len > 0) && (XdrvMailbox.payload >= 0) ){ - int16_t tempvar = atol(vars[index -1]) * XdrvMailbox.payload; - snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), tempvar ); + if ( XdrvMailbox.data_len > 0 ){ + double tempvar = CharToDouble(vars[index -1]) * CharToDouble(XdrvMailbox.data); + dtostrfd(tempvar,2,vars[index -1]); } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]); } else if ((CMND_SCALE == command_code) && (index > 0) && (index <= RULES_MAX_VARS)) { if ( XdrvMailbox.data_len > 0 ) { if (strstr(XdrvMailbox.data, ",")) { // Process parameter entry - uint8_t tpos = 0; // Parameter index - int16_t value = 0; - int16_t valueIN = 0; - int16_t fromLow = 0; - int16_t fromHigh = 0; - int16_t toLow = 0; - int16_t toHigh = 0; - char *p = XdrvMailbox.data; // Parameters like "1, 2, 3, 4" - char *q = p; // Value entered flag - while (p && (tpos < 6)) { - if (p > q) { // Any value entered - if (1 == tpos) { valueIN = value; } - if (2 == tpos) { fromLow = value; } - if (3 == tpos) { fromHigh = value; } - if (4 == tpos) { toLow = value; } - if (5 == tpos) { toHigh = value; } - } - p = LTrim(p); // Skip spaces - if (tpos && (*p == ',')) { p++; } // Skip separator - p = LTrim(p); // Skip spaces - q = p; // Reset any value entered flag - value = strtol(p, &p, 10); - tpos++; // Next parameter - } - value = map(valueIN, fromLow, fromHigh, toLow, toHigh); - snprintf_P(vars[index -1], sizeof(vars[index -1]), PSTR("%d"), value ); + double value = 0; + double valueIN = 0; + double fromLow = 0; + double fromHigh = 0; + double toLow = 0; + double toHigh = 0; + + valueIN = CharToDouble(subStr(XdrvMailbox.data, ",", 1)); + fromLow = CharToDouble(subStr(XdrvMailbox.data, ",", 2)); + fromHigh = CharToDouble(subStr(XdrvMailbox.data, ",", 3)); + toLow = CharToDouble(subStr(XdrvMailbox.data, ",", 4)); + toHigh = CharToDouble(subStr(XdrvMailbox.data, ",", 5)); + + value = map_double(valueIN, fromLow, fromHigh, toLow, toHigh); + dtostrfd(value,2,vars[index -1]); } } snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, vars[index -1]); @@ -612,6 +604,30 @@ boolean RulesCommand() return serviced; } +double map_double(double x, double in_min, double in_max, double out_min, double out_max) +{ + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + +// Function to return a substring defined by a delimiter at an index +char* subStr (char* str, const char *delim, int index) { + char *act, *sub, *ptr; + static char copy[10]; + int i; + + // Since strtok consumes the first arg, make a copy + strcpy(copy, str); + + for (i = 1, act = copy; i <= index; i++, act = NULL) { + sub = strtok_r(act, delim, &ptr); + if (sub == NULL) break; + } + sub = LTrim(sub); + sub = RTrim(sub); + return sub; +} + + /*********************************************************************************************\ * Interface \*********************************************************************************************/ diff --git a/sonoff/xdrv_11_knx.ino b/sonoff/xdrv_11_knx.ino index 72102fe99..2162fbcaa 100644 --- a/sonoff/xdrv_11_knx.ino +++ b/sonoff/xdrv_11_knx.ino @@ -1045,14 +1045,18 @@ boolean KnxCommand() byte i = KNX_GA_Search(index + KNX_SLOT1 -1); while ( i != KNX_Empty ) { KNX_addr.value = Settings.knx_GA_addr[i]; - knx.write_2byte_float(KNX_addr, XdrvMailbox.payload); + + float tempvar = CharToDouble(XdrvMailbox.data); + dtostrfd(tempvar,2,XdrvMailbox.data); + + knx.write_2byte_float(KNX_addr, tempvar); if (Settings.flag.knx_enable_enhancement) { - knx.write_2byte_float(KNX_addr, XdrvMailbox.payload); - knx.write_2byte_float(KNX_addr, XdrvMailbox.payload); + knx.write_2byte_float(KNX_addr, tempvar); + knx.write_2byte_float(KNX_addr, tempvar); } - snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "%s = %d " D_SENT_TO " %d.%d.%d"), - device_param_ga[index + KNX_SLOT1 -2], XdrvMailbox.payload, + snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "%s = %s " D_SENT_TO " %d.%d.%d"), + device_param_ga[index + KNX_SLOT1 -2], XdrvMailbox.data, KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member); AddLog(LOG_LEVEL_INFO); @@ -1062,8 +1066,8 @@ boolean KnxCommand() else { return false; } // Incomplete command - snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%d\"}"), - command, index, XdrvMailbox.payload ); + snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%s\"}"), + command, index, XdrvMailbox.data ); return true; } diff --git a/sonoff/xsns_27_apds9960.ino b/sonoff/xsns_27_apds9960.ino new file mode 100644 index 000000000..151d9b732 --- /dev/null +++ b/sonoff/xsns_27_apds9960.ino @@ -0,0 +1,2123 @@ +/* + xsns_27_apds9960.ino - Support for I2C APDS9960 Proximity Sensor for Sonoff-Tasmota + + Copyright (C) 2018 Shawn Hymel/Sparkfun and Theo Arends + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + - Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifdef USE_I2C +#ifdef USE_APDS9960 + +#define XSNS_27 27 + +/*********************************************************************************************\ + * APDS9960 - Digital Proximity Ambient Light RGB and Gesture Sensor + * + * Source: Shawn Hymel (SparkFun Electronics) + * Adaption for TASMOTA: Christian Baars + * + * I2C Address: 0x39 +\*********************************************************************************************/ + +#if defined(USE_SHT) || defined(USE_VEML6070) + #warning **** Turned off conflicting drivers SHT and VEML6070 **** + #ifdef USE_SHT + #undef USE_SHT // SHT-Driver blocks gesture sensor + #endif + #ifdef USE_VEML6070 + #undef USE_VEML6070 // address conflict on the I2C-bus + #endif +#endif + +#define APDS9960_I2C_ADDR 0x39 + +#define APDS9960_CHIPID_1 0xAB +#define APDS9960_CHIPID_2 0x9C + +/* Gesture parameters */ +#define GESTURE_THRESHOLD_OUT 10 +#define GESTURE_SENSITIVITY_1 50 +#define GESTURE_SENSITIVITY_2 20 + +uint8_t APDS9960addr; +uint8_t APDS9960type = 0; +char APDS9960stype[7]; +char currentGesture[6]; +uint8_t gesture_mode = 1; + +volatile uint8_t recovery_loop_counter = 0; //count number of stateloops to switch the sensor off, if needed +#define APDS9960_LONG_RECOVERY 50 //long pause after sensor overload in loops +#define APDS9960_MAX_GESTURE_CYCLES 50 //how many FIFO-reads are allowed to prevent crash +bool APDS9960_overload = false; + +#ifdef USE_WEBSERVER +const char HTTP_APDS_9960_SNS[] PROGMEM = "%s" + "{s}" "Red" "{m}%s{e}" + "{s}" "Green" "{m}%s{e}" + "{s}" "Blue" "{m}%s{e}" + "{s}" "Ambient" "{m}%s " D_UNIT_LUX "{e}" + "{s}" "Proximity" "{m}%s{e}"; // {s} = , {m} = , {e} = +#endif // USE_WEBSERVER + +/*********************************************************************************************\ + * APDS9960 + * + * Programmer : APDS9960 Datasheet and Sparkfun +\*********************************************************************************************/ + +/* Misc parameters */ +#define FIFO_PAUSE_TIME 30 // Wait period (ms) between FIFO reads + +/* APDS-9960 register addresses */ +#define APDS9960_ENABLE 0x80 +#define APDS9960_ATIME 0x81 +#define APDS9960_WTIME 0x83 +#define APDS9960_AILTL 0x84 +#define APDS9960_AILTH 0x85 +#define APDS9960_AIHTL 0x86 +#define APDS9960_AIHTH 0x87 +#define APDS9960_PILT 0x89 +#define APDS9960_PIHT 0x8B +#define APDS9960_PERS 0x8C +#define APDS9960_CONFIG1 0x8D +#define APDS9960_PPULSE 0x8E +#define APDS9960_CONTROL 0x8F +#define APDS9960_CONFIG2 0x90 +#define APDS9960_ID 0x92 +#define APDS9960_STATUS 0x93 +#define APDS9960_CDATAL 0x94 +#define APDS9960_CDATAH 0x95 +#define APDS9960_RDATAL 0x96 +#define APDS9960_RDATAH 0x97 +#define APDS9960_GDATAL 0x98 +#define APDS9960_GDATAH 0x99 +#define APDS9960_BDATAL 0x9A +#define APDS9960_BDATAH 0x9B +#define APDS9960_PDATA 0x9C +#define APDS9960_POFFSET_UR 0x9D +#define APDS9960_POFFSET_DL 0x9E +#define APDS9960_CONFIG3 0x9F +#define APDS9960_GPENTH 0xA0 +#define APDS9960_GEXTH 0xA1 +#define APDS9960_GCONF1 0xA2 +#define APDS9960_GCONF2 0xA3 +#define APDS9960_GOFFSET_U 0xA4 +#define APDS9960_GOFFSET_D 0xA5 +#define APDS9960_GOFFSET_L 0xA7 +#define APDS9960_GOFFSET_R 0xA9 +#define APDS9960_GPULSE 0xA6 +#define APDS9960_GCONF3 0xAA +#define APDS9960_GCONF4 0xAB +#define APDS9960_GFLVL 0xAE +#define APDS9960_GSTATUS 0xAF +#define APDS9960_IFORCE 0xE4 +#define APDS9960_PICLEAR 0xE5 +#define APDS9960_CICLEAR 0xE6 +#define APDS9960_AICLEAR 0xE7 +#define APDS9960_GFIFO_U 0xFC +#define APDS9960_GFIFO_D 0xFD +#define APDS9960_GFIFO_L 0xFE +#define APDS9960_GFIFO_R 0xFF + +/* Bit fields */ +#define APDS9960_PON 0b00000001 +#define APDS9960_AEN 0b00000010 +#define APDS9960_PEN 0b00000100 +#define APDS9960_WEN 0b00001000 +#define APSD9960_AIEN 0b00010000 +#define APDS9960_PIEN 0b00100000 +#define APDS9960_GEN 0b01000000 +#define APDS9960_GVALID 0b00000001 + +/* On/Off definitions */ +#define OFF 0 +#define ON 1 + +/* Acceptable parameters for setMode */ +#define POWER 0 +#define AMBIENT_LIGHT 1 +#define PROXIMITY 2 +#define WAIT 3 +#define AMBIENT_LIGHT_INT 4 +#define PROXIMITY_INT 5 +#define GESTURE 6 +#define ALL 7 + +/* LED Drive values */ +#define LED_DRIVE_100MA 0 +#define LED_DRIVE_50MA 1 +#define LED_DRIVE_25MA 2 +#define LED_DRIVE_12_5MA 3 + +/* Proximity Gain (PGAIN) values */ +#define PGAIN_1X 0 +#define PGAIN_2X 1 +#define PGAIN_4X 2 +#define PGAIN_8X 3 + +/* ALS Gain (AGAIN) values */ +#define AGAIN_1X 0 +#define AGAIN_4X 1 +#define AGAIN_16X 2 +#define AGAIN_64X 3 + +/* Gesture Gain (GGAIN) values */ +#define GGAIN_1X 0 +#define GGAIN_2X 1 +#define GGAIN_4X 2 +#define GGAIN_8X 3 + +/* LED Boost values */ +#define LED_BOOST_100 0 +#define LED_BOOST_150 1 +#define LED_BOOST_200 2 +#define LED_BOOST_300 3 + +/* Gesture wait time values */ +#define GWTIME_0MS 0 +#define GWTIME_2_8MS 1 +#define GWTIME_5_6MS 2 +#define GWTIME_8_4MS 3 +#define GWTIME_14_0MS 4 +#define GWTIME_22_4MS 5 +#define GWTIME_30_8MS 6 +#define GWTIME_39_2MS 7 + +/* Default values */ +#define DEFAULT_ATIME 219 // 103ms +#define DEFAULT_WTIME 246 // 27ms +#define DEFAULT_PROX_PPULSE 0x87 // 16us, 8 pulses +#define DEFAULT_GESTURE_PPULSE 0x89 // 16us, 10 pulses +#define DEFAULT_POFFSET_UR 0 // 0 offset +#define DEFAULT_POFFSET_DL 0 // 0 offset +#define DEFAULT_CONFIG1 0x60 // No 12x wait (WTIME) factor +#define DEFAULT_LDRIVE LED_DRIVE_100MA +#define DEFAULT_PGAIN PGAIN_4X +#define DEFAULT_AGAIN AGAIN_4X +#define DEFAULT_PILT 0 // Low proximity threshold +#define DEFAULT_PIHT 50 // High proximity threshold +#define DEFAULT_AILT 0xFFFF // Force interrupt for calibration +#define DEFAULT_AIHT 0 +#define DEFAULT_PERS 0x11 // 2 consecutive prox or ALS for int. +#define DEFAULT_CONFIG2 0x01 // No saturation interrupts or LED boost +#define DEFAULT_CONFIG3 0 // Enable all photodiodes, no SAI +#define DEFAULT_GPENTH 40 // Threshold for entering gesture mode +#define DEFAULT_GEXTH 30 // Threshold for exiting gesture mode +#define DEFAULT_GCONF1 0x40 // 4 gesture events for int., 1 for exit +#define DEFAULT_GGAIN GGAIN_4X +#define DEFAULT_GLDRIVE LED_DRIVE_100MA +#define DEFAULT_GWTIME GWTIME_2_8MS +#define DEFAULT_GOFFSET 0 // No offset scaling for gesture mode +#define DEFAULT_GPULSE 0xC9 // 32us, 10 pulses +#define DEFAULT_GCONF3 0 // All photodiodes active during gesture +#define DEFAULT_GIEN 0 // Disable gesture interrupts + +#define ERROR 0xFF + +/* Direction definitions */ +enum { + DIR_NONE, + DIR_LEFT, + DIR_RIGHT, + DIR_UP, + DIR_DOWN, + DIR_ALL +}; + +/* State definitions*/ +enum { + APDS9960_NA_STATE, + APDS9960_ALL_STATE +}; + +/* Container for gesture data */ +typedef struct gesture_data_type { + uint8_t u_data[32]; + uint8_t d_data[32]; + uint8_t l_data[32]; + uint8_t r_data[32]; + uint8_t index; + uint8_t total_gestures; + uint8_t in_threshold; + uint8_t out_threshold; +} gesture_data_type; + +/*Members*/ + gesture_data_type gesture_data_; + int16_t gesture_ud_delta_ = 0; + int16_t gesture_lr_delta_ = 0; + int16_t gesture_ud_count_ = 0; + int16_t gesture_lr_count_ = 0; + int16_t gesture_state_ = 0; + int16_t gesture_motion_ = DIR_NONE; + + /******************************************************************************* + * Helper functions + ******************************************************************************/ + + /** + * @brief Writes a single byte to the I2C device (no register) + * + * @param[in] val the 1-byte value to write to the I2C device + * @return True if successful write operation. False otherwise. + */ + bool wireWriteByte(uint8_t val) + { + Wire.beginTransmission(APDS9960_I2C_ADDR); + Wire.write(val); + if( Wire.endTransmission() != 0 ) { + return false; + } + + return true; + } + /** + * @brief Reads a block (array) of bytes from the I2C device and register + * + * @param[in] reg the register to read from + * @param[out] val pointer to the beginning of the data + * @param[in] len number of bytes to read + * @return Number of bytes read. -1 on read error. + */ +int8_t wireReadDataBlock( uint8_t reg, + uint8_t *val, + uint16_t len) +{ + unsigned char i = 0; + + /* Indicate which register we want to read from */ + if (!wireWriteByte(reg)) { + return -1; + } + + /* Read block data */ + Wire.requestFrom(APDS9960_I2C_ADDR, len); + while (Wire.available()) { + if (i >= len) { + return -1; + } + val[i] = Wire.read(); + i++; + } + + return i; +} + + /******************************************************************************* + * Getters and setters for register values + ******************************************************************************/ + + /** + * @brief Returns the lower threshold for proximity detection + * + * @return lower threshold + */ + uint8_t getProxIntLowThresh() + { + uint8_t val; + + /* Read value from PILT register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PILT) ; + return val; + } + + /** + * @brief Sets the lower threshold for proximity detection + * + * @param[in] threshold the lower proximity threshold + */ + void setProxIntLowThresh(uint8_t threshold) + { + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PILT, threshold); + } + + /** + * @brief Returns the high threshold for proximity detection + * + * @return high threshold + */ + uint8_t getProxIntHighThresh() + { + uint8_t val; + + /* Read value from PIHT register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PIHT) ; + return val; + } + + /** + * @brief Sets the high threshold for proximity detection + * + * @param[in] threshold the high proximity threshold + */ + + void setProxIntHighThresh(uint8_t threshold) + { + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PIHT, threshold); + } + + + /** + * @brief Returns LED drive strength for proximity and ALS + * + * Value LED Current + * 0 100 mA + * 1 50 mA + * 2 25 mA + * 3 12.5 mA + * + * @return the value of the LED drive strength. 0xFF on failure. + */ + uint8_t getLEDDrive() + { + uint8_t val; + + /* Read value from CONTROL register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL) ; + /* Shift and mask out LED drive bits */ + val = (val >> 6) & 0b00000011; + + return val; + } + + /** + * @brief Sets the LED drive strength for proximity and ALS + * + * Value LED Current + * 0 100 mA + * 1 50 mA + * 2 25 mA + * 3 12.5 mA + * + * @param[in] drive the value (0-3) for the LED drive strength + */ + void setLEDDrive(uint8_t drive) + { + uint8_t val; + + /* Read value from CONTROL register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL); + + /* Set bits in register to given value */ + drive &= 0b00000011; + drive = drive << 6; + val &= 0b00111111; + val |= drive; + + /* Write register value back into CONTROL register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val); + } + + + /** + * @brief Returns receiver gain for proximity detection + * + * Value Gain + * 0 1x + * 1 2x + * 2 4x + * 3 8x + * + * @return the value of the proximity gain. 0xFF on failure. + */ + uint8_t getProximityGain() + { + uint8_t val; + + /* Read value from CONTROL register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL) ; + /* Shift and mask out PDRIVE bits */ + val = (val >> 2) & 0b00000011; + + return val; + } + + /** + * @brief Sets the receiver gain for proximity detection + * + * Value Gain + * 0 1x + * 1 2x + * 2 4x + * 3 8x + * + * @param[in] drive the value (0-3) for the gain + */ + void setProximityGain(uint8_t drive) + { + uint8_t val; + + /* Read value from CONTROL register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL); + + /* Set bits in register to given value */ + drive &= 0b00000011; + drive = drive << 2; + val &= 0b11110011; + val |= drive; + + /* Write register value back into CONTROL register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val); + } + + + /** + * @brief Returns receiver gain for the ambient light sensor (ALS) + * + * Value Gain + * 0 1x + * 1 4x + * 2 16x + * 3 64x + * + * @return the value of the ALS gain. 0xFF on failure. + */ + + /** + * @brief Sets the receiver gain for the ambient light sensor (ALS) + * + * Value Gain + * 0 1x + * 1 4x + * 2 16x + * 3 64x + * + * @param[in] drive the value (0-3) for the gain + */ + void setAmbientLightGain(uint8_t drive) + { + uint8_t val; + + /* Read value from CONTROL register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONTROL); + + /* Set bits in register to given value */ + drive &= 0b00000011; + val &= 0b11111100; + val |= drive; + + /* Write register value back into CONTROL register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONTROL, val); + } + + /** + * @brief Get the current LED boost value + * + * Value Boost Current + * 0 100% + * 1 150% + * 2 200% + * 3 300% + * + * @return The LED boost value. 0xFF on failure. + */ + uint8_t getLEDBoost() + { + uint8_t val; + + /* Read value from CONFIG2 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG2) ; + + /* Shift and mask out LED_BOOST bits */ + val = (val >> 4) & 0b00000011; + + return val; + } + + /** + * @brief Sets the LED current boost value + * + * Value Boost Current + * 0 100% + * 1 150% + * 2 200% + * 3 300% + * + * @param[in] drive the value (0-3) for current boost (100-300%) + */ + void setLEDBoost(uint8_t boost) + { + uint8_t val; + + /* Read value from CONFIG2 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG2) ; + /* Set bits in register to given value */ + boost &= 0b00000011; + boost = boost << 4; + val &= 0b11001111; + val |= boost; + + /* Write register value back into CONFIG2 register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG2, val) ; + } + + /** + * @brief Gets proximity gain compensation enable + * + * @return 1 if compensation is enabled. 0 if not. 0xFF on error. + */ + uint8_t getProxGainCompEnable() + { + uint8_t val; + + /* Read value from CONFIG3 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ; + + /* Shift and mask out PCMP bits */ + val = (val >> 5) & 0b00000001; + + return val; + } + + /** + * @brief Sets the proximity gain compensation enable + * + * @param[in] enable 1 to enable compensation. 0 to disable compensation. + */ + void setProxGainCompEnable(uint8_t enable) + { + uint8_t val; + + /* Read value from CONFIG3 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ; + + /* Set bits in register to given value */ + enable &= 0b00000001; + enable = enable << 5; + val &= 0b11011111; + val |= enable; + + /* Write register value back into CONFIG3 register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val) ; + } + + /** + * @brief Gets the current mask for enabled/disabled proximity photodiodes + * + * 1 = disabled, 0 = enabled + * Bit Photodiode + * 3 UP + * 2 DOWN + * 1 LEFT + * 0 RIGHT + * + * @return Current proximity mask for photodiodes. 0xFF on error. + */ + uint8_t getProxPhotoMask() + { + uint8_t val; + + /* Read value from CONFIG3 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ; + + /* Mask out photodiode enable mask bits */ + val &= 0b00001111; + + return val; + } + + /** + * @brief Sets the mask for enabling/disabling proximity photodiodes + * + * 1 = disabled, 0 = enabled + * Bit Photodiode + * 3 UP + * 2 DOWN + * 1 LEFT + * 0 RIGHT + * + * @param[in] mask 4-bit mask value + */ + void setProxPhotoMask(uint8_t mask) + { + uint8_t val; + + /* Read value from CONFIG3 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CONFIG3) ; + + /* Set bits in register to given value */ + mask &= 0b00001111; + val &= 0b11110000; + val |= mask; + + /* Write register value back into CONFIG3 register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val) ; + } + + /** + * @brief Gets the entry proximity threshold for gesture sensing + * + * @return Current entry proximity threshold. + */ + uint8_t getGestureEnterThresh() + { + uint8_t val; + + /* Read value from GPENTH register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GPENTH) ; + + return val; + } + + /** + * @brief Sets the entry proximity threshold for gesture sensing + * + * @param[in] threshold proximity value needed to start gesture mode + */ + void setGestureEnterThresh(uint8_t threshold) + { + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GPENTH, threshold) ; + + } + + /** + * @brief Gets the exit proximity threshold for gesture sensing + * + * @return Current exit proximity threshold. + */ + uint8_t getGestureExitThresh() + { + uint8_t val; + + /* Read value from GEXTH register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GEXTH) ; + + return val; + } + + /** + * @brief Sets the exit proximity threshold for gesture sensing + * + * @param[in] threshold proximity value needed to end gesture mode + */ + void setGestureExitThresh(uint8_t threshold) + { + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GEXTH, threshold) ; + } + + /** + * @brief Gets the gain of the photodiode during gesture mode + * + * Value Gain + * 0 1x + * 1 2x + * 2 4x + * 3 8x + * + * @return the current photodiode gain. 0xFF on error. + */ + uint8_t getGestureGain() + { + uint8_t val; + + /* Read value from GCONF2 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ; + + /* Shift and mask out GGAIN bits */ + val = (val >> 5) & 0b00000011; + + return val; + } + + /** + * @brief Sets the gain of the photodiode during gesture mode + * + * Value Gain + * 0 1x + * 1 2x + * 2 4x + * 3 8x + * + * @param[in] gain the value for the photodiode gain + */ + void setGestureGain(uint8_t gain) + { + uint8_t val; + + /* Read value from GCONF2 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ; + + /* Set bits in register to given value */ + gain &= 0b00000011; + gain = gain << 5; + val &= 0b10011111; + val |= gain; + + /* Write register value back into GCONF2 register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ; + } + + /** + * @brief Gets the drive current of the LED during gesture mode + * + * Value LED Current + * 0 100 mA + * 1 50 mA + * 2 25 mA + * 3 12.5 mA + * + * @return the LED drive current value. 0xFF on error. + */ + uint8_t getGestureLEDDrive() + { + uint8_t val; + + /* Read value from GCONF2 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ; + + /* Shift and mask out GLDRIVE bits */ + val = (val >> 3) & 0b00000011; + + return val; + } + + /** + * @brief Sets the LED drive current during gesture mode + * + * Value LED Current + * 0 100 mA + * 1 50 mA + * 2 25 mA + * 3 12.5 mA + * + * @param[in] drive the value for the LED drive current + */ + void setGestureLEDDrive(uint8_t drive) + { + uint8_t val; + + /* Read value from GCONF2 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ; + + /* Set bits in register to given value */ + drive &= 0b00000011; + drive = drive << 3; + val &= 0b11100111; + val |= drive; + + /* Write register value back into GCONF2 register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ; + } + + /** + * @brief Gets the time in low power mode between gesture detections + * + * Value Wait time + * 0 0 ms + * 1 2.8 ms + * 2 5.6 ms + * 3 8.4 ms + * 4 14.0 ms + * 5 22.4 ms + * 6 30.8 ms + * 7 39.2 ms + * + * @return the current wait time between gestures. 0xFF on error. + */ + uint8_t getGestureWaitTime() + { + uint8_t val; + + /* Read value from GCONF2 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ; + + /* Mask out GWTIME bits */ + val &= 0b00000111; + + return val; + } + + /** + * @brief Sets the time in low power mode between gesture detections + * + * Value Wait time + * 0 0 ms + * 1 2.8 ms + * 2 5.6 ms + * 3 8.4 ms + * 4 14.0 ms + * 5 22.4 ms + * 6 30.8 ms + * 7 39.2 ms + * + * @param[in] the value for the wait time + */ + void setGestureWaitTime(uint8_t time) + { + uint8_t val; + + /* Read value from GCONF2 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF2) ; + + /* Set bits in register to given value */ + time &= 0b00000111; + val &= 0b11111000; + val |= time; + + /* Write register value back into GCONF2 register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val) ; + } + + /** + * @brief Gets the low threshold for ambient light interrupts + * + * @param[out] threshold current low threshold stored on the APDS-9960 + */ + void getLightIntLowThreshold(uint16_t &threshold) + { + uint8_t val_byte; + threshold = 0; + + /* Read value from ambient light low threshold, low byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_AILTL) ; + threshold = val_byte; + + /* Read value from ambient light low threshold, high byte register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTH, val_byte) ; + threshold = threshold + ((uint16_t)val_byte << 8); + } + + /** + * @brief Sets the low threshold for ambient light interrupts + * + * @param[in] threshold low threshold value for interrupt to trigger + */ + + void setLightIntLowThreshold(uint16_t threshold) + { + uint8_t val_low; + uint8_t val_high; + + /* Break 16-bit threshold into 2 8-bit values */ + val_low = threshold & 0x00FF; + val_high = (threshold & 0xFF00) >> 8; + + /* Write low byte */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTL, val_low) ; + + /* Write high byte */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AILTH, val_high) ; + + } + + + /** + * @brief Gets the high threshold for ambient light interrupts + * + * @param[out] threshold current low threshold stored on the APDS-9960 + */ + void getLightIntHighThreshold(uint16_t &threshold) + { + uint8_t val_byte; + threshold = 0; + + /* Read value from ambient light high threshold, low byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_AIHTL); + threshold = val_byte; + + /* Read value from ambient light high threshold, high byte register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AIHTH, val_byte) ; + threshold = threshold + ((uint16_t)val_byte << 8); + } + + /** + * @brief Sets the high threshold for ambient light interrupts + * + * @param[in] threshold high threshold value for interrupt to trigger + */ + void setLightIntHighThreshold(uint16_t threshold) + { + uint8_t val_low; + uint8_t val_high; + + /* Break 16-bit threshold into 2 8-bit values */ + val_low = threshold & 0x00FF; + val_high = (threshold & 0xFF00) >> 8; + + /* Write low byte */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AIHTL, val_low); + + /* Write high byte */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_AIHTH, val_high) ; + } + + + /** + * @brief Gets the low threshold for proximity interrupts + * + * @param[out] threshold current low threshold stored on the APDS-9960 + */ + void getProximityIntLowThreshold(uint8_t &threshold) + { + threshold = 0; + + /* Read value from proximity low threshold register */ + threshold = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PILT); + + } + + /** + * @brief Sets the low threshold for proximity interrupts + * + * @param[in] threshold low threshold value for interrupt to trigger + */ + void setProximityIntLowThreshold(uint8_t threshold) + { + + /* Write threshold value to register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PILT, threshold) ; + } + + + + /** + * @brief Gets the high threshold for proximity interrupts + * + * @param[out] threshold current low threshold stored on the APDS-9960 + */ + void getProximityIntHighThreshold(uint8_t &threshold) + { + threshold = 0; + + /* Read value from proximity low threshold register */ + threshold = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PIHT) ; + + } + + /** + * @brief Sets the high threshold for proximity interrupts + * + * @param[in] threshold high threshold value for interrupt to trigger + */ + void setProximityIntHighThreshold(uint8_t threshold) + { + + /* Write threshold value to register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PIHT, threshold) ; + } + + /** + * @brief Gets if ambient light interrupts are enabled or not + * + * @return 1 if interrupts are enabled, 0 if not. 0xFF on error. + */ + uint8_t getAmbientLightIntEnable() + { + uint8_t val; + + /* Read value from ENABLE register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE) ; + + /* Shift and mask out AIEN bit */ + val = (val >> 4) & 0b00000001; + + return val; + } + + /** + * @brief Turns ambient light interrupts on or off + * + * @param[in] enable 1 to enable interrupts, 0 to turn them off + */ + void setAmbientLightIntEnable(uint8_t enable) + { + uint8_t val; + + /* Read value from ENABLE register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE); + + /* Set bits in register to given value */ + enable &= 0b00000001; + enable = enable << 4; + val &= 0b11101111; + val |= enable; + + /* Write register value back into ENABLE register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ENABLE, val) ; + } + + /** + * @brief Gets if proximity interrupts are enabled or not + * + * @return 1 if interrupts are enabled, 0 if not. 0xFF on error. + */ + uint8_t getProximityIntEnable() + { + uint8_t val; + + /* Read value from ENABLE register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE) ; + + /* Shift and mask out PIEN bit */ + val = (val >> 5) & 0b00000001; + + return val; + } + + /** + * @brief Turns proximity interrupts on or off + * + * @param[in] enable 1 to enable interrupts, 0 to turn them off + */ + void setProximityIntEnable(uint8_t enable) + { + uint8_t val; + + /* Read value from ENABLE register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE) ; + + /* Set bits in register to given value */ + enable &= 0b00000001; + enable = enable << 5; + val &= 0b11011111; + val |= enable; + + /* Write register value back into ENABLE register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ENABLE, val) ; + } + + /** + * @brief Gets if gesture interrupts are enabled or not + * + * @return 1 if interrupts are enabled, 0 if not. 0xFF on error. + */ + uint8_t getGestureIntEnable() + { + uint8_t val; + + /* Read value from GCONF4 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF4) ; + + /* Shift and mask out GIEN bit */ + val = (val >> 1) & 0b00000001; + + return val; + } + + /** + * @brief Turns gesture-related interrupts on or off + * + * @param[in] enable 1 to enable interrupts, 0 to turn them off + */ + void setGestureIntEnable(uint8_t enable) + { + uint8_t val; + + /* Read value from GCONF4 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF4) ; + + /* Set bits in register to given value */ + enable &= 0b00000001; + enable = enable << 1; + val &= 0b11111101; + val |= enable; + + /* Write register value back into GCONF4 register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF4, val) ; + } + + /** + * @brief Clears the ambient light interrupt + * + */ + void clearAmbientLightInt() + { + uint8_t throwaway; + throwaway = I2cRead8(APDS9960_I2C_ADDR, APDS9960_AICLEAR); + } + + /** + * @brief Clears the proximity interrupt + * + */ + void clearProximityInt() + { + uint8_t throwaway; + throwaway = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PICLEAR) ; + + } + + /** + * @brief Tells if the gesture state machine is currently running + * + * @return 1 if gesture state machine is running, 0 if not. 0xFF on error. + */ + uint8_t getGestureMode() + { + uint8_t val; + + /* Read value from GCONF4 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF4) ; + + /* Mask out GMODE bit */ + val &= 0b00000001; + + return val; + } + + /** + * @brief Tells the state machine to either enter or exit gesture state machine + * + * @param[in] mode 1 to enter gesture state machine, 0 to exit. + */ + void setGestureMode(uint8_t mode) + { + uint8_t val; + + /* Read value from GCONF4 register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GCONF4) ; + + /* Set bits in register to given value */ + mode &= 0b00000001; + val &= 0b11111110; + val |= mode; + + /* Write register value back into GCONF4 register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF4, val) ; + } + + +bool APDS9960_init() +{ + /* Set default values for ambient light and proximity registers */ + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ATIME, DEFAULT_ATIME) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_WTIME, DEFAULT_WTIME) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PPULSE, DEFAULT_PROX_PPULSE) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG1, DEFAULT_CONFIG1) ; + + setLEDDrive(DEFAULT_LDRIVE); + + setProximityGain(DEFAULT_PGAIN); + + setAmbientLightGain(DEFAULT_AGAIN); + + setProxIntLowThresh(DEFAULT_PILT) ; + + setProxIntHighThresh(DEFAULT_PIHT); + + setLightIntLowThreshold(DEFAULT_AILT) ; + + setLightIntHighThreshold(DEFAULT_AIHT) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PERS, DEFAULT_PERS) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG2, DEFAULT_CONFIG2) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, DEFAULT_CONFIG3) ; + + /* Set default values for gesture sense registers */ + setGestureEnterThresh(DEFAULT_GPENTH); + + setGestureExitThresh(DEFAULT_GEXTH) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF1, DEFAULT_GCONF1) ; + + setGestureGain(DEFAULT_GGAIN) ; + + setGestureLEDDrive(DEFAULT_GLDRIVE) ; + + setGestureWaitTime(DEFAULT_GWTIME) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GPULSE, DEFAULT_GPULSE) ; + + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF3, DEFAULT_GCONF3) ; + + setGestureIntEnable(DEFAULT_GIEN); + + return true; +} +/******************************************************************************* + * Public methods for controlling the APDS-9960 + ******************************************************************************/ + +/** + * @brief Reads and returns the contents of the ENABLE register + * + * @return Contents of the ENABLE register. 0xFF if error. + */ +uint8_t getMode() +{ + uint8_t enable_value; + + /* Read current ENABLE register */ + enable_value = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ENABLE) ; + + return enable_value; +} + +/** + * @brief Enables or disables a feature in the APDS-9960 + * + * @param[in] mode which feature to enable + * @param[in] enable ON (1) or OFF (0) + */ +void setMode(uint8_t mode, uint8_t enable) +{ + uint8_t reg_val; + + /* Read current ENABLE register */ + reg_val = getMode(); + + + /* Change bit(s) in ENABLE register */ + enable = enable & 0x01; + if( mode >= 0 && mode <= 6 ) { + if (enable) { + reg_val |= (1 << mode); + } else { + reg_val &= ~(1 << mode); + } + } else if( mode == ALL ) { + if (enable) { + reg_val = 0x7F; + } else { + reg_val = 0x00; + } + } + + /* Write value back to ENABLE register */ + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ENABLE, reg_val) ; +} + +/** + * @brief Starts the light (R/G/B/Ambient) sensor on the APDS-9960 + * + * @param[in] interrupts true to enable hardware interrupt on high or low light + */ +void enableLightSensor(bool interrupts) +{ + + /* Set default gain, interrupts, enable power, and enable sensor */ + setAmbientLightGain(DEFAULT_AGAIN); + if( interrupts ) { + setAmbientLightIntEnable(1) ; + + } + else { + setAmbientLightIntEnable(0); + } + enablePower() ; + setMode(AMBIENT_LIGHT, 1) ; +} + +/** + * @brief Ends the light sensor on the APDS-9960 + * + */ +void disableLightSensor() +{ + setAmbientLightIntEnable(0) ; + setMode(AMBIENT_LIGHT, 0) ; +} + +/** + * @brief Starts the proximity sensor on the APDS-9960 + * + * @param[in] interrupts true to enable hardware external interrupt on proximity + */ +void enableProximitySensor(bool interrupts) +{ + /* Set default gain, LED, interrupts, enable power, and enable sensor */ + setProximityGain(DEFAULT_PGAIN); + setLEDDrive(DEFAULT_LDRIVE) ; + if( interrupts ) { + setProximityIntEnable(1) ; + } else { + setProximityIntEnable(0) ; + } + enablePower(); + setMode(PROXIMITY, 1) ; +} + +/** + * @brief Ends the proximity sensor on the APDS-9960 + * + */ +void disableProximitySensor() +{ + setProximityIntEnable(0) ; + setMode(PROXIMITY, 0) ; +} + +/** + * @brief Starts the gesture recognition engine on the APDS-9960 + * + * @param[in] interrupts true to enable hardware external interrupt on gesture + */ +void enableGestureSensor(bool interrupts) +{ + + /* Enable gesture mode + Set ENABLE to 0 (power off) + Set WTIME to 0xFF + Set AUX to LED_BOOST_300 + Enable PON, WEN, PEN, GEN in ENABLE + */ + resetGestureParameters(); + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_WTIME, 0xFF) ; + I2cWrite8(APDS9960_I2C_ADDR, APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ; + setLEDBoost(LED_BOOST_100); // tip from jonn26 - 100 for 300 + if( interrupts ) { + setGestureIntEnable(1) ; + } else { + setGestureIntEnable(0) ; + } + setGestureMode(1); + enablePower() ; + setMode(WAIT, 1) ; + setMode(PROXIMITY, 1) ; + setMode(GESTURE, 1); +} + +/** + * @brief Ends the gesture recognition engine on the APDS-9960 + * + */ +void disableGestureSensor() +{ + resetGestureParameters(); + setGestureIntEnable(0) ; + setGestureMode(0) ; + setMode(GESTURE, 0) ; +} + +/** + * @brief Determines if there is a gesture available for reading + * + * @return True if gesture available. False otherwise. + */ +bool isGestureAvailable() +{ + uint8_t val; + + /* Read value from GSTATUS register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GSTATUS) ; + + /* Shift and mask out GVALID bit */ + val &= APDS9960_GVALID; + + /* Return true/false based on GVALID bit */ + if( val == 1) { + return true; + } else { + return false; + } +} + +/** + * @brief Processes a gesture event and returns best guessed gesture + * + * @return Number corresponding to gesture. -1 on error. + */ +int16_t readGesture() +{ + uint8_t fifo_level = 0; + uint8_t bytes_read = 0; + uint8_t fifo_data[128]; + uint8_t gstatus; + uint16_t motion; + uint16_t i; + uint8_t gesture_loop_counter = 0; // don't loop forever later + + /* Make sure that power and gesture is on and data is valid */ + if( !isGestureAvailable() || !(getMode() & 0b01000001) ) { + return DIR_NONE; + } + + /* Keep looping as long as gesture data is valid */ + while(1) { + if (gesture_loop_counter == APDS9960_MAX_GESTURE_CYCLES){ // We will escape after a few loops + disableGestureSensor(); // stop the sensor to prevent problems with power consumption/blocking and return to the main loop + APDS9960_overload = true; // we report this as "long"-gesture + char log[LOGSZ]; + snprintf_P(log, sizeof(log), PSTR("Sensor overload")); + AddLog_P(LOG_LEVEL_DEBUG, log); + } + gesture_loop_counter += 1; + /* Wait some time to collect next batch of FIFO data */ + delay(FIFO_PAUSE_TIME); + + /* Get the contents of the STATUS register. Is data still valid? */ + gstatus = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GSTATUS); + + /* If we have valid data, read in FIFO */ + if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) { + + /* Read the current FIFO level */ + fifo_level = I2cRead8(APDS9960_I2C_ADDR,APDS9960_GFLVL) ; + +#if DEBUG + Serial.print("FIFO Level: "); + Serial.println(fifo_level); +#endif + + /* If there's stuff in the FIFO, read it into our data block */ + if( fifo_level > 0) { + bytes_read = wireReadDataBlock( APDS9960_GFIFO_U, + (uint8_t*)fifo_data, + (fifo_level * 4) ); + if( bytes_read == -1 ) { + return ERROR; + } +#if DEBUG + Serial.print("FIFO Dump: "); + for ( i = 0; i < bytes_read; i++ ) { + Serial.print(fifo_data[i]); + Serial.print(" "); + } + Serial.println(); +#endif + + /* If at least 1 set of data, sort the data into U/D/L/R */ + if( bytes_read >= 4 ) { + for( i = 0; i < bytes_read; i += 4 ) { + gesture_data_.u_data[gesture_data_.index] = \ + fifo_data[i + 0]; + gesture_data_.d_data[gesture_data_.index] = \ + fifo_data[i + 1]; + gesture_data_.l_data[gesture_data_.index] = \ + fifo_data[i + 2]; + gesture_data_.r_data[gesture_data_.index] = \ + fifo_data[i + 3]; + gesture_data_.index++; + gesture_data_.total_gestures++; + } + +#if DEBUG + Serial.print("Up Data: "); + for ( i = 0; i < gesture_data_.total_gestures; i++ ) { + Serial.print(gesture_data_.u_data[i]); + Serial.print(" "); + } + Serial.println(); +#endif + + /* Filter and process gesture data. Decode near/far state */ + if( processGestureData() ) { + if( decodeGesture() ) { + //***TODO: U-Turn Gestures +#if DEBUG + //Serial.println(gesture_motion_); +#endif + } + } + + /* Reset data */ + gesture_data_.index = 0; + gesture_data_.total_gestures = 0; + } + } + } else { + + /* Determine best guessed gesture and clean up */ + delay(FIFO_PAUSE_TIME); + decodeGesture(); + motion = gesture_motion_; +#if DEBUG + Serial.print("END: "); + Serial.println(gesture_motion_); +#endif + resetGestureParameters(); + return motion; + } + } +} + +/** + * Turn the APDS-9960 on + * + */ +void enablePower() +{ + setMode(POWER, 1) ; +} + +/** + * Turn the APDS-9960 off + * + */ +void disablePower() +{ + setMode(POWER, 0) ; +} + +/******************************************************************************* + * Ambient light and color sensor controls + ******************************************************************************/ + +/** + * @brief Reads the ambient (clear) light level as a 16-bit value + * + * @param[out] val value of the light sensor. + */ +void readAmbientLight(uint16_t &val) +{ + uint8_t val_byte; + val = 0; + + /* Read value from clear channel, low byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CDATAL); + val = val_byte; + + /* Read value from clear channel, high byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_CDATAH); + val = val + ((uint16_t)val_byte << 8); +} + +/** + * @brief Reads the red light level as a 16-bit value + * + * @param[out] val value of the light sensor. + */ +void readRedLight(uint16_t &val) +{ + uint8_t val_byte; + val = 0; + + /* Read value from clear channel, low byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_RDATAL) ; + val = val_byte; + + /* Read value from clear channel, high byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_RDATAH) ; + val = val + ((uint16_t)val_byte << 8); +} + +/** + * @brief Reads the green light level as a 16-bit value + * + * @param[out] val value of the light sensor. + */ +void readGreenLight(uint16_t &val) +{ + uint8_t val_byte; + val = 0; + + /* Read value from clear channel, low byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GDATAL) ; + val = val_byte; + + /* Read value from clear channel, high byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GDATAH) ; + val = val + ((uint16_t)val_byte << 8); +} + +/** + * @brief Reads the red light level as a 16-bit value + * + * @param[out] val value of the light sensor. + */ +void readBlueLight(uint16_t &val) +{ + uint8_t val_byte; + val = 0; + + /* Read value from clear channel, low byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_BDATAL) ; + val = val_byte; + + /* Read value from clear channel, high byte register */ + val_byte = I2cRead8(APDS9960_I2C_ADDR, APDS9960_BDATAH) ; + val = val + ((uint16_t)val_byte << 8); +} + +/******************************************************************************* + * Proximity sensor controls + ******************************************************************************/ + +/** + * @brief Reads the proximity level as an 8-bit value + * + * @param[out] val value of the proximity sensor. + */ +void readProximity(uint8_t &val) +{ + val = 0; + + /* Read value from proximity data register */ + val = I2cRead8(APDS9960_I2C_ADDR, APDS9960_PDATA) ; +} + +/******************************************************************************* + * High-level gesture controls + ******************************************************************************/ + +/** + * @brief Resets all the parameters in the gesture data member + */ +void resetGestureParameters() +{ + gesture_data_.index = 0; + gesture_data_.total_gestures = 0; + + gesture_ud_delta_ = 0; + gesture_lr_delta_ = 0; + + gesture_ud_count_ = 0; + gesture_lr_count_ = 0; + + gesture_state_ = 0; + gesture_motion_ = DIR_NONE; +} + +/** + * @brief Processes the raw gesture data to determine swipe direction + * + * @return True if near or far state seen. False otherwise. + */ +bool processGestureData() +{ + uint8_t u_first = 0; + uint8_t d_first = 0; + uint8_t l_first = 0; + uint8_t r_first = 0; + uint8_t u_last = 0; + uint8_t d_last = 0; + uint8_t l_last = 0; + uint8_t r_last = 0; + uint16_t ud_ratio_first; + uint16_t lr_ratio_first; + uint16_t ud_ratio_last; + uint16_t lr_ratio_last; + uint16_t ud_delta; + uint16_t lr_delta; + uint16_t i; + + /* If we have less than 4 total gestures, that's not enough */ + if( gesture_data_.total_gestures <= 4 ) { + return false; + } + + /* Check to make sure our data isn't out of bounds */ + if( (gesture_data_.total_gestures <= 32) && \ + (gesture_data_.total_gestures > 0) ) { + + /* Find the first value in U/D/L/R above the threshold */ + for( i = 0; i < gesture_data_.total_gestures; i++ ) { + if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) && + (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) && + (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) && + (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) { + + u_first = gesture_data_.u_data[i]; + d_first = gesture_data_.d_data[i]; + l_first = gesture_data_.l_data[i]; + r_first = gesture_data_.r_data[i]; + break; + } + } + + /* If one of the _first values is 0, then there is no good data */ + if( (u_first == 0) || (d_first == 0) || \ + (l_first == 0) || (r_first == 0) ) { + + return false; + } + /* Find the last value in U/D/L/R above the threshold */ + for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) { +#if DEBUG + Serial.print(F("Finding last: ")); + Serial.print(F("U:")); + Serial.print(gesture_data_.u_data[i]); + Serial.print(F(" D:")); + Serial.print(gesture_data_.d_data[i]); + Serial.print(F(" L:")); + Serial.print(gesture_data_.l_data[i]); + Serial.print(F(" R:")); + Serial.println(gesture_data_.r_data[i]); +#endif + if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) && + (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) && + (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) && + (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) { + + u_last = gesture_data_.u_data[i]; + d_last = gesture_data_.d_data[i]; + l_last = gesture_data_.l_data[i]; + r_last = gesture_data_.r_data[i]; + break; + } + } + } + + /* Calculate the first vs. last ratio of up/down and left/right */ + ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first); + lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first); + ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last); + lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last); + +#if DEBUG + Serial.print(F("Last Values: ")); + Serial.print(F("U:")); + Serial.print(u_last); + Serial.print(F(" D:")); + Serial.print(d_last); + Serial.print(F(" L:")); + Serial.print(l_last); + Serial.print(F(" R:")); + Serial.println(r_last); + + Serial.print(F("Ratios: ")); + Serial.print(F("UD Fi: ")); + Serial.print(ud_ratio_first); + Serial.print(F(" UD La: ")); + Serial.print(ud_ratio_last); + Serial.print(F(" LR Fi: ")); + Serial.print(lr_ratio_first); + Serial.print(F(" LR La: ")); + Serial.println(lr_ratio_last); +#endif + + /* Determine the difference between the first and last ratios */ + ud_delta = ud_ratio_last - ud_ratio_first; + lr_delta = lr_ratio_last - lr_ratio_first; + +#if DEBUG + Serial.print("Deltas: "); + Serial.print("UD: "); + Serial.print(ud_delta); + Serial.print(" LR: "); + Serial.println(lr_delta); +#endif + + /* Accumulate the UD and LR delta values */ + gesture_ud_delta_ += ud_delta; + gesture_lr_delta_ += lr_delta; + +#if DEBUG + Serial.print("Accumulations: "); + Serial.print("UD: "); + Serial.print(gesture_ud_delta_); + Serial.print(" LR: "); + Serial.println(gesture_lr_delta_); +#endif + + /* Determine U/D gesture */ + if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) { + gesture_ud_count_ = 1; + } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) { + gesture_ud_count_ = -1; + } else { + gesture_ud_count_ = 0; + } + + /* Determine L/R gesture */ + if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) { + gesture_lr_count_ = 1; + } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) { + gesture_lr_count_ = -1; + } else { + gesture_lr_count_ = 0; + } + +#if DEBUG + Serial.print("UD_CT: "); + Serial.print(gesture_ud_count_); + Serial.print(" LR_CT: "); + Serial.print(gesture_lr_count_); + Serial.println("----------"); +#endif + + return false; +} + +/** + * @brief Determines swipe direction or near/far state + * + * @return True if near/far event. False otherwise. + */ +bool decodeGesture() +{ + + /* Determine swipe direction */ + if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 0) ) { + gesture_motion_ = DIR_UP; + } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 0) ) { + gesture_motion_ = DIR_DOWN; + } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 1) ) { + gesture_motion_ = DIR_RIGHT; + } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == -1) ) { + gesture_motion_ = DIR_LEFT; + } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 1) ) { + if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) { + gesture_motion_ = DIR_UP; + } else { + gesture_motion_ = DIR_RIGHT; + } + } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == -1) ) { + if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) { + gesture_motion_ = DIR_DOWN; + } else { + gesture_motion_ = DIR_LEFT; + } + } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == -1) ) { + if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) { + gesture_motion_ = DIR_UP; + } else { + gesture_motion_ = DIR_LEFT; + } + } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 1) ) { + if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) { + gesture_motion_ = DIR_DOWN; + } else { + gesture_motion_ = DIR_RIGHT; + } + } else { + return false; + } + + return true; +} + +void handleGesture() { + if (isGestureAvailable() ) { + char log[LOGSZ]; + switch (readGesture()) { + case DIR_UP: + snprintf_P(log, sizeof(log), PSTR("UP")); + snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Up")); + break; + case DIR_DOWN: + snprintf_P(log, sizeof(log), PSTR("DOWN")); + snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Down")); + break; + case DIR_LEFT: + snprintf_P(log, sizeof(log), PSTR("LEFT")); + snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Left")); + break; + case DIR_RIGHT: + snprintf_P(log, sizeof(log), PSTR("RIGHT")); + snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Right")); + break; + default: + if(APDS9960_overload) + { + snprintf_P(log, sizeof(log), PSTR("LONG")); + snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Long")); + } + else{ + snprintf_P(log, sizeof(log), PSTR("NONE")); + snprintf_P(currentGesture, sizeof(currentGesture), PSTR("None")); + } + } + AddLog_P(LOG_LEVEL_DEBUG, log); + + mqtt_data[0] = '\0'; + if (MqttShowSensor()) { + MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); +#ifdef USE_RULES + RulesTeleperiod(); // Allow rule based HA messages +#endif // USE_RULES + } + } +} + + +void APDS9960_loop() +{ + if (recovery_loop_counter > 0){ + recovery_loop_counter -= 1; + } + if (recovery_loop_counter == 1 && APDS9960_overload){ //restart sensor just before the end of recovery from long press + enableGestureSensor(false); + APDS9960_overload = false; + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"On\"}")); + MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data); // only after the long break we report, that we are online again + gesture_mode = 1; + } + + if (gesture_mode) { + if (recovery_loop_counter == 0){ + handleGesture(); + + if (APDS9960_overload) + { + disableGestureSensor(); + recovery_loop_counter = APDS9960_LONG_RECOVERY; // long pause after overload/long press - number of stateloops + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"Off\"}")); + MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data); + gesture_mode = 0; + } + } + } +} + +bool APDS9960_detect(void) +{ + if (APDS9960type) { + return true; + } + + boolean success = false; + APDS9960type = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ID); + + if (APDS9960type == APDS9960_CHIPID_1 || APDS9960type == APDS9960_CHIPID_2) { + strcpy_P(APDS9960stype, PSTR("APDS9960")); + snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, APDS9960stype, APDS9960_I2C_ADDR); + AddLog(LOG_LEVEL_DEBUG); + if (APDS9960_init()) { + success = true; + AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "APDS9960 initialized")); + enableGestureSensor(false); + } + } else { + snprintf_P(log_data, sizeof(log_data), PSTR("APDS9960 not found at address 0x%x"), APDS9960_I2C_ADDR); + AddLog(LOG_LEVEL_DEBUG); + } + currentGesture[0] = '\0'; + return success; +} + +/*********************************************************************************************\ + * Presentation +\*********************************************************************************************/ + +void APDS9960_show(boolean json) +{ + if (!APDS9960type) { + return; + } + if (!gesture_mode) { + char red_chr[10]; + char green_chr[10]; + char blue_chr[10]; + char ambient_chr[10]; + char prox_chr[10]; + uint16_t val; + uint8_t val_prox; + + readRedLight(val); + sprintf (red_chr, "%u", val); + readGreenLight(val); + sprintf (green_chr, "%u", val); + readBlueLight(val); + sprintf (blue_chr, "%u", val ); + readAmbientLight(val); + sprintf (ambient_chr, "%u", val); + + readProximity(val_prox); + sprintf (prox_chr, "%u", val_prox ); + + if (json) { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"Red\":%s,\"Green\":%s,\"Blue\":%s,\"Ambient\":%s,\"Proximity\":%s}"), + mqtt_data, APDS9960stype, red_chr, green_chr, blue_chr, ambient_chr, prox_chr); +#ifdef USE_WEBSERVER + } else { + snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_APDS_9960_SNS, mqtt_data, red_chr, green_chr, blue_chr, ambient_chr, prox_chr ); +#endif // USE_WEBSERVER + } + } + else { + if (json && (currentGesture[0] != '\0' )) { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"%s\":1}"), mqtt_data, APDS9960stype, currentGesture); + currentGesture[0] = '\0'; + } + } +} + +/*********************************************************************************************\ + * Command Sensor27 + * + * Command | Payload | Description + * ---------|---------|-------------------------- + * Sensor27 | | Show current gesture mode + * Sensor27 | 0 / Off | Disable gesture mode + * Sensor27 | 1 / On | Enable gesture mode +\*********************************************************************************************/ + +bool APDS9960CommandSensor() +{ + boolean serviced = true; + + switch (XdrvMailbox.payload) { + case 0: // Off + disableGestureSensor(); + gesture_mode = 0; + enableLightSensor(false); + enableProximitySensor(false); + break; + case 1: // On + if (APDS9960type) { + disableLightSensor(); + disableProximitySensor(); + enableGestureSensor(false); + gesture_mode = 1; + } + } + snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_27, GetStateText(gesture_mode)); + + return serviced; +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +boolean Xsns27(byte function) +{ + boolean result = false; + + if (i2c_flg) { + if (FUNC_INIT == function) { + APDS9960_detect(); + } else if (APDS9960type) { + switch (function) { + case FUNC_EVERY_50_MSECOND: + APDS9960_loop(); + break; + case FUNC_COMMAND: + if (XSNS_27 == XdrvMailbox.index) { + result = APDS9960CommandSensor(); + } + break; + case FUNC_JSON_APPEND: + APDS9960_show(1); + break; +#ifdef USE_WEBSERVER + case FUNC_WEB_APPEND: + APDS9960_show(0); + break; +#endif // USE_WEBSERVER + } + } + } + return result; +} +#endif // USE_APDS9960 +#endif // USE_I2C diff --git a/sonoff/xsns_28_tm1638.ino b/sonoff/xsns_28_tm1638.ino new file mode 100644 index 000000000..a50389c23 --- /dev/null +++ b/sonoff/xsns_28_tm1638.ino @@ -0,0 +1,219 @@ +/* + xsns_28_tm1638.ino - TM1638 8 switch, led and 7 segment unit support for Sonoff-Tasmota + + Copyright (C) 2018 Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifdef USE_TM1638 +/*********************************************************************************************\ + * TM1638 8 switch, led and 7 segment + * + * Uses GPIO TM16 DIO, TM16 CLK and TM16 STB +\*********************************************************************************************/ + +#define TM1638_COLOR_NONE 0 +#define TM1638_COLOR_RED 1 +#define TM1638_COLOR_GREEN 2 + +uint8_t tm1638_type = 1; +uint8_t tm1638_clock_pin = 0; +uint8_t tm1638_data_pin = 0; +uint8_t tm1638_strobe_pin = 0; +uint8_t tm1638_displays = 8; +uint8_t tm1638_active_display = 0; +uint8_t tm1638_intensity = 0; + +/*********************************************************************************************\ + * Pieces from library https://github.com/rjbatista/tm1638-library +\*********************************************************************************************/ + +void Tm16XXSend(byte data) +{ + for (int i = 0; i < 8; i++) { + digitalWrite(tm1638_clock_pin, LOW); + digitalWrite(tm1638_data_pin, data & 1 ? HIGH : LOW); + data >>= 1; + digitalWrite(tm1638_clock_pin, HIGH); + } +} + +void Tm16XXSendCommand(byte cmd) +{ + digitalWrite(tm1638_strobe_pin, LOW); + Tm16XXSend(cmd); + digitalWrite(tm1638_strobe_pin, HIGH); +} + +void TM16XXSendData(byte address, byte data) +{ + Tm16XXSendCommand(0x44); + digitalWrite(tm1638_strobe_pin, LOW); + Tm16XXSend(0xC0 | address); + Tm16XXSend(data); + digitalWrite(tm1638_strobe_pin, HIGH); +} + +byte Tm16XXReceive() +{ + byte temp = 0; + + // Pull-up on + pinMode(tm1638_data_pin, INPUT); + digitalWrite(tm1638_data_pin, HIGH); + + for (int i = 0; i < 8; i++) { + temp >>= 1; + digitalWrite(tm1638_clock_pin, LOW); + if (digitalRead(tm1638_data_pin)) { + temp |= 0x80; + } + digitalWrite(tm1638_clock_pin, HIGH); + } + + // Pull-up off + pinMode(tm1638_data_pin, OUTPUT); + digitalWrite(tm1638_data_pin, LOW); + + return temp; +} + +/*********************************************************************************************/ + +void Tm16XXClearDisplay() +{ + for (int i = 0; i < tm1638_displays; i++) { + TM16XXSendData(i << 1, 0); + } +} + +void Tm1638SetLED(byte color, byte pos) +{ + TM16XXSendData((pos << 1) + 1, color); +} + +void Tm1638SetLEDs(word leds) +{ + for (int i = 0; i < tm1638_displays; i++) { + byte color = 0; + + if ((leds & (1 << i)) != 0) { + color |= TM1638_COLOR_RED; + } + + if ((leds & (1 << (i + 8))) != 0) { + color |= TM1638_COLOR_GREEN; + } + + Tm1638SetLED(color, i); + } +} + +byte Tm1638GetButtons() +{ + byte keys = 0; + + digitalWrite(tm1638_strobe_pin, LOW); + Tm16XXSend(0x42); + for (int i = 0; i < 4; i++) { + keys |= Tm16XXReceive() << i; + } + digitalWrite(tm1638_strobe_pin, HIGH); + + return keys; +} + +/*********************************************************************************************/ + +void TmInit() +{ + tm1638_type = 0; + if ((pin[GPIO_TM16CLK] < 99) && (pin[GPIO_TM16DIO] < 99) && (pin[GPIO_TM16STB] < 99)) { + tm1638_clock_pin = pin[GPIO_TM16CLK]; + tm1638_data_pin = pin[GPIO_TM16DIO]; + tm1638_strobe_pin = pin[GPIO_TM16STB]; + + pinMode(tm1638_data_pin, OUTPUT); + pinMode(tm1638_clock_pin, OUTPUT); + pinMode(tm1638_strobe_pin, OUTPUT); + + digitalWrite(tm1638_strobe_pin, HIGH); + digitalWrite(tm1638_clock_pin, HIGH); + + Tm16XXSendCommand(0x40); + Tm16XXSendCommand(0x80 | (tm1638_active_display ? 8 : 0) | min(7, tm1638_intensity)); + + digitalWrite(tm1638_strobe_pin, LOW); + Tm16XXSend(0xC0); + for (int i = 0; i < 16; i++) { + Tm16XXSend(0x00); + } + digitalWrite(tm1638_strobe_pin, HIGH); + + tm1638_type = 1; + } +} + +void TmLoop() +{ + byte buttons = Tm1638GetButtons(); + for (byte i = 0; i < 8; i++) { + virtualswitch[i] = buttons &1; + byte color = virtualswitch[i] ? TM1638_COLOR_RED : TM1638_COLOR_NONE; + Tm1638SetLED(color, i); + buttons >>= 1; + } + SwitchHandler(1); +} + +void TmShow(boolean json) +{ + +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +#define XSNS_28 + +boolean Xsns28(byte function) +{ + boolean result = false; + + if (tm1638_type) { + switch (function) { + case FUNC_INIT: + TmInit(); + break; + case FUNC_EVERY_50_MSECOND: + TmLoop(); + break; +/* + case FUNC_JSON_APPEND: + TmShow(1); + break; +#ifdef USE_WEBSERVER + case FUNC_WEB_APPEND: + TmShow(0); + break; +#endif // USE_WEBSERVER +*/ + } + } + return result; +} + +#endif // USE_TM1638 \ No newline at end of file