1 | // plow_main.c |
2 | // Main logic controller for the snow plow |
3 | // Copyright (c) Joshua 'joshuas3' Stockin 2019 |
4 |
|
5 | #include "plow_main.h" |
6 | #include "pulseIn.h" |
7 | #include "util.h" |
8 |
|
9 | #include <stdio.h> |
10 |
|
11 | #include "esp_system.h" |
12 | #include "esp_task_wdt.h" |
13 | #include "freertos/FreeRTOS.h" |
14 | #include "freertos/task.h" |
15 | #include "driver/gpio.h" |
16 |
|
17 | static void onrestart(void) { |
18 | gpio_set_level(STATUS_RED, HIGH); |
19 | delay(250); |
20 | } |
21 |
|
22 | static void pingTask(void * pingOK) { |
23 | configASSERT(((bool)pingOK) == true); |
24 | static int beeps; |
25 | for (;;) { |
26 | delay(2400); |
27 | printf("ping #%d: %i ticks\n", ++beeps, xTaskGetTickCount()); |
28 | gpio_set_level(STATUS_YELLOW, HIGH); |
29 | delay(0200); |
30 | gpio_set_level(STATUS_YELLOW, LOW); |
31 | delay(0200); |
32 | gpio_set_level(STATUS_YELLOW, HIGH); |
33 | delay(0200); |
34 | gpio_set_level(STATUS_YELLOW, LOW); |
35 | } |
36 | } |
37 |
|
38 | static void radioTask(void * arg) { |
39 | configASSERT(((bool)arg) == true); |
40 | unsigned int R1; |
41 | unsigned int R2; |
42 | unsigned int R3; |
43 | unsigned int R4; |
44 | unsigned int R5; |
45 | unsigned int R6; |
46 | for (;;) { |
47 | R1 = pulseIn(RADIO_1, true); |
48 | R2 = pulseIn(RADIO_2, true); |
49 | R3 = pulseIn(RADIO_3, true); |
50 | R4 = pulseIn(RADIO_4, true); |
51 | R5 = pulseIn(RADIO_5, true); |
52 | R6 = pulseIn(RADIO_6, true); |
53 | if (R1 + R2 + R3 + R4 + R5 + R6 != 0) { |
54 | printf("Radio RX:\t%i\t%i\t%i\t%i\t%i\t%i\n", |
55 | map(R1, 160000, 320000, 0, 1000), |
56 | map(R2, 160000, 320000, 0, 1000), |
57 | map(R3, 160000, 320000, 0, 1000), |
58 | map(R4, 160000, 320000, 0, 1000), |
59 | map(R5, 160000, 320000, 0, 1000)>500, |
60 | map(R6, 160000, 320000, 0, 1000)>500); |
61 | } else { |
62 | printf("Radio RX:\t-\t-\t-\t-\t-\t-\n"); |
63 | } |
64 | esp_task_wdt_reset(); |
65 | delay(10); |
66 | } |
67 | } |
68 |
|
69 | void app_main(void) |
70 | { |
71 | printf("entering app_main\n"); |
72 |
|
73 | printf("configuring STATUS_[GREEN|YELLOW|RED] GPIO output\n"); |
74 | gpio_config_t io_conf; |
75 | io_conf.pin_bit_mask = (1ULL<<STATUS_GREEN) | (1ULL<<STATUS_YELLOW) | (1ULL<<STATUS_RED); |
76 | io_conf.mode = GPIO_MODE_OUTPUT; |
77 | io_conf.pull_up_en = 0; |
78 | io_conf.pull_down_en = 1; |
79 | io_conf.intr_type = GPIO_INTR_DISABLE; |
80 | gpio_config(&io_conf); |
81 |
|
82 | printf("pulling STATUS_[GREEN|YELLOW|RED] HIGH\n"); |
83 | gpio_set_level(STATUS_GREEN, HIGH); |
84 | gpio_set_level(STATUS_YELLOW, HIGH); |
85 | gpio_set_level(STATUS_RED, HIGH); |
86 |
|
87 | printf("configuring RADIO_[1-6] GPIO input\n"); |
88 | io_conf.pin_bit_mask = (1ULL<<RADIO_1) | (1ULL<<RADIO_2) | (1ULL<<RADIO_3) | (1ULL<<RADIO_4) | (1ULL<<RADIO_5) | (1ULL<<RADIO_6); |
89 | io_conf.mode = GPIO_MODE_INPUT; |
90 | io_conf.pull_up_en = 0; |
91 | io_conf.pull_down_en = 0; |
92 | io_conf.intr_type = GPIO_INTR_DISABLE; |
93 | gpio_config(&io_conf); |
94 |
|
95 | printf("registering shutdown function\n"); |
96 | esp_register_shutdown_handler(onrestart); |
97 |
|
98 | printf("creating ping task\n"); |
99 | TaskHandle_t pingTaskHandle = NULL; |
100 | xTaskCreate(pingTask, "pingTask", 2000, (void *)true, tskIDLE_PRIORITY, &pingTaskHandle); |
101 |
|
102 | printf("creating radio task\n"); |
103 | TaskHandle_t radioTaskHandle = NULL; |
104 | xTaskCreate(radioTask, "radioTask", 10000, (void *)true, 1, &radioTaskHandle); |
105 |
|
106 | printf("pulling STATUS_[YELLOW|RED] LOW\n"); |
107 | gpio_set_level(STATUS_YELLOW, LOW); |
108 | gpio_set_level(STATUS_RED, LOW); |
109 | } |
110 |
|