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