add a quick LED helper / prepare SqueezeAMP menuconfig

This commit is contained in:
philippe44
2019-08-06 13:21:00 -07:00
parent 3c025c8454
commit da63df4d93
9 changed files with 213 additions and 23 deletions

View File

@@ -0,0 +1,10 @@
#
# Component Makefile
#
# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default,
# this will take the sources in the src/ directory, compile them and link them into
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
# please read the SDK documents if you need to do this.
#
COMPONENT_ADD_INCLUDEDIRS := .

101
components/io/led.c Normal file
View File

@@ -0,0 +1,101 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "led.h"
#define MAX_LED 8
#define BLOCKTIME 10 // up to portMAX_DELAY
static struct led_s {
gpio_num_t gpio;
bool on;
int onstate;
int ontime, offtime;
int waiton, waitoff;
TimerHandle_t timer;
} leds[MAX_LED];
static void vCallbackFunction( TimerHandle_t xTimer ) {
struct led_s *led = (struct led_s*) pvTimerGetTimerID (xTimer);
if (!led->timer) return;
led->on = !led->on;
gpio_set_level(led->gpio, led->on ? led->onstate : !led->onstate);
// was just on for a while
if (!led->on && led->offtime == -1) return;
// regular blinking
xTimerChangePeriod(xTimer, (led->on ? led->ontime : led->offtime) / portTICK_RATE_MS, BLOCKTIME);
}
bool led_blink_core(int idx, int ontime, int offtime, bool wait) {
if (!leds[idx].gpio) return false;
if (leds[idx].timer) {
// low priority timers will wait
if (wait && xTimerIsTimerActive(leds[idx].timer)) {
leds[idx].waiton = ontime;
leds[idx].waitoff = offtime;
return true;
}
xTimerStop(leds[idx].timer, BLOCKTIME);
}
leds[idx].ontime = ontime;
leds[idx].offtime = offtime;
if (ontime == 0) {
gpio_set_level(leds[idx].gpio, !leds[idx].onstate);
} else if (offtime == 0) {
gpio_set_level(leds[idx].gpio, leds[idx].onstate);
} else {
if (!leds[idx].timer) leds[idx].timer = xTimerCreate("ledTimer", ontime / portTICK_RATE_MS, pdFALSE, (void *)&leds[idx], vCallbackFunction);
leds[idx].on = true;
gpio_set_level(leds[idx].gpio, leds[idx].onstate);
if (xTimerStart(leds[idx].timer, BLOCKTIME) == pdFAIL) return false;
}
return true;
}
bool led_release(int idx) {
if (!leds[idx].gpio) return false;
if (leds[idx].waiton) {
led_blink_core(idx, leds[idx].waiton, leds[idx].waitoff, false);
leds[idx].waiton = 0;
} else {
gpio_set_level(leds[idx].gpio, !leds[idx].onstate);
}
return true;
}
bool led_config(int idx, gpio_num_t gpio, int onstate) {
if (idx >= MAX_LED) return false;
leds[idx].gpio = gpio;
leds[idx].onstate = onstate;
gpio_pad_select_gpio(gpio);
gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
gpio_set_level(gpio, !onstate);
return true;
}

37
components/io/led.h Normal file
View File

@@ -0,0 +1,37 @@
/*
* Squeezelite for esp32
*
* (c) Sebastien 2019
* Philippe G. 2019, philippe_44@outlook.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef LED_H
#include "driver/gpio.h"
enum { LED_GREEN = 0, LED_RED };
#define led_on(idx) led_blink_core(idx, 1, 0, false)
#define led_off(idx) led_blink_core(idx, 0, 0, false)
#define led_blink(idx, on, off) led_blink_core(idx, on, off, false)
#define led_blink_wait(idx, on, off) led_blink_core(idx, on, off, true)
bool led_blink_core(int idx, int ontime, int offtime, bool wait);
bool led_release(int idx);
bool led_config(int idx, gpio_num_t gpio, int onstate);
#endif