MeterLogger
ac_out.c
Go to the documentation of this file.
1 #include <esp8266.h>
2 
3 #include "led.h"
4 #include "ac_out.h"
5 #include "config.h"
6 
10 
11 unsigned int ac_pwm_duty_cycle;
12 typedef enum {ON, OFF} ac_pwm_state_t;
14 
15 
17 void ac_out_init() {
18  //Set GPIO14 and GPIO15 to output mode
20 #ifdef THERMO_ON_AC_2
22 #endif
23 }
24 
26 void static ac_test_timer_func(void *arg) {
27  // do blinky stuff
29  //Set GPI14 to LOW
30  gpio_output_set(0, BIT14, BIT14, 0);
31 #ifdef LED_ON_AC
32  led_pattern_b();
33 #endif
34  }
35  else {
36  //Set GPI14 to HIGH
37  gpio_output_set(BIT14, 0, BIT14, 0);
38 #ifdef LED_ON_AC
39  led_pattern_a();
40 #endif
41  }
42 
43 // if (GPIO_REG_READ(GPIO_OUT_ADDRESS) & BIT15) {
44 // //Set GPI15 to LOW
45 // gpio_output_set(0, BIT15, BIT15, 0);
46 // }
47 // else {
48 // //Set GPI15 to HIGH
49 // gpio_output_set(BIT15, 0, BIT15, 0);
50 // }
51 }
52 
53 ICACHE_FLASH_ATTR void static ac_out_off_timer_func(void *arg) {
54 #ifdef DEBUG
55  os_printf("\n\rac 1 and 2 off\n\r");
56 #endif
57 
58  //Set GPI14 to LOW
59  gpio_output_set(0, BIT14, BIT14, 0);
60 
61 #ifdef THERMO_ON_AC_2
62  //Set GPI15 to LOW
63  gpio_output_set(0, BIT15, BIT15, 0);
64 #endif
65 
67 #ifdef LED_ON_AC
68  led_off();
69 #endif
70 }
71 
72 ICACHE_FLASH_ATTR void static ac_pwm_timer_func(void *arg) {
73  // do ac 1 pwm
74  if (ac_pwm_state == OFF) {
75  ac_pwm_state = ON;
76  if (ac_pwm_duty_cycle > 0) {
77 #ifdef LED_ON_AC
78  led_on();
79 #endif
80 #ifdef THERMO_NO
81  //Set GPI14 to LOW
82  gpio_output_set(0, BIT14, BIT14, 0);
83 #else // THERMO_NC
84  //Set GPI14 to HIGH
85  gpio_output_set(BIT14, 0, BIT14, 0);
86 #endif
87  }
88  // reload timer
91  os_timer_arm(&ac_pwm_timer, ac_pwm_duty_cycle * 10, 1); // pwm frequency 10 second
92  }
93  else if (ac_pwm_state == ON) {
94  ac_pwm_state = OFF;
95  if (ac_pwm_duty_cycle < 1000) {
96 #ifdef LED_ON_AC
97  led_off();
98 #endif
99 #ifdef THERMO_NO
100  //Set GPI14 to HIGHT
101  gpio_output_set(BIT14, 0, BIT14, 0);
102 #else // THERMO_NC
103  //Set GPI14 to LOW
104  gpio_output_set(0, BIT14, BIT14, 0);
105 #endif
106  }
107  // reload timer
110  os_timer_arm(&ac_pwm_timer, (1000 - ac_pwm_duty_cycle) * 10, 1); // pwm frequency 10 second
111  }
112 }
113 
115 void ac_test() {
116 #ifdef DEBUG
117  os_printf("\n\rac test on\n\r");
118 #endif
119 #ifdef LED_ON_AC
120  led_pattern_a();
121 #endif
122 
123  // set GPIO14 high and GPIO15 low
124  gpio_output_set(BIT14, 0, BIT14, 0);
125 #ifdef THERMO_ON_AC_2
126  gpio_output_set(0, BIT15, BIT15, 0);
127 #endif
128 
131  os_timer_arm(&ac_test_timer, 120000, 1);
132 }
133 
136 #ifdef DEBUG
137  os_printf("\n\rac 1 on\n\r");
138 #endif
139  ac_off();
140 #ifdef LED_ON_AC
141  led_pattern_a();
142 #endif
143 
144  //Set GPI14 to HIGH
145  gpio_output_set(BIT14, 0, BIT14, 0);
146 
147  // wait 60 seconds and turn ac output off
150  os_timer_arm(&ac_out_off_timer, 60000, 0);
151 }
152 
155 #ifdef DEBUG
156  os_printf("\n\rac 2 on\n\r");
157 #endif
158  ac_off();
159 #ifdef LED_ON_AC
160  led_pattern_b();
161 #endif
162 
163  //Set GPI15 to HIGH
164 // gpio_output_set(BIT15, 0, BIT15, 0);
165 
166  // wait 60 seconds and turn ac output off
169  os_timer_arm(&ac_out_off_timer, 60000, 0);
170 }
171 
174 #ifdef DEBUG
175  os_printf("\n\rac 1 open\n\r");
176 #endif
177 #ifdef LED_ON_AC
178  led_pattern_b();
179 #endif
180 
181 #ifdef THERMO_NO
182  //Set GPI14 to LOW
183  gpio_output_set(0, BIT14, BIT14, 0);
184 #ifdef THERMO_ON_AC_2
185  //Set GPI15 to LOW
186  gpio_output_set(0, BIT15, BIT15, 0);
187 #endif
188 #else // THERMO_NC
189  //Set GPI14 to HIGH
190  gpio_output_set(BIT14, 0, BIT14, 0);
191 #ifdef THERMO_ON_AC_2
192  //Set GPI15 to HIGH
193  gpio_output_set(BIT15, 0, BIT15, 0);
194 #endif
195 #endif
196  if (!sys_cfg.ac_thermo_state) { // only save if state changed
199  }
200 }
201 
204 #ifdef DEBUG
205  os_printf("\n\rac 1 close\n\r");
206 #endif
207 #ifdef LED_ON_AC
208  led_pattern_a();
209 #endif
210 
211 #ifdef THERMO_NO
212  //Set GPI14 to HIGHT
213  gpio_output_set(BIT14, 0, BIT14, 0);
214 #ifdef THERMO_ON_AC_2
215  //Set GPI15 to HIGH
216  gpio_output_set(BIT15, 0, BIT15, 0);
217 #endif
218 #else // THERMO_NC
219  //Set GPI14 to LOW
220  gpio_output_set(0, BIT14, BIT14, 0);
221 #ifdef THERMO_ON_AC_2
222  //Set GPI15 to LOW
223  gpio_output_set(0, BIT15, BIT15, 0);
224 #endif
225 #endif
226  if (sys_cfg.ac_thermo_state) { // only save if state changed
229  }
230 }
231 
233 void ac_thermo_pwm(unsigned int duty_cycle) {
234 #ifdef DEBUG
235  os_printf("\n\rac 1 pwm\n\r");
236 #endif
237 #ifdef LED_ON_AC
239 #endif
240  ac_pwm_duty_cycle = duty_cycle;
243  os_timer_arm(&ac_pwm_timer, 0, 0); // fire now once, ac_pwm_timer start itself
244 }
245 
247 void ac_off() {
248  // turn ac output off
253 }
254 
ICACHE_FLASH_ATTR void led_pattern_b(void)
Definition: led.c:113
ICACHE_FLASH_ATTR void ac_thermo_close()
Definition: ac_out.c:203
#define PIN_FUNC_SELECT(PIN_NAME, FUNC)
Definition: eagle_soc.h:270
ICACHE_FLASH_ATTR void ac_out_init()
Definition: ac_out.c:17
#define os_timer_t
Definition: os_type.h:34
#define os_timer_disarm
Definition: osapi.h:51
ac_pwm_state_t
Definition: ac_out.c:12
static os_timer_t ac_out_off_timer
Definition: ac_out.c:8
#define NULL
Definition: def.h:47
ICACHE_FLASH_ATTR void ac_motor_valve_open()
Definition: ac_out.c:135
static os_timer_t ac_pwm_timer
Definition: ac_out.c:9
#define FUNC_GPIO15
Definition: eagle_soc.h:226
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
#define os_timer_func_t
Definition: os_type.h:35
#define BIT15
Definition: eagle_soc.h:45
ICACHE_FLASH_ATTR void ac_thermo_open()
Definition: ac_out.c:173
ICACHE_FLASH_ATTR void ac_off()
Definition: ac_out.c:247
#define os_printf
Definition: osapi.h:62
#define os_timer_setfn
Definition: osapi.h:52
#define GPIO_REG_READ(reg)
Definition: eagle_soc.h:97
ICACHE_FLASH_ATTR void led_off(void)
Definition: led.c:93
ICACHE_FLASH_ATTR void ac_thermo_pwm(unsigned int duty_cycle)
Definition: ac_out.c:233
void ICACHE_FLASH_ATTR cfg_save_defered()
Definition: config.c:144
ICACHE_FLASH_ATTR static void ac_pwm_timer_func(void *arg)
Definition: ac_out.c:72
#define FUNC_GPIO14
Definition: eagle_soc.h:224
Definition: ac_out.c:12
#define BIT14
Definition: eagle_soc.h:46
#define PERIPHS_IO_MUX_MTDO_U
Definition: eagle_soc.h:225
ICACHE_FLASH_ATTR static void ac_out_off_timer_func(void *arg)
Definition: ac_out.c:53
ICACHE_FLASH_ATTR void led_on(void)
Definition: led.c:88
ICACHE_FLASH_ATTR void ac_motor_valve_close()
Definition: ac_out.c:154
ac_pwm_state_t ac_pwm_state
Definition: ac_out.c:13
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
void gpio_output_set(uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask)
unsigned int ac_pwm_duty_cycle
Definition: ac_out.c:11
ICACHE_FLASH_ATTR void led_pattern_a(void)
Definition: led.c:106
#define GPIO_OUT_ADDRESS
Definition: eagle_soc.h:99
Definition: ac_out.c:12
#define PERIPHS_IO_MUX_MTMS_U
Definition: eagle_soc.h:223
ICACHE_FLASH_ATTR void ac_test()
Definition: ac_out.c:115
ICACHE_FLASH_ATTR void led_stop_pattern(void)
Definition: led.c:128
uint8_t ac_thermo_state
Definition: config.h:61
syscfg_t sys_cfg
Definition: config.c:12
static os_timer_t ac_test_timer
Definition: ac_out.c:7
ICACHE_FLASH_ATTR static void ac_test_timer_func(void *arg)
Definition: ac_out.c:26