Skip to content

Commit

Permalink
开关模块
Browse files Browse the repository at this point in the history
  • Loading branch information
qlwz committed Feb 26, 2020
0 parents commit fc38d3b
Show file tree
Hide file tree
Showing 73 changed files with 7,219 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
/firmware.map
/mklink.bat
/output
67 changes: 67 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < https://docs.platformio.org/page/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < https://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
#
#
# Please choose one of the following templates (proposed below) and uncomment
# it (remove "# " before each line) or use own configuration according to the
# Travis CI documentation (see above).
#


#
# Template #1: General project. Test it using existing `platformio.ini`.
#

# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio run


#
# Template #2: The project is intended to be used as a library with examples.
#

# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# env:
# - PLATFORMIO_CI_SRC=path/to/test/file.c
# - PLATFORMIO_CI_SRC=examples/file.ino
# - PLATFORMIO_CI_SRC=path/to/test/directory
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files.associations": {
"functional": "cpp",
"cmath": "cpp",
"string": "cpp",
"*.tcc": "cpp",
"sstream": "cpp"
}
}
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

Binary file added file/1路开关模块/BOM.xlsx
Binary file not shown.
Binary file added file/1路开关模块/PCB_2020-01-15.zip
Binary file not shown.
Binary file added file/1路开关模块/PCB_反面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/1路开关模块/PCB_正面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/1路开关模块/原理图.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/2路开关模块/BOM.xlsx
Binary file not shown.
Binary file added file/2路开关模块/PCB_2020-01-14.zip
Binary file not shown.
Binary file added file/2路开关模块/PCB_反面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/2路开关模块/PCB_正面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/2路开关模块/原理图.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/3路开关模块/BOM.xlsx
Binary file not shown.
Binary file added file/3路开关模块/PCB_2020-01-14.zip
Binary file not shown.
Binary file added file/3路开关模块/PCB_反面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/3路开关模块/PCB_正面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/3路开关模块/原理图.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/images/LED状态.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/images/tab1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/images/tab2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/images/tab3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/images/tab4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/images/反面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/images/成品.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added file/images/正面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
29 changes: 29 additions & 0 deletions include/RadioReceive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifdef USE_RCSWITCH

// RadioReceive.h
#ifndef _RADIORECEIVE_h
#define _RADIORECEIVE_h

#include "Relay.h"
#include <RCSwitch.h>

class RadioReceive
{
protected:
RCSwitch *mySwitch;
unsigned long lastVaue = 0;
int lastTime = 0;
int studyTime = 0;
Relay *relay;

public:
uint8_t studyCH = 0;
void init(Relay *_relay, uint8_t io);
void study(uint8_t ch);
void del(uint8_t ch);
void delAll();
void loop();
};

#endif
#endif
105 changes: 105 additions & 0 deletions include/Relay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Relay.h
#ifndef _RELAY_h
#define _RELAY_h

#include "Module.h"
#include "RelayConfig.pb.h"
#include "Template.h"

#define MODULE_CFG_VERSION 1001 //1001 - 1500

#define MAX_STUDY_RECEIVER_NUM 10 // 遥控最大学习数

const char HASS_DISCOVER_RELAY[] PROGMEM =
"{\"name\":\"%s_%d\","
"\"cmd_t\":\"%s\","
"\"stat_t\":\"%s\","
"\"pl_off\":\"OFF\","
"\"pl_on\":\"ON\","
"\"avty_t\":\"%s\","
"\"pl_avail\":\"online\","
"\"pl_not_avail\":\"offline\"}";

enum LightColor
{
None,
WhiteColor,
YellowColor,
YellowWhiteColor
};

#ifdef USE_RCSWITCH
class RadioReceive;
#endif
class RelayButton;
class Relay : public Module
{
private:
uint8_t GPIO_PIN[MAX_GPIO_PIN - MIN_FLASH_PINS];
uint8_t operationFlag = 0; // 0每秒

char powerStatTopic[80];
RelayButton *btns;

// PWM
Ticker *ledTicker;
int ledLevel = 0;
int ledLight = 2023;
bool ledUp = true;
bool canLed = true;
void led(uint8_t ch, bool isOn);
void ledPWM(uint8_t ch, bool isOn);
void ledTickerHandle();
bool checkCanLed(bool re = false);

void httpDo(ESP8266WebServer *server);
void httpSetting(ESP8266WebServer *server);
void httpHa(ESP8266WebServer *server);
#ifdef USE_RCSWITCH
void httpRadioReceive(ESP8266WebServer *server);
#endif

void loadModule(uint8_t module);
void reportPower();

#ifdef USE_TRICOLOR
void httpDownlightSetting(ESP8266WebServer *server);
unsigned long colorOffTime = 0;
void colorOnOff(uint8_t ch, bool isOn);
#endif

public:
RelayConfigMessage config;
uint8_t lastState = 0;
uint8_t channels = 0;

#ifdef USE_RCSWITCH
RadioReceive *radioReceive = NULL;
#endif

void init();
String getModuleName() { return F("relay"); }
String getModuleCNName();
String getModuleVersion() { return F("2020.02.26.2100"); }
String getModuleAuthor() { return F("情留メ蚊子"); }
bool moduleLed();

void loop();
void perSecondDo();

void readConfig();
void resetConfig();
void saveConfig(bool isEverySecond);

void mqttCallback(String topicStr, String str);
void mqttConnected();
void mqttDiscovery(bool isEnable = true);

void httpAdd(ESP8266WebServer *server);
void httpHtml(ESP8266WebServer *server);
String httpGetStatus(ESP8266WebServer *server);

void switchRelay(uint8_t ch, bool isOn, bool isSave = true);
};

#endif
41 changes: 41 additions & 0 deletions include/RelayButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// RelayButton.h
#ifndef _RELAYBUTTON_h
#define _RELAYBUTTON_h

#include "Arduino.h"

static const uint8_t DEBOUNCED_STATE = 0b00000001;
static const uint8_t UNSTABLE_STATE = 0b00000010;
//static const uint8_t CHANGED_STATE = 0b00000100;

class RelayButton
{
protected:
uint8_t ch = -1;
uint8_t io = -1;
// 按键
int debounceTime = 50;
unsigned long timingStart = 0;
unsigned long intervalStart;
int switchCount = 0;
bool currentState;

// 等待开关再次切换的时间(以毫秒为单位)。
// 300对我来说效果很好,几乎没有引起注意。 如果您不想使用此功能,请设置为0。
unsigned long specialFunctionTimeout = 300;
int lastTime = 0;

Relay *relay;

uint8_t stateFlag;
inline void setStateFlag(const uint8_t flag) { stateFlag |= flag; }
inline void unsetStateFlag(const uint8_t flag) { stateFlag &= ~flag; }
inline void toggleStateFlag(const uint8_t flag) { stateFlag ^= flag; }
inline bool getStateFlag(const uint8_t flag) { return ((stateFlag & flag) != 0); }

public:
void init(Relay *_relay, uint8_t _ch, uint8_t _io);
void loop();
};

#endif
83 changes: 83 additions & 0 deletions include/RelayConfig.pb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.3.9.4 at Thu Feb 20 15:26:50 2020. */

#ifndef PB_RELAYCONFIG_PB_H_INCLUDED
#define PB_RELAYCONFIG_PB_H_INCLUDED
#include <pb.h>

/* @@protoc_insertion_point(includes) */
#if PB_PROTO_HEADER_VERSION != 30
#error Regenerate this file with the current version of nanopb generator.
#endif

#ifdef __cplusplus
extern "C" {
#endif

/* Struct definitions */
typedef struct _RelayConfigMessage {
uint8_t led_type;
uint16_t led_start;
uint16_t led_end;
uint8_t power_on_state;
uint8_t last_state;
uint32_t study_index[4];
uint32_t study[40];
uint8_t led_light;
uint8_t led_time;
uint8_t downlight_ch;
uint8_t downlight_index;
uint8_t downlight_color[3];
uint8_t downlight_default;
uint16_t downlight_interval;
uint8_t power_mode;
uint8_t module_type;
uint16_t report_interval;
/* @@protoc_insertion_point(struct:RelayConfigMessage) */
} RelayConfigMessage;

/* Default values for struct fields */

/* Initializer values for message structs */
#define RelayConfigMessage_init_default {0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0, 0, 0, {0, 0, 0}, 0, 0, 0, 0, 0}
#define RelayConfigMessage_init_zero {0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0, 0, 0, {0, 0, 0}, 0, 0, 0, 0, 0}

/* Field tags (for use in manual encoding/decoding) */
#define RelayConfigMessage_led_type_tag 1
#define RelayConfigMessage_led_start_tag 2
#define RelayConfigMessage_led_end_tag 3
#define RelayConfigMessage_power_on_state_tag 4
#define RelayConfigMessage_last_state_tag 5
#define RelayConfigMessage_study_index_tag 6
#define RelayConfigMessage_study_tag 7
#define RelayConfigMessage_led_light_tag 8
#define RelayConfigMessage_led_time_tag 9
#define RelayConfigMessage_downlight_ch_tag 10
#define RelayConfigMessage_downlight_index_tag 11
#define RelayConfigMessage_downlight_color_tag 12
#define RelayConfigMessage_downlight_default_tag 13
#define RelayConfigMessage_downlight_interval_tag 14
#define RelayConfigMessage_power_mode_tag 19
#define RelayConfigMessage_module_type_tag 20
#define RelayConfigMessage_report_interval_tag 21

/* Struct field encoding specification for nanopb */
extern const pb_field_t RelayConfigMessage_fields[18];

/* Maximum encoded size of messages (where known) */
#define RelayConfigMessage_size 369

/* Message IDs (where set with "msgid" option) */
#ifdef PB_MSGID

#define RELAYCONFIG_MESSAGES \


#endif

#ifdef __cplusplus
} /* extern "C" */
#endif
/* @@protoc_insertion_point(eof) */

#endif
Loading

0 comments on commit fc38d3b

Please sign in to comment.