File: make_conf.m

package info (click to toggle)
octave-arduino 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,616 kB
  • sloc: cpp: 3,221; python: 438; makefile: 152; xml: 22; sh: 1
file content (203 lines) | stat: -rw-r--r-- 5,960 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
## Copyright (C) 2018 John Donoghue <john.donoghue@ieee.org>
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see
## <https://www.gnu.org/licenses/>.

#Usage: octave --eval "make_conf('/usr/share/arduino/hardware/arduino/avr/variants/mega/pins_arduino.h')"

function retval = make_conf (header_file)

  if nargin != 1
    error ('Expected single input argument of header filename')
  endif

  pins = {};
  ispwm = {};
  isalong = {};
  num_digital_pins = 0;
  num_analog_pins = 0;

  fd = fopen (header_file, "rt");
  while !feof(fd)
    l = fgetl(fd);
    [t, m] = regexp(l, '#define\s+(\w+)\s+([^\s]+)$', 'tokens', 'match');
    if !isempty(t)
      toks = t(1){1};

      if numel(toks) == 2
        if strcmpi(toks{1}, "NUM_DIGITAL_PINS")
	  cnt = strrep(toks{2}, "u", "");
          num_digital_pins = str2num(cnt);
        elseif strcmpi(toks{1}, "NUM_ANALOG_INPUTS")
	  cnt = strrep(toks{2}, "u", "");
          num_analog_pins = str2num(cnt);
        else 
          idx = index(toks{1}, "PIN_");
          if idx > 0
            name = tolower(strtrim(toks{1}(idx+4:end)));
	    # id have other crap in it ?
	    id = strrep(toks{2}, "u", "");
            id = str2num(id);

	    if !isempty(id)

              # we have pin already ? 
              idx = find (cellfun(@(x) (x.id == id), pins), 1);
              if isempty(idx)
                p = [];
                p.id = id;
                p.name = sprintf("D%d", id);
                p.modes = { 'digital' };

                pins{end+1} = p;
                idx = numel(pins);
              endif

              if name(1) == 'a'
                pins{idx}.name = toupper(name);
                pins{idx}.modes{end+1} = "analog";
              elseif strncmp(name, "wire_", 5)
                pins{idx}.modes{end+1} = ["i2c" name(5:end)];
              elseif strncmp(name, "pin_led_", 8)
                pins{idx}.modes{end+1} = ["led"];
              else
                pins{idx}.modes{end+1} = name;
              endif
            endif  
          elseif strcmp(toks{1}, "LED_BUILTIN")
            id = str2num(toks{2});
	    if !isempty(id)

              idx = find (cellfun(@(x) (x.id == id), pins), 1);
              if isempty(idx)
                p = {};
                p.id = id;
                p.name = sprintf("D%d", id);
                p.modes = { 'digital' };

                pins{end+1} = p;
                idx = numel(pins);
              endif
            
              pins{idx}.modes{end+1} = "led";
            endif
          endif  
        endif
      endif
    endif
    
    [t, m] = regexp(l, '#define\s+digitalPinHasPWM\((\w+)\)\s+(.*)$', 'tokens', 'match');
    if ! isempty(t)
      toks = t(1){1};
      if numel(toks) == 2
         ispwm.var = toks{1};
	 testv = strrep(toks{2}, "u", "");
         ispwm.test = testv;
	 ispwm = {};
      endif
    endif
 
    [t, m] = regexp(l, '#define\s+analogInputToDigitalPin\((\w+)\)\s+(.*)$', 'tokens', 'match');
    if ! isempty(t)
      toks = t(1){1};
      if numel(toks) == 2
         isanalog.var = toks{1};
	 testv = strrep(toks{2}, "u", "");
	 # simple expect analog after digital
	 testv = sprintf("(p + %d)", num_digital_pins);
         isanalog.test = testv;
	 #isanalog={};
      endif
    endif

  endwhile

  fclose(fd);

  #num_digital_pins
  #num_analog_pins

  # fill in any missing digitals
  for i=0:num_analog_pins-1
    name = sprintf("A%d", i);
    idx = find (cellfun(@(x) (strcmpi(x.name,name)), pins), 1);
    if isempty(idx)
      t = sprintf("%s=%i; id=(%s);", isanalog.var, i, isanalog.test);
      eval(t);
      if id >= 0
        p = {};
        p.id = id;
        p.name = sprintf("A%d", i);
        p.modes = { 'digital', 'analog' };
        pins{end+1} = p;
        idx = numel(pins);
      endif
    endif
  endfor

  # fill in any missing digitals
  for i=0:num_digital_pins-1
    idx = find (cellfun(@(x) (x.id == i), pins), 1);
    if isempty(idx)
      p = {};
      p.id = i;
      p.name = sprintf("D%d", i);
      p.modes = { 'digital' };

      pins{end+1} = p;
      idx = numel(pins);
    endif
    if !isempty(ispwm)
      t = sprintf("%s=%i; pwm=(%s);", ispwm.var, i, ispwm.test);
      eval(t);
      if pwm
        pins{idx}.modes{end+1} = "pwm";
      endif
    endif
  endfor

  # sort by pin num (id)
  [tmp ind]=sort(cellfun(@(x) (x.id), pins));

  # output file
  printf ("# configuration generated from %s\n", header_file)
  printf("function retval = config_XXX (initdata)\n");
  printf("  retval = {};\n");
  printf("\n");
  printf("  # default board info - must be provided\n");
  printf("  # will be filled in on connection.\n");
  printf("  retval.board = '';\n");
  printf("  retval.baudrate = 9600;\n");
  printf("  retval.mcu = '';\n");
  printf("  retval.voltref = 0;\n");
  printf("  retval.libs = {};\n");
  printf("  retval.port = '';\n");
  printf("\n");
  printf("  # info expected to be provided by config.\n");
  printf("  retval.description = 'a board description';\n");
  printf("\n");

  printf("  # pin config\n");
  printf("  retval.pins = {};\n");
  for i = 1:numel(ind)
    p = pins{ind(i)};
    modes = [ "'" p.modes{1} "'"];
    for m = 2:numel(p.modes)
      modes = [ modes ", '" p.modes{m} "'" ];
    endfor
    printf("  retval.pins{end+1} = arduinoio.config.pin_info('%s', %d, { %s });\n", p.name, p.id, modes);
  endfor

  printf("endfunction\n");
endfunction