Compare commits

..

10 Commits
rotary ... main

Author SHA1 Message Date
Vasily Davydov
ed461524af root: add script for reading logs 2023-07-07 17:03:57 +03:00
Vasily Davydov
486b4a40b2 main: working version 0.2 2023-07-07 17:03:36 +03:00
Vasily Davydov
9be6c99e2c
Merge pull request #68 from vas-dav/pinmap-hdr
threadcommon: [#67] common pinmap header
2023-07-07 15:22:14 +03:00
Vasily Davydov
8789e8d886 threadcommon: [#67] common pinmap header 2023-06-17 10:28:16 +03:00
Vasily Davydov
eaf0338497
Merge pull request #66 from vas-dav/temperature
Temperature
2023-06-15 11:16:10 +03:00
RedHawk
7a25e14a8f temp: [#61] Make things a little clearer.
* Newline in log isn't nice.
* I managed to confuse even myself with this 0x4a.
It's actually not used anywhere currently.
2023-06-14 20:36:26 +03:00
RedHawk
88a7bac525 temp: [#61] Temperature parsing and documentation. 2023-06-13 23:52:14 +03:00
RedHawk
cb64ae7e0f temp: [#61] CRC check. 2023-06-13 19:52:53 +03:00
RedHawk
4613fce370 temp: [#61] Communication with sensor.
* Up/Down state.
* Raw temperature data.
* Can't believe it actually worked.
2023-06-13 19:27:00 +03:00
RedHawk
7cc658bc2e temp sensor: [#61] Added placeholder for the sensor interface. 2023-06-13 16:50:27 +03:00
12 changed files with 276 additions and 18 deletions

28
read_logs.py Normal file
View File

@ -0,0 +1,28 @@
import serial
from datetime import datetime
# Configure serial port settings
port = '/dev/cu.usbmodemNRAQBQER2' # Replace with your specific serial port
baud_rate = 115200 # Replace with the appropriate baud rate
timeout = 1 # Specify the timeout for reading from the serial port
# Open the serial port
ser = serial.Serial(port, baud_rate, timeout=timeout)
# Generate the filename based on current date and time
now = datetime.now()
date_time = now.strftime("%Y-%m-%d_%H-%M")
filename = f"{date_time}_SHOH.txt"
# Open a file to write the logs
with open(filename, 'w') as file:
# Read and write the logs from the serial port
while True:
line = ser.readline().decode().strip()
if line:
file.write(line + '\n')
print(line) # Optional: Print the logs to the console as well
# Close the serial port when done
ser.close()

View File

@ -57,8 +57,12 @@ I2C::I2C (const I2C_config &cfg) : device (nullptr)
*/
//Manual: Table 83 & 90
Chip_SYSCTL_PeriphReset(RESET_I2C0);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 4, IOCON_FUNC1 | IOCON_DIGMODE_EN | cfg.i2c_mode);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 5, IOCON_FUNC1 | IOCON_DIGMODE_EN | cfg.i2c_mode);
Chip_IOCON_PinMuxSet(LPC_IOCON, ThreadCommon::PORT_I2C_SCL,
ThreadCommon::PIN_I2C_SCL, IOCON_FUNC1 |
IOCON_DIGMODE_EN | cfg.i2c_mode);
Chip_IOCON_PinMuxSet(LPC_IOCON, ThreadCommon::PORT_I2C_SDA,
ThreadCommon::PIN_I2C_SDA, IOCON_FUNC1 |
IOCON_DIGMODE_EN | cfg.i2c_mode);
//}
// else {
// currently we support only I2C number 0

View File

@ -10,6 +10,7 @@
#include "chip.h"
#include "board.h"
#include "pinportmap.h"
struct I2C_config {
unsigned int device_number;

View File

@ -13,12 +13,12 @@
#include <string>
#include <map>
#include "queue.h"
#include "pinportmap.h"
#include "task.h"
#include <assert.h>
namespace ThreadCommon
{
enum RotaryAction
{
Right,

View File

@ -0,0 +1,49 @@
/*
* pinportmap.h
*
* Created on: 17 Jun 2023
* Author: dave
*/
#ifndef THREADS_COMMON_PINPORTMAP_H_
#define THREADS_COMMON_PINPORTMAP_H_
namespace ThreadCommon
{
enum PINMAP
{
PIN_LCD_RS = 24,
PIN_LCD_EN = 26,
PIN_LCD_D4 = 27,
PIN_LCD_D5 = 25,
PIN_LCD_D6 = 28,
PIN_LCD_D7 = 3,
PIN_I2C_SCL = 4,
PIN_I2C_SDA = 5,
PIN_ROTARY_SIG_A = 18,
PIN_ROTARY_SIG_B = 12,
PIN_ROTARY_PRESS = 2,
PIN_BOARD_SW1 = 1
};
enum PORTMAP
{
PORT_LCD_RS = 1,
PORT_LCD_EN = 1,
PORT_LCD_D4 = 1,
PORT_LCD_D5 = 1,
PORT_LCD_D6 = 1,
PORT_LCD_D7 = 2,
PORT_I2C_SCL = 0,
PORT_I2C_SDA = 0,
PORT_ROTARY_SIG_A = 1,
PORT_ROTARY_SIG_B = 2,
PORT_ROTARY_PRESS = 0,
PORT_BOARD_SW1 = 0
};
}
#endif /* THREADS_COMMON_PINPORTMAP_H_ */

View File

@ -16,7 +16,7 @@ extern QueueHandle_t logging_queue;
/* ================= Settings ================== */
#define LOG_COLORED_OUTPUT
#define HIGH_PRIORITY_DEBUG
#define LOG_DEBUG_MESSAGES 1
#define LOG_DEBUG_MESSAGES 0
/* ================= Settings ================== */
// internal debug defines

View File

@ -50,7 +50,7 @@ void Master::HandleEventType(Event* e)
//Comes from rotary, goes to manager
send = _qm->send<Event>(ThreadCommon::QueueManager::manager_event_master, e, 0);
//LOG_WARNING("Timestamp: %zus, Clock: %zu, Chip freq: %zu", LPC_SCT1->COUNT_U / Chip_Clock_GetMainClockRate(), LPC_SCT1->COUNT_U, Chip_Clock_GetMainClockRate());
if (send) LOG_DEBUG("Rotary: %s has been forwarded to manager", rotary_direction[rd]);
if (send) LOG_INFO("Rotary: %s has been forwarded to manager", rotary_direction[rd]);
break;
case Event::InternalTemp:
// TODO remove (deprecated)

View File

@ -20,9 +20,15 @@ public:
private:
Event* message;
ThreadCommon::QueueManager* _qm;
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)
DigitalIoPin signal[3] = { { ThreadCommon::PORT_ROTARY_SIG_A,
ThreadCommon::PIN_ROTARY_SIG_A,
true, true, false, true, PIN_INT0_IRQn},
{ ThreadCommon::PORT_ROTARY_SIG_B,
ThreadCommon::PIN_ROTARY_SIG_B,
true, true, false},
{ ThreadCommon::PORT_ROTARY_PRESS,
ThreadCommon::PIN_ROTARY_PRESS,
true, true, false, true, PIN_INT1_IRQn} };
};
void thread_rotary(void* pvParams);

View File

@ -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;
}

View File

@ -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_ */

View File

@ -5,7 +5,8 @@
*/
#include "Temperature.h"
#include "SensorTempTC74.h"
//#include "SensorTempTC74.h"
#include "SensorTempSHT20.h"
#include "Event.h"
#include "Log.h"
@ -15,10 +16,11 @@ 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<Event>(ThreadCommon::QueueManager::master_event_all, &t, 0);
while(ext_temp_sensor.is_up() != true);
for (;;)
{
if (ext_temp_sensor.is_up())
@ -41,7 +43,7 @@ void Temperature::taskFunction()
void thread_temperature(void* pvParams)
{
ThreadCommon::CommonManagers * manager = static_cast<ThreadCommon::CommonManagers*>(pvParams);
I2C_config conf{0x4a, 100000};
I2C_config conf{0, 100000};
I2C i2c(conf);
Temperature t(manager->qm, &i2c);
t.taskFunction();

View File

@ -75,12 +75,18 @@ void UserInterface::handleLCD(LiquidCrystal *lcd, const char *str)
void UserInterface::initLCD1()
{
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 = new DigitalIoPin(ThreadCommon::PORT_LCD_RS,
ThreadCommon::PIN_LCD_RS, false);//(1, 18, false);
this->lcd1_en = new DigitalIoPin(ThreadCommon::PORT_LCD_EN,
ThreadCommon::PIN_LCD_EN, false);//(1, 24, false);
this->lcd1_d4 = new DigitalIoPin(ThreadCommon::PORT_LCD_D4,
ThreadCommon::PIN_LCD_D4, false);//(1, 19, false);
this->lcd1_d5 = new DigitalIoPin(ThreadCommon::PORT_LCD_D5,
ThreadCommon::PIN_LCD_D5, false);//(1, 26, false);
this->lcd1_d6 = new DigitalIoPin(ThreadCommon::PORT_LCD_D6,
ThreadCommon::PIN_LCD_D6, false);//(1, 27, false);
this->lcd1_d7 = new DigitalIoPin(ThreadCommon::PORT_LCD_D7,
ThreadCommon::PIN_LCD_D7, false);//(1, 25, false);
this->lcd1_rs->write(false);
this->lcd1_en->write(false);