1
- #include "esp_err.h"
2
- #include "esp_log.h"
3
-
4
- #include <driver/gpio.h>
5
- #include <driver/dac.h>
6
-
7
- static dac_channel_t fuel_gauge_channel = -1 ;
8
- static gpio_num_t low_fuel_indicator_pin = -1 ;
9
-
10
- /**
11
- * Setup the temperature gauge output.
12
- *
13
- * @param gauge dac output pin
14
- * @param low_fuel_indicator output pin
15
- */
16
- void setup_fuel_gauge (dac_channel_t gauge , gpio_num_t low_fuel_indicator ) {
17
- if (gauge != -1 ) {
18
- esp_err_t error = dac_output_enable (gauge );
19
- if (error == ESP_OK ) {
20
- fuel_gauge_channel = gauge ;
21
- }
22
- }
23
-
24
- if (low_fuel_indicator != -1 &&
25
- (
26
- low_fuel_indicator < GPIO_NUM_34 || // pins 34 - 39 on esp32 are
27
- low_fuel_indicator > GPIO_NUM_39 // input only
28
- )) {
29
- gpio_config_t config = {
30
- .pin_bit_mask = low_fuel_indicator ,
31
- .mode = GPIO_MODE_OUTPUT ,
32
- .pull_up_en = GPIO_PULLUP_DISABLE ,
33
- .pull_down_en = GPIO_PULLDOWN_ENABLE ,
34
- .intr_type = GPIO_INTR_DISABLE
35
- };
36
- esp_err_t error = gpio_config (& config );
37
- if (error == ESP_OK ) {
38
- low_fuel_indicator_pin = low_fuel_indicator ;
39
- }
40
-
41
- }
42
- }
43
-
44
- /**
45
- * Set the position of the fuel gauge in terms of % full
46
- *
47
- * @param percent_full for the gauge to point to
48
- */
49
- void write_to_fuel_gauge (int percent_full ) {
50
- if (fuel_gauge_channel == -1 ) {
51
- ESP_LOGE ("write_to_fuel_gauge" , "Fuel gauge not yet initialized, please call setup_fuel_gauge first." );
52
- } else {
53
- int voltage = 70 + ((percent_full * 80 ) / 100 );
54
- voltage &= 255 ;
55
- ESP_LOGI ("FUEL VOLTAGE" , "[V: %d, F: %d]" , voltage , percent_full );
56
- dac_output_voltage (fuel_gauge_channel , voltage );
57
- }
58
- }
59
-
60
- /**
61
- * Turns the low fuel indicator light on if it's hooked up.
62
- */
63
- void enable_low_fuel_indicator () {
64
- if (low_fuel_indicator_pin == -1 ) {
65
- ESP_LOGE ("enable_low_fuel_indicator" , "low fuel indicator not yet initialized, please call setup_fuel_gauge first." );
66
- } else {
67
- gpio_set_level (low_fuel_indicator_pin , 1 );
68
- }
69
- }
70
-
71
- /**
72
- * Turns the low fuel indicator light off if it's hooked up.
73
- */
74
- void disable_low_fuel_indicator () {
75
- if (low_fuel_indicator_pin == -1 ) {
76
- ESP_LOGE ("enable_low_fuel_indicator" , "low fuel indicator not yet initialized, please call setup_fuel_gauge first." );
77
- } else {
78
- gpio_set_level (low_fuel_indicator_pin , 0 );
79
- }
1
+ #include "esp_err.h"
2
+ #include "esp_log.h"
3
+
4
+ #include <driver/gpio.h>
5
+ #include <driver/dac.h>
6
+
7
+ static dac_channel_t fuel_gauge_channel = -1 ;
8
+ static gpio_num_t low_fuel_indicator_pin = -1 ;
9
+
10
+ /**
11
+ * Setup the temperature gauge output.
12
+ *
13
+ * @param gauge dac output pin
14
+ * @param low_fuel_indicator output pin
15
+ */
16
+ void setup_fuel_gauge (dac_channel_t gauge , gpio_num_t low_fuel_indicator ) {
17
+ if (gauge != -1 ) {
18
+ esp_err_t error = dac_output_enable (gauge );
19
+ if (error == ESP_OK ) {
20
+ fuel_gauge_channel = gauge ;
21
+ }
22
+ }
23
+
24
+ if (low_fuel_indicator != -1 &&
25
+ (
26
+ low_fuel_indicator < GPIO_NUM_34 || // pins 34 - 39 on esp32 are
27
+ low_fuel_indicator > GPIO_NUM_39 // input only
28
+ )) {
29
+ gpio_config_t config = {
30
+ .pin_bit_mask = low_fuel_indicator ,
31
+ .mode = GPIO_MODE_OUTPUT ,
32
+ .pull_up_en = GPIO_PULLUP_DISABLE ,
33
+ .pull_down_en = GPIO_PULLDOWN_ENABLE ,
34
+ .intr_type = GPIO_INTR_DISABLE
35
+ };
36
+ esp_err_t error = gpio_config (& config );
37
+ if (error == ESP_OK ) {
38
+ low_fuel_indicator_pin = low_fuel_indicator ;
39
+ }
40
+
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Set the position of the fuel gauge in terms of % full
46
+ *
47
+ * @param percent_full for the gauge to point to
48
+ */
49
+ void write_to_fuel_gauge (int percent_full ) {
50
+ if (fuel_gauge_channel == -1 ) {
51
+ ESP_LOGE ("write_to_fuel_gauge" , "Fuel gauge not yet initialized, please call setup_fuel_gauge first." );
52
+ } else {
53
+ int voltage = 70 + ((percent_full * 80 ) / 100 );
54
+ voltage &= 255 ;
55
+ ESP_LOGI ("FUEL VOLTAGE" , "[V: %d, F: %d]" , voltage , percent_full );
56
+ dac_output_voltage (fuel_gauge_channel , voltage );
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Turns the low fuel indicator light on if it's hooked up.
62
+ */
63
+ void enable_low_fuel_indicator () {
64
+ if (low_fuel_indicator_pin == -1 ) {
65
+ ESP_LOGE ("enable_low_fuel_indicator" , "low fuel indicator not yet initialized, please call setup_fuel_gauge first." );
66
+ } else {
67
+ gpio_set_level (low_fuel_indicator_pin , 1 );
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Turns the low fuel indicator light off if it's hooked up.
73
+ */
74
+ void disable_low_fuel_indicator () {
75
+ if (low_fuel_indicator_pin == -1 ) {
76
+ ESP_LOGE ("enable_low_fuel_indicator" , "low fuel indicator not yet initialized, please call setup_fuel_gauge first." );
77
+ } else {
78
+ gpio_set_level (low_fuel_indicator_pin , 0 );
79
+ }
80
80
}
0 commit comments