Skip to content

Commit ade8af2

Browse files
committed
revisione del codice per l'uso di un protocollo seriale a pacchetti
1 parent bb4306d commit ade8af2

14 files changed

+970
-200
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ HEADERS=adc.h\
1919
eeprom.h\
2020
eepromParams.h\
2121
lcd_lib.h\
22+
packet_header.h\
23+
packet_operations.h\
24+
packet_handler.h\
2225
pins.h\
2326
timer.h\
2427
uart.h\
@@ -31,12 +34,13 @@ OBJS=adc.o\
3134
eeprom.o\
3235
eepromParams.o\
3336
lcd_lib.o\
37+
packet_handler.o\
3438
pins.o\
3539
timer.o\
3640
uart.o\
3741

3842
BINS=eeprom_test.elf\
39-
main.elf\
43+
main.elf\
4044

4145
.phony: clean all
4246

eeprom_test.c

+16-16
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,55 @@ int main(void){
1717
uart = UART_init("uart_0", 115200);
1818

1919
while(1){
20-
char msg[16];
20+
char msg[32];
2121

2222
int timer = get_EEPROM_timer();
23-
sprintf(msg, "%d\n", timer);
23+
sprintf(msg, "timer = %d\n", timer);
2424
printString(msg);
2525
delayMs(300);
2626

2727
int minLightValue = get_EEPROM_minLight();
28-
sprintf(msg, "%d\n", minLightValue);
28+
sprintf(msg, "minLight = %d\n", minLightValue);
2929
printString(msg);
3030
delayMs(300);
3131

3232
int maxLightValue = get_EEPROM_maxLight();
33-
sprintf(msg, "%d\n", maxLightValue);
33+
sprintf(msg, "maxLight = %d\n", maxLightValue);
3434
printString(msg);
3535
delayMs(300);
3636

3737
int minTempValue = get_EEPROM_minTemp();
38-
sprintf(msg, "%d\n", minTempValue);
38+
sprintf(msg, "minTemp = %d\n", minTempValue);
3939
printString(msg);
4040
delayMs(300);
4141

4242
int maxTempValue = get_EEPROM_maxTemp();
43-
sprintf(msg, "%d\n", maxTempValue);
43+
sprintf(msg, "maxTemp = %d\n", maxTempValue);
4444
printString(msg);
4545
delayMs(300);
4646

47-
int maxPollValue = get_EEPROM_maxPoll();
48-
sprintf(msg, "%d\n", maxPollValue);
49-
printString(msg);
50-
delayMs(300);
51-
52-
int r1Value = get_EEPROM_r1();
53-
sprintf(msg, "%d\n", r1Value);
47+
float r1Value = get_EEPROM_r1();
48+
sprintf(msg, "R1 = %.1f\n", r1Value);
5449
printString(msg);
5550
delayMs(300);
5651

5752
float c1Value = get_EEPROM_c1();
58-
sprintf(msg, "%f\n", c1Value);
53+
sprintf(msg, "C1 = %12.7e\n", c1Value);
5954
printString(msg);
6055
delayMs(300);
6156

6257
float c2Value = get_EEPROM_c2();
63-
sprintf(msg, "%f\n", c2Value);
58+
sprintf(msg, "C2 = %12.7e\n", c2Value);
6459
printString(msg);
6560
delayMs(300);
6661

6762
float c3Value = get_EEPROM_c3();
68-
sprintf(msg, "%f\n", c3Value);
63+
sprintf(msg, "C3 = %12.7e\n", c3Value);
64+
printString(msg);
65+
delayMs(300);
66+
67+
int maxPollValue = get_EEPROM_maxPoll();
68+
sprintf(msg, "maxPoll = %d\n", maxPollValue);
6969
printString(msg);
7070
delayMs(300);
7171
}

host/Makefile

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
PREFIX = .
2+
CC = gcc
3+
CC_OPTS = -Wall -g --std=gnu99 -I$(PREFIX)/../include
4+
5+
6+
OBJS = packet_handler.o\
7+
serial_linux.o\
8+
9+
10+
HEADERS = packet_header.h\
11+
packet_operations.h\
12+
packet_handler.h\
13+
14+
15+
INCLUDES = $(addprefix $(PREFIX)/../include/, $(HEADERS))
16+
17+
18+
BINS = sendParams.bin\
19+
20+
21+
.phony: clean all
22+
23+
24+
all: $(BINS)
25+
26+
27+
%.o: $(PREFIX)/../src/%.c $(INCLUDES)
28+
$(CC) $(CC_OPTS) -c $<
29+
30+
%.o: $(PREFIX)/%.c $(INCLUDES)
31+
$(CC) $(CC_OPTS) -c $<
32+
33+
%.bin: %.o $(OBJS) $(INCLUDES)
34+
$(CC) $(CC_OPTS) -o $@ $< $(OBJS) $(LIBS)
35+
36+
clean:
37+
rm -rf $(OBJS) $(BINS) *~

host/sendParams.c

+227
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#include "packet_handler.h"
2+
#include "serial_linux.h"
3+
#include <assert.h>
4+
#include <stdint.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
#include <unistd.h>
9+
10+
#define STR_LENGTH 16
11+
char inputString[STR_LENGTH];
12+
13+
PacketHandler packet_handler;
14+
15+
/* -------------------- TimerConfigPacket -------------------- */
16+
17+
#pragma pack(push, 1)
18+
typedef struct TimerConfigPacket{
19+
PacketHeader header;
20+
uint32_t duration;
21+
} TimerConfigPacket;
22+
#pragma pack(pop)
23+
24+
#define TIMER_CONFIG_PACKET_TYPE 0
25+
#define TIMER_CONFIG_PACKET_SIZE (sizeof(TimerConfigPacket))
26+
27+
TimerConfigPacket timer_config_packet_buffer;
28+
29+
PacketHeader* TimerConfigPacket_initializeBuffer(PacketType type, PacketSize size, void* args __attribute__((unused))) {
30+
if (type != TIMER_CONFIG_PACKET_TYPE || size != TIMER_CONFIG_PACKET_SIZE)
31+
return 0;
32+
return (PacketHeader*) &timer_config_packet_buffer;
33+
}
34+
35+
PacketOperations TimerConfigPacket_ops = {
36+
TIMER_CONFIG_PACKET_TYPE,
37+
sizeof(TimerConfigPacket),
38+
TimerConfigPacket_initializeBuffer,
39+
0,
40+
0,
41+
0
42+
};
43+
44+
/* -------------------- LightConfigPacket -------------------- */
45+
46+
#pragma pack(push, 1)
47+
typedef struct LightConfigPacket{
48+
PacketHeader header;
49+
uint16_t minLight;
50+
uint16_t maxLight;
51+
} LightConfigPacket;
52+
#pragma pack(pop)
53+
54+
#define LIGHT_CONFIG_PACKET_TYPE 1
55+
#define LIGHT_CONFIG_PACKET_SIZE (sizeof(LightConfigPacket))
56+
57+
LightConfigPacket light_config_packet_buffer;
58+
59+
PacketHeader* LightConfigPacket_initializeBuffer(PacketType type, PacketSize size, void* args __attribute__((unused))) {
60+
if (type != LIGHT_CONFIG_PACKET_TYPE || size != LIGHT_CONFIG_PACKET_SIZE)
61+
return 0;
62+
return (PacketHeader*) &light_config_packet_buffer;
63+
}
64+
65+
PacketOperations LightConfigPacket_ops = {
66+
LIGHT_CONFIG_PACKET_TYPE,
67+
sizeof(LightConfigPacket),
68+
LightConfigPacket_initializeBuffer,
69+
0,
70+
0,
71+
0
72+
};
73+
74+
/* -------------------- TemperatureConfigPacket -------------------- */
75+
76+
#pragma pack(push, 1)
77+
typedef struct TemperatureConfigPacket{
78+
PacketHeader header;
79+
uint16_t minTemp;
80+
uint16_t maxTemp;
81+
float r1;
82+
float c1;
83+
float c2;
84+
float c3;
85+
} TemperatureConfigPacket;
86+
#pragma pack(pop)
87+
88+
#define TEMPERATURE_CONFIG_PACKET_TYPE 2
89+
#define TEMPERATURE_CONFIG_PACKET_SIZE (sizeof(TemperatureConfigPacket))
90+
91+
TemperatureConfigPacket temperature_config_packet_buffer;
92+
93+
PacketHeader* TemperatureConfigPacket_initializeBuffer(PacketType type, PacketSize size, void* args __attribute__((unused))) {
94+
if (type != TEMPERATURE_CONFIG_PACKET_TYPE || size != TEMPERATURE_CONFIG_PACKET_SIZE)
95+
return 0;
96+
return (PacketHeader*) &temperature_config_packet_buffer;
97+
}
98+
99+
PacketOperations TemperatureConfigPacket_ops = {
100+
TEMPERATURE_CONFIG_PACKET_TYPE,
101+
sizeof(TemperatureConfigPacket),
102+
TemperatureConfigPacket_initializeBuffer,
103+
0,
104+
0,
105+
0
106+
};
107+
108+
/* -------------------- PollutionConfigPacket -------------------- */
109+
110+
#pragma pack(push, 1)
111+
typedef struct PollutionConfigPacket{
112+
PacketHeader header;
113+
uint16_t maxPoll;
114+
} PollutionConfigPacket;
115+
#pragma pack(pop)
116+
117+
#define POLLUTION_CONFIG_PACKET_TYPE 3
118+
#define POLLUTION_CONFIG_PACKET_SIZE (sizeof(PollutionConfigPacket))
119+
120+
PollutionConfigPacket pollution_config_packet_buffer;
121+
122+
PacketHeader* PollutionConfigPacket_initializeBuffer(PacketType type, PacketSize size, void* args __attribute__((unused))) {
123+
if (type != POLLUTION_CONFIG_PACKET_TYPE || size != POLLUTION_CONFIG_PACKET_SIZE)
124+
return 0;
125+
return (PacketHeader*) &pollution_config_packet_buffer;
126+
}
127+
128+
PacketOperations PollutionConfigPacket_ops = {
129+
POLLUTION_CONFIG_PACKET_TYPE,
130+
sizeof(PollutionConfigPacket),
131+
PollutionConfigPacket_initializeBuffer,
132+
0,
133+
0,
134+
0
135+
};
136+
137+
void read_from_stdin(void){
138+
fgets(inputString, STR_LENGTH, stdin);
139+
if((strlen(inputString) > 0) && (inputString[strlen(inputString) - 1] == '\n'))
140+
inputString[strlen(inputString) - 1] = '\0';
141+
}
142+
143+
struct UART* uart;
144+
145+
void flushOutputBuffer(int fd){
146+
while (packet_handler.tx_size){
147+
uint8_t c = PacketHandler_txByte(&packet_handler);
148+
write(fd, &c, 1);
149+
usleep(1000);
150+
}
151+
}
152+
153+
int main(int argc, char** argv){
154+
assert(argc > 1);
155+
int fd = serial_open(argv[1]);
156+
if (fd < 0)
157+
return 0;
158+
if (serial_set_interface_attribs(fd, 115200, 0) < 0)
159+
return 0;
160+
serial_set_blocking(fd, 1);
161+
if (! fd)
162+
return 0;
163+
164+
PacketHandler_initialize(&packet_handler);
165+
PacketHandler_installPacket(&packet_handler, &TimerConfigPacket_ops);
166+
PacketHandler_installPacket(&packet_handler, &LightConfigPacket_ops);
167+
PacketHandler_installPacket(&packet_handler, &TemperatureConfigPacket_ops);
168+
PacketHandler_installPacket(&packet_handler, &PollutionConfigPacket_ops);
169+
170+
/* -------------------- TimerConfigPacket -------------------- */
171+
172+
printf("Insert timer duration: ");
173+
read_from_stdin();
174+
uint32_t duration = atoi(inputString);
175+
176+
TimerConfigPacket p0 = { {TIMER_CONFIG_PACKET_TYPE, TIMER_CONFIG_PACKET_SIZE, 0}, duration };
177+
PacketHandler_sendPacket(&packet_handler, (PacketHeader*) &p0);
178+
flushOutputBuffer(fd);
179+
180+
/* -------------------- LightConfigPacket -------------------- */
181+
182+
printf("Insert min light: ");
183+
read_from_stdin();
184+
uint16_t minLight = atoi(inputString);
185+
186+
printf("Insert max light: ");
187+
read_from_stdin();
188+
uint16_t maxLight = atoi(inputString);
189+
190+
LightConfigPacket p1 = { {LIGHT_CONFIG_PACKET_TYPE, LIGHT_CONFIG_PACKET_SIZE, 0}, minLight, maxLight };
191+
PacketHandler_sendPacket(&packet_handler, (PacketHeader*) &p1);
192+
flushOutputBuffer(fd);
193+
194+
/* -------------------- TemperatureConfigPacket -------------------- */
195+
196+
printf("Insert min temperature: ");
197+
read_from_stdin();
198+
uint16_t minTemp = atoi(inputString);
199+
200+
printf("Insert max temperature: ");
201+
read_from_stdin();
202+
uint16_t maxTemp = atoi(inputString);
203+
204+
printf("Insert thermistor resistance: ");
205+
read_from_stdin();
206+
float r1 = atof(inputString);
207+
208+
float c1 = 1.009249522e-03;
209+
float c2 = 2.378405444e-04;
210+
float c3 = 2.019202697e-07;
211+
212+
TemperatureConfigPacket p2 = { {TEMPERATURE_CONFIG_PACKET_TYPE, TEMPERATURE_CONFIG_PACKET_SIZE, 0}, minTemp, maxTemp, r1, c1, c2, c3 };
213+
PacketHandler_sendPacket(&packet_handler, (PacketHeader*) &p2);
214+
flushOutputBuffer(fd);
215+
216+
/* -------------------- PollutionConfigPacket -------------------- */
217+
218+
printf("Insert max pollution: ");
219+
read_from_stdin();
220+
uint16_t maxPoll = atoi(inputString);
221+
222+
PollutionConfigPacket p3 = { {POLLUTION_CONFIG_PACKET_TYPE, POLLUTION_CONFIG_PACKET_SIZE, 0}, maxPoll };
223+
PacketHandler_sendPacket(&packet_handler, (PacketHeader*) &p3);
224+
flushOutputBuffer(fd);
225+
226+
return 0;
227+
}

include/eepromParams.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ uint16_t get_EEPROM_minLight(void);
77
uint16_t get_EEPROM_maxLight(void);
88
uint16_t get_EEPROM_minTemp(void);
99
uint16_t get_EEPROM_maxTemp(void);
10-
uint16_t get_EEPROM_maxPoll(void);
11-
uint32_t get_EEPROM_r1(void);
10+
float get_EEPROM_r1(void);
1211
float get_EEPROM_c1(void);
1312
float get_EEPROM_c2(void);
1413
float get_EEPROM_c3(void);
14+
uint16_t get_EEPROM_maxPoll(void);
1515

1616
/* SET functions */
17-
void set_EEPROM_timer(char* str);
18-
void set_EEPROM_minLight(char* str);
19-
void set_EEPROM_maxLight(char* str);
20-
void set_EEPROM_minTemp(char* str);
21-
void set_EEPROM_maxTemp(char* str);
22-
void set_EEPROM_maxPoll(char* str);
23-
void set_EEPROM_r1(char* str);
24-
void set_EEPROM_c1(char* str);
25-
void set_EEPROM_c2(char* str);
26-
void set_EEPROM_c3(char* str);
17+
void set_EEPROM_timer(uint16_t duration);
18+
void set_EEPROM_minLight(uint16_t minLight);
19+
void set_EEPROM_maxLight(uint16_t maxLight);
20+
void set_EEPROM_minTemp(uint16_t minTemp);
21+
void set_EEPROM_maxTemp(uint16_t maxTemp);
22+
void set_EEPROM_r1(float R1);
23+
void set_EEPROM_c1(float C1);
24+
void set_EEPROM_c2(float C2);
25+
void set_EEPROM_c3(float C3);
26+
void set_EEPROM_maxPoll(uint16_t maxPoll);

0 commit comments

Comments
 (0)