Skip to content

Commit

Permalink
moved to use of shared_functions
Browse files Browse the repository at this point in the history
  • Loading branch information
maccoylton committed Nov 12, 2019
1 parent 693a898 commit 7ecd34b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 107 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
[submodule "components/UDPlogger"]
path = components/UDPlogger
url = https://github.com/HomeACcessoryKid/UDPlogger.git
[submodule "components/esp-homekit-common-functions"]
path = components/esp-homekit-common-functions
url = https://github.com/maccoylton/esp-homekit-common-functions
12 changes: 9 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ EXTRA_COMPONENTS = \
extras/dht \
extras/http-parser \
extras/rboot-ota \
extras/dhcpserver \
$(abspath ../components/esp-wolfssl) \
$(abspath ../components/esp-cjson) \
$(abspath ../components/esp-homekit) \
$(abspath ../components/esp-adv-button)\
$(abspath ../components/esp-wifi-config)\
$(abspath ../components/UDPlogger)\
$(abspath ../components/esp-adv-button)\
$(abspath ../components/esp-homekit-common-functions/led_codes)\
$(abspath ../components/esp-homekit-common-functions/ota)\
$(abspath ../components/esp-homekit-common-functions/custom_characteristics)
$(abspath ../components/esp-homekit-common-functions/custom_characteristics)\
$(abspath ../components/esp-homekit-common-functions/shared_functions)



FLASH_SIZE ?= 8
Expand All @@ -24,10 +28,12 @@ HOMEKIT_SMALL = 0
HOMEKIT_SPI_FLASH_BASE_ADDR ?= 0x8c000

EXTRA_CFLAGS += -I../.. -DHOMEKIT_SHORT_APPLE_UUIDS
#EXTRA_CFLAGS += -DHOMEKIT_DEBUG
EXTRA_CFLAGS += -DHOMEKIT_DEBUG
EXTRA_CFLAGS += -DHOMEKIT_OVERCLOCK_PAIR_VERIFY
EXTRA_CFLAGS += -DHOMEKIT_OVERCLOCK_PAIR_SETUP
EXTRA_CFLAGS += -DUDPLOG_PRINTF_TO_UDP
EXTRA_CFLAGS += -DUDPLOG_PRINTF_ALSO_SERIAL



include $(SDK_PATH)/common.mk
Expand Down
137 changes: 33 additions & 104 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@

#include <homekit/homekit.h>
#include <homekit/characteristics.h>
//#include <wifi_config.h>
#include <wifi_config.h>


#include <adv_button.h>
#include <led_codes.h>
#include <udplogger.h>
#include <custom_characteristics.h>
#include <shared_functions.h>



// add this section to make your device OTA capable
Expand All @@ -48,6 +52,8 @@

void switch_on_callback(homekit_characteristic_t *_ch, homekit_value_t on, void *context);


homekit_characteristic_t wifi_reset = HOMEKIT_CHARACTERISTIC_(CUSTOM_WIFI_RESET, false, .setter=wifi_reset_set);
homekit_characteristic_t ota_trigger = API_OTA_TRIGGER;
homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, DEVICE_NAME);
homekit_characteristic_t manufacturer = HOMEKIT_CHARACTERISTIC_(MANUFACTURER, DEVICE_MANUFACTURER);
Expand All @@ -68,98 +74,45 @@ const int reset_button_gpio = 0;
const int s2_gpio = 4;
int led_off_value=1; /* global varibale to support LEDs set to 0 where the LED is connected to GND, 1 where +3.3v */

const int status_led_gpio = 13; /*set the gloabl variable for the led to be sued for showing status */

void relay_write(bool on) {
gpio_write(relay_gpio, on ? 1 : 0);
}

void led_write(bool on) {
gpio_write(LED_GPIO, on ? 0 : 1);
}

void reset_configuration_task() {
//Flash the LED first before we start the reset
led_code (LED_GPIO, WIFI_CONFIG_RESET);
printf("Resetting Wifi Config\n");

// wifi_config_reset();

vTaskDelay(1000 / portTICK_PERIOD_MS);

printf("Resetting HomeKit Config\n");

homekit_server_reset();

vTaskDelay(1000 / portTICK_PERIOD_MS);

printf("Restarting\n");

sdk_system_restart();

vTaskDelete(NULL);
}

void reset_configuration() {
printf("Resetting Sonoff configuration\n");
xTaskCreate(reset_configuration_task, "Reset configuration", 256, NULL, 2, NULL);
}

void reset_button_callback(uint8_t gpio, void* args) {

printf("Reset Button event long press on GPIO : %d\n", gpio);
reset_configuration();

}

void s2_button_callback(uint8_t gpio, void* args) {
void s2_button_callback(uint8_t gpio, void* args, const uint8_t param) {

printf("S2 Button event single press on GPIO : %d\n", gpio);
printf("Toggling relay\n");
switch_on.value.bool_value = !switch_on.value.bool_value;
relay_write(switch_on.value.bool_value);
led_write(switch_on.value.bool_value);
relay_write(switch_on.value.bool_value, relay_gpio);
led_write(switch_on.value.bool_value, LED_GPIO);
homekit_characteristic_notify(&switch_on, switch_on.value);
}

void gpio_init() {

const uint8_t toggle_press = 0, single_press = 1, double_press = 2, long_press = 3, very_long_press = 4, hold_press = 5;

adv_button_set_evaluate_delay(10);

/* GPIO for button, pull-up resistor, inverted */
printf("Initialising buttons\n");
adv_button_create(s2_gpio, true, false);
adv_button_register_callback_fn(s2_gpio, s2_button_callback, toggle_press, NULL);
adv_button_register_callback_fn(s2_gpio, s2_button_callback, single_press, NULL);
adv_button_register_callback_fn(s2_gpio, s2_button_callback, INVSINGLEPRESS_TYPE, NULL, 0);
adv_button_register_callback_fn(s2_gpio, s2_button_callback, SINGLEPRESS_TYPE, NULL, 0);

adv_button_create(reset_button_gpio, true, false);
adv_button_register_callback_fn(reset_button_gpio, reset_button_callback, very_long_press, NULL);
adv_button_register_callback_fn(reset_button_gpio, reset_button_callback, VERYLONGPRESS_TYPE, NULL, 0);


gpio_enable(LED_GPIO, GPIO_OUTPUT);
led_write(false);
led_write(false, LED_GPIO);
gpio_enable(relay_gpio, GPIO_OUTPUT);
relay_write(switch_on.value.bool_value);
relay_write(switch_on.value.bool_value, relay_gpio);

}

void switch_on_callback(homekit_characteristic_t *_ch, homekit_value_t on, void *context) {
printf("Switch on callback\n");
relay_write(switch_on.value.bool_value);
led_write(switch_on.value.bool_value);
}


void switch_identify_task(void *_args) {
// We identify the Sonoff by Flashing it's LED.
led_code( LED_GPIO, IDENTIFY_ACCESSORY);
vTaskDelete(NULL);
}

void switch_identify(homekit_value_t _value) {
printf("Switch identify\n");
xTaskCreate(switch_identify_task, "Switch identify", 128, NULL, 2, NULL);
relay_write(switch_on.value.bool_value, relay_gpio);
led_write(switch_on.value.bool_value, LED_GPIO);
}


Expand All @@ -171,65 +124,41 @@ homekit_accessory_t *accessories[] = {
&serial,
&model,
&revision,
HOMEKIT_CHARACTERISTIC(IDENTIFY, switch_identify),
HOMEKIT_CHARACTERISTIC(IDENTIFY, identify),
NULL
}),
HOMEKIT_SERVICE(SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]){
HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
&switch_on,
&ota_trigger,
&wifi_reset,
NULL
}),
NULL
}),
NULL
};

void accessory_init (void ){
/* initalise anything you don't want started until wifi and pairing is confirmed */

}

homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111"
.password = "111-11-111",
.setupId = "1234",
.on_event = on_homekit_event
};

void create_accessory_name() {

int serialLength = snprintf(NULL, 0, "%d", sdk_system_get_chip_id());

char *serialNumberValue = malloc(serialLength + 1);

snprintf(serialNumberValue, serialLength + 1, "%d", sdk_system_get_chip_id());

int name_len = snprintf(NULL, 0, "%s-%s-%s",
DEVICE_NAME,
DEVICE_MODEL,
serialNumberValue);

if (name_len > 63) {
name_len = 63;
}

char *name_value = malloc(name_len + 1);

snprintf(name_value, name_len + 1, "%s-%s-%s",
DEVICE_NAME, DEVICE_MODEL, serialNumberValue);
void user_init(void) {


name.value = HOMEKIT_STRING(name_value);
serial.value = name.value;
}
standard_init (&name, &manufacturer, &model, &serial, &revision);

void user_init(void) {
uart_set_baud(0, 115200);

udplog_init(3);
gpio_init();


create_accessory_name();


int c_hash=ota_read_sysparam(&manufacturer.value.string_value,&serial.value.string_value,
&model.value.string_value,&revision.value.string_value);
if (c_hash==0) c_hash=1;
config.accessories[0]->config_number=c_hash;

homekit_server_init(&config);
wifi_config_init("SonoffMini", NULL, on_wifi_ready);

}

0 comments on commit 7ecd34b

Please sign in to comment.