Skip to content

Commit

Permalink
Merge pull request #169 from mbehr1/s0_uart_gpio
Browse files Browse the repository at this point in the history
Added S0 support for GPIO
  • Loading branch information
andig committed Jul 6, 2015
2 parents 899bd6c + 17b497d commit bc20ce7
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 55 deletions.
7 changes: 5 additions & 2 deletions etc/vzlogger_generic.schema.json
Expand Up @@ -146,8 +146,11 @@
{ "$ref": "#/definitions/meter" },
{ "properties": {
"protocol": { "type": "string", "enum": ["s0"] },
"device": { "type": "string", "description": "device the meter is connected to. E.g. /dev/ttyUSB0" },
"resolution": { "type": "integer", "default": 1000, "description": "number of impulses per kWh" }
"device": { "type": "string", "description": "UART device the meter is connected to. E.g. /dev/ttyUSB0" },
"gpio": { "type": "integer", "default": -1, "description": "Number of GPIO port to be used. If this is set >-1 then device is ignored." },
"configureGPIO": { "type": "boolean", "default": true, "description": "If true then vzlogger will export, configure,... the GPIO port. Needs proper access rights to /sys/class/gpio/." },
"resolution": { "type": "integer", "default": 1000, "description": "number of impulses per kWh" },
"debounce_delay": { "type": "integer", "default": 30, "description": "Delay in ms until the next edge is detected" }
},
"required": [ "protocol", "device" ]
}
Expand Down
48 changes: 40 additions & 8 deletions include/protocols/MeterS0.hpp
Expand Up @@ -32,6 +32,43 @@

class MeterS0 : public vz::protocol::Protocol {

class HWIF {
public:
virtual ~HWIF() {};
virtual bool _open() = 0;
virtual bool _close() = 0;
virtual bool waitForImpulse() = 0;
};

class HWIF_UART : public HWIF {
public:
HWIF_UART(const std::list<Option> &options);
virtual ~HWIF_UART();

virtual bool _open();
virtual bool _close();
virtual bool waitForImpulse();
protected:
std::string _device;
int _fd; // file descriptor of UART
struct termios _old_tio; // required to reset UART
};

class HWIF_GPIO : public HWIF {
public:
HWIF_GPIO(const std::list<Option> &options);
virtual ~HWIF_GPIO();

virtual bool _open();
virtual bool _close();
virtual bool waitForImpulse();
protected:
int _fd;
int _gpiopin;
bool _configureGPIO; // try export,...
std::string _device;
};

public:
MeterS0(std::list<Option> options);
virtual ~MeterS0();
Expand All @@ -40,18 +77,13 @@ class MeterS0 : public vz::protocol::Protocol {
int close();
ssize_t read(std::vector<Reading> &rds, size_t n);

private:
int _open_socket(const char *node, const char *service);
int _open_device(struct termios *old_tio, speed_t baudrate);

protected:
std::string _device;
HWIF * _hwif;

int _resolution;
int _debounce_delay_ms;
int _counter;

int _fd; // file descriptor of port
struct termios _old_tio; // required to reset port

bool _impulseReceived; // first impulse received
struct timeval _time_last; // timestamp of last impulse
};
Expand Down

0 comments on commit bc20ce7

Please sign in to comment.