Skip to content

Commit 3c72b6d

Browse files
committed
Added the deleted content from code generation
1 parent 9c87b50 commit 3c72b6d

7 files changed

Lines changed: 161 additions & 107 deletions

File tree

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
#pragma once
22
#include "ST-LIB.hpp"
33

4-
/*Data packets for {{board}}
4+
/*Data packets for {{board}}
55
-AUTOGENERATED CODE, DO NOT MODIFY-*/
66
class DataPackets{
77
public:
88
{% for enum in enums -%}
9-
enum class {{enum.name}} : uint8_t
9+
enum class {{enum.name}} : uint8_t
1010
{
1111
{%- for value in enum["values"] %}
1212
{{value}} = {{loop.index0}},
1313
{%- endfor %}
1414
};
1515
{% endfor %}
16-
16+
1717
{% for packet in packets -%}
1818
static void {{packet.name}}_init({% for variable in packet.variables %}{{variable.type}} &{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %})
1919
{
2020
{{packet.name}}_packet = new HeapPacket(static_cast<uint16_t>({{packet.id}}){% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %});
2121
}
22-
22+
2323
{% endfor -%}
24-
24+
2525
public:
2626
{%for packet in packets -%}
2727
inline static HeapPacket *{{packet.name}}_packet{nullptr};
2828
{% endfor %}
2929
{% for socket in DatagramSockets -%}
3030
inline static {{socket.type}} *{{socket.name}}{nullptr};
3131
{% endfor %}
32-
32+
3333
static void start()
34-
{
34+
{
3535
{% for packet in packets -%}
3636
if ({{packet.name}}_packet == nullptr) {
3737
ErrorHandler("Packet {{packet.name}} not initialized");
@@ -41,7 +41,7 @@ class DataPackets{
4141
{% for socket in DatagramSockets -%}
4242
{{socket.name}} = new DatagramSocket("{{socket.board_ip}}",{{socket.port}},"{{socket.remote_ip}}",{{socket.port}});
4343
{% endfor %}
44-
44+
4545
{%- for group in sending_packets %}
4646
Scheduler::register_task({% if group.period_type == "ms" %}{{ (group.period*1000)|round|int }}{% else %}{{ group.period|round|int }}{% endif %}, +[](){
4747
{% for packet in group.packets -%}
@@ -52,5 +52,5 @@ class DataPackets{
5252
}
5353

5454

55-
55+
5656
};

Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#pragma once
22
#include "ST-LIB.hpp"
33

4-
/*Order packets for {{board}}
4+
/*Order packets for {{board}}
55
-AUTOGENERATED CODE, DO NOT MODIFY- */
66

77

88
class OrderPackets{
99
public:
1010
{% for enum in enums -%}
11-
enum class {{enum.name}} : uint8_t
11+
enum class {{enum.name}} : uint8_t
1212
{
1313
{%- for value in enum["values"] %}
1414
{{value}} = {{loop.index0}},
@@ -25,7 +25,7 @@ class OrderPackets{
2525
{% for packet in packets -%}
2626
inline static HeapOrder *{{packet.name}}_order{nullptr};
2727
{% endfor %}
28-
28+
2929
{% for packet in packets -%}
3030
static void {{packet.name}}_init({% for variable in packet.variables %}{{variable.type}} &{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %})
3131
{
@@ -63,4 +63,4 @@ class OrderPackets{
6363
{{packet.name}}_flag = true;
6464
}
6565
{% endfor %}
66-
};
66+
};

Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import re
1+
import re
22
import json
33

44
class BoardDescription:
@@ -13,7 +13,7 @@ def __init__(self,name:str,board:dict,JSONpath:str):
1313
self.sockets=self.SocketsDescription(socks,self.ip)
1414
except Exception as e:
1515
raise Exception(f"Error in file {JSONpath}/boards/{name}/sockets.json: {e}")
16-
#Packets:
16+
#Packets:
1717
self.sending_packets = []
1818
self.data_size =0
1919
self.order_size =0
@@ -40,15 +40,15 @@ def __init__(self,name:str,board:dict,JSONpath:str):
4040
aux_sending= PacketDescription.check_for_sending(packet)
4141
if aux_sending is not None:
4242
self.sending_packets.append(aux_sending)
43-
43+
4444
if self.packets[packets_name][i].type != "order":
4545
self.data_size += 1
4646
else:
4747
self.order_size += 1
4848
i += 1
49-
49+
5050
self.sending_packets = self.fix_sendind_packets(self.sending_packets)
51-
51+
5252
@staticmethod
5353
def fix_sendind_packets(sending_packets:list):
5454
fixed_packets = []
@@ -72,9 +72,9 @@ def fix_sendind_packets(sending_packets:list):
7272
fixed_packets.append(entry)
7373

7474
return fixed_packets
75-
76-
77-
75+
76+
77+
7878
class SocketsDescription:
7979
def __init__(self,sockets:list,board_ip:str):
8080
self.allSockets=[]
@@ -86,16 +86,16 @@ def __init__(self,sockets:list,board_ip:str):
8686
name = sock["name"].replace(" ", "_").replace("-", "_")
8787
sock_type = sock["type"]
8888
self.allSockets.append({"name": name,"type":sock_type})
89-
89+
9090
if sock_type == "ServerSocket":
9191
self.ServerSockets.append({"name": name,"type":sock_type,"board_ip":self.board_ip, "port": sock["port"]})
9292
elif sock_type == "Socket":
9393
self.Sockets.append({"name": name,"type":sock_type,"board_ip":self.board_ip, "local_port": sock["local_port"], "remote_ip": sock["remote_ip"], "remote_port": sock["remote_port"]})
9494
elif sock_type == "DatagramSocket":
9595
self.DatagramSockets.append({"name": name,"type":sock_type,"board_ip":self.board_ip, "port": sock["port"],"remote_ip":sock["remote_ip"]})
96+
9697

97-
98-
98+
9999
class PacketDescription:
100100
def __init__(self, packet:dict,measurements:list, filename:str="Unknown"):
101101
self.id =packet["id"]
@@ -108,13 +108,13 @@ def __init__(self, packet:dict,measurements:list, filename:str="Unknown"):
108108
for variable in packet["variables"]:
109109
self.variables.append(variable)
110110
self.measurements.append(MeasurmentsDescription(measurements,variable, filename))
111-
111+
112112
@staticmethod
113113
def check_for_sending(packet:dict):
114114
if "period" in packet and "period_type" in packet and "socket" in packet:
115115
name = packet["name"].replace(" ", "_").replace("-", "_")
116116
return {"name": name,"period": packet["period"],"period_type":packet["period_type"],"socket": packet["socket"]}
117-
117+
118118
elif "period_ms" in packet and "socket" in packet:
119119
name = packet["name"].replace(" ", "_").replace("-", "_")
120120
return {"name": name,"period": packet["period_ms"],"period_type":"ms","socket": packet["socket"]}
@@ -126,11 +126,11 @@ def __init__(self,measurements:list, variable:str, filename:str="Unknown"):
126126
if not hasattr(self.__class__, 'viewed_measurements'):
127127
self.__class__.viewed_measurements = {}
128128
measurement = self._MeasurementSearch(measurements,variable)
129-
129+
130130
if measurement is None:
131131
print(f"Measurement not found for variable: {variable} in file: {filename}\n")
132132
raise Exception(f"Measurement not found for variable: {variable} in file: {filename}")
133-
133+
134134
self.name = measurement["name"]
135135
self.type = (self._unsigned_int_correction(measurement["type"]).replace(" ", "_").replace("-", "_"))
136136
if self.type == "enum":
@@ -145,8 +145,8 @@ def _Enum_values_correction(values:list):
145145
for i in range(len(values)):
146146
values[i] = values[i].replace(" ", "_").replace("-", "_")
147147
return values
148-
149-
148+
149+
150150
@staticmethod
151151
def _MeasurementSearch(measurements:list, variable:str):
152152
if variable in MeasurmentsDescription.viewed_measurements:
@@ -157,13 +157,15 @@ def _MeasurementSearch(measurements:list, variable:str):
157157
MeasurmentsDescription.viewed_measurements[variable] = measurment
158158
return measurment
159159
return None
160-
161-
160+
161+
162162
@staticmethod
163163
def _unsigned_int_correction(type:str):
164164
aux_type = type[:4]
165165
if aux_type == "uint":
166166
type += "_t"
167167
elif type == "float32":
168168
type = "float"
169-
return type
169+
elif type == "float64":
170+
type = "double"
171+
return type

Core/Inc/Code_generation/Packet_generation/Packet_generation.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
templates_path = "Core/Inc/Code_generation/Packet_generation"
88

9-
def Generate_PacketDescription(JSONpath:str,board:str):
9+
def Generate_PacketDescription(JSONpath:str,board:str):
1010
with open(JSONpath+"/boards.json") as f:
1111
boards = json.load(f)
1212
boards_name = []
@@ -20,9 +20,9 @@ def Generate_PacketDescription(JSONpath:str,board:str):
2020
else:
2121
print(f"Board {board} not found, exiting...")
2222
sys.exit()
23-
23+
2424
return boards_name
25-
25+
2626

2727
#--------------DataPackets.hpp generation---------------#
2828

@@ -36,8 +36,8 @@ def GenerateDataEnum(board:BoardDescription):
3636
if hasattr(measurement, "enum")and measurement.enum not in Enums:
3737
Enums.append(measurement.enum)
3838
return Enums
39-
40-
39+
40+
4141
def GenerateDataPackets(board:BoardDescription):
4242
Packets =[]
4343
totaldata = []
@@ -50,10 +50,10 @@ def GenerateDataPackets(board:BoardDescription):
5050
tempdata +=(str(variable) +",")
5151
tempdata_but_pointer +=("&"+str(variable) +",")
5252
if tempdata.endswith(","):
53-
tempdata = tempdata[:-1]
53+
tempdata = tempdata[:-1]
5454
if tempdata_but_pointer.endswith(","):
5555
tempdata_but_pointer = tempdata_but_pointer[:-1]
56-
56+
5757
packet_variables = []
5858
for measurement in packet_instance.measurements:
5959
packet_variables.append({
@@ -67,11 +67,11 @@ def GenerateDataPackets(board:BoardDescription):
6767
aux_data = {"type": measurement.type, "name": measurement.id.replace(" ", "_").replace("-", "_")}
6868
if not any(x["name"] == aux_data["name"] for x in totaldata):
6969
totaldata.append(aux_data)
70-
70+
7171
return Packets,totaldata
72-
72+
7373
packets,data = GenerateDataPackets(board)
74-
74+
7575
def GenerateGroupedSendingPackets(board: BoardDescription):
7676
datagram_sockets = [s["name"] for s in board.sockets.DatagramSockets]
7777
grouped_lookup = {}
@@ -84,17 +84,17 @@ def GenerateGroupedSendingPackets(board: BoardDescription):
8484
period = packet["period"]
8585
period_type = packet["period_type"]
8686
names = packet["name"]
87-
87+
8888
key = (period, period_type)
8989
if key not in grouped_lookup:
9090
grouped_lookup[key] = []
91-
91+
9292
if isinstance(names, list):
9393
for name in names:
9494
grouped_lookup[key].append({"socket": socket_name, "name": name})
9595
else:
9696
grouped_lookup[key].append({"socket": socket_name, "name": names})
97-
97+
9898
grouped_list = []
9999
for (period, period_type), items in grouped_lookup.items():
100100
grouped_list.append({
@@ -123,16 +123,16 @@ def Generate_DataPackets_hpp(board_input:str):
123123
if board_instance.data_size == 0:
124124
if os.path.exists(data_packets_path):
125125
os.remove(data_packets_path)
126-
return
127-
126+
return
127+
128128
env= jinja2.Environment(loader=jinja2.FileSystemLoader(templates_path))
129129
template = env.get_template("DataTemplate.hpp")
130130
context = Get_data_context(board_instance)
131131

132-
132+
133133
with open(data_packets_path,"w") as Output:
134134
Output.write(template.render(context))
135-
135+
136136
#--------------OrderPackets.hpp generation---------------#
137137

138138
def Get_order_context(board:BoardDescription):
@@ -145,8 +145,8 @@ def GenerateOrderEnum(board:BoardDescription):
145145
if hasattr(measurement, "enum") and measurement.enum not in Enums:
146146
Enums.append(measurement.enum)
147147
return Enums
148-
149-
148+
149+
150150
def GenerateOrderPackets(board:BoardDescription):
151151
Packets =[]
152152
totaldata = []
@@ -159,8 +159,8 @@ def GenerateOrderPackets(board:BoardDescription):
159159
tempdata +=(str(variable) +",")
160160
tempdata_but_pointer +=("&"+str(variable) +",")
161161
if tempdata.endswith(","):
162-
tempdata = tempdata[:-1]
163-
tempdata_but_pointer = tempdata_but_pointer[:-1]
162+
tempdata = tempdata[:-1]
163+
tempdata_but_pointer = tempdata_but_pointer[:-1]
164164

165165
packet_variables = []
166166
for measurement in packet_instance.measurements:
@@ -175,10 +175,10 @@ def GenerateOrderPackets(board:BoardDescription):
175175
aux_data = {"type": measurement.type, "name": measurement.id}
176176
if not any(x["name"] == aux_data["name"] for x in totaldata):
177177
totaldata.append(aux_data)
178-
178+
179179
return Packets,totaldata
180-
181-
180+
181+
182182
packets,data = GenerateOrderPackets(board)
183183
context = {
184184
"board": board.name,
@@ -197,12 +197,14 @@ def Generate_OrderPackets_hpp(board_input:str):
197197
if board_instance.order_size == 0:
198198
if os.path.exists(order_packets_path):
199199
os.remove(order_packets_path)
200-
return
201-
200+
return
201+
202202
env= jinja2.Environment(loader=jinja2.FileSystemLoader(templates_path))
203203
template = env.get_template("OrderTemplate.hpp")
204204
context = Get_order_context(board_instance)
205205

206-
206+
207207
with open(order_packets_path,"w") as Output:
208208
Output.write(template.render(context))
209+
210+

0 commit comments

Comments
 (0)