user_plug.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_plug.c
  5. *
  6. * Description: plug demo's function realization
  7. *
  8. * Modification history:
  9. * 2014/5/1, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "ets_sys.h"
  12. #include "osapi.h"
  13. #include "os_type.h"
  14. #include "mem.h"
  15. #include "user_interface.h"
  16. #include "user_plug.h"
  17. #if PLUG_DEVICE
  18. LOCAL struct plug_saved_param plug_param;
  19. LOCAL struct keys_param keys;
  20. LOCAL struct single_key_param *single_key[PLUG_KEY_NUM];
  21. LOCAL os_timer_t link_led_timer;
  22. LOCAL uint8 link_led_level = 0;
  23. /******************************************************************************
  24. * FunctionName : user_plug_get_status
  25. * Description : get plug's status, 0x00 or 0x01
  26. * Parameters : none
  27. * Returns : uint8 - plug's status
  28. *******************************************************************************/
  29. uint8 ICACHE_FLASH_ATTR
  30. user_plug_get_status(void)
  31. {
  32. return plug_param.status;
  33. }
  34. /******************************************************************************
  35. * FunctionName : user_plug_set_status
  36. * Description : set plug's status, 0x00 or 0x01
  37. * Parameters : uint8 - status
  38. * Returns : none
  39. *******************************************************************************/
  40. void ICACHE_FLASH_ATTR
  41. user_plug_set_status(bool status)
  42. {
  43. if (status != plug_param.status) {
  44. if (status > 1) {
  45. os_printf("error status input!\n");
  46. return;
  47. }
  48. plug_param.status = status;
  49. PLUG_STATUS_OUTPUT(PLUG_RELAY_LED_IO_NUM, status);
  50. }
  51. }
  52. /******************************************************************************
  53. * FunctionName : user_plug_short_press
  54. * Description : key's short press function, needed to be installed
  55. * Parameters : none
  56. * Returns : none
  57. *******************************************************************************/
  58. LOCAL void ICACHE_FLASH_ATTR
  59. user_plug_short_press(void)
  60. {
  61. user_plug_set_status((~plug_param.status) & 0x01);
  62. spi_flash_erase_sector(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE);
  63. spi_flash_write((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
  64. (uint32 *)&plug_param, sizeof(struct plug_saved_param));
  65. }
  66. /******************************************************************************
  67. * FunctionName : user_plug_long_press
  68. * Description : key's long press function, needed to be installed
  69. * Parameters : none
  70. * Returns : none
  71. *******************************************************************************/
  72. LOCAL void ICACHE_FLASH_ATTR
  73. user_plug_long_press(void)
  74. {
  75. user_esp_platform_set_active(0);
  76. system_restore();
  77. system_restart();
  78. }
  79. LOCAL void ICACHE_FLASH_ATTR
  80. user_link_led_init(void)
  81. {
  82. PIN_FUNC_SELECT(PLUG_LINK_LED_IO_MUX, PLUG_LINK_LED_IO_FUNC);
  83. }
  84. void ICACHE_FLASH_ATTR
  85. user_link_led_output(uint8 level)
  86. {
  87. GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), level);
  88. }
  89. LOCAL void ICACHE_FLASH_ATTR
  90. user_link_led_timer_cb(void)
  91. {
  92. link_led_level = (~link_led_level) & 0x01;
  93. GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), link_led_level);
  94. }
  95. void ICACHE_FLASH_ATTR
  96. user_link_led_timer_init(void)
  97. {
  98. os_timer_disarm(&link_led_timer);
  99. os_timer_setfn(&link_led_timer, (os_timer_func_t *)user_link_led_timer_cb, NULL);
  100. os_timer_arm(&link_led_timer, 50, 1);
  101. link_led_level = 0;
  102. GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), link_led_level);
  103. }
  104. void ICACHE_FLASH_ATTR
  105. user_link_led_timer_done(void)
  106. {
  107. os_timer_disarm(&link_led_timer);
  108. GPIO_OUTPUT_SET(GPIO_ID_PIN(PLUG_LINK_LED_IO_NUM), 0);
  109. }
  110. /******************************************************************************
  111. * FunctionName : user_plug_init
  112. * Description : init plug's key function and relay output
  113. * Parameters : none
  114. * Returns : none
  115. *******************************************************************************/
  116. void ICACHE_FLASH_ATTR
  117. user_plug_init(void)
  118. {
  119. user_link_led_init();
  120. wifi_status_led_install(PLUG_WIFI_LED_IO_NUM, PLUG_WIFI_LED_IO_MUX, PLUG_WIFI_LED_IO_FUNC);
  121. single_key[0] = key_init_single(PLUG_KEY_0_IO_NUM, PLUG_KEY_0_IO_MUX, PLUG_KEY_0_IO_FUNC,
  122. user_plug_long_press, user_plug_short_press);
  123. keys.key_num = PLUG_KEY_NUM;
  124. keys.single_key = single_key;
  125. key_init(&keys);
  126. spi_flash_read((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
  127. (uint32 *)&plug_param, sizeof(struct plug_saved_param));
  128. PIN_FUNC_SELECT(PLUG_RELAY_LED_IO_MUX, PLUG_RELAY_LED_IO_FUNC);
  129. // no used SPI Flash
  130. if (plug_param.status == 0xff) {
  131. plug_param.status = 1;
  132. }
  133. PLUG_STATUS_OUTPUT(PLUG_RELAY_LED_IO_NUM, plug_param.status);
  134. }
  135. #endif