Skip to content

Commit

Permalink
Merge pull request #1 from atc1441/development
Browse files Browse the repository at this point in the history
Release Version 1.0 of NETSGPClient
  • Loading branch information
atc1441 authored May 30, 2021
2 parents e8bfe63 + 5321ec3 commit 4218906
Show file tree
Hide file tree
Showing 11 changed files with 720 additions and 166 deletions.
58 changes: 58 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
# Based on Webkit style
BasedOnStyle: Webkit
IndentWidth: 4
ColumnLimit: 120
---
Language: Cpp
Standard: Cpp11
# Pointers aligned to the left
DerivePointerAlignment: false
PointerAlignment: Left
AccessModifierOffset: -4
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakTemplateDeclarations: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakConstructorInitializers: BeforeColon
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
Cpp11BracedListStyle: true
FixNamespaceComments: true
IncludeBlocks: Regroup
IncludeCategories:
# C++ standard headers (no .h)
- Regex: '<[[:alnum:]_-]+>'
Priority: 1
# Extenal libraries (with .h)
- Regex: '<[[:alnum:]_./-]+>'
Priority: 2
# Headers from same folder
- Regex: '"[[:alnum:]_.-]+"'
Priority: 3
# Headers from other folders
- Regex: '"[[:alnum:]_/.-]+"'
Priority: 4
IndentCaseLabels: false
NamespaceIndentation: All
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterTemplateKeyword: true
SpacesInAngles: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
UseTab: Never
156 changes: 0 additions & 156 deletions ArduinoPollDemo/ArduinoPollDemo.ino

This file was deleted.

36 changes: 36 additions & 0 deletions AsyncNETSGPClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <Arduino.h>

#include "AsyncNETSGPClient.h"

AsyncNETSGPClient::AsyncNETSGPClient(Stream& stream, const uint8_t progPin, const uint8_t interval)
: NETSGPClient(stream, progPin), mIntervalMS(1000 * interval)
{ }

void AsyncNETSGPClient::update()
{
const uint32_t currentMillis = millis();

// Send comands at mIntervalMS
if (currentMillis - mLastUpdateMS >= mIntervalMS)
{
mLastUpdateMS = currentMillis;

for (const uint32_t deviceID : mDevices)
{
sendCommand(Command::STATUS, 0x00, deviceID);
}
}

// Check for answers
while (mStream.available() >= 27)
{
// Search for a read status message
if (findAndReadStatusMessage() && mCallback)
{
InverterStatus status;
fillInverterStatusFromBuffer(&mBuffer[0], status);
status.valid = true; // TODO maybe use checksum for this
mCallback(status);
}
}
}
46 changes: 46 additions & 0 deletions AsyncNETSGPClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <set>

#include "NETSGPClient.h"

/// @brief Async version of NETSGPClient
class AsyncNETSGPClient : public NETSGPClient
{
/// @brief Callback function type definition for inverter status updates
typedef void (*InverterStatusCallback)(const NETSGPClient::InverterStatus&);

public:
/// @brief Construct a new AsyncNETSGPClient object.
///
/// @param stream Stream to communicate with the RF module
/// @param progPin Programming enable pin of RF module (active low)
/// @param interval The update interval in seconds, default is 2 seconds
AsyncNETSGPClient(Stream& stream, const uint8_t progPin, const uint8_t interval = 2);

/// @brief Set the callback for inverter status updates
///
/// @param callback Callback that gets called on updates, may be nullptr
void setStatusCallback(InverterStatusCallback callback) { mCallback = callback; }

/// @brief Register a new inverter to receive status updates
///
/// @param deviceID The device identifier of the inverter
void registerInverter(const uint32_t deviceID) { mDevices.insert(deviceID); }

/// @brief Deregister an inverter to not receive status updates
///
/// @param deviceID The device identifier of the inverter
void deregisterInverter(const uint32_t deviceID) { mDevices.erase(deviceID); }

/// @brief Update the internal state
///
/// @note Needs to be called inside loop()
void update();

private:
uint16_t mIntervalMS; /// Update interval in milliseconds
uint32_t mLastUpdateMS; /// Last update time in milliseconds
std::set<uint32_t> mDevices; /// All devices to poll
InverterStatusCallback mCallback = nullptr; /// Callback for status updates
};
Loading

0 comments on commit 4218906

Please sign in to comment.