Skip to content
Open
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
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"arduino.additionalUrls": [
"http://arduino.esp8266.com/stable/package_esp8266com_index.json"
],
"arduino.defaultBaudRate": 115200
"arduino.defaultBaudRate": 115200,
"files.associations": {
"istream": "cpp",
"streambuf": "cpp"
}
}
14 changes: 14 additions & 0 deletions Esp8266PinBasedLedStripAdapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "Arduino.h"
#include "Esp8266PinBasedLedStripAdapter.h"

Esp8266PinLedStripAdapter::Esp8266PinLedStripAdapter(byte gpioPin)
{
_gpioPin = gpioPin;
pinMode(gpioPin, OUTPUT);
analogWrite(_gpioPin, 0);
}

void Esp8266PinLedStripAdapter::changeLight(byte value)
{
analogWrite(_gpioPin, value * 10);
};
13 changes: 13 additions & 0 deletions Esp8266PinBasedLedStripAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "LedStripAdapter.h"

class Esp8266PinLedStripAdapter : public LedStripAdapter
{
public:
Esp8266PinLedStripAdapter(byte gpioPin);
virtual void changeLight(byte value);

private:
byte _gpioPin;
};
26 changes: 26 additions & 0 deletions LedStrip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "LedStrip.h"

LedStrip::LedStrip() : LedStrip::LedStrip(0, NULL)
{
}

LedStrip::LedStrip(byte initialValue, LedStripAdapter *adapter)
{
_adapter = adapter;
_currentValue = initialValue;
}

void LedStrip::manipulate(byte value, String effect)
{
_toBeValue = value;
_effect = effect;
}

void LedStrip::controlLight()
{
if (_currentValue != _toBeValue)
{
_currentValue = _toBeValue > _currentValue ? _currentValue + 1 : _currentValue - 1;
_adapter->changeLight(_currentValue);
}
}
22 changes: 22 additions & 0 deletions LedStrip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <Arduino.h>
#include "LedStripAdapter.h"

class LedStrip
{

public:
LedStrip();
LedStrip(byte initialValue, LedStripAdapter *adapter);

void manipulate(byte value, String effect);
void controlLight();

private:
volatile byte _toBeValue = 0;
volatile byte _currentValue = 0;
String _effect = "dim";

LedStripAdapter *_adapter;
};
9 changes: 9 additions & 0 deletions LedStripAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <Arduino.h>

class LedStripAdapter
{
public:
virtual void changeLight(byte value) = 0;
};
47 changes: 47 additions & 0 deletions RestPayloadValidator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "RestPayloadValidator.h"

#define ERR_CODE 400
#define SUC_CODE 200

#define JSON_LED "led"
#define JSON_VALUE "value"
#define JSON_EFFECT "effect"

RestPayloadValidator::RestPayloadValidator()
{
}

struct RestPayloadValidationResult RestPayloadValidator::validate(StaticJsonDocument<150> json, DeserializationError error)
{

struct RestPayloadValidationResult result;
result.faulty = false;
result.message = "OK.";
result.httpErrorCode = SUC_CODE;

if (error)
{
result.faulty = true;
result.message = "Cannot parse message.";
result.httpErrorCode = ERR_CODE;
return result;
}

if (json[JSON_LED] != 1 && json[JSON_LED] != 2)
{
result.faulty = true;
result.message = "The led number has to be either 1 or 2.";
result.httpErrorCode = ERR_CODE;
return result;
}

if (json[JSON_VALUE] < 0 || json[JSON_VALUE] > 100)
{
result.faulty = true;
result.message = "The value has to be in range <0,100>.";
result.httpErrorCode = ERR_CODE;
return result;
}

return result;
}
17 changes: 17 additions & 0 deletions RestPayloadValidator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <Arduino.h>
#include <ArduinoJson.h>

class RestPayloadValidator
{
public:
RestPayloadValidator();

struct RestPayloadValidationResult validate(StaticJsonDocument<150> StaticJsonDocument, DeserializationError error);
};

struct RestPayloadValidationResult
{
boolean faulty = false;
String message;
short httpErrorCode;
};
36 changes: 20 additions & 16 deletions controller.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
#include <ArduinoJson.h>
#include "led.h"
#include "controller.h"
#include "RestPayloadValidator.h"

#define PORT 8080
#define ERR_CODE 400
#define SUC_CODE 200
#define PLAIN "plain"
#define APP_JSON "application/json"

#define JSON_LED "led"
#define JSON_VALUE "value"
#define JSON_EFFECT "effect"

ESP8266WebServer server(PORT);
StaticJsonDocument<150> json;
RestPayloadValidator payloadValidator = RestPayloadValidator();

void setupWebServer()
{
Expand All @@ -20,12 +26,11 @@ void setupWebServer()
server.begin();
}

void loopWebServer()
void loopWebServer()
{
server.handleClient();
server.handleClient();
}


void handleRoot()
{
server.send(SUC_CODE, APP_JSON, "{\"app\": \"Wledimir\"}");
Expand All @@ -48,34 +53,33 @@ void handleNotFound()
*/
void handlePost()
{

if (server.hasArg(PLAIN) == false)
{
failWith(ERR_CODE, "Body not received.");
return;
}

auto error = deserializeJson(json, server.arg(PLAIN));
RestPayloadValidationResult validationResult = payloadValidator.validate(json, deserializeJson(json, server.arg(PLAIN)));

if (error)
if (validationResult.faulty)
{
failWith(ERR_CODE, "Cannot parse message.");
failWith(validationResult.httpErrorCode, validationResult.message);
delay(1000);
return;
}

manipulateLed(json["led"], json["value"], json["effect"]);

char message[] = "OK.";
succeedWith(message);
else
{
manipulateLed(json[JSON_LED], json[JSON_VALUE], json[JSON_EFFECT]);
succeedWith(validationResult.message);
}
}

void succeedWith(char message[])
void succeedWith(String message)
{
server.send(SUC_CODE, APP_JSON, "{\"message\": \"" + String(message) + "\"}");
server.send(SUC_CODE, APP_JSON, "{\"message\": \"" + message + "\"}");
}

void failWith(int code, char message[])
void failWith(int code, String message)
{
server.send(code, APP_JSON, "{\"error\": \"" + String(message) + "\"}");
server.send(code, APP_JSON, "{\"error\": \"" + message + "\"}");
}
4 changes: 3 additions & 1 deletion led.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
void manipulateLed(int led, byte value, String effect);
#include <Arduino.h>

void manipulateLed(byte led, byte value, String effect);

void setupLed();

Expand Down
65 changes: 17 additions & 48 deletions led.ino
Original file line number Diff line number Diff line change
@@ -1,70 +1,39 @@
#include <Arduino.h>
#include "LedStrip.h"
#include "LedStripAdapter.h"
#include "Esp8266PinBasedLedStripAdapter.h"

#define led1 0
#define led2 2
#define DELAY 10
#define LED_1_GPIO 0
#define LED_2_GPIO 2

volatile int led1_toBeValue = 0;
volatile int led1_currentValue = 0;
volatile int led2_toBeValue = 0;
volatile int led2_currentValue = 0;
#define DELAY 5

LedStrip ledStrip1;
LedStrip ledStrip2;

void setupLed()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);

analogWrite(led1, 0);
analogWrite(led2, 0);
ledStrip1 = LedStrip((byte)0, new Esp8266PinLedStripAdapter(LED_1_GPIO));
ledStrip2 = LedStrip((byte)0, new Esp8266PinLedStripAdapter(LED_2_GPIO));
}

void loopLed()
{
byte delayMs = 0;

if (led1_currentValue != led1_toBeValue)
{
if (led1_toBeValue > led1_currentValue)
{ // more light
led1_currentValue = led1_currentValue + 1;
}
else
{ // less light
led1_currentValue = led1_currentValue - 1;
}
delayMs = DELAY;
analogWrite(led1, led1_currentValue);
}

if (led2_currentValue != led2_toBeValue)
{
if (led2_toBeValue > led2_currentValue)
{ // more light
led2_currentValue = led2_currentValue + 1;
}
else
{ // less light
led2_currentValue = led2_currentValue - 1;
}
delayMs = DELAY;
analogWrite(led2, led2_currentValue); // analogWrite values from 0 to 255
}
ledStrip1.controlLight();
ledStrip2.controlLight();

delay(delayMs);
delay(DELAY);
}

// value might be from 0 to 100
// led might be eiter 1 or 2
void manipulateLed(int led, byte value, String effect)
void manipulateLed(byte led, byte value, String effect)
{
if (led == 1)
{
led1_toBeValue = value * 10;
ledStrip1.manipulate(value, effect);
}

if (led == 2)
{
led2_toBeValue = value * 10;
ledStrip2.manipulate(value, effect);
}
return;
}
1 change: 0 additions & 1 deletion wledimir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);

setupLed();
Expand Down