-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+Plug S: Support for Neopixel Status LEDs (#1601)
- Loading branch information
Showing
4 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) Shelly-HomeKit Contributors | ||
* All rights reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "shelly_statusled.hpp" | ||
|
||
#ifdef MGOS_CONFIG_HAVE_LED | ||
|
||
namespace shelly { | ||
|
||
StatusLED::StatusLED(int id, int pin, int num_pixel, | ||
enum mgos_neopixel_order pixel_type, Output *chained_led, | ||
const struct mgos_config_led *cfg) | ||
: Output(id), | ||
pin_(pin), | ||
num_pixel_(num_pixel), | ||
chained_led_(chained_led), | ||
cfg_(cfg) { | ||
pixel_ = mgos_neopixel_create(pin_, num_pixel_, pixel_type); | ||
value_ = false; | ||
} | ||
|
||
StatusLED::~StatusLED() { | ||
mgos_neopixel_free(pixel_); | ||
} | ||
|
||
bool StatusLED::GetState() { | ||
return value_; | ||
} | ||
|
||
int StatusLED::pin() const { | ||
return pin_; | ||
} | ||
|
||
struct rgb { | ||
int r; | ||
int g; | ||
int b; | ||
}; | ||
|
||
Status StatusLED::SetState(bool on, const char *source) { | ||
if (chained_led_ != nullptr) { | ||
chained_led_->SetState(on, source); | ||
} | ||
value_ = on; | ||
// get color from config | ||
struct rgb colorOn = {(cfg_->color_on >> 16) & 0xFF, | ||
(cfg_->color_on >> 8) & 0xFF, | ||
(cfg_->color_on >> 0) & 0xFF}; | ||
struct rgb colorOff = {(cfg_->color_off >> 16) & 0xFF, | ||
(cfg_->color_off >> 8) & 0xFF, | ||
(cfg_->color_off >> 0) & 0xFF}; | ||
|
||
struct rgb color = on ? colorOn : colorOff; | ||
|
||
for (int i = 0; i < num_pixel_; i++) { | ||
mgos_neopixel_set(pixel_, i, color.r, color.b, color.g); | ||
} | ||
mgos_neopixel_show(pixel_); | ||
return Status::OK(); | ||
} | ||
|
||
} // namespace shelly | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) Shelly-HomeKit Contributors | ||
* All rights reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "mgos_config.h" | ||
|
||
#ifdef MGOS_CONFIG_HAVE_LED | ||
|
||
#pragma once | ||
|
||
#include "shelly_output.hpp" | ||
|
||
#include "mgos_neopixel.h" | ||
|
||
namespace shelly { | ||
|
||
class StatusLED : public Output { | ||
public: | ||
StatusLED(int id, int pin, int num_pixel, enum mgos_neopixel_order pixel_type, | ||
Output *chained_led, const struct mgos_config_led *cfg); | ||
virtual ~StatusLED(); | ||
|
||
// Output interface impl. | ||
bool GetState() override; | ||
Status SetState(bool on, const char *source) override; | ||
Status SetStatePWM(float duty, const char *source) override { | ||
return Status::UNIMPLEMENTED(); | ||
}; | ||
Status Pulse(bool on, int duration_ms, const char *source) override { | ||
return Status::UNIMPLEMENTED(); | ||
}; | ||
void SetInvert(bool out_invert) override{}; | ||
int pin() const; | ||
|
||
protected: | ||
private: | ||
const int pin_; | ||
const int num_pixel_; | ||
|
||
bool value_; | ||
|
||
struct mgos_neopixel *pixel_; | ||
|
||
Output *chained_led_; | ||
|
||
const struct mgos_config_led *cfg_; | ||
|
||
StatusLED(const StatusLED &other) = delete; | ||
}; | ||
|
||
} // namespace shelly | ||
|
||
#endif |