Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lights: fix broken unsigned arithmetic + refactoring #1938

Merged
merged 7 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions code/espurna/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include <DebounceEvent.h>
#include <vector>

#include "light.h"

typedef struct {
DebounceEvent * button;
unsigned long actions;
Expand Down
6 changes: 6 additions & 0 deletions code/espurna/config/progmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
// PROGMEM definitions
//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
// Various strings
//--------------------------------------------------------------------------------

PROGMEM const char pstr_unknown[] = "UNKNOWN";

//--------------------------------------------------------------------------------
// Reset reasons
//--------------------------------------------------------------------------------
Expand Down
18 changes: 0 additions & 18 deletions code/espurna/config/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,24 +177,6 @@ int16_t i2c_read_int16(uint8_t address, uint8_t reg);
int16_t i2c_read_int16_le(uint8_t address, uint8_t reg);
void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len);

// -----------------------------------------------------------------------------
// Lights
// -----------------------------------------------------------------------------

unsigned char lightChannels();

void lightState(unsigned char i, bool state);
bool lightState(unsigned char i);

void lightState(bool state);
bool lightState();

void lightBrightness(unsigned int brightness);
unsigned int lightBrightness();

unsigned int lightChannel(unsigned char id);
void lightChannel(unsigned char id, unsigned char value);

// -----------------------------------------------------------------------------
// MQTT
// -----------------------------------------------------------------------------
Expand Down
36 changes: 19 additions & 17 deletions code/espurna/encoder.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ Copyright (C) 2018-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "libs/Encoder.h"
#include <vector>

typedef struct {
struct encoder_t {
Encoder * encoder;
unsigned char button_pin;
unsigned char button_logic;
unsigned char button_mode;
unsigned char mode;
unsigned char channel1; // default
unsigned char channel2; // only if button defined and pressed
} encoder_t;
};

std::vector<encoder_t> _encoders;
unsigned long _encoder_min_delta = 1;

void _encoderConfigure() {

// Clean previous encoders
for (unsigned char i=0; i<_encoders.size(); i++) {
free(_encoders[i].encoder);
_encoder_min_delta = getSetting("encMinDelta", ENCODER_MINIMUM_DELTA).toInt();
if (!_encoder_min_delta) _encoder_min_delta = 1;

// no need to reload objects right now
if (_encoders.size()) return;

// Clean previous encoders and re-add them
for (auto& encoder : _encoders) {
delete encoder.encoder;
}
_encoders.clear();

// Load encoders
// TODO: encEnable
// TODO: implement reloading without re-allocating objects
#if (ENCODER1_PIN1 != GPIO_NONE) && (ENCODER1_PIN2 != GPIO_NONE)
{
_encoders.push_back({
Expand Down Expand Up @@ -79,26 +86,21 @@ void _encoderConfigure() {
}
#endif

// Setup encoders
for (unsigned char i=0; i<_encoders.size(); i++) {
if (GPIO_NONE != _encoders[i].button_pin) {
pinMode(_encoders[i].button_pin, _encoders[i].button_mode);
// TODO: manage buttons through debounceevent?
for (auto& encoder : _encoders) {
if (GPIO_NONE != encoder.button_pin) {
pinMode(encoder.button_pin, encoder.button_mode);
}
}

_encoder_min_delta = getSetting("encMinDelta", ENCODER_MINIMUM_DELTA).toInt();
if (!_encoder_min_delta) _encoder_min_delta = 1;

}

void _encoderLoop() {

// for each encoder, read delta (read()) and map button action
for (unsigned char i=0; i<_encoders.size(); i++) {

encoder_t encoder = _encoders[i];
for (auto& encoder : _encoders) {

long delta = encoder.encoder->read();
const auto delta = encoder.encoder->read();
encoder.encoder->write(0);
if ((0 == delta) || (_encoder_min_delta > abs(delta))) continue;

Expand Down
46 changes: 34 additions & 12 deletions code/espurna/light.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
// -----------------------------------------------------------------------------
// Light
// Lights
// -----------------------------------------------------------------------------

#pragma once

namespace Light {
constexpr const unsigned char VALUE_MIN = LIGHT_MIN_VALUE;
constexpr const unsigned char VALUE_MAX = LIGHT_MAX_VALUE;
constexpr const long VALUE_MIN = LIGHT_MIN_VALUE;
constexpr const long VALUE_MAX = LIGHT_MAX_VALUE;

constexpr const unsigned int BRIGHTNESS_MIN = LIGHT_MIN_BRIGHTNESS;
constexpr const unsigned int BRIGHTNESS_MAX = LIGHT_MAX_BRIGHTNESS;
constexpr const long BRIGHTNESS_MIN = LIGHT_MIN_BRIGHTNESS;
constexpr const long BRIGHTNESS_MAX = LIGHT_MAX_BRIGHTNESS;

// Default to the Philips Hue value that HA also use.
// https://developers.meethue.com/documentation/core-concepts
constexpr const unsigned int MIREDS_COLDWHITE = LIGHT_COLDWHITE_MIRED;
constexpr const unsigned int MIREDS_WARMWHITE = LIGHT_WARMWHITE_MIRED;
constexpr const long MIREDS_COLDWHITE = LIGHT_COLDWHITE_MIRED;
constexpr const long MIREDS_WARMWHITE = LIGHT_WARMWHITE_MIRED;

constexpr const unsigned int KELVIN_WARMWHITE = LIGHT_WARMWHITE_KELVIN;
constexpr const unsigned int KELVIN_COLDWHITE = LIGHT_COLDWHITE_KELVIN;
constexpr const long KELVIN_WARMWHITE = LIGHT_WARMWHITE_KELVIN;
constexpr const long KELVIN_COLDWHITE = LIGHT_COLDWHITE_KELVIN;

constexpr const unsigned int PWM_MIN = LIGHT_MIN_PWM;
constexpr const unsigned int PWM_MAX = LIGHT_MAX_PWM;
constexpr const unsigned int PWM_LIMIT = LIGHT_LIMIT_PWM;
constexpr const long PWM_MIN = LIGHT_MIN_PWM;
constexpr const long PWM_MAX = LIGHT_MAX_PWM;
constexpr const long PWM_LIMIT = LIGHT_LIMIT_PWM;

enum Communications : unsigned char {
COMMS_NONE = 0,
COMMS_NORMAL = 1 << 0,
COMMS_GROUP = 1 << 1
};
}

size_t lightChannels();

void lightState(unsigned char i, bool state);
bool lightState(unsigned char i);

void lightState(bool state);
bool lightState();

void lightBrightness(long brightness);
long lightBrightness();

long lightChannel(unsigned char id);
void lightChannel(unsigned char id, long value);

void lightBrightnessStep(long steps, long multiplier = LIGHT_STEP);
void lightChannelStep(unsigned char id, long steps, long multiplier = LIGHT_STEP);
Loading