diff --git a/esp-vent-main/.cproject b/esp-vent-main/.cproject
index 40c5924..99019c9 100644
--- a/esp-vent-main/.cproject
+++ b/esp-vent-main/.cproject
@@ -41,14 +41,6 @@
-
-
-
-
-
-
-
-
@@ -72,7 +64,6 @@
@@ -103,12 +94,12 @@
-
-
+
+
-
-
-
+
+
+
@@ -128,14 +119,6 @@
diff --git a/esp-vent-main/inc/I2C.h b/esp-vent-main/inc/I2C.h
index 2ac6c2f..910a596 100644
--- a/esp-vent-main/inc/I2C.h
+++ b/esp-vent-main/inc/I2C.h
@@ -15,7 +15,7 @@ struct I2C_config {
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) {};
+ I2C_config(unsigned int dn, unsigned int sp, unsigned int cd): device_number(dn), speed(sp), clock_divider(cd), i2c_mode(IOCON_SFI2C_EN) {};
};
class I2C {
diff --git a/esp-vent-main/inc/StateHandler/StateHandler.h b/esp-vent-main/inc/StateHandler/StateHandler.h
index de0b26d..ddb0e75 100644
--- a/esp-vent-main/inc/StateHandler/StateHandler.h
+++ b/esp-vent-main/inc/StateHandler/StateHandler.h
@@ -122,7 +122,7 @@ private:
* 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 };
+ Counter fan_speed = { 80, 1000 };
/*integral controller for PID. should be global, since it
* accumulates error signals encountered since startup*/
int integral = 0;
diff --git a/esp-vent-main/inc/SwitchController.h b/esp-vent-main/inc/SwitchController.h
index 9720fa7..5a1add9 100644
--- a/esp-vent-main/inc/SwitchController.h
+++ b/esp-vent-main/inc/SwitchController.h
@@ -15,7 +15,7 @@
class SwitchController
{
public:
- SwitchController (DigitalIoPin *button, Timer *timer, StateHandler *handler,
+ SwitchController (DigitalIoPin *button, StateHandler *handler,
int button_mode);
virtual ~SwitchController ();
/** Listen to switch button
@@ -24,7 +24,6 @@ public:
private:
DigitalIoPin *b;
- Timer *t;
StateHandler *h;
bool b_pressed;
int b_mode;
diff --git a/esp-vent-main/inc/Timer.h b/esp-vent-main/inc/Timer.h
index 840d386..884b444 100644
--- a/esp-vent-main/inc/Timer.h
+++ b/esp-vent-main/inc/Timer.h
@@ -13,7 +13,7 @@
#include
static volatile std::atomic_int timer;
-static volatile std::atomic_int systicks;
+static volatile std::atomic systicks;
extern "C"
{
@@ -34,6 +34,8 @@ public:
*
*/
Timer (uint32_t freq = 1000);
+
+ Timer (bool mode);
virtual ~Timer ();
/**
@@ -75,6 +77,7 @@ public:
private:
volatile std::atomic_int counter;
uint32_t freq;
+ bool mode;
};
#endif /* TIMER_H_ */
diff --git a/esp-vent-main/src/I2C.cpp b/esp-vent-main/src/I2C.cpp
index 7b11a09..19a0b6f 100644
--- a/esp-vent-main/src/I2C.cpp
+++ b/esp-vent-main/src/I2C.cpp
@@ -3,7 +3,8 @@
*
* Created on: 21.2.2016
* Author: krl
- * Based on example provided by NXP Semiconductors. See copyright notice below.
+ * Based on example provided by NXP Semiconductors. See copyright notice
+ * below.
*/
/*
@@ -21,123 +22,139 @@
* all warranties, express or implied, including all implied warranties of
* merchantability, fitness for a particular purpose and non-infringement of
* intellectual property rights. NXP Semiconductors assumes no responsibility
- * or liability for the use of the software, conveys no license or rights under any
- * patent, copyright, mask work right, or any other intellectual property rights in
- * or to any products. NXP Semiconductors reserves the right to make changes
- * in the software without notification. NXP Semiconductors also makes no
- * representation or warranty that such application will be suitable for the
+ * or liability for the use of the software, conveys no license or rights under
+ * any patent, copyright, mask work right, or any other intellectual property
+ * rights in or to any products. NXP Semiconductors reserves the right to make
+ * changes in the software without notification. NXP Semiconductors also makes
+ * no representation or warranty that such application will be suitable for the
* specified use without further testing or modification.
*
* @par
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors' and its
- * licensor's relevant copyrights in the software, without fee, provided that it
- * is used in conjunction with NXP Semiconductors microcontrollers. This
+ * licensor's relevant copyrights in the software, without fee, provided that
+ * it is used in conjunction with NXP Semiconductors microcontrollers. This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
*/
#include "I2C.h"
-
-I2C::I2C(const I2C_config &cfg): device(nullptr) {
- if(cfg.device_number == 0) {
- device = LPC_I2C0;
- // board init must have been called before the pins can be configured
- Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 22, IOCON_DIGMODE_EN | cfg.i2c_mode);
- Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 23, IOCON_DIGMODE_EN | cfg.i2c_mode);
- Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SCL);
- Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SDA);
- }
- else {
- // currently we support only I2C number 0
- }
-
- if(device) {
- /* Enable I2C clock and reset I2C peripheral - the boot ROM does not
- do this */
- Chip_I2C_Init(device);
-
- /* Setup clock rate for I2C */
- Chip_I2C_SetClockDiv(device, cfg.clock_divider);
-
- /* Setup I2CM transfer rate */
- Chip_I2CM_SetBusSpeed(device, cfg.speed);
-
- /* Enable Master Mode */
- Chip_I2CM_Enable(device);
- }
-}
-
-I2C::~I2C() {
- // TODO Auto-generated destructor stub
-}
-
-bool I2C::write(uint8_t devAddr, uint8_t *txBuffPtr, uint16_t txSize)
+I2C::I2C (const I2C_config &cfg) : device (nullptr)
{
- return transaction(devAddr, txBuffPtr, txSize, nullptr, 0);
+ // if(cfg.device_number == 0) {
+ device = LPC_I2C0;
+ // board init must have been called before the pins can be configured
+ Chip_IOCON_PinMuxSet (LPC_IOCON, 0, 22, IOCON_DIGMODE_EN | cfg.i2c_mode);
+ Chip_IOCON_PinMuxSet (LPC_IOCON, 0, 23, IOCON_DIGMODE_EN | cfg.i2c_mode);
+ Chip_SWM_EnableFixedPin (SWM_FIXED_I2C0_SCL);
+ Chip_SWM_EnableFixedPin (SWM_FIXED_I2C0_SDA);
+ //}
+ // else {
+ // currently we support only I2C number 0
+ //}
+
+ if (LPC_I2C0)
+ {
+ /* Enable I2C clock and reset I2C peripheral - the boot ROM does not
+ do this */
+ Chip_I2C_Init (LPC_I2C0);
+
+ /* Setup clock rate for I2C */
+ Chip_I2C_SetClockDiv (LPC_I2C0, cfg.clock_divider);
+
+ /* Setup I2CM transfer rate */
+ Chip_I2CM_SetBusSpeed (LPC_I2C0, cfg.speed);
+
+ /* Enable Master Mode */
+ Chip_I2CM_Enable (LPC_I2C0);
+ }
}
-bool I2C::read(uint8_t devAddr, uint8_t *rxBuffPtr, uint16_t rxSize)
+I2C::~I2C ()
{
- return transaction(devAddr, nullptr, 0, rxBuffPtr, rxSize);
+ // TODO Auto-generated destructor stub
}
+bool
+I2C::write (uint8_t devAddr, uint8_t *txBuffPtr, uint16_t txSize)
+{
+ return transaction (devAddr, txBuffPtr, txSize, nullptr, 0);
+}
-bool I2C::transaction(uint8_t devAddr, uint8_t *txBuffPtr, uint16_t txSize, uint8_t *rxBuffPtr, uint16_t rxSize) {
- I2CM_XFER_T i2cmXferRec;
+bool
+I2C::read (uint8_t devAddr, uint8_t *rxBuffPtr, uint16_t rxSize)
+{
+ return transaction (devAddr, nullptr, 0, rxBuffPtr, rxSize);
+}
- // make sure that master is idle
- while(!Chip_I2CM_IsMasterPending(device));
+bool
+I2C::transaction (uint8_t devAddr, uint8_t *txBuffPtr, uint16_t txSize,
+ uint8_t *rxBuffPtr, uint16_t rxSize)
+{
+ I2CM_XFER_T i2cmXferRec;
- /* Setup I2C transfer record */
- i2cmXferRec.slaveAddr = devAddr;
- i2cmXferRec.status = 0;
- i2cmXferRec.txSz = txSize;
- i2cmXferRec.rxSz = rxSize;
- i2cmXferRec.txBuff = txBuffPtr;
- i2cmXferRec.rxBuff = rxBuffPtr;
+ // make sure that master is idle
+ while (!Chip_I2CM_IsMasterPending (LPC_I2C0))
+ ;
- I2CM_XferBlocking(device, &i2cmXferRec);
- // Chip_I2CM_XferBlocking returns before stop condition is fully completed
- // therefore we need to wait for master to be idle when doing back-to-back transactions (see beginning of the function)
+ /* Setup I2C transfer record */
+ i2cmXferRec.slaveAddr = devAddr;
+ i2cmXferRec.status = 0;
+ i2cmXferRec.txSz = txSize;
+ i2cmXferRec.rxSz = rxSize;
+ i2cmXferRec.txBuff = txBuffPtr;
+ i2cmXferRec.rxBuff = rxBuffPtr;
- /* Test for valid operation */
- if (i2cmXferRec.status == I2CM_STATUS_OK) {
- return true;
- }
- else {
- return false;
- }
+ I2CM_XferBlocking (LPC_I2C0, &i2cmXferRec);
+ // Chip_I2CM_XferBlocking returns before stop condition is fully completed
+ // therefore we need to wait for master to be idle when doing back-to-back
+ // transactions (see beginning of the function)
+
+ /* Test for valid operation */
+ if (i2cmXferRec.status == I2CM_STATUS_OK)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
}
/* Transmit and Receive data in master mode */
-/* This duplicates (and combines) the functionality of Chip_I2CM_Xfer and Chip_I2CM_XferBlocking with a modification
- * that allows us to do a zero length write (needed to use honeywell humidity/temp sensor)
+/* This duplicates (and combines) the functionality of Chip_I2CM_Xfer and
+ * Chip_I2CM_XferBlocking with a modification that allows us to do a zero
+ * length write (needed to use honeywell humidity/temp sensor)
*/
-uint32_t I2C::I2CM_XferBlocking(LPC_I2C_T *pI2C, I2CM_XFER_T *xfer)
+uint32_t
+I2C::I2CM_XferBlocking (LPC_I2C_T *pI2C, I2CM_XFER_T *xfer)
{
- uint32_t ret = 0;
- /* start transfer */
+ uint32_t ret = 0;
+ /* start transfer */
- /* set the transfer status as busy */
- xfer->status = I2CM_STATUS_BUSY;
- /* Clear controller state. */
- Chip_I2CM_ClearStatus(pI2C, I2C_STAT_MSTRARBLOSS | I2C_STAT_MSTSTSTPERR);
- /* Write Address and RW bit to data register */
- //Chip_I2CM_WriteByte(pI2C, (xfer->slaveAddr << 1) | (xfer->txSz == 0)); // original NXP version
- // krl : both read and write lenght is 0 --> write (for honeywell temp sensor)
- Chip_I2CM_WriteByte(pI2C, (xfer->slaveAddr << 1) | (xfer->txSz == 0 && xfer->rxSz != 0));
- /* Enter to Master Transmitter mode */
- Chip_I2CM_SendStart(pI2C);
+ /* set the transfer status as busy */
+ xfer->status = I2CM_STATUS_BUSY;
+ /* Clear controller state. */
+ Chip_I2CM_ClearStatus (pI2C, I2C_STAT_MSTRARBLOSS | I2C_STAT_MSTSTSTPERR);
+ /* Write Address and RW bit to data register */
+ // Chip_I2CM_WriteByte(pI2C, (xfer->slaveAddr << 1) | (xfer->txSz == 0)); //
+ // original NXP version
+ // krl : both read and write lenght is 0 --> write (for honeywell temp
+ // sensor)
+ Chip_I2CM_WriteByte (pI2C, (xfer->slaveAddr << 1)
+ | (xfer->txSz == 0 && xfer->rxSz != 0));
+ /* Enter to Master Transmitter mode */
+ Chip_I2CM_SendStart (pI2C);
- while (ret == 0) {
- /* wait for status change interrupt */
- while (!Chip_I2CM_IsMasterPending(pI2C)) {}
- /* call state change handler */
- ret = Chip_I2CM_XferHandler(pI2C, xfer);
- }
- return ret;
+ while (ret == 0)
+ {
+ /* wait for status change interrupt */
+ while (!Chip_I2CM_IsMasterPending (pI2C))
+ {
+ }
+ /* call state change handler */
+ ret = Chip_I2CM_XferHandler (pI2C, xfer);
+ }
+ return ret;
}
-
-
diff --git a/esp-vent-main/src/PressureWrapper.cpp b/esp-vent-main/src/PressureWrapper.cpp
index e5cd2e6..353056c 100644
--- a/esp-vent-main/src/PressureWrapper.cpp
+++ b/esp-vent-main/src/PressureWrapper.cpp
@@ -25,7 +25,7 @@ static uint8_t crc8(uint8_t *data, size_t size) {
PressureWrapper::PressureWrapper ()
{
NVIC_DisableIRQ(I2C0_IRQn);
- I2C_config config;
+ I2C_config config(0, 55000, 1309);
I2C i2c_c(config);
i2c = &i2c_c;
}
diff --git a/esp-vent-main/src/StateHandler/StateHandler.cpp b/esp-vent-main/src/StateHandler/StateHandler.cpp
index afa1ad8..524faa8 100644
--- a/esp-vent-main/src/StateHandler/StateHandler.cpp
+++ b/esp-vent-main/src/StateHandler/StateHandler.cpp
@@ -6,7 +6,7 @@
*/
#include "StateHandler/StateHandler.h"
-#define PID 0
+#define PID 1
StateHandler::StateHandler (LiquidCrystal *lcd, ModbusRegister *A01,
PressureWrapper *pressure, Timer *global)
@@ -81,7 +81,7 @@ StateHandler::stateInit (const Event &event)
switch (event.type)
{
case Event::eEnter:
- SetState (&StateHandler::stateSensors);
+ //SetState (&StateHandler::stateSensors);
break;
case Event::eExit:
_lcd->clear ();
@@ -219,13 +219,6 @@ StateHandler::handleControlButtons (uint8_t button)
void
StateHandler::save (int eventValue, size_t mode)
{
- /* if pressure is not provided from main it checks it in following if{}*/
- if (!eventValue)
- {
- /* Small delay for modbus communications with pressure sensor */
- state_timer->tickCounter (1);
- eventValue = pressure->getPressure ();
- }
int counterValue = value[mode].getCurrent ();
if (saved_curr_value[mode] != eventValue
|| saved_set_value[mode] != counterValue)
diff --git a/esp-vent-main/src/SwitchController.cpp b/esp-vent-main/src/SwitchController.cpp
index e294417..b808e90 100644
--- a/esp-vent-main/src/SwitchController.cpp
+++ b/esp-vent-main/src/SwitchController.cpp
@@ -5,13 +5,12 @@
* Author: tylen
*/
-#include
+#include "SwitchController.h"
-SwitchController::SwitchController (DigitalIoPin *button, Timer *timer,
+SwitchController::SwitchController (DigitalIoPin *button,
StateHandler *handler, int button_mode)
{
b = button;
- t = timer;
h = handler;
b_pressed = false;
b_mode = button_mode;
@@ -25,51 +24,17 @@ SwitchController::~SwitchController ()
void
SwitchController::listen ()
{
- int timer = t->getCounter ();
/** Button is pressed for the first time*/
if (b->read () && !b_pressed)
{
- t->resetCounter ();
b_pressed = true;
}
/** Button is released before 2 seconds*/
- if (!b->read () && b_pressed && timer < 2000)
+ if (!b->read () && b_pressed)
{
h->HandleState (Event (Event::eKey, b_mode));
b_pressed = false;
- t->resetCounter ();
- }
- /** Button is pressed after 2 seconds*/
- if (b->read () && b_pressed && timer >= 2000)
- {
- buttonOnHold ();
}
+
}
-void
-SwitchController::buttonOnHold ()
-{
- t->resetCounter ();
- while (b->read ())
- {
- buttonInLoop ();
- }
- if (b_mode == BUTTON_CONTROL_TOG_MODE)
- {
- h->HandleState (Event (Event::eKey, b_mode));
- }
- b_pressed = false;
- t->resetCounter ();
-}
-
-void
-SwitchController::buttonInLoop ()
-{
- if (t->getCounter () > 50 && b_mode != BUTTON_CONTROL_TOG_MODE)
- {
- h->HandleState (Event (Event::eKey, b_mode));
- h->HandleState (Event (Event::eTick));
- t->resetCounter ();
- }
- t->tickCounter (2);
-}
diff --git a/esp-vent-main/src/Timer.cpp b/esp-vent-main/src/Timer.cpp
index c66d9ea..1190c1d 100644
--- a/esp-vent-main/src/Timer.cpp
+++ b/esp-vent-main/src/Timer.cpp
@@ -20,12 +20,21 @@ extern "C"
Timer::Timer (uint32_t freq) : freq (freq)
{
+mode = true;
Chip_Clock_SetSysTickClockDiv (1);
uint32_t sysTickRate = Chip_Clock_GetSysTickClockRate ();
SysTick_Config (sysTickRate / freq);
- counter = 0;
+ resetCounter();
timer = 0;
- systicks = 0;
+ systicks.store (0, std::memory_order_relaxed);
+}
+
+Timer::Timer(bool mode)
+{
+ this->mode = false;
+ resetCounter();
+ timer = 0;
+ systicks.store (0, std::memory_order_relaxed);
}
Timer::~Timer ()
@@ -69,5 +78,5 @@ Timer::resetCounter ()
uint32_t
millis ()
{
- return systicks;
+ return systicks.load(std::memory_order_relaxed);
}
diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp
index 10c4362..f595427 100644
--- a/esp-vent-main/src/esp-vent-main.cpp
+++ b/esp-vent-main/src/esp-vent-main.cpp
@@ -35,7 +35,7 @@ main (void)
#if defined(__USE_LPCOPEN)
// Read clock settings and update SystemCoreClock variable
- SystemCoreClockUpdate ();
+ SystemCoreClockUpdate ();
#if !defined(NO_BOARD_LIB)
// Set b_up_state and initialize all required blocks and
// functions related to the board hardware
@@ -47,6 +47,7 @@ main (void)
/** Lcd & stateHandler */
Chip_RIT_Init (LPC_RITIMER);
Timer glob_time;
+ Timer switch_time(false);
DigitalIoPin rs (0, 29, false, true, false);
DigitalIoPin en (0, 9, false, true, false);
DigitalIoPin d4 (0, 10, false, true, false);
@@ -56,7 +57,7 @@ main (void)
LiquidCrystal lcd (&rs, &en, &d4, &d5, &d6, &d7);
//
lcd.setCursor (0, 0);
- lcd.print ("Test");
+ lcd.print ("Vent-Machine");
/* FAN object */
ModbusMaster fan (1);
@@ -65,38 +66,32 @@ main (void)
// ModbusRegister DI1(&fan, 4, false);
PressureWrapper sens;
+ glob_time.Sleep(1000);
StateHandler ventMachine (&lcd, &A01, &sens, &glob_time);
/** Common pins */
DigitalIoPin b_up (0, 7, true, true, true); // A5
- SwitchController sw_up (&b_up, &glob_time, &ventMachine, BUTTON_CONTROL_UP);
+ SwitchController sw_up (&b_up, &ventMachine, BUTTON_CONTROL_UP);
DigitalIoPin b_down (0, 6, true, true, true); // A4
- SwitchController sw_down (&b_down, &glob_time, &ventMachine,
+ SwitchController sw_down (&b_down, &ventMachine,
BUTTON_CONTROL_DOWN);
DigitalIoPin b_toggle (0, 5, true, true, true); // A3
- SwitchController sw_toggle (&b_toggle, &glob_time, &ventMachine,
+ SwitchController sw_toggle (&b_toggle, &ventMachine,
BUTTON_CONTROL_TOG_MODE);
- int pressure = 0, pressure_time = 0;
+ int pressure = 0;
while (1)
{
sw_up.listen ();
sw_down.listen ();
sw_toggle.listen ();
- /**
- * TODO:
- * - Update current pressure to eTick
- */
-#if 0
- if(pressure_time == 5) {
+ if(glob_time.getCounter() > 3) {
pressure = sens.getPressure();
- pressure_time = 0;
+ glob_time.resetCounter();
}
- ++pressure_time;
-#endif
ventMachine.HandleState (Event (Event::eTick, pressure));
glob_time.tickCounter (1);
}