2019-05-12 23:04:36 +02:00
|
|
|
import math
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
from esphome import pins
|
|
|
|
from esphome.components import output
|
|
|
|
import esphome.config_validation as cv
|
|
|
|
import esphome.codegen as cg
|
2019-05-12 23:04:36 +02:00
|
|
|
from esphome.const import CONF_BIT_DEPTH, CONF_CHANNEL, CONF_FREQUENCY, \
|
2019-04-17 12:06:00 +02:00
|
|
|
CONF_ID, CONF_PIN, ESP_PLATFORM_ESP32
|
|
|
|
|
|
|
|
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
|
|
|
|
|
|
|
|
|
2019-05-12 23:04:36 +02:00
|
|
|
def calc_max_frequency(bit_depth):
|
|
|
|
return 80e6 / (2**bit_depth)
|
|
|
|
|
|
|
|
|
|
|
|
def calc_min_frequency(bit_depth):
|
|
|
|
# LEDC_DIV_NUM_HSTIMER is 15-bit unsigned integer
|
|
|
|
# lower 8 bits represent fractional part
|
|
|
|
max_div_num = ((1 << 16) - 1) / 256.0
|
|
|
|
return 80e6 / (max_div_num * (2**bit_depth))
|
|
|
|
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
def validate_frequency_bit_depth(obj):
|
2019-05-08 15:26:44 +02:00
|
|
|
frequency = obj[CONF_FREQUENCY]
|
2019-05-12 23:04:36 +02:00
|
|
|
if CONF_BIT_DEPTH not in obj:
|
|
|
|
obj = obj.copy()
|
|
|
|
for bit_depth in range(15, 0, -1):
|
|
|
|
if calc_min_frequency(bit_depth) <= frequency <= calc_max_frequency(bit_depth):
|
|
|
|
obj[CONF_BIT_DEPTH] = bit_depth
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
min_freq = min(calc_min_frequency(x) for x in range(1, 16))
|
|
|
|
max_freq = max(calc_max_frequency(x) for x in range(1, 16))
|
|
|
|
if frequency < min_freq:
|
|
|
|
raise cv.Invalid("This frequency setting is not possible, please choose a higher "
|
|
|
|
"frequency (at least {}Hz)".format(int(min_freq)))
|
|
|
|
if frequency > max_freq:
|
|
|
|
raise cv.Invalid("This frequency setting is not possible, please choose a lower "
|
|
|
|
"frequency (at most {}Hz)".format(int(max_freq)))
|
|
|
|
raise cv.Invalid("Invalid frequency!")
|
|
|
|
|
2019-05-08 15:26:44 +02:00
|
|
|
bit_depth = obj[CONF_BIT_DEPTH]
|
2019-05-12 23:04:36 +02:00
|
|
|
min_freq = calc_min_frequency(bit_depth)
|
|
|
|
max_freq = calc_max_frequency(bit_depth)
|
2019-04-17 12:06:00 +02:00
|
|
|
if frequency > max_freq:
|
2019-05-12 23:04:36 +02:00
|
|
|
raise cv.Invalid('Maximum frequency for bit depth {} is {}Hz. Please decrease the '
|
|
|
|
'bit_depth.'.format(bit_depth, int(math.floor(max_freq))))
|
|
|
|
if frequency < calc_min_frequency(bit_depth):
|
|
|
|
raise cv.Invalid('Minimum frequency for bit depth {} is {}Hz. Please increase the '
|
|
|
|
'bit_depth.'.format(bit_depth, int(math.ceil(min_freq))))
|
2019-04-17 12:06:00 +02:00
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
ledc_ns = cg.esphome_ns.namespace('ledc')
|
|
|
|
LEDCOutput = ledc_ns.class_('LEDCOutput', output.FloatOutput, cg.Component)
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = cv.All(output.FLOAT_OUTPUT_SCHEMA.extend({
|
2019-04-22 21:56:30 +02:00
|
|
|
cv.Required(CONF_ID): cv.declare_id(LEDCOutput),
|
2019-04-17 12:06:00 +02:00
|
|
|
cv.Required(CONF_PIN): pins.internal_gpio_output_pin_schema,
|
|
|
|
cv.Optional(CONF_FREQUENCY, default='1kHz'): cv.frequency,
|
2019-05-12 23:04:36 +02:00
|
|
|
cv.Optional(CONF_BIT_DEPTH): cv.int_range(min=1, max=15),
|
2019-05-11 11:40:29 +02:00
|
|
|
cv.Optional(CONF_CHANNEL): cv.int_range(min=0, max=15),
|
2019-04-17 12:06:00 +02:00
|
|
|
}).extend(cv.COMPONENT_SCHEMA), validate_frequency_bit_depth)
|
|
|
|
|
|
|
|
|
|
|
|
def to_code(config):
|
|
|
|
gpio = yield cg.gpio_pin_expression(config[CONF_PIN])
|
|
|
|
var = cg.new_Pvariable(config[CONF_ID], gpio)
|
|
|
|
yield cg.register_component(var, config)
|
|
|
|
yield output.register_output(var, config)
|
|
|
|
if CONF_CHANNEL in config:
|
|
|
|
cg.add(var.set_channel(config[CONF_CHANNEL]))
|