fuckit: clean slate

This commit is contained in:
Vasily Davydov
2022-10-24 12:16:38 +03:00
parent e2cbbf4322
commit 84e50cdec7
95 changed files with 280 additions and 3791 deletions

View File

@@ -0,0 +1,43 @@
/*
* DigitalIoPin.h
*
* Created on: Aug 29, 2022
* Author: Vasily Davydov
*/
#ifndef DIGITALIOPIN_H_
#define DIGITALIOPIN_H_
#define UINT8_MAX_VALUE 255
#include <assert.h>
#include <chip.h>
typedef struct DigitalIOConfigStruct
{
uint8_t _port;
uint8_t _pin;
bool _input;
bool _pullup;
bool _invert;
uint32_t IOCON_mode;
uint32_t IOCON_inv;
uint32_t DigitalEn;
} DigitalIOConfigStruct;
class DigitalIoPin
{
public:
DigitalIoPin (int port, int pin, bool input = true, bool pullup = true,
bool invert = false);
DigitalIoPin (const DigitalIoPin &) = delete;
virtual ~DigitalIoPin ();
bool read ();
void write (bool value);
private:
DigitalIOConfigStruct _io = { 0, 0, false, false, false, 0, 0, IOCON_DIGMODE_EN};
void setIoPin ();
};
#endif /* DIGITALIOPIN_H_ */

View File

@@ -0,0 +1,27 @@
/*
* GMP252.h
*
* Created on: 20 Oct 2022
* Author: evgenymeshcheryakov
*/
#ifndef GMP252_H_
#define GMP252_H_
#include "Modbus/ModbusMaster.h"
#include "Modbus/ModbusRegister.h"
class GMP252
{
public:
GMP252 ();
int read ();
virtual ~GMP252 ();
private:
ModbusMaster sens;
ModbusRegister regInt;
ModbusRegister regFloat;
};
#endif /* GMP252_H_ */

29
esp-vent-main/inc/HMP60.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* HMP60.h
*
* Created on: 20 Oct 2022
* Author: evgenymeshcheryakov
*/
#ifndef HMP60_H_
#define HMP60_H_
#include "Modbus/ModbusMaster.h"
#include "Modbus/ModbusRegister.h"
class HMP60
{
public:
HMP60 ();
int readRH ();
int readT ();
virtual ~HMP60 ();
private:
ModbusMaster sens;
ModbusRegister regRHint;
ModbusRegister regRHfloat;
ModbusRegister regTint;
};
#endif /* HMP60_H_ */

33
esp-vent-main/inc/I2C.h Normal file
View File

@@ -0,0 +1,33 @@
/*
* I2C.h
*
* Created on: 21.2.2016
* Author: krl
*/
#ifndef I2C_H_
#define I2C_H_
#include "chip.h"
struct I2C_config {
unsigned int device_number;
unsigned int speed;
unsigned int clock_divider;
unsigned int i2c_mode;
I2C_config(): device_number(0), speed(100000), clock_divider(40), i2c_mode(IOCON_SFI2C_EN) {};
};
class I2C {
public:
I2C(const I2C_config &cfg);
virtual ~I2C();
bool transaction(uint8_t devAddr, uint8_t *txBuffPtr, uint16_t txSize, uint8_t *rxBuffPtr, uint16_t rxSize);
bool write(uint8_t devAddr, uint8_t *txBuffPtr, uint16_t txSize);
bool read(uint8_t devAddr, uint8_t *rxBuffPtr, uint16_t rxSize);
private:
LPC_I2C_T *device;
static uint32_t I2CM_XferBlocking(LPC_I2C_T *pI2C, I2CM_XFER_T *xfer);
};
#endif /* I2C_H_ */

View File

@@ -0,0 +1,98 @@
#ifndef LiquidCrystal_h
#define LiquidCrystal_h
#include "DigitalIoPin.h"
#include "chip.h"
#include <cstddef>
#include <string>
// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
class LiquidCrystal
{
public:
LiquidCrystal (DigitalIoPin *rs, DigitalIoPin *enable, DigitalIoPin *d0,
DigitalIoPin *d1, DigitalIoPin *d2, DigitalIoPin *d3);
void begin (uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
void clear ();
void home ();
void noDisplay ();
void display ();
void noBlink ();
void blink ();
void noCursor ();
void cursor ();
void scrollDisplayLeft ();
void scrollDisplayRight ();
void leftToRight ();
void rightToLeft ();
void autoscroll ();
void noAutoscroll ();
void createChar (uint8_t, uint8_t[]);
void setCursor (uint8_t, uint8_t);
virtual size_t write (uint8_t);
void command (uint8_t);
void print (std::string const &s);
void print (const char *s);
private:
void send (uint8_t, uint8_t);
void write4bits (uint8_t);
void pulseEnable ();
DigitalIoPin *rs_pin; // LOW(false): command. HIGH(true): character.
DigitalIoPin *enable_pin; // activated by a HIGH pulse.
DigitalIoPin *data_pins[4];
uint8_t _displayfunction;
uint8_t _displaycontrol;
uint8_t _displaymode;
uint8_t _initialized;
uint8_t _numlines, _currline;
uint8_t rows, col;
};
#endif

View File

@@ -0,0 +1,282 @@
/**
@file
Arduino library for communicating with Modbus slaves over RS232/485 (via RTU protocol).
@defgroup setup ModbusMaster Object Instantiation/Initialization
@defgroup buffer ModbusMaster Buffer Management
@defgroup discrete Modbus Function Codes for Discrete Coils/Inputs
@defgroup register Modbus Function Codes for Holding/Input Registers
@defgroup constant Modbus Function Codes, Exception Codes
*/
/*
ModbusMaster.h - Arduino library for communicating with Modbus slaves
over RS232/485 (via RTU protocol).
This file is part of ModbusMaster.
ModbusMaster is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ModbusMaster is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ModbusMaster. If not, see <http://www.gnu.org/licenses/>.
Written by Doc Walker (Rx)
Copyright © 2009-2013 Doc Walker <4-20ma at wvfans dot net>
*/
#ifndef ModbusMaster_h
#define ModbusMaster_h
/**
@def __MODBUSMASTER_DEBUG__ (1).
Set to 1 to enable debugging features within class:
- pin 4 cycles for each byte read in the Modbus response
- pin 5 cycles for each millisecond timeout during the Modbus response
*/
#define __MODBUSMASTER_DEBUG__ (0)
/* _____STANDARD INCLUDES____________________________________________________ */
// include types & constants of Wiring core API
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
//#include "WProgram.h"
#include <stdint.h>
#include <cstddef>
#endif
uint32_t millis();
#define BYTE 0xA5
/* _____UTILITY MACROS_______________________________________________________ */
/* _____PROJECT INCLUDES_____________________________________________________ */
// functions to calculate Modbus Application Data Unit CRC
//#include "util/crc16.h"
// moved inlcuding crc16.h to ModbusMaster.cpp
// functions to manipulate words
///#include "util/word.h"
#include "word.h"
#include "SerialPort.h"
/* _____CLASS DEFINITIONS____________________________________________________ */
/**
Arduino class library for communicating with Modbus slaves over
RS232/485 (via RTU protocol).
*/
class ModbusMaster
{
public:
ModbusMaster();
ModbusMaster(uint8_t);
ModbusMaster(uint8_t, uint8_t);
void begin();
void begin(uint16_t);
void idle(void (*)());
// Modbus exception codes
/**
Modbus protocol illegal function exception.
The function code received in the query is not an allowable action for
the server (or slave). This may be because the function code is only
applicable to newer devices, and was not implemented in the unit
selected. It could also indicate that the server (or slave) is in the
wrong state to process a request of this type, for example because it is
unconfigured and is being asked to return register values.
@ingroup constant
*/
static const uint8_t ku8MBIllegalFunction = 0x01;
/**
Modbus protocol illegal data address exception.
The data address received in the query is not an allowable address for
the server (or slave). More specifically, the combination of reference
number and transfer length is invalid. For a controller with 100
registers, the ADU addresses the first register as 0, and the last one
as 99. If a request is submitted with a starting register address of 96
and a quantity of registers of 4, then this request will successfully
operate (address-wise at least) on registers 96, 97, 98, 99. If a
request is submitted with a starting register address of 96 and a
quantity of registers of 5, then this request will fail with Exception
Code 0x02 "Illegal Data Address" since it attempts to operate on
registers 96, 97, 98, 99 and 100, and there is no register with address
100.
@ingroup constant
*/
static const uint8_t ku8MBIllegalDataAddress = 0x02;
/**
Modbus protocol illegal data value exception.
A value contained in the query data field is not an allowable value for
server (or slave). This indicates a fault in the structure of the
remainder of a complex request, such as that the implied length is
incorrect. It specifically does NOT mean that a data item submitted for
storage in a register has a value outside the expectation of the
application program, since the MODBUS protocol is unaware of the
significance of any particular value of any particular register.
@ingroup constant
*/
static const uint8_t ku8MBIllegalDataValue = 0x03;
/**
Modbus protocol slave device failure exception.
An unrecoverable error occurred while the server (or slave) was
attempting to perform the requested action.
@ingroup constant
*/
static const uint8_t ku8MBSlaveDeviceFailure = 0x04;
// Class-defined success/exception codes
/**
ModbusMaster success.
Modbus transaction was successful; the following checks were valid:
- slave ID
- function code
- response code
- data
- CRC
@ingroup constant
*/
static const uint8_t ku8MBSuccess = 0x00;
/**
ModbusMaster invalid response slave ID exception.
The slave ID in the response does not match that of the request.
@ingroup constant
*/
static const uint8_t ku8MBInvalidSlaveID = 0xE0;
/**
ModbusMaster invalid response function exception.
The function code in the response does not match that of the request.
@ingroup constant
*/
static const uint8_t ku8MBInvalidFunction = 0xE1;
/**
ModbusMaster response timed out exception.
The entire response was not received within the timeout period,
ModbusMaster::ku8MBResponseTimeout.
@ingroup constant
*/
static const uint8_t ku8MBResponseTimedOut = 0xE2;
/**
ModbusMaster invalid response CRC exception.
The CRC in the response does not match the one calculated.
@ingroup constant
*/
static const uint8_t ku8MBInvalidCRC = 0xE3;
uint16_t getResponseBuffer(uint8_t);
void clearResponseBuffer();
uint8_t setTransmitBuffer(uint8_t, uint16_t);
void clearTransmitBuffer();
void beginTransmission(uint16_t);
uint8_t requestFrom(uint16_t, uint16_t);
void sendBit(bool);
void send(uint8_t);
void send(uint16_t);
void send(uint32_t);
uint8_t available(void);
uint16_t receive(void);
uint8_t readCoils(uint16_t, uint16_t);
uint8_t readDiscreteInputs(uint16_t, uint16_t);
uint8_t readHoldingRegisters(uint16_t, uint16_t);
uint8_t readInputRegisters(uint16_t, uint8_t);
uint8_t writeSingleCoil(uint16_t, uint8_t);
uint8_t writeSingleRegister(uint16_t, uint16_t);
uint8_t writeMultipleCoils(uint16_t, uint16_t);
uint8_t writeMultipleCoils();
uint8_t writeMultipleRegisters(uint16_t, uint16_t);
uint8_t writeMultipleRegisters();
uint8_t maskWriteRegister(uint16_t, uint16_t, uint16_t);
uint8_t readWriteMultipleRegisters(uint16_t, uint16_t, uint16_t, uint16_t);
uint8_t readWriteMultipleRegisters(uint16_t, uint16_t);
private:
uint8_t _u8SerialPort; ///< serial port (0..3) initialized in constructor
uint8_t _u8MBSlave; ///< Modbus slave (1..255) initialized in constructor
uint16_t _u16BaudRate; ///< baud rate (300..115200) initialized in begin()
static const uint8_t ku8MaxBufferSize = 64; ///< size of response/transmit buffers
uint16_t _u16ReadAddress; ///< slave register from which to read
uint16_t _u16ReadQty; ///< quantity of words to read
uint16_t _u16ResponseBuffer[ku8MaxBufferSize]; ///< buffer to store Modbus slave response; read via GetResponseBuffer()
uint16_t _u16WriteAddress; ///< slave register to which to write
uint16_t _u16WriteQty; ///< quantity of words to write
uint16_t _u16TransmitBuffer[ku8MaxBufferSize]; ///< buffer containing data to transmit to Modbus slave; set via SetTransmitBuffer()
uint16_t* txBuffer; // from Wire.h -- need to clean this up Rx
uint8_t _u8TransmitBufferIndex;
uint16_t u16TransmitBufferLength;
uint16_t* rxBuffer; // from Wire.h -- need to clean this up Rx
uint8_t _u8ResponseBufferIndex;
uint8_t _u8ResponseBufferLength;
// Modbus function codes for bit access
static const uint8_t ku8MBReadCoils = 0x01; ///< Modbus function 0x01 Read Coils
static const uint8_t ku8MBReadDiscreteInputs = 0x02; ///< Modbus function 0x02 Read Discrete Inputs
static const uint8_t ku8MBWriteSingleCoil = 0x05; ///< Modbus function 0x05 Write Single Coil
static const uint8_t ku8MBWriteMultipleCoils = 0x0F; ///< Modbus function 0x0F Write Multiple Coils
// Modbus function codes for 16 bit access
static const uint8_t ku8MBReadHoldingRegisters = 0x03; ///< Modbus function 0x03 Read Holding Registers
static const uint8_t ku8MBReadInputRegisters = 0x04; ///< Modbus function 0x04 Read Input Registers
static const uint8_t ku8MBWriteSingleRegister = 0x06; ///< Modbus function 0x06 Write Single Register
static const uint8_t ku8MBWriteMultipleRegisters = 0x10; ///< Modbus function 0x10 Write Multiple Registers
static const uint8_t ku8MBMaskWriteRegister = 0x16; ///< Modbus function 0x16 Mask Write Register
static const uint8_t ku8MBReadWriteMultipleRegisters = 0x17; ///< Modbus function 0x17 Read Write Multiple Registers
// Modbus timeout [milliseconds]
static const uint16_t ku16MBResponseTimeout = 2000; ///< Modbus timeout [milliseconds]
// master function that conducts Modbus transactions
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction);
// idle callback function; gets called during idle time between TX and RX
void (*_idle)();
SerialPort *MBSerial = NULL; // added by KRL
};
#endif
/**
@example examples/Basic/Basic.pde
@example examples/PhoenixContact_nanoLC/PhoenixContact_nanoLC.pde
*/

View File

@@ -0,0 +1,19 @@
#ifndef MODBUSREGISTER_H_
#define MODBUSREGISTER_H_
#include "ModbusMaster.h"
class ModbusRegister {
public:
ModbusRegister(ModbusMaster *master, int address, bool holdingRegister = true);
ModbusRegister(const ModbusRegister &) = delete;
virtual ~ModbusRegister();
int read();
void write(int value);
private:
ModbusMaster *m;
int addr;
bool hr;
};
#endif /* MODBUSREGISTER_H_ */

View File

@@ -0,0 +1,20 @@
#ifndef SERIALPORT_H_
#define SERIALPORT_H_
#include "Uart.h"
class SerialPort {
public:
SerialPort();
virtual ~SerialPort();
int available();
void begin(int speed = 9600);
int read();
int write(const char* buf, int len);
int print(int val, int format);
void flush();
private:
static LpcUart *u;
};
#endif /* SERIALPORT_H_ */

View File

@@ -0,0 +1,54 @@
#ifndef LPCUART_H_
#define LPCUART_H_
#include "chip.h"
struct LpcPinMap {
int port; /* set to -1 to indicate unused pin */
int pin; /* set to -1 to indicate unused pin */
};
struct LpcUartConfig {
LPC_USART_T *pUART;
uint32_t speed;
uint32_t data;
bool rs485;
LpcPinMap tx;
LpcPinMap rx;
LpcPinMap rts; /* used as output enable if RS-485 mode is enabled */
LpcPinMap cts;
};
class LpcUart {
public:
LpcUart(const LpcUartConfig &cfg);
LpcUart(const LpcUart &) = delete;
virtual ~LpcUart();
int free(); /* get amount of free space in transmit buffer */
int peek(); /* get number of received characters in receive buffer */
int write(char c);
int write(const char *s);
int write(const char *buffer, int len);
int read(char &c); /* get a single character. Returns number of characters read --> returns 0 if no character is available */
int read(char *buffer, int len);
void txbreak(bool brk); /* set break signal on */
bool rxbreak(); /* check if break is received */
void speed(int bps); /* change transmission speed */
bool txempty();
void isr(); /* ISR handler. This will be called by the HW ISR handler. Do not call from application */
private:
LPC_USART_T *uart;
IRQn_Type irqn;
/* currently we support only fixed size ring buffers */
static const int UART_RB_SIZE = 256;
/* Transmit and receive ring buffers */
RINGBUFF_T txring;
RINGBUFF_T rxring;
uint8_t rxbuff[UART_RB_SIZE];
uint8_t txbuff[UART_RB_SIZE];
static bool init; /* set when first UART is initialized. We have a global clock setting for all UARTSs */
};
#endif /* LPCUART_H_ */

View File

@@ -0,0 +1,86 @@
/**
@file
CRC Computations
@defgroup util_crc16 "util/crc16.h": CRC Computations
@code#include "util/crc16.h"@endcode
This header file provides functions for calculating
cyclic redundancy checks (CRC) using common polynomials.
Modified by Doc Walker to be processor-independent (removed inline
assembler to allow it to compile on SAM3X8E processors).
@par References:
Jack Crenshaw's "Implementing CRCs" article in the January 1992 issue of @e
Embedded @e Systems @e Programming. This may be difficult to find, but it
explains CRC's in very clear and concise terms. Well worth the effort to
obtain a copy.
*/
/* Copyright (c) 2002, 2003, 2004 Marek Michalkiewicz
Copyright (c) 2005, 2007 Joerg Wunsch
Copyright (c) 2013 Dave Hylands
Copyright (c) 2013 Frederic Nadeau
Copyright (c) 2015 Doc Walker
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
#ifndef _UTIL_CRC16_H_
#define _UTIL_CRC16_H_
/** @ingroup util_crc16
Processor-independent CRC-16 calculation.
Polynomial: x^16 + x^15 + x^2 + 1 (0xA001)<br>
Initial value: 0xFFFF
This CRC is normally used in disk-drive controllers.
@param uint16_t crc (0x0000..0xFFFF)
@param uint8_t a (0x00..0xFF)
@return calculated CRC (0x0000..0xFFFF)
*/
static uint16_t
crc16_update (uint16_t crc, uint8_t a)
{
int i;
crc ^= a;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = (crc >> 1);
}
return crc;
}
#endif /* _UTIL_CRC16_H_ */

View File

@@ -0,0 +1,98 @@
/**
@file
Utility Functions for Manipulating Words
@defgroup util_word "util/word.h": Utility Functions for Manipulating Words
@code#include "util/word.h"@endcode
This header file provides utility functions for manipulating words.
*/
/*
word.h - Utility Functions for Manipulating Words
This file is part of ModbusMaster.
ModbusMaster is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ModbusMaster is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ModbusMaster. If not, see <http://www.gnu.org/licenses/>.
Written by Doc Walker (Rx)
Copyright © 2009-2015 Doc Walker <4-20ma at wvfans dot net>
*/
#ifndef _UTIL_WORD_H_
#define _UTIL_WORD_H_
/** @ingroup util_word
Return low word of a 32-bit integer.
@param uint32_t ww (0x00000000..0xFFFFFFFF)
@return low word of input (0x0000..0xFFFF)
*/
static inline uint16_t lowWord(uint32_t ww)
{
return (uint16_t) ((ww) & 0xFFFF);
}
/** @ingroup util_word
Return high word of a 32-bit integer.
@param uint32_t ww (0x00000000..0xFFFFFFFF)
@return high word of input (0x0000..0xFFFF)
*/
static inline uint16_t highWord(uint32_t ww)
{
return (uint16_t) ((ww) >> 16);
}
/* utility functions for porting ModbusMaster to LPCXpresso
* added by krl
*/
static inline uint16_t word(uint8_t ww)
{
return (uint16_t) (ww);
}
static inline uint16_t word(uint8_t h, uint8_t l)
{
return (uint16_t) ((h << 8) | l);
}
static inline uint8_t highByte(uint16_t v)
{
return (uint8_t) ((v >> 8) & 0xFF);
}
static inline uint16_t lowByte(uint16_t v)
{
return (uint8_t) (v & 0xFF);
}
static inline uint8_t bitRead(uint8_t v, uint8_t n)
{
return (uint8_t) (v & (1 << n) ? 1 : 0);
}
static inline void bitWrite(uint16_t& v, uint8_t n, uint8_t b)
{
if(b) v = v | (1 << n);
else v = v & ~(1 << n);
}
#endif /* _UTIL_WORD_H_ */

View File

@@ -0,0 +1,48 @@
/*
* PressureWrapper.h
*
* Created on: 5 Oct 2022
* Author: evgenymeshcheryakov
*/
#ifndef PRESSUREWRAPPER_H_
#define PRESSUREWRAPPER_H_
#include "I2C.h"
#include <cstdio>
#define ADDRESS 0x40
/**
* @brief structure to hold a raw data from
* the pressure sensor
*/
typedef struct _PRESSURE{
uint8_t rBuffer[2];
uint8_t crc;
}PRESSURE_DATA;
class PressureWrapper
{
public:
PressureWrapper ();
/*
* @return pressure in Pascal
*/
int getPressure ();
virtual ~PressureWrapper ();
private:
I2C *i2c;
PRESSURE_DATA data = {{0, 0}, 0};
/*
* @return struct with pressure data in
* rBuffer and CRC check in crc
*/
bool getRawPressure ();
};
#endif /* PRESSUREWRAPPER_H_ */

View File

@@ -0,0 +1,27 @@
/*
* Counter.h
*
* Created on: Sep 1, 2022
* Author: tylen
*/
#ifndef COUNTER_H_
#define COUNTER_H_
class Counter
{
public:
Counter (unsigned int i, unsigned int up);
void inc ();
void dec ();
unsigned int getCurrent ();
void setInit (unsigned int i);
~Counter () = default;
private:
unsigned int init;
unsigned int up_lim;
unsigned int down_lim;
};
#endif /* COUNTER_H_ */

View File

@@ -0,0 +1,33 @@
/*
* Event.h
*
* Created on: Oct 5, 2022
* Author: tylen
*/
#ifndef EVENT_H_
#define EVENT_H_
class Event
{
public:
virtual ~Event (){};
enum eventType
{
/** Start of the event */
eEnter,
/** End of the event*/
eExit,
/** Button toggle event type (has values:
* temperature or button) */
eKey,
/** Time event */
eTick
};
Event (eventType e = eTick, int val = 0) : type (e), value (val){};
eventType type;
int value;
};
#endif /* EVENT_H_ */

View File

@@ -0,0 +1,195 @@
/*
* StateHandler.h
*
* Created on: Sep 21, 2022
* Author: tylen
*
* Purpose of this class is to store and pass
* the events of the current mode to further process.
*
* Current goal is to make it to operate on interrupts
* caused by button presses.
*
*/
#ifndef STATE_HANDLER_H_
#define STATE_HANDLER_H_
#include "Counter.h"
#include "DigitalIoPin.h"
#include "Event.h"
#include "GMP252.h"
#include "HMP60.h"
#include "LiquidCrystal.h"
#include "Modbus/ModbusMaster.h"
#include "Modbus/ModbusRegister.h"
#include "PressureWrapper.h"
#include "Timer.h"
/** Buttons enumeration
*
* Current switch state is being passed
* from main to StateHandler through
* a keyEvent. Enumeration determines the state
* of the particular button.
* */
enum _buttons
{
/** Raises the bar up */
BUTTON_CONTROL_UP,
/** Raises the bar down */
BUTTON_CONTROL_DOWN,
/** Toggles the mode between auto and
* manual, which changes the state */
BUTTON_CONTROL_TOG_MODE,
/** Optional button to toggle the
* activation of the current setting.
* Not compulsory to be used. */
BUTTON_CONTROL_TOG_ACTIVE
};
enum _bars
{
/** 0-100 % */
FAN_SPEED,
/** 0-120 Pa */
PRESSURE
};
enum _mode
{
MANUAL,
AUTO
};
enum _sensors
{
PRESSUREDAT,
TEMPERATURE,
HUMIDITY,
CO2
};
class StateHandler;
typedef void (StateHandler::*state_pointer) (const Event &);
class StateHandler
{
public:
StateHandler (LiquidCrystal *lcd, ModbusRegister *A01,
PressureWrapper *pressure, Timer *global);
virtual ~StateHandler ();
/** Get currently set pressure
*
* @return pressure in range of 0-120
*/
unsigned int getSetPressure ();
/** Get currently set FanSpeed
*
* @return speed in range of 0-100
*/
unsigned int getSetSpeed ();
/** Display values on LCD depending on current mode
*
* MANUAL MODE: SPEED: XX% PRESSURE: XXPa
*
* AUTO MODE: P. SET: XXPa P. CURR: XXPa
*
* @param value1 value to be displayed on LCD line 0
* @param value2 value to be displayed on LCD line 1
*/
void displaySet (unsigned int value1, unsigned int value2);
/** Handle the given event of the current state
*
* @param event event to be handled in the current state
*/
void HandleState (const Event &event);
private:
state_pointer current;
/** Set a new curremt state
* @param newstate new state to be set to current
*/
void SetState (state_pointer newstate);
bool current_mode;
Counter value[2] = { { 0, 100 }, { 0, 120 } };
/* motor of fan starts at value 90. probably because of some
* weigh of fan, so voltage within range of 0-89 is not
* sufficient to start motor.
* TODO: Value 89 should be scaled to 0 at some point */
Counter fan_speed = { 20, 1000 };
/*integral controller for PID. should be global, since it
* accumulates error signals encountered since startup*/
int integral = 0;
int saved_set_value[2] = { 0, 0 };
int saved_curr_value[2] = { 0, 0 };
int sensors_data[4] = { 0 };
LiquidCrystal *_lcd;
ModbusRegister *A01;
PressureWrapper *pressure;
Timer *state_timer;
/* CO2 sensor object */
GMP252 co2;
/* Humidity and temperature sensor object */
HMP60 humidity;
/** Initialization state
*
* @param event event of the state
*/
void stateInit (const Event &event);
/** Manual state
*
* - set current speed
* - print current pressure
*
* @param event event of the state
*/
void stateManual (const Event &event);
/** Automated state
*
* - print current pressure
* - print set pressure
* - inc/dec fan speed
*
* @param event
*/
void stateAuto (const Event &event);
/** Sensors state
*
* - print current sensrs readings
*
* @param event
*/
void stateSensors (const Event &event);
/** Handle button presses
*
* @param button current button
*/
void handleControlButtons (uint8_t button);
/** Save values to class' varibales
*
* @param eventValue value coming from an event
* @param counterValue value of the inner Counter
* @param mode current mode
*/
void save (int eventValue, size_t mode);
/** Calculates pid for fan control value
*
*/
void pid ();
};
#endif /* STATE_HANDLER_H_ */

View File

@@ -0,0 +1,35 @@
/*
* SwitchController.h
*
* Created on: Oct 17, 2022
* Author: tylen
*/
#ifndef SWITCHCONTROLLER_H_
#define SWITCHCONTROLLER_H_
#include "DigitalIoPin.h"
#include "StateHandler/StateHandler.h"
#include "Timer.h"
class SwitchController
{
public:
SwitchController (DigitalIoPin *button, Timer *timer, StateHandler *handler,
int button_mode);
virtual ~SwitchController ();
/** Listen to switch button
*/
void listen ();
private:
DigitalIoPin *b;
Timer *t;
StateHandler *h;
bool b_pressed;
int b_mode;
void buttonOnHold ();
void buttonInLoop ();
};
#endif /* SWITCHCONTROLLER_H_ */

80
esp-vent-main/inc/Timer.h Normal file
View File

@@ -0,0 +1,80 @@
/*
* Timer.h
*
* Created on: Oct 14, 2022
* Author: tylen
*/
#ifndef TIMER_H_
#define TIMER_H_
#include "board.h"
#include <atomic>
#include <climits>
static volatile std::atomic_int timer;
static volatile std::atomic_int systicks;
extern "C"
{
/**
* @brief Handle interrupt from SysTick timer
* @return Nothing
*/
void SysTick_Handler (void);
}
uint32_t millis ();
class Timer
{
public:
/**
* @brief Initalize the systick configuration with your frequency
*
*/
Timer (uint32_t freq = 1000);
virtual ~Timer ();
/**
* @brief Tick the counter.
*
* Counter is incremented by one every tick,
* if it gets over the INT_MAX (see limits.h),
* the counter rolls up back to zero and starts
* over.
*
*
* @param ms Counter ticking frequency is provided in milliseconds
*/
void tickCounter (int ms);
/**
* @brief Get the current counter value
*
* @return int counter value
*/
int getCounter ();
/**
* @brief Set counter to 0.
*
*/
void resetCounter ();
/**
* @brief Sleep for amount of time
*
* Time is either in ms or in sec, defined
* by systickInit_xx()
*
* @param ms milliseconds
*/
void Sleep (int ms);
private:
volatile std::atomic_int counter;
uint32_t freq;
};
#endif /* TIMER_H_ */