Skip to content

Commit

Permalink
Merge pull request #39 from mposzywak/hold-#35
Browse files Browse the repository at this point in the history
Added Switch based light input
  • Loading branch information
mposzywak authored Jul 21, 2021
2 parents 591cf55 + eaaacae commit 96b5b3f
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 71 deletions.
19 changes: 12 additions & 7 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ Where:
- `0` - the regular on/off light device
- `1` - a timer based light device
* **light input type**
Command used to set the type of the light's input type. This setting is used to tell Arduino how the physical light switch will behave. It can either return to its normal position after pressing (push button) or it can stay in the position which he was switched to (toggle switch).
Command used to set the type of the light's input type. This setting is used to tell Arduino how the physical light switch will behave. It can either return to its normal position after pressing (push button) or it can stay in the position which he was switched to (toggle switch)
Direction: in
URL: `/?devID=<devid>&ardID=<ardid>&raspyID=<raspyid>&cmd=<cmd>`
URL: `/?devID=<devid>&ardID=<ardid>&raspyID=<raspyid>&cmd=inputHold`
URL: `/?devID=<devid>&ardID=<ardid>&raspyID=<raspyid>&cmd=inputRelease`
Where:
- `<cmd>` indicates the type of the light's input. The following two commands are used:
- `inputHold` - the toggle switch
- `inputRelease` - the push button
- `inputHold` - the switch input type
- `inputRelease` - the button input type

**Shade Device**
----
Expand Down Expand Up @@ -84,13 +85,13 @@ Where:
- `45` - Tilt is half open
- `90` - tilt is open

* **shade position timer** *(not implemented)*
* **shade position timer**
This command is used to set the timer value of the shade. It specifies how many seconds it takes for the shade to reach from fully open to fully closed positions.
Direction: in
URL: `/?devID=<devid>&ardID=<ardid>&raspyID=<raspyid>&cmd=shadePTimer&value=<value>`
Where:
- `<value>` indicates the position timer of the shade. Timer value can be set in range 10 - 65535 in unit of seconds.
* **shade tilt timer** *(not implemented)*
* **shade tilt timer**
This command is used to set the tilt timer value of the shade. It specifies how many miliseconds it takes for the shade tilt move to reach from fully open to fully closed.
Direction: in
URL: `/?devID=<devid>&ardID=<ardid>&raspyID=<raspyid>&cmd=shadeTTimer&value=<value>`
Expand All @@ -111,7 +112,11 @@ Where:
* **registration**
This command is used to register the VelenHub in a Raspy (VelenGW). It is sent by the VelenGW and the `<devid>` is set always to `0`. VelenHub will read the `<ardid>` value and treat it as its own ArdID. VelenHub will also save the `<raspyid>`.
Direction: in
URL: `/?devID=0&ardID=<ardid>&raspyID=<raspyid>&cmd=register`
URL: `/?devID=0&ardID=<ardid>&raspyID=<raspyid>&cmd=register`
* **deregistration**
This command is used to deregister VelenHub . This command will delete the obtained `<raspyid>`, the VelenHub will start advertising beacon with ardID set to 0.
Direction: in
URL: `/?devID=0&ardID=<ardid>&raspyID=<raspyid>&cmd=deregister`
* **heartbeat**
This command is used to send heartbeats by the VelenHub.
Direction: in
Expand Down
73 changes: 54 additions & 19 deletions ARiF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,34 +265,68 @@ static byte ARiFClass::update() {
return CMD_MODE_SHADES;
break;
case CMD_TIMER_POS:
lastDevID = getValue(buff, DEVID);
lastShadePositionTimer = getValue(buff, VALUE);
client.println(F(HTTP_200_OK));
client.println();
client.stop();
return CMD_TIMER_POS;
if (mode == M_SHADES) {
lastDevID = getValue(buff, DEVID);
lastShadePositionTimer = getValue(buff, VALUE);
client.println(F(HTTP_200_OK));
client.println();
client.stop();
return CMD_TIMER_POS;
} else {
client.println(F(HTTP_403_Error));
client.println();
client.stop();
return U_NOTHING;
}
break;
case CMD_TIMER_TILT:
lastDevID = getValue(buff, DEVID);
lastShadeTiltTimer = getValue(buff, VALUE);
client.println(F(HTTP_200_OK));
client.println();
client.stop();
return CMD_TIMER_TILT;
if (mode == M_SHADES) {
lastDevID = getValue(buff, DEVID);
lastShadeTiltTimer = getValue(buff, VALUE);
client.println(F(HTTP_200_OK));
client.println();
client.stop();
return CMD_TIMER_TILT;
} else {
client.println(F(HTTP_403_Error));
client.println();
client.stop();
return U_NOTHING;
}
break;
case CMD_INPUT_HOLD:
lastDevID = getValue(buff, DEVID);
client.println(F(HTTP_200_OK));
client.println();
client.stop();
return CMD_INPUT_HOLD;
if (mode == M_LIGHTS) {
lastDevID = getValue(buff, DEVID);
client.println(F(HTTP_200_OK));
client.println();
client.stop();
return CMD_INPUT_HOLD;
} else {
client.println(F(HTTP_403_Error));
client.println();
client.stop();
return U_NOTHING;
}
break;
case CMD_INPUT_REL:
lastDevID = getValue(buff, DEVID);
if (mode == M_LIGHTS) {
lastDevID = getValue(buff, DEVID);
client.println(F(HTTP_200_OK));
client.println();
client.stop();
return CMD_INPUT_REL;
} else {
client.println(F(HTTP_403_Error));
client.println();
client.stop();
return U_NOTHING;
}
break;
case CMD_DEREGISTER:
client.println(F(HTTP_200_OK));
client.println();
client.stop();
return CMD_INPUT_REL;
return CMD_DEREGISTER;
break;
case CMD_UNKNOWN:
client.println(F(HTTP_500_Error));
Expand Down Expand Up @@ -367,6 +401,7 @@ static long ARiFClass::getValue(char *buff, int value) {
if (strstr(buff, "cmd=shadeTTimer")) return CMD_TIMER_TILT;
if (strstr(buff, "cmd=inputHold")) return CMD_INPUT_HOLD;
if (strstr(buff, "cmd=inputRelease")) return CMD_INPUT_REL;
if (strstr(buff, "cmd=deregister")) return CMD_DEREGISTER;
return CMD_UNKNOWN;
}
}
Expand Down
1 change: 1 addition & 0 deletions ARiF.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#define CMD_TIMER_TILT 16
#define CMD_INPUT_HOLD 17
#define CMD_INPUT_REL 18
#define CMD_DEREGISTER 19
#define CMD_UNKNOWN 200

/* values returned by update() other than the CMDs above */
Expand Down
12 changes: 6 additions & 6 deletions Light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ byte Light::isPressed() {
inPinPressed = true;
delay(10); // this delay here was placed in order for the press button result to be predictable
return PHY_NO_PRESS;
} else {
} else { /* inPinState == LOW */
if (inPinPressed) { /* Button is released */
/* EXECUTED ON BUTTON RELEASE - START */

/* EXECUTED ON BUTTON RELEASE - END */
if (timeCheck(&buttonHold)) {
if (this->lightID == Platform.getLastLightDevID() && getCentralCtrl() == DIGITOUT_CENTRAL_CTRL_ENABLE) {
Expand All @@ -95,6 +95,7 @@ byte Light::isPressed() {
return PHY_CENTRAL_CTRL_MOMENTARY_PRESS;
}
inPinPressed = false;
delay(10);
return PHY_MOMENTARY_PRESS;
} else {
return PHY_NO_PRESS;
Expand All @@ -105,20 +106,19 @@ byte Light::isPressed() {
if (inPinState != Settings::getInputPinValue(inPin) && inPinPressed == false) { /* indication that state of the input changed */
inPinState = Settings::getInputPinValue(inPin);
inPinPressed = true;

Serial.println("Press button -----");
timeRun(&buttonPressHold);
delay(10); // this delay here was placed in order for the press button result to be predictable
return PHY_NO_PRESS;
}
if (timeCheck(&buttonPressHold)) {
Serial.println("Toggle output");
inPinPressed = false;
delay(10); // this delay here was placed in order for the press button result to be predictable
Serial.println(lightID);
return PHY_MOMENTARY_PRESS;
}

return PHY_NO_PRESS; /* function must always return a value even if situation "shouldn't" happen. Otherwise the return value is unpredictable */
}
return PHY_NO_PRESS;
}

void Light::setON() {
Expand Down
36 changes: 25 additions & 11 deletions Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,43 +359,58 @@ static void Settings::EEPROMSetMode(byte mode) {

static void Settings::EEPROMSetLightConfig(byte devID, byte type, unsigned long timer) {
if (devID > LIGHTS) return;
byte index = EEPROM_IDX_LIGHTS + (6 * (devID - 1)); /* start address for Light data structure */
byte index = EEPROM_IDX_LIGHTS + (EEPROM_IDX_LIGHTS_LENGTH * (devID - 1)); /* start address for Light data structure */
EEPROM.write(index + 1, type);
EEPROMWritelong(index + 2, timer);
}

static byte Settings::EEPROMGetLightType(byte devID) {
byte index = EEPROM_IDX_LIGHTS + (6 * (devID - 1)); /* start address for Light data structure */
byte index = EEPROM_IDX_LIGHTS + (EEPROM_IDX_LIGHTS_LENGTH * (devID - 1)); /* start address for Light data structure */
byte type;
type = EEPROM.read(index + 1);
return type;
}

static void Settings::EEPROMSetLightType(byte devID, byte type) {
if (devID > LIGHTS) return;
byte index = EEPROM_IDX_LIGHTS + (6 * (devID - 1)); /* start address for Light data structure */
byte index = EEPROM_IDX_LIGHTS + (EEPROM_IDX_LIGHTS_LENGTH * (devID - 1)); /* start address for Light data structure */
EEPROM.write(index + 1, type);
}

static unsigned long Settings::EEPROMGetLightTimer(byte devID) {
byte index = EEPROM_IDX_LIGHTS + (6 * (devID - 1)); /* start address for Light data structure */
byte index = EEPROM_IDX_LIGHTS + (EEPROM_IDX_LIGHTS_LENGTH * (devID - 1)); /* start address for Light data structure */
unsigned long timer;
timer = EEPROMReadlong(index + 2);
return timer;
}

static void Settings::EEPROMSetLightTimer(byte devID, unsigned long timer) {
if (devID > LIGHTS) return;
byte index = EEPROM_IDX_LIGHTS + (6 * (devID - 1)); /* start address for Light data structure */
byte index = EEPROM_IDX_LIGHTS + (EEPROM_IDX_LIGHTS_LENGTH * (devID - 1)); /* start address for Light data structure */
EEPROMWritelong(index + 2, timer);
}

static void Settings::EEPROMSetLightStatus(byte devID, byte status) {
if (devID > LIGHTS) return;
byte index = EEPROM_IDX_LIGHTS + (6 * (devID - 1)); /* start address for Light data structure */
byte index = EEPROM_IDX_LIGHTS + (EEPROM_IDX_LIGHTS_LENGTH * (devID - 1)); /* start address for Light data structure */
EEPROM.write(index, status);
}

static byte Settings::EEPROMGetLightInputType(byte devID) {
if (devID > LIGHTS) return;
byte index = EEPROM_IDX_LIGHTS + (EEPROM_IDX_LIGHTS_LENGTH * (devID - 1));
byte inputType;
inputType = EEPROM.read(index + 6);
return inputType;
}

static void Settings::EEPROMSetLightInputType(byte devID, byte inputType) {
if (devID > LIGHTS) return;
byte index = EEPROM_IDX_LIGHTS + (EEPROM_IDX_LIGHTS_LENGTH * (devID - 1));
EEPROM.write(index + 6, inputType);
}


static unsigned long Settings::EEPROMReadlong(unsigned long address) {
unsigned long value;
long four = EEPROM.read(address);
Expand Down Expand Up @@ -508,22 +523,21 @@ static byte Settings::SDCardInit() {

if (!SD.begin(SD_ARD_MEGA_CS))
{
Serial.println("Comm issue.");
Serial.println("FAIL");
Serial.println(F("Comm issue with SD Card ctrl. No SD Card available."));
digitalWrite(SD_ARD_MEGA_CS, HIGH);
return SD_INIT_NOHW;
}
if (!SD.exists("MAIN.CSS")) {
Serial.println("ERROR - Can't find MAIN.CSS file!");
Serial.println(F("ERROR - Can't find MAIN.CSS file!"));
digitalWrite(SD_ARD_MEGA_CS, HIGH);
return SD_INIT_NOFILE;
} else {
if (!SD.exists("MAIN.JS")) {
Serial.println("ERROR - Can't find MAIN.JS file!");
Serial.println(F("ERROR - Can't find MAIN.JS file!"));
digitalWrite(SD_ARD_MEGA_CS, HIGH);
return SD_INIT_NOFILE;
} else {
Serial.println("SUCCESS - All files present!");
Serial.println(F("SUCCESS - All files present!"));
digitalWrite(SD_ARD_MEGA_CS, HIGH);
return SD_INIT_SUCCESS;
}
Expand Down
29 changes: 21 additions & 8 deletions Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@
#endif

/* types of physical button press */
#define PHY_NO_PRESS 0
#define PHY_MOMENTARY_PRESS 1
#define PHY_PRESS_MORE_THAN_2SEC 2
#define PHY_CENTRAL_CTRL_MOMENTARY_PRESS 3
#define PHY_CENTRAL_CTRL_PRESS_MORE_THAN_2SEC 4
#define PHY_NO_PRESS 10
#define PHY_MOMENTARY_PRESS 11
#define PHY_PRESS_MORE_THAN_2SEC 12
#define PHY_CENTRAL_CTRL_MOMENTARY_PRESS 13
#define PHY_CENTRAL_CTRL_PRESS_MORE_THAN_2SEC 14

/* indexes for EEPROM information holding */
#define EEPROM_IDX_ARDID 0 // length 1
#define EEPROM_IDX_RASPYID 1 // length 1
#define EEPROM_IDX_REG 2 // length 1
#define EEPROM_IDX_RASPYIP 3 // length 6
#define EEPROM_IDX_MODE 9 // length 1
#define EEPROM_IDX_LIGHTS 10 // length 210 -> 30 (lights) x 6 (1 status + 1 type + 4 timer) + 30 (buffer for future use)
#define EEPROM_IDX_CENT_CTRL 220 // length
#define EEPROM_IDX_SHADES 221 // length 150 -> 15 (shades) x 8 (4 status + 1 type + 1 pos timer + 2 tilt timer) + 30 (buffer for future use)
#define EEPROM_IDX_LIGHTS 10 // length 240 -> 30 (lights) x 7 (1 status + 1 type + 4 timer + 1 input) + 30 (buffer for future use)
#define EEPROM_IDX_CENT_CTRL 250 // length
#define EEPROM_IDX_SHADES 251 // length 150 -> 15 (shades) x 8 (4 status + 1 type + 1 pos timer + 2 tilt timer) + 30 (buffer for future use)

/* length (in bytes) of the lights and shades fields */
#define EEPROM_IDX_LIGHTS_LENGTH 7
#define EEPROM_IDX_SHADES_LENGTH 8

/* flags */
#define EEPROM_FLG_SHADE_SYNC 1
Expand All @@ -53,6 +57,9 @@
#define MODE_SHADES 1
#define MODE_FAIL 100 /* returned by EEPROMGetMode() if couldn't load mode from EEPROM */

/* input modes */
#define EEPROM_INPUT_HOLD 0
#define EEPROM_INPUT_RELEASE 1

/* SD Card variables */
#define SD_ARD_MEGA_CS 4
Expand Down Expand Up @@ -180,6 +187,12 @@ class Settings {
/* Write light status into the EEPROM */
static void EEPROMSetLightStatus(byte devID, byte status);

/* Write light input type into the EEPROM */
static byte EEPROMGetLightInputType(byte devID);

/* Read light input type from the EEPROM */
static void EEPROMSetLightInputType(byte devID, byte inputType);

/* get last devID from the array of lightIDs */
static byte getLastLightDevID();

Expand Down
Loading

0 comments on commit 96b5b3f

Please sign in to comment.