Skip to content

Commit 454b0cc

Browse files
committed
improved a bit
1 parent 4450ccf commit 454b0cc

13 files changed

Lines changed: 376 additions & 130 deletions

‎boards/pico.cpp‎

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ namespace fs = std::filesystem;
88

99
std::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
100118
target_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+
}

‎boards/pico.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef PICO_H
22
#define PICO_H
33

4-
#include <string>
4+
#include "globals.h"
55

6-
void build_pico(std::string project_name, std::string board_name);
6+
void build_pico(std::string project_name, Board board);
77

88
#endif // PICO_H

‎globals.h‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <set>
1717
#include <utility>
1818
#include <cassert>
19-
#include "boards/pico.h"
19+
#include <boost/algorithm/string/replace.hpp>
2020

2121
using u1 = uint8_t;
2222
using u2 = uint16_t;
@@ -85,14 +85,19 @@ enum
8585
ishl = 0x78,
8686
iinc = 0x84,
8787
ifne = 0x9a,
88+
if_icmpeq = 0x9f,
8889
if_icmpne = 0xa0,
90+
if_icmplt = 0xa1,
91+
if_icmpge = 0xa2,
92+
if_icmpgt = 0xa3,
8993
if_icmple = 0xa4,
9094
goto_ = 0xa7,
9195
iand = 0x7e,
9296
return_ = 0xb1,
9397
getstatic = 0xb2,
9498
putstatic = 0xb3,
9599
invokestatic = 0xb8,
100+
invokedynamic = 0xba,
96101
newarray = 0xbc,
97102
arraylength = 0xbe,
98103
};
@@ -130,6 +135,26 @@ enum {
130135
CONSTANT_InvokeDynamic = 18,
131136
};
132137

138+
enum {
139+
REF_getField = 1,
140+
REF_getStatic = 2,
141+
REF_putField = 3,
142+
REF_putStatic = 4,
143+
REF_invokeVirtual = 5,
144+
REF_invokeStatic = 6,
145+
REF_invokeSpecial = 7,
146+
REF_newInvokeSpecial = 8,
147+
REF_invokeInterface = 9,
148+
};
149+
150+
enum class Board
151+
{
152+
Pico,
153+
PicoW,
154+
Tiny2040,
155+
Tiny2040_2mb,
156+
};
157+
133158
struct Fieldref
134159
{
135160
u2 class_index;
@@ -148,6 +173,18 @@ struct InterfaceMethodref
148173
u2 name_and_type_index;
149174
};
150175

176+
struct InvokeDynamic
177+
{
178+
u2 bootstrap_method_attr_index;
179+
u2 name_and_type_index;
180+
};
181+
182+
struct MethodHandle
183+
{
184+
u1 reference_kind;
185+
u2 reference_index;
186+
};
187+
151188
struct Class
152189
{
153190
u2 name_index;
@@ -181,13 +218,15 @@ struct method_info
181218
std::vector<attribute_info> attributes;
182219
};
183220

184-
using Constant = std::variant<Fieldref, Methodref, InterfaceMethodref, Class, NameAndType, Utf8>;
221+
using Constant = std::variant<Fieldref, Methodref, InterfaceMethodref, Class, NameAndType, Utf8, InvokeDynamic, MethodHandle>;
185222
using ConstantPool = std::vector<Constant>;
186223
extern ConstantPool constantPool;
224+
extern std::vector<std::string> callbacksMethods;
187225

188226
std::string getStringFromUtf8(int index);
189227
u4 countArgs(std::string str);
190-
std::string getTypeFromDescriptor(std::string descriptor);
191228
std::string getReturnType(std::string descriptor);
192229

230+
#define UNDEFINED_POSITION 999999
231+
193232
#endif // GLOBALS_H

‎java/Tiny2040blinky.java‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,49 @@ class Tiny2040blinky
66
static int RED_LED = 18;
77
static int GREEN_LED = 19;
88
static int BLUE_LED = 20;
9+
static int IRQ_PIN = 23;
10+
static int counter = 0;
911

1012
public static void main(String[] args)
1113
{
1214
stdio.init_all();
1315

1416
gpio.init(RED_LED);
1517
gpio.set_dir(RED_LED, gpio.OUTPUT);
16-
gpio.put(RED_LED, 1);
18+
gpio.put(RED_LED, 0);
1719

1820
gpio.init(GREEN_LED);
1921
gpio.set_dir(GREEN_LED, gpio.OUTPUT);
20-
gpio.put(GREEN_LED, 1);
22+
gpio.put(GREEN_LED, 0);
2123

2224
gpio.init(BLUE_LED);
2325
gpio.set_dir(BLUE_LED, gpio.OUTPUT);
24-
gpio.put(BLUE_LED, 1);
26+
gpio.put(BLUE_LED, 0);
2527

26-
int counter = 0;
28+
gpio.pull_down(IRQ_PIN);
29+
gpio.set_input_enabled(IRQ_PIN, true);
30+
gpio.set_irq_enabled_with_callback(IRQ_PIN, gpio.IRQ_EDGE_RISE, true, Tiny2040blinky::gpio_irq_callback);
2731

2832
while (true)
2933
{
30-
gpio.put(RED_LED, counter & 1);
31-
gpio.put(GREEN_LED, counter & 2);
32-
gpio.put(BLUE_LED, counter & 4);
3334
time.sleep_ms(1000);
35+
}
36+
}
3437

35-
counter = (counter + 1) % 8;
38+
static void gpio_irq_callback(int pin, int events)
39+
{
40+
if (events == gpio.IRQ_EDGE_RISE)
41+
{
42+
updateColour();
3643
}
3744
}
45+
46+
static void updateColour()
47+
{
48+
counter = (counter + 1) % 8;
49+
50+
gpio.put(RED_LED, counter & 1);
51+
gpio.put(GREEN_LED, counter & 2);
52+
gpio.put(BLUE_LED, counter & 4);
53+
}
3854
}

‎java/pico/BoardType.class‎

-981 Bytes
Binary file not shown.

‎java/pico/board.class‎

-172 Bytes
Binary file not shown.

‎java/pico/gpio.class‎

-445 Bytes
Binary file not shown.

‎java/pico/gpio.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package pico;
22

3+
import java.util.function.BiConsumer;
4+
35
public class gpio
46
{
5-
public static int INPUT = 0;
6-
public static int OUTPUT = 1;
7+
public static int INPUT;
8+
public static int OUTPUT;
9+
public static int IRQ_EDGE_RISE;
10+
public static int IRQ_EDGE_FALL;
711

812
public static native void init(int pin);
913
public static native void set_dir(int pin, int dir);
1014
public static native void put(int pin, int value);
1115
public static native void pull_up(int pin);
16+
public static native void pull_down(int pin);
17+
public static native void set_input_enabled(int pin, boolean enabled);
18+
public static native void set_irq_enabled_with_callback(int pin, int events, boolean enabled, BiConsumer<Integer, Integer> callback);
1219
public static native boolean get(int pin);
1320
public static native void set_mask(int mask);
1421
public static native void clr_mask(int mask);

‎java/pico/stdio.class‎

-208 Bytes
Binary file not shown.

‎java/pico/time.class‎

-213 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)