@@ -8,18 +8,17 @@ namespace fs = std::filesystem;
88
99std::set<std::string> usedFieldNames;
1010
11- std::string get_cmake_board_name (std::string board_name);
11+ std::string get_cmake_board_name (Board board);
12+ std::string generateParameters (std::string descriptor);
1213
13- void build_pico (std::string project_name, std::string board_name )
14+ void build_pico (std::string project_name, Board board )
1415{
1516 if (!getenv (" PICO_SDK_PATH" ))
1617 {
1718 fmt::print (" $PICO_SDK_PATH is not set nor accessible. Aborting.\n " );
18- // return;
19+ return ;
1920 }
2021
21- bool isPicoW = board_name == " PicoW" ;
22-
2322 auto currentPath = fs::current_path ();
2423 auto tempPath = fs::temp_directory_path ();
2524 std::string tempDir = " java-pico" ;
@@ -32,15 +31,20 @@ void build_pico(std::string project_name, std::string board_name)
3231 std::ofstream output_c (" main.cpp" );
3332
3433 output_c << " #include \" pico-java.h\"\n "
35- " \n "
36- " void static_init();\n "
3734 " \n " ;
3835
3936 output_c << " namespace " << project_name << " \n {\n " ;
4037 for (auto & field : fields)
4138 {
4239 output_c << ' \t ' << field.type << " " << field.name << " ;\n " ;
4340 }
41+ for (auto & func : functions)
42+ {
43+ if (func.name != " main" )
44+ {
45+ output_c << ' \t ' << getReturnType (func.descriptor ) << " " << func.name << " (" << generateParameters (func.descriptor ) << " );\n " ;
46+ }
47+ }
4448 output_c << " }\n\n " ;
4549
4650 bool has_static_init = false ;
@@ -50,10 +54,22 @@ void build_pico(std::string project_name, std::string board_name)
5054 {
5155 has_static_init = true ;
5256 }
57+ }
5358
59+ for (auto & func : functions)
60+ {
5461 bool isMain = func.name == " main" ;
5562
56- output_c << (isMain ? " int" : getReturnType (func.descriptor )) << " " << func.name << " ()\n {\n " ;
63+ if (!isMain)
64+ {
65+ output_c << " namespace " << project_name << " {\n " ;
66+ }
67+
68+ output_c << (isMain ? " int" : getReturnType (func.descriptor )) << " " << func.name << " (" << (isMain ? " " : generateParameters (func.descriptor )) << " )\n {\n " ;
69+ if (isMain && has_static_init)
70+ {
71+ output_c << " \t " << project_name << " ::static_init();\n " ;
72+ }
5773 for (auto & inst : func.instructions )
5874 {
5975 if (inst.label .has_value ())
@@ -63,15 +79,17 @@ void build_pico(std::string project_name, std::string board_name)
6379
6480 output_c << ' \t ' << inst.opcode << ' \n ' ;
6581 }
66- output_c << " }\n\n " ;
67- }
82+ output_c << " }\n " ;
6883
69- if (!has_static_init)
70- {
71- output_c << " void static_init() {}\n\n " ;
84+ if (!isMain)
85+ {
86+ output_c << " }\n " ;
87+ }
88+
89+ output_c << ' \n ' ;
7290 }
7391
74- if (isPicoW )
92+ if (board == Board::PicoW )
7593 {
7694 libs = " pico_cyw43_arch_none" ;
7795 }
@@ -99,7 +117,7 @@ pico_add_extra_outputs({0})
99117
100118target_link_libraries({0} pico_stdlib {1})
101119
102- )___" , project_name, libs, get_cmake_board_name (board_name ));
120+ )___" , project_name, libs, get_cmake_board_name (board ));
103121
104122 output_cmake.close ();
105123
@@ -122,6 +140,8 @@ namespace pico
122140 {
123141 static inline int INPUT = GPIO_IN;
124142 static inline int OUTPUT = GPIO_OUT;
143+ static inline int IRQ_EDGE_RISE = GPIO_IRQ_EDGE_RISE;
144+ static inline int IRQ_EDGE_FALL = GPIO_IRQ_EDGE_FALL;
125145
126146 inline void init(int pin)
127147 {
@@ -148,6 +168,21 @@ namespace pico
148168 gpio_pull_up(pin);
149169 }
150170
171+ inline void pull_down(int pin)
172+ {
173+ gpio_pull_down(pin);
174+ }
175+
176+ inline void set_input_enabled(int pin, bool enabled)
177+ {
178+ gpio_set_input_enabled(pin, enabled);
179+ }
180+
181+ inline void set_irq_enabled_with_callback(int pin, int events, bool enabled, gpio_irq_callback_t callback)
182+ {
183+ gpio_set_irq_enabled_with_callback(pin, events, enabled, callback);
184+ }
185+
151186 inline void set_mask(int pin)
152187 {
153188 gpio_set_mask(pin);
@@ -169,7 +204,7 @@ namespace pico
169204}
170205)___" ;
171206
172- if (isPicoW )
207+ if (board == Board::PicoW )
173208 {
174209 output_header << R"___(
175210#include "pico/cyw43_arch.h"
@@ -202,12 +237,49 @@ namespace pico
202237 system (fmt::format (" cp {}.uf2 {}" , project_name, currentPath.string ()).data ());
203238}
204239
205- std::string get_cmake_board_name (std::string board_name )
240+ std::string get_cmake_board_name (Board board )
206241{
207- if (board_name == " Pico" ) return " pico" ;
208- if (board_name == " PicoW" ) return " pico_w" ;
209- if (board_name == " Tiny2040" ) return " pimoroni_tiny2040" ;
210- if (board_name == " Tiny2040_2mb" ) return " pimoroni_tiny2040_2mb" ;
242+ if (board == Board:: Pico) return " pico" ;
243+ if (board == Board:: PicoW) return " pico_w" ;
244+ if (board == Board:: Tiny2040) return " pimoroni_tiny2040" ;
245+ if (board == Board:: Tiny2040_2mb) return " pimoroni_tiny2040_2mb" ;
211246
212247 assert (false );
213248}
249+
250+ std::string generateParameters (std::string descriptor)
251+ {
252+ std::string ret;
253+
254+ int count = 0 ;
255+ assert (descriptor[0 ] == ' (' );
256+ for (size_t index = 1 ; index < descriptor.size (); ++index)
257+ {
258+ switch (descriptor[index])
259+ {
260+ case ' )' :
261+ index = descriptor.size ();
262+ break ;
263+ case ' L' :
264+ while (descriptor[index] != ' ;' ) ++index;
265+ assert (false );
266+ break ;
267+ case ' I' :
268+ ret += fmt::format (" , int ilocal_{}" , count);
269+ ++count;
270+ break ;
271+ case ' Z' :
272+ ret += fmt::format (" , int ilocal_{}" , count);
273+ ++count;
274+ break ;
275+ default :
276+ assert (false );
277+ }
278+ }
279+
280+ if (ret.size () > 0 )
281+ {
282+ ret = ret.substr (2 );
283+ }
284+ return ret;
285+ }
0 commit comments