Skip to content

Commit f0da49d

Browse files
committed
gets timer working for unix and macOS ports.
1 parent f6b0a4b commit f0da49d

File tree

8 files changed

+319
-152
lines changed

8 files changed

+319
-152
lines changed

builder/esp32.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -944,40 +944,36 @@ def update_mpconfigport():
944944
'IRAM_ATTR f\n'
945945
)
946946

947-
for i in range(2):
948-
pattern = f'#define MP_USE_DUAL_CORE ({i})'
949-
if pattern in data:
950-
text = (
951-
f'#define MP_USE_DUAL_CORE '
947+
has_dual_core = 'MP_USE_DUAL_CORE' in data
948+
949+
data = data.split('\n')
950+
951+
for i, line in enumerate(data):
952+
if has_dual_core and line.startswith('#define MP_USE_DUAL_CORE'):
953+
data[i] = (
954+
'#define MP_USE_DUAL_CORE '
952955
f'({int(dual_core_threads)})'
953956
)
954-
break
955-
else:
956-
pattern = '#define MICROPY_PY_THREAD_GIL'
957-
text = (
958-
f'#define MP_USE_DUAL_CORE '
959-
f'({int(dual_core_threads)})\n{pattern}'
960-
)
961-
962-
data = data.replace(pattern, text)
963-
text = (
964-
f'#define MICROPY_PY_THREAD_GIL '
965-
f'({int(not dual_core_threads)})'
966-
)
967-
for i in range(2):
968-
pattern = f'#define MICROPY_PY_THREAD_GIL ({i})'
957+
continue
969958

970-
if pattern in data:
971-
data = data.replace(pattern, text)
972-
break
959+
if line.startswith('#define MICROPY_PY_THREAD_GIL'):
960+
data[i] = (
961+
f'#define MICROPY_PY_THREAD_GIL '
962+
f'({int(not dual_core_threads)})'
963+
)
964+
if not has_dual_core:
965+
data[i] += (
966+
'\n#define MP_USE_DUAL_CORE '
967+
f'({int(dual_core_threads)})'
968+
)
969+
continue
973970

974-
data = data.split('\n')
975-
for i, line in enumerate(data):
976971
if line.startswith('#define MICROPY_TASK_STACK_SIZE'):
977972
data[i] = (
978973
f'#define MICROPY_TASK_STACK_SIZE ({task_stack_size})'
979974
)
980-
break
975+
continue
976+
981977
data = '\n'.join(data)
982978

983979
write_file(MPCONFIGPORT_PATH, data)

builder/macOS.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
update_modmachine,
1010
update_main,
1111
update_mpconfigvariant_common,
12-
copy_updated_files
12+
copy_updated_files,
13+
update_input,
14+
update_mpconfigport_mk
1315
)
1416

1517
from argparse import ArgumentParser
@@ -211,8 +213,9 @@ def compile(*args): # NOQA
211213
update_modmachine()
212214
update_main()
213215
update_mpconfigvariant_common()
216+
update_input()
217+
update_mpconfigport_mk()
214218
copy_updated_files()
215-
216219
build_sdl()
217220

218221
cmd_ = compile_cmd[:]

builder/unix.py

Lines changed: 107 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shutil
44
from . import spawn
55
from . import generate_manifest
6-
from . import update_mphalport
6+
from . import update_mphalport as _update_mphalport
77
from argparse import ArgumentParser
88

99

@@ -73,20 +73,22 @@ def build_commands(_, extra_args, script_dir, lv_cflags, board):
7373
if board:
7474
unix_cmd.append(f'VARIANT={board}')
7575

76-
unix_cmd.extend([
77-
f'LV_CFLAGS="{lv_cflags}"',
78-
f'LV_PORT=unix',
79-
f'USER_C_MODULES="{script_dir}/ext_mod"',
80-
(
81-
'"CFLAGS_EXTRA='
82-
'-Wno-sign-compare '
83-
'-Wno-unused-function '
84-
'-Wno-double-promotion '
85-
'-Wno-unused-command-line-argument '
86-
'-Wno-missing-field-initializers"'
87-
# 'export CPPFLAGS="-I/opt/homebrew/opt/libffi/include"'
88-
)
89-
])
76+
unix_cmd.extend(
77+
[
78+
f'LV_CFLAGS="{lv_cflags}"',
79+
f'LV_PORT=unix',
80+
f'USER_C_MODULES="{script_dir}/ext_mod"',
81+
(
82+
'"CFLAGS_EXTRA='
83+
'-Wno-sign-compare '
84+
'-Wno-unused-function '
85+
'-Wno-double-promotion '
86+
'-Wno-unused-command-line-argument '
87+
'-Wno-missing-field-initializers"'
88+
# 'export CPPFLAGS="-I/opt/homebrew/opt/libffi/include"'
89+
)
90+
]
91+
)
9092

9193
# unix_cmd.extend(extra_args)
9294

@@ -108,7 +110,7 @@ def build_manifest(
108110

109111
SCRIPT_PATH = script_dir
110112

111-
update_mphalport(target)
113+
_update_mphalport(target)
112114

113115
manifest_path = 'lib/micropython/ports/unix/variants/manifest.py'
114116

@@ -226,10 +228,13 @@ def update_makefile():
226228
data = read_file(makefile_path)
227229

228230
if 'machine_timer.c' not in data:
229-
data = data.replace(
230-
'SRC_C += \\\n',
231-
'SRC_C += \\\n\tmachine_timer.c\\\n'
232-
)
231+
code = [
232+
'modjni.c \\',
233+
'\tmachine_timer.c \\',
234+
'\tmachine_sdl.c \\',
235+
''
236+
]
237+
data = data.replace('modjni.c \\\n', '\n'.join(code))
233238

234239
write_file(makefile_path, data)
235240

@@ -241,8 +246,9 @@ def update_modmachine():
241246

242247
if 'MICROPY_PY_MACHINE_EXTRA_GLOBALS' not in data:
243248
data += (
244-
'\n#define MICROPY_PY_MACHINE_EXTRA_GLOBALS \\\n'
245-
' { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \\\n' # NOQA
249+
'\n#define MICROPY_PY_MACHINE_EXTRA_GLOBALS '
250+
' { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \n\n'
251+
# NOQA
246252
)
247253

248254
write_file(modmachine_path, data)
@@ -253,30 +259,67 @@ def update_main():
253259

254260
data = read_file(main_path)
255261

256-
if 'modmachine.h' not in data:
257-
data = data.replace(
262+
if 'machine_timer.h' not in data:
263+
code = [
258264
'#include "input.h"',
259-
'#include "input.h"\n#include "modmachine.h"'
260-
)
265+
'#include "machine_timer.h"'
266+
]
267+
data = data.replace('#include "input.h"', '\n'.join(code))
268+
269+
if 'machine_sdl.h' not in data:
270+
code = [
271+
'#include "input.h"',
272+
'#include "machine_sdl.h"'
273+
]
274+
data = data.replace('#include "input.h"', '\n'.join(code))
261275

262276
if 'machine_timer_deinit_all()' not in data:
263277
data = data.replace(
264278
'#if MICROPY_PY_SYS_ATEXIT',
265279
'machine_timer_deinit_all();\n #if MICROPY_PY_SYS_ATEXIT'
266280
)
267281

268-
if 'lcd_bus_deinit_sdl()' not in data:
282+
if 'deinit_sdl()' not in data:
269283
data = data.replace(
270284
'#if MICROPY_PY_SYS_ATEXIT',
271-
'lcd_bus_deinit_sdl();\n #if MICROPY_PY_SYS_ATEXIT'
285+
'deinit_sdl();\n #if MICROPY_PY_SYS_ATEXIT'
272286
)
273287

274-
if 'lcd_bus_init_sdl()' not in data:
288+
if 'init_sdl()' not in data:
275289
data = data.replace(
276290
'mp_init();',
277-
'mp_init();\n lcd_bus_init_sdl();'
291+
'mp_init();\n init_sdl();'
278292
)
279293

294+
if '*mp_repl_get_ps3' not in data:
295+
code = [
296+
'char *mp_repl_get_ps3(void)',
297+
'{',
298+
' return "";',
299+
'}',
300+
'',
301+
'',
302+
'static int do_repl(void) {'
303+
]
304+
305+
data = data.replace('static int do_repl(void) {', '\n'.join(code))
306+
307+
if 'EWOULDBLOCK' not in data:
308+
code = [
309+
'if (errno != EWOULDBLOCK) {',
310+
' return 0;',
311+
' } else {',
312+
' while (line == NULL && errno == EWOULDBLOCK) {',
313+
' mp_handle_pending(true);',
314+
' usleep(1000);',
315+
' line = prompt(mp_repl_get_ps3());',
316+
' }',
317+
' if (line == NULL) return 0;',
318+
' }',
319+
]
320+
321+
data = data.replace('// EOF\n return 0;', '\n'.join(code))
322+
280323
data = data.split('\n')
281324

282325
for i, line in enumerate(data):
@@ -289,6 +332,24 @@ def update_main():
289332
write_file(main_path, data)
290333

291334

335+
def update_input():
336+
input_path = 'lib/micropython/ports/unix/input.c'
337+
338+
data = read_file(input_path)
339+
if 'O_NONBLOCK' not in data:
340+
code = [
341+
'char *prompt(char *p) {',
342+
' int stdin_fd = fileno(stdin);',
343+
' int flags = fcntl(stdin_fd, F_GETFL);',
344+
' flags |= O_NONBLOCK;',
345+
' fcntl(stdin_fd, F_SETFL, flags);',
346+
''
347+
]
348+
349+
data = data.replace('char *prompt(char *p) {', '\n'.join(code))
350+
write_file(input_path, data)
351+
352+
292353
def update_mpconfigvariant_common():
293354
mpconfigvariant_common_path = (
294355
'lib/micropython/ports/unix/variants/mpconfigvariant_common.h'
@@ -311,7 +372,7 @@ def update_mpconfigvariant_common():
311372
)
312373

313374
macros = (
314-
'#define MICROPY_SCHEDULER_DEPTH (128)'
375+
'#define MICROPY_SCHEDULER_DEPTH (128)',
315376
'#define MICROPY_STACK_CHECK (0)'
316377
)
317378

@@ -323,12 +384,27 @@ def update_mpconfigvariant_common():
323384
write_file(mpconfigvariant_common_path, data)
324385

325386

387+
def update_mpconfigport_mk():
388+
mpconfigport_path = 'lib/micropython/ports/unix/mpconfigport.mk'
389+
390+
data = read_file(mpconfigport_path)
391+
if 'MICROPY_USE_READLINE = 1' in data:
392+
data = data.replace(
393+
'MICROPY_USE_READLINE = 1',
394+
'MICROPY_USE_READLINE = 0'
395+
)
396+
397+
write_file(mpconfigport_path, data)
398+
399+
326400
def compile(*args): # NOQA
327401

328402
update_makefile()
329403
update_modmachine()
330404
update_main()
331405
update_mpconfigvariant_common()
406+
update_input()
407+
update_mpconfigport_mk()
332408
copy_updated_files()
333409

334410
build_sdl()

micropy_updates/unix/machine_sdl.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "machine_sdl.h"
2+
#include "SDL.h"
3+
4+
void init_sdl(void)
5+
{
6+
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
7+
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
8+
SDL_InitSubSystem(SDL_INIT_EVENTS);
9+
}
10+
11+
12+
void deinit_sdl(void)
13+
{
14+
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
15+
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
16+
SDL_QuitSubSystem(SDL_INIT_EVENTS);
17+
}

micropy_updates/unix/machine_sdl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
#ifndef __MACHINE_SDL_H__
3+
#define __MACHINE_SDL_H__
4+
5+
#define SDL_MAIN_HANDLED
6+
7+
void init_sdl(void);
8+
void deinit_sdl(void);
9+
10+
#endif

0 commit comments

Comments
 (0)