Skip to content

Commit

Permalink
🎉 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinroger committed Mar 21, 2017
0 parents commit cb9ae11
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[keywords.txt]
indent_style = tab
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: python
python:
- "2.7"

# Cache PlatformIO packages using Travis CI container-based infrastructure
sudo: false
cache:
directories:
- "~/.platformio"

env:
- PLATFORMIO_CI_SRC=examples/Example/Example.ino

install:
- pip install -U platformio
# install current build as a library with all dependencies
- platformio lib -g install file://.

script:
- platformio ci --board=esp01
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Marvin Roger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Arduino Sonoff Dual

[![Build Status](https://travis-ci.org/marvinroger/arduino-sonoff-dual.svg?branch=master)](https://travis-ci.org/marvinroger/arduino-sonoff-dual)

This Arduino library provides helpers for a custom Sonoff Dual firmware

## Features

* Control the LED
* Control the relays
* Detect short press and long press of the hardware button

## Installation

1. Download the [latest version](https://github.com/marvinroger/arduino-sonoff-dual/archive/master.zip)
2. Load the `.zip` with **Sketch → Include Library → Add .ZIP Library**

## API

See examples folder for examples.

#### SonoffDualClass ()

Constructor. You don't have to instantiate an instance of the class, use the `SonoffDual` instance directly.

#### void .setup ()

Setup the Serial connection, turn the LED off and set both relays off.

#### SonoffDualButton .handleButton ()

Return `SonoffDualButton::NONE` if nothing happened, or `SonoffDualButton::SHORT` or `SonoffDualButton::LONG`.

#### void .setLed (bool `on`)

Set the state of the LED.

* **`on`**: should the LED be on?

#### bool .setRelays (bool `first`, bool `second`)

Set the state of the relays.

* **`first`**: should the first relay be on?
* **`second`**: should the second relay be on?
54 changes: 54 additions & 0 deletions SonoffDual.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "SonoffDual.h"

using namespace SonoffDualInternal;

SonoffDualClass::SonoffDualClass() {
}

void SonoffDualClass::setup() {
Serial.begin(19200);

pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);

setRelays(false, false);
setLed(false);
}

SonoffDualButton SonoffDualClass::handleButton() {
if (Serial.available() >= 4) {
uint8_t values[4] = {0, 0, 0, 0};
values[0] = Serial.read();
values[1] = Serial.read();
values[2] = Serial.read();
values[3] = Serial.read();
if (values[0] != 0xA0 || values[3] != 0xA1) return SonoffDualButton::NONE;
uint16_t buttonCode = (values[1] << 8) | values[2];
if (buttonCode == 0xF500) {
return SonoffDualButton::LONG;
} else {
return SonoffDualButton::SHORT;
}
}

return SonoffDualButton::NONE;
}

void SonoffDualClass::setLed(bool on) {
digitalWrite(LED_PIN, on ? LOW : HIGH);
}

void SonoffDualClass::setRelays(bool first, bool second) {
uint8_t state = 0;

state ^= ((first ? 1 : 0) << 0);
state ^= ((second ? 1 : 0) << 1);

Serial.write(0xA0);
Serial.write(0x04);
Serial.write(state);
Serial.write(0xA1);
Serial.flush();
}

SonoffDualClass SonoffDual;
27 changes: 27 additions & 0 deletions SonoffDual.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef SonoffDual_h
#define SonoffDual_h

#include <Arduino.h>

enum class SonoffDualButton : uint8_t {
NONE,
SHORT,
LONG
};

namespace SonoffDualInternal {
const uint8_t LED_PIN = 13;

class SonoffDualClass {
public:
SonoffDualClass();
void setup();
::SonoffDualButton handleButton();
void setLed(bool on);
void setRelays(bool first, bool second);
};
}

extern SonoffDualInternal::SonoffDualClass SonoffDual;

#endif
17 changes: 17 additions & 0 deletions examples/Example/Example.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <SonoffDual.h>

void setup() {
SonoffDual.setup();
}

bool state = false;

void loop() {
SonoffDualButton buttonState = SonoffDual.handleButton();
if (buttonState == SonoffDualButton::NONE) return;

state = !state;

SonoffDual.setLed(state);
SonoffDual.setRelays(state, state);
}
24 changes: 24 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#######################################
# Datatypes (KEYWORD1)
#######################################

SonoffDual KEYWORD1

SonoffDualButton KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

setup KEYWORD2
handleButton KEYWORD2
setLed KEYWORD2
setRelays KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

NONE LITERAL1
SHORT LITERAL1
LONG LITERAL1
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=SonoffDual
version=1.0.0
author=Marvin ROGER
maintainer=Marvin ROGER
sentence=Provides helpers for a custom Sonoff Dual firmware
paragraph=
category=Device Control
url=https://github.com/marvinroger/arduino-sonoff-dual
architectures=esp8266
includes=SonoffDual.h

0 comments on commit cb9ae11

Please sign in to comment.