-
Notifications
You must be signed in to change notification settings - Fork 21
/
generate.rb
132 lines (120 loc) · 1.58 KB
/
generate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Ruby script to help generate the FastGPIO library code.
class PinConfig
attr_reader :name
attr_reader :pin_list
def initialize(pin_list_str)
@pin_list = pin_list_str.split.map { |s| { port: s[0], bit: s[1].to_i } }
end
def macros
h = {}
each_pin do |num, port, bit|
h["IO_#{port}#{bit}"] = num
end
h
end
def macro_definitions
r = ''
macros.each do |name, num|
r << "#define #{name} #{num}\n"
end
r
end
def array
r = ''
r << " const IOStruct pinStructs[] = {\n"
each_pin do |num, port, bit|
r << " _FG_PIN(#{port}, #{bit}),\n"
end
r << " };"
r
end
def table
r = ''
r << <<END
<table>
<tr><th>Number</th><th>AVR pin macro</th><th>Alternative name</th></tr>
END
each_pin do |num, port, bit|
r << "<tr><td>#{num}</td><td>IO_#{port}#{bit}</td><td></td></tr>\n"
end
r << '</table>'
r
end
def each_pin
@pin_list.each_with_index do |pin, num|
yield num, pin.fetch(:port), pin.fetch(:bit)
end
end
end
def keywords(pin_configs)
r = ''
pin_keywords = pin_configs.flat_map { |c| c.macros.keys }.uniq.sort
pin_keywords.each do |pin_keyword|
r << (pin_keyword + "\tLITERAL1\n")
end
r << "IO_NONE\tLITERAL1\n"
r
end
leonardo = PinConfig.new <<END
D2
D3
D1
D0
D4
C6
D7
E6
B4
B5
B6
B7
D6
C7
B3
B1
B2
B0
F7
F6
F5
F4
F1
F0
D4
D7
B4
B5
B6
D6
D5
E2
E0
END
uno = PinConfig.new <<END
D0
D1
D2
D3
D4
D5
D6
D7
B0
B1
B2
B3
B4
B5
C0
C1
C2
C3
C4
C5
C6
C7
END
#puts leonardo.macros
#puts uno.table
#puts leonardo.table
puts keywords([leonardo, uno])