Skip to content

Commit

Permalink
Merge pull request SoC-Arch-polito#14 from SoC-Arch-polito/comm_help
Browse files Browse the repository at this point in the history
COMM help and prompt
  • Loading branch information
thegabriele97 authored Dec 21, 2021
2 parents b4d0bbe + 65bb87d commit c2d3cec
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 180 deletions.
4 changes: 3 additions & 1 deletion include/i2c_lcd.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
void lcd_init (void); // initialize lcd
#include "stm32f4xx_hal.h"

void lcd_init (I2C_HandleTypeDef * i2c_ref); // initialize lcd

void lcd_create_char(char customChar[], int location);

Expand Down
9 changes: 6 additions & 3 deletions include/log.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "stm32f4xx_hal.h"
#include <stdbool.h>
#include "comm.h"

//Typedefs
//1. data size
Expand All @@ -23,6 +25,7 @@ void flashWrite(uint32_t idx, void *wrBuf, uint32_t Nsize, DataTypeDef dataType)
void flashRead(uint32_t idx, void *rdBuf, uint32_t Nsize, DataTypeDef dataType);


int unix_timestamp(RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate);
void start(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate, uint8_t start[]);
int updateNumber(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate, uint16_t number);
int log_unix_timestamp(RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate);
void log_rtc_setup(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate, const char *start);
void log_set_start_number(uint16_t this_number);
int log_update_number(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate, uint16_t this_number, struct COMM_Handle *hcomm);
52 changes: 0 additions & 52 deletions serial.py

This file was deleted.

26 changes: 26 additions & 0 deletions serialInterface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/python3.9

from os import times
import serial
from sys import argv
from datetime import datetime
import time

if __name__ == "__main__":
ser = serial.Serial(port= argv[1], baudrate=argv[2])
print(ser.name)

ser.write(bytearray("get\r".encode()))
entry = ser.read(5)

totb = 0
time.sleep(1)
while ser.inWaiting() > 5:
entry = ser.read(6)
totb += 6
# print(entry.hex())
timestamp = int.from_bytes(entry[0:4], "little")
cnt = int.from_bytes(entry[4:6], "little")

print(f"{datetime.utcfromtimestamp(timestamp).strftime('%d/%m/%Y %H/%M/%S')} -- {cnt} -- tot: {totb}")

51 changes: 34 additions & 17 deletions src/comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@

#define INVOKE_CB(cb, args...) {if (cb) (cb(args));}

#define STR_PROMPT "\nthor@cnt++ > "
#define STR_NOTFOUND "\r\nCOMMAND NOT FOUND!\r\n"
#define STR_HELP "\r\n\
The following commands are available:\r\n\
set_time : Set the system date and time. Usage: set_time dd/mm/yyyy hh/mm/ss\r\n\
set : Set the threshold for people count. Usage: set <N>\r\n\
get : Retrieves the log of the system. Usage: <use external script to decode>\r\n\
help : Shows this help. Usage: help\r\n"


static struct COMM_Handle *phcomm;
static uint8_t buf[0x20];
static uint8_t buf[0x200];
static uint8_t send_etx = 0;
static uint8_t buf_cnt = 0;
static volatile uint32_t setValue = 0;
static uint32_t rem;

static void txCpltCback(UART_HandleTypeDef *huart) {
static uint8_t ts;

if (send_etx > 1) {
send_etx--;
rem = phcomm->SrcMemory.size - 0x400;
ts = rem <= 0x400 ? 1 : (rem / 0x400 + 1);
GAL_UART_Transmit_DMA(phcomm->SrcMemory.basePtr, ts == 1 ? rem : 0x400);
GAL_UART_Transmit_DMA(phcomm->SrcMemory.basePtr, send_etx == 1 ? phcomm->SrcMemory.size : 0xffff);
} else if (send_etx == 1) {
INVOKE_CB(phcomm->Callback.onUARTDownload, true);
buf[0] = '\n';
Expand Down Expand Up @@ -58,7 +64,7 @@ static void rxCpltCback(UART_HandleTypeDef *huart) {

*pbuf = '\0';
n = atoi((char *)(pbuf - j));
*pbuf = '/';
*pbuf = (j == 2 ? '/' : ' ');

if (i == 0 && (n <= 0 || n > 31)) break;
else if (i == 1 && (n <= 0 || n > 12)) break;
Expand All @@ -73,10 +79,10 @@ static void rxCpltCback(UART_HandleTypeDef *huart) {
if (i == 0x6) {
INVOKE_CB(phcomm->Callback.onNewSysDateTime, (const char *)(buf + 0x9));
strcpy((char *)buf, "\r\nDONE!\r\n");
len = 7;
len = 6;
} else {
strcpy((char *)buf, "\r\nINVALID FORMAT!\r\n");
len = 17;
len = 16;
}

} else if (buf[0x3] == ' ') {
Expand All @@ -94,24 +100,35 @@ static void rxCpltCback(UART_HandleTypeDef *huart) {

if (cvrted) {
buf[buf_cnt + (len = (2 + 6 + i + 0))] = '\r';
strcpy((char *)buf + buf_cnt + 2, "New value");
strcpy((char *)buf + buf_cnt + 2, "NEW VALUE");
buf[buf_cnt + 9 + 2] = ' ';
}
}

} else if (!strncmp((char *)buf, "get", 0x3)) {

INVOKE_CB(phcomm->Callback.onUARTDownload, false);
GAL_UART_Transmit(buf + buf_cnt + 1, 1);

send_etx = phcomm->SrcMemory.size <= 0x400 ? 1 : (phcomm->SrcMemory.size / 0x400 + 1);
rem = phcomm->SrcMemory.size - 0x400;
// send_etx = phcomm->SrcMemory.size / 0x400;
GAL_UART_Transmit_DMA(phcomm->SrcMemory.basePtr, send_etx == 1 ? phcomm->SrcMemory.size : 0x400);
len = -2;
send_etx = (phcomm->SrcMemory.size >> 16) + 1;
GAL_UART_Transmit_DMA(phcomm->SrcMemory.basePtr, send_etx == 1 ? phcomm->SrcMemory.size : 0xffff);
len = -4;

} else if (!strncmp((char *)buf, "help", 0x4)) {
strcpy((char *)buf, STR_HELP);
len = sizeof(STR_HELP) - 3;
buf_cnt = 0;
} else if (buf_cnt) {
strcpy((char *)buf, STR_NOTFOUND);
len = sizeof(STR_NOTFOUND) - 3;
buf_cnt = 0;
}

buf[buf_cnt + len + 1] = '\n';
GAL_UART_Transmit_DMA(buf + buf_cnt, len + 2);
strcpy((char *)(buf + buf_cnt + len + 1), STR_PROMPT);
// buf[buf_cnt + len + 1] = '\n';
// buf[buf_cnt + len + 2] = '>';
// buf[buf_cnt + len + 3] = ' ';
GAL_UART_Transmit_DMA(buf + buf_cnt, len + sizeof(STR_PROMPT));
buf_cnt = 0xff;
}

Expand Down
34 changes: 14 additions & 20 deletions src/i2c_lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "stm32f4xx_hal.h"
#include "stdio.h"

extern I2C_HandleTypeDef hi2c1; // change your handler here accordingly

/*LCD Commands*/
#define LCD_FUNCTION_SET1 0x31
#define LCD_FUNCTION_SET2 0x22
Expand All @@ -28,8 +26,10 @@ extern I2C_HandleTypeDef hi2c1; // change your handler here accordingly
#define EN 0x04
#define RS 0x01

static I2C_HandleTypeDef *hi2c1;

// https://maxpromer.github.io/LCD-Character-Creator/
char customCharPerson[]= {0x0E, 0x0E, 0x04, 0x0E, 0x15, 0x04, 0x0A, 0x0A};
static char customCharPerson[] = {0x0E, 0x0E, 0x04, 0x0E, 0x15, 0x04, 0x0A, 0x0A};

void lcd_create_char(char customChar[], int location){
location &= 0x7; // we only have 8 locations 0-7
Expand All @@ -52,26 +52,22 @@ void lcd_send_internal(char cmd, int mode){
data_t[1] = data_u|mode|LCD_BACKLIGHT; //en=0, rs=0
data_t[2] = data_l|mode|LCD_BACKLIGHT|EN; //en=1, rs=0
data_t[3] = data_l|mode|LCD_BACKLIGHT; //en=0, rs=0
HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
HAL_I2C_Master_Transmit (hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
}

void lcd_send_cmd (char cmd)
{
void lcd_send_cmd (char cmd) {
lcd_send_internal(cmd, 0x00);
}

void lcd_send_data (char data)
{
void lcd_send_data (char data) {
lcd_send_internal(data, RS);
}

void lcd_clear (void)
{
void lcd_clear (void) {
lcd_send_cmd (LCD_CLEAR_DISPLAY);
}

void lcd_put_cur(int row, int col)
{
void lcd_put_cur(int row, int col) {
switch (row)
{
case 0:
Expand All @@ -85,12 +81,12 @@ void lcd_put_cur(int row, int col)
lcd_send_cmd (col);
}

void lcd_go_home(void){
void lcd_go_home(void) {
lcd_put_cur(0,0);
}

void lcd_init (void)
{
void lcd_init (I2C_HandleTypeDef *i2c_ref) {
hi2c1 = i2c_ref;
// 4 bit initialisation
HAL_Delay(50); // wait for >40ms
lcd_send_cmd (LCD_FUNCTION_SET1);
Expand Down Expand Up @@ -118,15 +114,13 @@ void lcd_init (void)
lcd_create_char(customCharPerson, 1);
}

void lcd_send_string (char *str)
{
void lcd_send_string (char *str) {
while (*str){
lcd_send_data (*str++);
}
}

void lcd_send_two_string (char *str1, char * str2)
{
void lcd_send_two_string (char *str1, char * str2) {
lcd_clear();
lcd_go_home();
lcd_send_string(str1);
Expand All @@ -144,7 +138,7 @@ void lcd_set_text_downloading(){
void lcd_set_number_people(int n_people){
char num_char[15];
sprintf(num_char, "%d", n_people);

lcd_go_home();
lcd_send_string("Number of people");
lcd_put_cur(1,0);
Expand Down
36 changes: 23 additions & 13 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,61 @@
#include "stm32f4xx_hal.h"
#include "time.h"

static int start_number;
static uint16_t number;
static uint16_t i = 0;

static uint32_t MY_SectorAddrs;
static uint8_t MY_SectorNum;

int unix_timestamp(RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate) {
int log_unix_timestamp(RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate) {
struct tm t;
int t_of_day;

t.tm_year = (int)gDate->Year + 100; // Year - 1900
t.tm_year = (int)gDate->Year + 2000 - 1900; // Year - 1900
t.tm_mon = (int)gDate->Month - 1; // Month, where 0 = jan
t.tm_mday = (int)gDate->Date; // Day of the month
t.tm_hour = (int)gTime->Hours;
t.tm_hour = (int)gTime->Hours + (gTime->TimeFormat == RTC_HOURFORMAT12_PM ? 12 : 0);
t.tm_min = (int)gTime->Minutes;
t.tm_sec = (int)gTime->Seconds;
t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown
t_of_day = (int)mktime(&t);
return t_of_day;
}

void start(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate, uint8_t start[]) {
void log_rtc_setup(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate, const char *start) {
int hour, min, sec, day, month, year;
flashSetSectorAddrs(11, 0x080E0000);
HAL_RTC_GetTime(hrtc, gTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(hrtc, gDate, RTC_FORMAT_BIN);
sscanf((char *)start, "%d:%d:%d %d/%d/%d %d", &hour, &min, &sec, &day, &month, &year, &start_number);
gTime->Hours = hour;
// https://community.st.com/s/question/0D53W000004KJtn/cannot-set-time-with-halrtcsettime-on-l412kb-nucleo32-after-init
// HAL_RTC_GetDate(hrtc, gDate, RTC_FORMAT_BIN);
// HAL_RTC_GetTime(hrtc, gTime, RTC_FORMAT_BIN);
sscanf((char *)start, "%d/%d/%d %d/%d/%d", &day, &month, &year, &hour, &min, &sec);
gTime->Hours = hour % 12;
gTime->Minutes = min;
gTime->Seconds = sec;
gTime->TimeFormat = hour > 12 ? RTC_HOURFORMAT12_PM : RTC_HOURFORMAT12_AM;
HAL_RTC_SetTime(hrtc, gTime, RTC_FORMAT_BIN);
gDate->Date = day;
gDate->Month = month;
gDate->Year = year - 2000;
HAL_RTC_SetDate(hrtc, gDate, RTC_FORMAT_BIN);
}

int updateNumber(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate, uint16_t number) {
void log_set_start_number(uint16_t this_number){
number = this_number;
}

int log_update_number(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *gTime, RTC_DateTypeDef *gDate, uint16_t this_number, struct COMM_Handle *hcomm) {
int timestamp;

number = this_number;
HAL_RTC_GetTime(hrtc, gTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(hrtc, gDate, RTC_FORMAT_BIN);
timestamp = unix_timestamp(gTime, gDate);
timestamp = log_unix_timestamp(gTime, gDate);
flashWrite(i, &timestamp, 1, DATA_TYPE_32);
flashWrite(i + 4, &number, 1, DATA_TYPE_16);
i = i + 5;
flashWrite(i + 4, &this_number, 1, DATA_TYPE_16);
hcomm->SrcMemory.size = hcomm->SrcMemory.size + 6;
i = i + 6;

return timestamp;
}

Expand Down
Loading

0 comments on commit c2d3cec

Please sign in to comment.