Appiko
separate_values.py
1 #!/usr/bin/env python
2 import glob, os
3 import datetime
4 import sys
5 
6 ledseq_header = open("led_seq.h" ,"w")
7 ledseq_source = open("led_seq.c" ,"w")
8 
9 #TODO read the heading in the sequence to create the appropriate
10 #enums (RED, GREEN etc.), defines and mention so in the header too
11 header1 = """/**
12  * @file led_seq.h Header to access the PWM values for the
13  * one or more color LED(s) for the different sequences
14  *
15  * Automagically created on: """
16 date = datetime.datetime.now().strftime('%d-%m-%Y at %H:%M')
17 
18 source1 = """/**
19  * @file led_seq.c Contains the PWM values for the one or more color LED(s)
20  * for the different sequences
21  *
22  * Automagically created on: """
23 
24 source2 = """ */
25 
26 #include "led_seq.h"
27 #include "boards.h"
28 
29 """
30 
31 source3 = """
32 const uint32_t led_pin_num[LED_COLOR_MAX] = {LED_RED, LED_GREEN};
33 
34 const uint32_t * const led_seq_get_pin_ptr(void)
35 {
36  return led_pin_num;
37 }
38 
39 uint32_t led_seq_get_pin_num(led_sequences seq)
40 {
41  return led_num_len[seq];
42 }
43 
44 uint32_t led_seq_get_seg_len(led_sequences seq)
45 {
46  return led_seq_len[seq];
47 }
48 
49 uint16_t * led_seq_get_seq_color_ptr(led_sequences seq, led_seq_color color)
50 {
51  return ((uint16_t *)(led_seq_ptr[seq] + color*led_seq_len[seq]));
52 }
53 
54 uint16_t * led_seq_get_seq_duration_ptr(led_sequences seq)
55 {
56  return (uint16_t *) led_seq_duration_ptr[seq];
57 }
58 
59 """
60 
61 header2 = "\n */\n\n#ifndef _LED_SEQ_H_\n#define _LED_SEQ_H_\n\n#include <stdint.h>\n"
62 rgb_strut = """#define LED_STRUCT1(name, count) const struct { \\
63  uint16_t red[count]; \\
64  } __attribute__((packed)) name
65 
66 #define LED_STRUCT2(name, count) const struct { \\
67  uint16_t red[count]; \\
68  uint16_t green[count]; \\
69  } __attribute__((packed)) name
70 """
71 
72 enum_def = """
73 /** Specify the different LED patterns possible */
74 typedef enum {\n"""
75 
76 enum_end = """ LED_SEQ_NULL = 255,
77 } led_sequences;
78 
79 /** @brief The different color LEDs present in this instance */
80 typedef enum {
81  LED_COLOR_RED,
82  LED_COLOR_GREEN,
83  /// To specify the number of LEDs presents
84  LED_COLOR_MAX
85 }led_seq_color;
86 
87 /**
88  * @brief Gets the pointer to the array containing LED pin numbers
89  * with a length of @ref LED_COLOR_MAX
90  * @return Constant pointer to a const array for reason mention in brief
91  */
92 const uint32_t * const led_seq_get_pin_ptr(void);
93 
94 /**
95  * @brief Gets the number of LEDs used in the sequence
96  * @param seq The sequence to query the number of LEDs
97  * @return Returns the number of LEDs in the sequence
98  */
99 uint32_t led_seq_get_pin_num(led_sequences seq);
100 
101 /**
102  * @brief Get the number of segments in the sequence
103  * @param seq The sequence whose number of segments is required
104  * @return The number of segments in the parameter's sequence
105  */
106 uint32_t led_seq_get_seg_len(led_sequences seq);
107 
108 /**
109  * @brief Gets the pointer to the start of values for a color for a sequence
110  * @param seq The sequence whose pointer is required
111  * @param color The color in question
112  * @return Pointer to the start of a color in a sequence
113  */
114 uint16_t * led_seq_get_seq_color_ptr(led_sequences seq, led_seq_color color);
115 
116 /**
117  * @brief Gets the pointer to the start of segment duration array for a sequence
118  * @param seq The sequence whose pointer is required
119  * @return Pointer to the start of duration in a sequence
120  */
121 uint16_t * led_seq_get_seq_duration_ptr(led_sequences seq);
122 
123 #endif /* _LED_SEQ_H_ */
124 """
125 
126 unit_time_ms = 1
127 
128 pwm_max_value = 1000
129 inp_res = 1000
130 
131 ledseq_header.write(header1 + date + header2)
132 
133 ledseq_source.write(source1 + date + source2 + rgb_strut)
134 
135 ledseq_header.write(enum_def)
136 
137 i = 0
138 for file in glob.glob("*.txt"):
139  curr_seq = file.split(".")[0]
140  ledseq_header.write(" LED_SEQ_" + curr_seq.upper() + " = " + str(i) + ",\n" )
141  i=i+1
142 
143 ledseq_header.write(enum_end)
144 
145 len_list = []
146 seq_list = []
147 duration_list = []
148 led_num_list = []
149 
150 for file in glob.glob("*.txt"):
151  ip0_file = open(file, "r")
152  ip0_txt = ip0_file.read().replace("\r","\n")
153  ip0 = ip0_txt.split("\n")
154  ip0_file.close()
155 
156  curr_seq = file.split(".")[0]
157  len_list.append(len(ip0))
158  seq_list.append("(uint16_t *) &" + str(curr_seq) + "_seq")
159  duration_list.append(str(curr_seq) + "_duration")
160 
161  num_elements = 0
162 
163  R_list = []
164  G_list = []
165  ms_list = []
166 
167  for line in ip0:
168  line = line.replace(";","")
169  num_elements = len(line.split())
170  if num_elements == 3 :
171  R, G, ms = line.split()
172  R = round((int(R) * pwm_max_value/inp_res),0)
173  G = round((int(G) * pwm_max_value/inp_res),0)
174  ms = round((int(ms)/unit_time_ms),0)
175  R_list.append(int(R))
176  G_list.append(int(G))
177  ms_list.append(int(ms))
178  elif num_elements == 2 :
179  R, ms = line.split()
180  R = round((int(R) * pwm_max_value/inp_res),0)
181  ms = round((int(ms)/unit_time_ms),0)
182  R_list.append(int(R))
183  ms_list.append(int(ms))
184  else:
185  sys.exit("Error: "+ file + "has number of elements that's not 2 or 3")
186 
187  if num_elements == 3 :
188  ledseq_source.write("\nLED_STRUCT2(" + curr_seq + "_seq, " + str(len(ip0)) +") = {\n")
189  ledseq_source.write(" {" + str(R_list).strip('[]') + "},\n")
190  ledseq_source.write(" {" + str(G_list).strip('[]') + "}\n};\n")
191 
192  if num_elements == 2 :
193  ledseq_source.write("\nLED_STRUCT1(" + curr_seq + "_seq, " + str(len(ip0)) +") = {\n")
194  ledseq_source.write(" {" + str(R_list).strip('[]') + "}\n};\n")
195 
196  total_duration = sum(ms_list) - ms_list[0]
197  ms_list.append(total_duration)
198  led_num_list.append(num_elements-1)
199 
200  ledseq_source.write("const uint16_t " + curr_seq + "_duration[] = { " + str(ms_list).strip('[]') + " };\n")
201 
202 ledseq_source.write("\nconst uint32_t led_num_len[] = {" + str(led_num_list).strip('[]') + "};\n")
203 ledseq_source.write("const uint32_t led_seq_len[] = {" + str(len_list).strip('[]') + "};\n")
204 ledseq_source.write("const uint16_t * const led_seq_ptr[] = {" + ', '.join(seq_list) + " };\n")
205 ledseq_source.write("const uint16_t * const led_seq_duration_ptr[] = { " + ', '.join(duration_list) + " };\n")
206 
207 #ledseq_header.write(header3)
208 ledseq_source.write(source3)
209 
210 ledseq_header.close()
211 ledseq_source.close()