Initial commit
This commit is contained in:
2
main/CMakeLists.txt
Normal file
2
main/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.c" "buttons/buttons.c"
|
||||
INCLUDE_DIRS "." "buttons")
|
||||
43
main/buttons/buttons.c
Normal file
43
main/buttons/buttons.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "buttons.h"
|
||||
#include "iot_button.h"
|
||||
#include "button_gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_check.h"
|
||||
|
||||
static const char* TAG = "TrainThing/buttons";
|
||||
|
||||
const action actions[] = {
|
||||
{.btnAcc = FRONT_LIGHT_INCREASE, .name = "FRONT_LIGHT_INCREASE", .pin = 1, .shortcut = 'a'},
|
||||
{.btnAcc = FRONT_LIGHT_DECREASE, .name = "FRONT_LIGHT_DECREASE", .pin = 2, .shortcut = 'b'},
|
||||
{.btnAcc = BACK_LIGHT_INCREASE, .name = "BACK_LIGHT_INCREASE", .pin = 3, .shortcut = 'c'},
|
||||
{.btnAcc = BACK_LIGHT_DECREASE, .name = "BACK_LIGHT_DECREASE", .pin = 4, .shortcut = 'd'},
|
||||
{.btnAcc = CAB_LIGHT, .name = "CAB_LIGHT", .pin = 5, .shortcut = 'e'},
|
||||
{.btnAcc = WIPPERS, .name = "WIPPERS", .pin = 6, .shortcut = 'f'},
|
||||
{.btnAcc = SANDER, .name = "SANDER", .pin = 7, .shortcut = 'g'},
|
||||
{.btnAcc = STARTER, .name = "STARTER", .pin = 8, .shortcut = 'h'},
|
||||
{.btnAcc = CUT, .name = "CUT", .pin = 9, .shortcut = 'i'},
|
||||
{.btnAcc = HORN, .name = "HORN", .pin = 10, .shortcut = 'j'},
|
||||
};
|
||||
void button_event_cb(void *arg, void *data)
|
||||
{
|
||||
action *a = (action*)data;
|
||||
|
||||
button_event_t event = iot_button_get_event(arg);
|
||||
ESP_LOGI(TAG, "%s pin:%d name:%s shortcut:%c", iot_button_get_event_str(event), (int)a->pin, (char*)a->name, (char)a->shortcut);
|
||||
}
|
||||
|
||||
void initialize_buttons(){
|
||||
const button_config_t btn_cfg = {0};
|
||||
button_handle_t btns[5];
|
||||
|
||||
for (size_t i = 0; i < sizeof(actions) / sizeof(actions[0]); i++)
|
||||
{
|
||||
const button_gpio_config_t btn_gpio_cfg = {
|
||||
.gpio_num = actions[i].pin,
|
||||
.active_level = 0,
|
||||
};
|
||||
ESP_RETURN_VOID_ON_ERROR(iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btns[i]), TAG, "");
|
||||
iot_button_register_cb(btns[i], BUTTON_PRESS_DOWN, NULL, button_event_cb, (void *) &actions[i]);
|
||||
}
|
||||
}
|
||||
30
main/buttons/buttons.h
Normal file
30
main/buttons/buttons.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef BUTTONS_H
|
||||
|
||||
#define BUTTONS_H
|
||||
|
||||
typedef enum {
|
||||
FRONT_LIGHT_INCREASE,
|
||||
FRONT_LIGHT_DECREASE,
|
||||
BACK_LIGHT_INCREASE,
|
||||
BACK_LIGHT_DECREASE,
|
||||
CAB_LIGHT,
|
||||
WIPPERS,
|
||||
SANDER,
|
||||
STARTER,
|
||||
CUT,
|
||||
HORN,
|
||||
}buttonAction;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
buttonAction btnAcc;
|
||||
const char* name;
|
||||
char shortcut;
|
||||
int pin;
|
||||
|
||||
}action;
|
||||
|
||||
void button_event_cb(void *arg, void *data);
|
||||
|
||||
void initialize_buttons();
|
||||
#endif // BUTTONS_H
|
||||
17
main/idf_component.yml
Normal file
17
main/idf_component.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
## IDF Component Manager Manifest File
|
||||
dependencies:
|
||||
## Required IDF version
|
||||
idf:
|
||||
version: '>=4.1.0'
|
||||
# # Put list of dependencies here
|
||||
# # For components maintained by Espressif:
|
||||
# component: "~1.0.0"
|
||||
# # For 3rd party components:
|
||||
# username/component: ">=1.0.0,<2.0.0"
|
||||
# username2/component2:
|
||||
# version: "~1.0.0"
|
||||
# # For transient dependencies `public` flag can be set.
|
||||
# # `public` flag doesn't have an effect dependencies of the `main` component.
|
||||
# # All dependencies of `main` are public by default.
|
||||
# public: true
|
||||
espressif/button: ^4.1.6
|
||||
87
main/main.c
Normal file
87
main/main.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include "iot_button.h"
|
||||
#include "button_gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_check.h"
|
||||
#include "buttons/buttons.h"
|
||||
|
||||
static const char* TAG = "TrainThing";
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
static void button_single_click_cb(void *arg,void *usr_data)
|
||||
{
|
||||
ESP_LOGI(TAG, "BUTTON_SINGLE_CLICK");
|
||||
}
|
||||
static void button_long_press_1_cb(void *arg,void *usr_data)
|
||||
{
|
||||
ESP_LOGI(TAG, "BUTTON_LONG_PRESS_START_1");
|
||||
}
|
||||
|
||||
static void button_long_press_2_cb(void *arg,void *usr_data)
|
||||
{
|
||||
ESP_LOGI(TAG, "BUTTON_LONG_PRESS_START_2");
|
||||
}
|
||||
|
||||
static void button_click_2_cb(void *arg,void *usr_data)
|
||||
{
|
||||
ESP_LOGI(TAG, "BUTTON 2 click");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
initialize_buttons();
|
||||
/*
|
||||
for (size_t i = 0; i < sizeof(btn_name_table) / sizeof(btn_name_table[0]); ++i) {
|
||||
ESP_LOGI(TAG, "%s %d %c", btn_name_table[i].name, btn_name_table[i].btnAcc, btn_name_table[i].shortcut );
|
||||
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//ESP_RETURN_VOID_ON_ERROR(iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btn), TAG, "");
|
||||
|
||||
|
||||
/*ESP_RETURN_VOID_ON_ERROR(iot_button_register_cb(gpio_btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb,NULL), TAG, "");
|
||||
|
||||
button_event_args_t args ={.long_press.press_time = 2000};
|
||||
ESP_RETURN_VOID_ON_ERROR(iot_button_register_cb(gpio_btn, BUTTON_LONG_PRESS_UP, NULL, button_event_cb, NULL), TAG, "");
|
||||
|
||||
args.long_press.press_time = 5000;
|
||||
//ESP_RETURN_VOID_ON_ERROR(iot_button_register_cb(gpio_btn, BUTTON_LONG_PRESS_UP, &args, button_long_press_2_cb, NULL), TAG, "");
|
||||
|
||||
ESP_RETURN_VOID_ON_ERROR(iot_button_register_cb(gpio_btn, BUTTON_DOUBLE_CLICK, NULL, button_event_cb, NULL), TAG, "");
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
iot_button_register_cb(btn, BUTTON_PRESS_DOWN, NULL, button_event_cb, NULL);
|
||||
iot_button_register_cb(btn, BUTTON_PRESS_UP, NULL, button_event_cb, NULL);
|
||||
iot_button_register_cb(btn, BUTTON_PRESS_REPEAT, NULL, button_event_cb, NULL);
|
||||
iot_button_register_cb(btn, BUTTON_PRESS_REPEAT_DONE, NULL, button_event_cb, NULL);
|
||||
iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb, NULL);
|
||||
iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, NULL, button_event_cb, NULL);
|
||||
|
||||
|
||||
button_event_args_t args = {
|
||||
.multiple_clicks.clicks = 2,
|
||||
};
|
||||
iot_button_register_cb(btn, BUTTON_MULTIPLE_CLICK, &args, button_event_cb, (void *)2);
|
||||
args.multiple_clicks.clicks = 3;
|
||||
iot_button_register_cb(btn, BUTTON_MULTIPLE_CLICK, &args, button_event_cb, (void *)3);
|
||||
|
||||
iot_button_register_cb(btn, BUTTON_LONG_PRESS_START, NULL, button_event_cb, NULL);
|
||||
iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, NULL, button_event_cb, NULL);
|
||||
iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, NULL, button_event_cb, NULL);
|
||||
iot_button_register_cb(btn, BUTTON_PRESS_END, NULL, button_event_cb, NULL);
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user