diff --git a/source/shoh/src/threads/relay/Relay.h b/source/shoh/src/threads/relay/Relay.h index c626ff7..417b721 100644 --- a/source/shoh/src/threads/relay/Relay.h +++ b/source/shoh/src/threads/relay/Relay.h @@ -48,8 +48,8 @@ public: void utilizeEventData(); private: ThreadCommon::QueueManager* _qm; - RelayDevice relays [2] = {{0, 24, 0}, - {0, 26, 1}}; + RelayDevice relays [2] = {{0, 11, 0}, + {0, 23, 1}}; void parseEvent(Event * e); int8_t setpoint, ext_temp; diff --git a/source/shoh/src/threads/rotary/Rotary.cpp b/source/shoh/src/threads/rotary/Rotary.cpp index fcdce48..4ea786d 100644 --- a/source/shoh/src/threads/rotary/Rotary.cpp +++ b/source/shoh/src/threads/rotary/Rotary.cpp @@ -12,6 +12,8 @@ static QueueHandle_t * p_rotary_isr_q; +static DigitalIoPin * p_sigB; + extern "C" { void @@ -19,11 +21,17 @@ extern "C" { Chip_PININT_ClearIntStatus (LPC_PININT, PININTCH (PIN_INT0_IRQn)); portBASE_TYPE xHigherPriorityWoken = pdFALSE; - uint8_t data = ThreadCommon::RotaryAction::Right; + uint8_t data; + + if (p_sigB->read()) + data = ThreadCommon::RotaryAction::Left; + else + data = ThreadCommon::RotaryAction::Right; + xQueueSendFromISR (*p_rotary_isr_q, &data, &xHigherPriorityWoken); portEND_SWITCHING_ISR(xHigherPriorityWoken); } - +/* void PIN_INT1_IRQHandler (void) { @@ -33,11 +41,11 @@ extern "C" xQueueSendFromISR (*p_rotary_isr_q, &data, &xHigherPriorityWoken); portEND_SWITCHING_ISR(xHigherPriorityWoken); } - +*/ void - PIN_INT2_IRQHandler (void) + PIN_INT1_IRQHandler (void) { - Chip_PININT_ClearIntStatus (LPC_PININT, PININTCH (PIN_INT2_IRQn)); + Chip_PININT_ClearIntStatus (LPC_PININT, PININTCH (PIN_INT1_IRQn)); portBASE_TYPE xHigherPriorityWoken = pdFALSE; uint8_t data = ThreadCommon::RotaryAction::Press; xQueueSendFromISR (*p_rotary_isr_q, &data, &xHigherPriorityWoken); @@ -48,6 +56,7 @@ extern "C" Rotary::Rotary(ThreadCommon::QueueManager* qm) : _qm(qm) { LOG_DEBUG("Creating Rotary"); + p_sigB = &(this->signal[1]); } Rotary::~Rotary() diff --git a/source/shoh/src/threads/rotary/Rotary.h b/source/shoh/src/threads/rotary/Rotary.h index dafe910..e21077b 100644 --- a/source/shoh/src/threads/rotary/Rotary.h +++ b/source/shoh/src/threads/rotary/Rotary.h @@ -20,9 +20,9 @@ public: private: Event* message; ThreadCommon::QueueManager* _qm; - DigitalIoPin signal[3] = { { 0, 1, true, true, false, true, PIN_INT0_IRQn}, //SW1 - { 0, 16, true, true, false, true, PIN_INT1_IRQn}, //SW2 - { 1, 8, true, false, false, true, PIN_INT2_IRQn} }; + DigitalIoPin signal[3] = { { 1, 18, true, true, false, true, PIN_INT0_IRQn}, //sigA + { 2, 12, true, true, false}, //sigB + { 0, 2, true, true, false, true, PIN_INT1_IRQn} }; //Press, sw //2 11 (pin without interrupt) //0 1 (board button) }; void thread_rotary(void* pvParams); diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp new file mode 100644 index 0000000..25ad4b0 --- /dev/null +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp @@ -0,0 +1,128 @@ +/* + * SensorTempSHT20.cpp + * + * Created on: 16 May 2023 + */ + +#include "SensorTempSHT20.h" +#include "FreeRTOS.h" +#include "task.h" +#include "Log.h" + +//D6U9H: +//D - Digital (i2c) +//6 - 2016 year of production +//U9H - Sensirion undecodable crap +//Address: 0x40 + +//Trigger T measurement | hold master | 1110 0011 - 0xe3 (implemented) +//Trigger T measurement | no hold master | 1111 0011 - 0xf3 +//Write user register | | 1110 0110 - 0xe6 +//Read user register | | 1110 0111 - 0xe7 +//Soft reset | | 1111 1110 - 0xfe + + +SensorTempSHT20::SensorTempSHT20(I2C* pi2c) +: _pi2c(pi2c), _dev_addr(0x40), _up_flag(false), +_com_read_hold(0xe3), _com_read_nohold(0xf3), +_com_write_ur(0xe6), _com_read_ur(0xe7), +_com_soft_reset(0xfe), _polynominal(0x131) +{ + //Read sensor during the initialisation to get the "UP" state earlier. + this->read(); +} + +SensorTempSHT20::~SensorTempSHT20() +{} + +/** + * @brief Gets temperature from SHT20 sensor. + * + * @return int8_t temperature value trimmed from -128 to 127. + */ +int8_t SensorTempSHT20::getTemperature() +{ + uint16_t raw_temp = this->read(); + bool err_bit = raw_temp & 0x1; + int temp = 0; + if (err_bit) + return -128; + + //Formula: (St / 2 ^ 16) * 175.72 - 46.85 + temp = ((double)raw_temp / 65536) * 175.72 - 46.85; + + if(temp > 127) + temp = 127; + if (temp < -128) + temp = -128; + + return temp; +} + +/** + * @brief Makes sure that the sensor is up. + * + * @return true It is. + * @return false It is not. + */ +bool SensorTempSHT20::is_up() +{ + this->read(); + return this->_up_flag; +} + +//Use hold master - read(com = 0xe3) +//Reading is done via 3 bytes. +//WRITE: I2C address + write | read command +//READ: Data (MSB) | Data (LSB) + Stat. | Checksum +/** + * @brief Gets raw temperature measurement data from sensor + * using hold master mode. + * + * @return uint16_t Raw temperature measurement data. + */ +uint16_t SensorTempSHT20::read() +{ + uint8_t tbuf = this->_com_read_hold; + uint8_t rbuf[3] = {0x0, 0x2, 0x0}; + uint8_t crc = 0x0; + uint16_t raw_temp = 0; + this->_up_flag = this->_pi2c->transaction(this->_dev_addr, &tbuf, 1, rbuf, 3); + + //Sensor changes this bit to 0 on temp measurement. + if (rbuf[1] & 0x2) + return 0x1; + + raw_temp |= (rbuf[0] << 8); + raw_temp |= (rbuf[1] & 0xfc); + crc = rbuf[2]; + + if (this->crc_check(rbuf, 2, crc)) + LOG_WARNING("Temperature sensor reported crc mismatch. Raw data: %04x; CRC: %x", raw_temp, crc); + + return raw_temp; +} + +/** + * @brief Checks if checksum is correct. + * + * @param data array of raw data to check. + * @param nbrOfBytes size of an array. + * @param checksum received checksum to compare calculated crc to. + * @return true - checksum is incorrect. + * @return false - checksum is the same as calculated. + */ +bool SensorTempSHT20::crc_check(uint8_t * data, uint8_t nbrOfBytes, uint8_t checksum) +{ + uint8_t crc = 0; + uint8_t bit; + uint8_t byteCtr; + //Calculates 8-Bit checksum with given _polynomial + for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) + { + crc ^= (data[byteCtr]); + for ( bit = 8; bit > 0; --bit) + crc = (crc & 0x80) ? ((crc << 1) ^ this->_polynominal) : (crc << 1); + } + return crc != checksum; +} diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.h b/source/shoh/src/threads/temperature/SensorTempSHT20.h new file mode 100644 index 0000000..6006bc3 --- /dev/null +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.h @@ -0,0 +1,34 @@ +/* + * SensorTempSHT20.h + * + * Created on: 16 May 2023 + */ + +#ifndef THREADS_TEMPERATURE_SENSORTEMPTC74_H_ +#define THREADS_TEMPERATURE_SENSORTEMPTC74_H_ + +#include "I2C.h" +#include "chip.h" + +class SensorTempSHT20 { +public: + SensorTempSHT20(I2C* pi2c); + virtual ~SensorTempSHT20(); + int8_t getTemperature(); + bool is_up(); +private: + uint16_t read(); + bool crc_check(uint8_t * data, uint8_t nbrOfBytes, uint8_t checksum); + + I2C* _pi2c; + const uint8_t _dev_addr; + bool _up_flag; + const uint8_t _com_read_hold; + const uint8_t _com_read_nohold; + const uint8_t _com_write_ur; //user register + const uint8_t _com_read_ur; //user register + const uint8_t _com_soft_reset; + const uint16_t _polynominal; +}; + +#endif /* THREADS_TEMPERATURE_SENSORTEMPSHT20_H_ */ diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 7929184..45efd81 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -5,7 +5,8 @@ */ #include "Temperature.h" -#include "SensorTempTC74.h" +//#include "SensorTempTC74.h" +#include "SensorTempSHT20.h" #include "Event.h" #include "Log.h" @@ -15,7 +16,8 @@ Temperature::~Temperature() {} void Temperature::taskFunction() { - SensorTempTC74 ext_temp_sensor(this->_pi2c, 0x4a); + //SensorTempTC74 ext_temp_sensor(this->_pi2c, 0x4a); + SensorTempSHT20 ext_temp_sensor(this->_pi2c); Event t (Event::ExternalTemp, -128); int8_t temp_value = -128; _qm->send(ThreadCommon::QueueManager::master_event_all, &t, 0); @@ -27,6 +29,7 @@ void Temperature::taskFunction() if(temp_value == -128) { LOG_ERROR("Failed to get temperature."); + vTaskDelay(10000); continue; } @@ -40,7 +43,7 @@ void Temperature::taskFunction() void thread_temperature(void* pvParams) { ThreadCommon::CommonManagers * manager = static_cast(pvParams); - I2C_config conf{0x4a, 100000}; + I2C_config conf{0, 100000}; I2C i2c(conf); Temperature t(manager->qm, &i2c); t.taskFunction(); diff --git a/source/shoh/src/threads/user_interface/UserInterface.cpp b/source/shoh/src/threads/user_interface/UserInterface.cpp index 4ea61c0..9bcc0fd 100644 --- a/source/shoh/src/threads/user_interface/UserInterface.cpp +++ b/source/shoh/src/threads/user_interface/UserInterface.cpp @@ -75,19 +75,19 @@ void UserInterface::handleLCD(LiquidCrystal *lcd, const char *str) void UserInterface::initLCD1() { - this->lcd1_rs = new DigitalIoPin(1, 9, false); - this->lcd1_en = new DigitalIoPin(0, 14, false); - this->lcd1_d4 = new DigitalIoPin(0, 13, false); - this->lcd1_d5 = new DigitalIoPin(0, 12, false); - this->lcd1_d6 = new DigitalIoPin(0, 23, false); - this->lcd1_d7 = new DigitalIoPin(0, 11, false); + this->lcd1_rs = new DigitalIoPin(1, 24, false);//(1, 18, false); + this->lcd1_en = new DigitalIoPin(1, 26, false);//(1, 24, false); + this->lcd1_d4 = new DigitalIoPin(1, 27, false);//(1, 19, false); + this->lcd1_d5 = new DigitalIoPin(1, 25, false);//(1, 26, false); + this->lcd1_d6 = new DigitalIoPin(1, 28, false);//(1, 27, false); + this->lcd1_d7 = new DigitalIoPin(2, 3, false);//(1, 25, false); this->lcd1_rs->write(false); this->lcd1_en->write(false); this->lcd1_d4->write(false); this->lcd1_d5->write(false); - this->lcd1_d6->write(false); - this->lcd1_d7->write(false); + this->lcd1_d6->write(false); + this->lcd1_d7->write(false); // LCD init. this->lcd1 = new LiquidCrystal(this->lcd1_rs, this->lcd1_en, this->lcd1_d4, this->lcd1_d5, this->lcd1_d6, this->lcd1_d7);