66#include "nlr.h"
77#include "misc.h"
88#include "mpconfig.h"
9- #include "mpqstr .h"
9+ #include "qstr .h"
1010#include "lexer.h"
1111#include "lexermemzip.h"
1212#include "parse.h"
1515#include "runtime0.h"
1616#include "runtime.h"
1717#include "repl.h"
18+ #include "servo.h"
1819#include "usb.h"
1920#include "gc.h"
2021#include "led.h"
@@ -54,6 +55,32 @@ static const char *help_text =
5455#endif
5556;
5657
58+ mp_obj_t pyb_analog_read (mp_obj_t pin_obj ) {
59+ uint pin = mp_obj_get_int (pin_obj );
60+ int val = analogRead (pin );
61+ return MP_OBJ_NEW_SMALL_INT (val );
62+ }
63+
64+ mp_obj_t pyb_analog_write (mp_obj_t pin_obj , mp_obj_t val_obj ) {
65+ uint pin = mp_obj_get_int (pin_obj );
66+ int val = mp_obj_get_int (val_obj );
67+ analogWrite (pin , val );
68+ return mp_const_none ;
69+ }
70+
71+ mp_obj_t pyb_analog_write_resolution (mp_obj_t res_obj ) {
72+ int res = mp_obj_get_int (res_obj );
73+ analogWriteResolution (res );
74+ return mp_const_none ;
75+ }
76+
77+ mp_obj_t pyb_analog_write_frequency (mp_obj_t pin_obj , mp_obj_t freq_obj ) {
78+ uint pin = mp_obj_get_int (pin_obj );
79+ int freq = mp_obj_get_int (freq_obj );
80+ analogWriteFrequency (pin , freq );
81+ return mp_const_none ;
82+ }
83+
5784// get some help about available functions
5885static mp_obj_t pyb_help (void ) {
5986 printf ("%s" , help_text );
@@ -181,16 +208,26 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
181208}
182209#endif
183210
184- static qstr pyb_config_source_dir = 0 ;
185- static qstr pyb_config_main = 0 ;
211+ static mp_obj_t pyb_config_source_dir = MP_OBJ_NULL ;
212+ static mp_obj_t pyb_config_main = MP_OBJ_NULL ;
186213
187214mp_obj_t pyb_source_dir (mp_obj_t source_dir ) {
188- pyb_config_source_dir = mp_obj_get_qstr (source_dir );
215+ if (MP_OBJ_IS_STR (source_dir )) {
216+ pyb_config_source_dir = source_dir ;
217+ printf ("source_dir = '" );
218+ mp_obj_print (source_dir , PRINT_STR );
219+ printf ("'\n" );
220+ }
189221 return mp_const_none ;
190222}
191223
192224mp_obj_t pyb_main (mp_obj_t main ) {
193- pyb_config_main = mp_obj_get_qstr (main );
225+ if (MP_OBJ_IS_STR (main )) {
226+ pyb_config_main = main ;
227+ printf ("main = '" );
228+ mp_obj_print (main , PRINT_STR );
229+ printf ("'\n" );
230+ }
194231 return mp_const_none ;
195232}
196233
@@ -205,7 +242,7 @@ mp_obj_t pyb_led(mp_obj_t state) {
205242}
206243
207244mp_obj_t pyb_run (mp_obj_t filename_obj ) {
208- const char * filename = qstr_str (mp_obj_get_qstr (filename_obj ));
245+ const char * filename = qstr_str (mp_obj_str_get_qstr (filename_obj ));
209246 do_file (filename );
210247 return mp_const_none ;
211248}
@@ -309,15 +346,24 @@ bool do_file(const char *filename) {
309346 return false;
310347 }
311348
312- mp_parse_node_t pn = mp_parse (lex , MP_PARSE_FILE_INPUT );
349+ qstr parse_exc_id ;
350+ const char * parse_exc_msg ;
351+ mp_parse_node_t pn = mp_parse (lex , MP_PARSE_FILE_INPUT , & parse_exc_id , & parse_exc_msg );
313352 qstr source_name = mp_lexer_source_name (lex );
314- mp_lexer_free (lex );
315353
316354 if (pn == MP_PARSE_NODE_NULL ) {
355+ // parse error
356+ mp_lexer_show_error_pythonic_prefix (lex );
357+ printf ("%s: %s\n" , qstr_str (parse_exc_id ), parse_exc_msg );
358+ mp_lexer_free (lex );
317359 return false;
318360 }
319361
362+ mp_lexer_free (lex );
363+
320364 mp_obj_t module_fun = mp_compile (pn , source_name , false);
365+ mp_parse_node_free (pn );
366+
321367 if (module_fun == mp_const_none ) {
322368 return false;
323369 }
@@ -329,7 +375,7 @@ bool do_file(const char *filename) {
329375 return true;
330376 } else {
331377 // uncaught exception
332- mp_obj_print ((mp_obj_t )nlr .ret_val );
378+ mp_obj_print ((mp_obj_t )nlr .ret_val , PRINT_REPR );
333379 printf ("\n" );
334380 return false;
335381 }
@@ -340,7 +386,7 @@ void do_repl(void) {
340386 stdout_tx_str ("Type \"help()\" for more information.\r\n" );
341387
342388 vstr_t line ;
343- vstr_init (& line );
389+ vstr_init (& line , 32 );
344390
345391 for (;;) {
346392 vstr_reset (& line );
@@ -366,12 +412,21 @@ void do_repl(void) {
366412 }
367413 }
368414
369- mp_lexer_t * lex = mp_lexer_new_from_str_len ("<stdin>" , vstr_str (& line ), vstr_len (& line ), 0 );
370- mp_parse_node_t pn = mp_parse (lex , MP_PARSE_SINGLE_INPUT );
371- mp_lexer_free (lex );
372-
373- if (pn != MP_PARSE_NODE_NULL ) {
374- mp_obj_t module_fun = mp_compile (pn , true);
415+ mp_lexer_t * lex = mp_lexer_new_from_str_len (MP_QSTR__lt_stdin_gt_ , vstr_str (& line ), vstr_len (& line ), 0 );
416+ qstr parse_exc_id ;
417+ const char * parse_exc_msg ;
418+ mp_parse_node_t pn = mp_parse (lex , MP_PARSE_SINGLE_INPUT , & parse_exc_id , & parse_exc_msg );
419+ qstr source_name = mp_lexer_source_name (lex );
420+
421+ if (pn == MP_PARSE_NODE_NULL ) {
422+ // parse error
423+ mp_lexer_show_error_pythonic_prefix (lex );
424+ printf ("%s: %s\n" , qstr_str (parse_exc_id ), parse_exc_msg );
425+ mp_lexer_free (lex );
426+ } else {
427+ // parse okay
428+ mp_lexer_free (lex );
429+ mp_obj_t module_fun = mp_compile (pn , source_name , true);
375430 if (module_fun != mp_const_none ) {
376431 nlr_buf_t nlr ;
377432 uint32_t start = micros ();
@@ -381,11 +436,11 @@ void do_repl(void) {
381436 // optional timing
382437 if (0 ) {
383438 uint32_t ticks = micros () - start ; // TODO implement a function that does this properly
384- printf ("(took %lu us )\n" , ticks );
439+ printf ("(took %lu ms )\n" , ticks );
385440 }
386441 } else {
387442 // uncaught exception
388- mp_obj_print ((mp_obj_t )nlr .ret_val );
443+ mp_obj_print ((mp_obj_t )nlr .ret_val , PRINT_REPR );
389444 printf ("\n" );
390445 }
391446 }
@@ -396,11 +451,15 @@ void do_repl(void) {
396451}
397452
398453int main (void ) {
399- pinMode (LED_BUILTIN , OUTPUT );
454+ pinMode (LED_BUILTIN , OUTPUT );
455+ #if 0
400456 // Wait for host side to get connected
401457 while (!usb_vcp_is_connected ()) {
402458 ;
403459 }
460+ #else
461+ delay (1000 );
462+ #endif
404463
405464 led_init ();
406465 led_state (PYB_LED_BUILTIN , 1 );
@@ -415,28 +474,34 @@ int main(void) {
415474 qstr_init ();
416475 rt_init ();
417476
418- #if 1
419477 // add some functions to the python namespace
420478 {
421- rt_store_name (qstr_from_str_static ("help" ), rt_make_function_0 (pyb_help ));
422- mp_obj_t m = mp_obj_new_module (qstr_from_str_static ("pyb" ));
423- rt_store_attr (m , qstr_from_str_static ("info" ), rt_make_function_0 (pyb_info ));
424- rt_store_attr (m , qstr_from_str_static ("source_dir" ), rt_make_function_1 (pyb_source_dir ));
425- rt_store_attr (m , qstr_from_str_static ("main" ), rt_make_function_1 (pyb_main ));
426- rt_store_attr (m , qstr_from_str_static ("gc" ), rt_make_function_0 (pyb_gc ));
427- rt_store_attr (m , qstr_from_str_static ("delay" ), rt_make_function_1 (pyb_delay ));
428- rt_store_attr (m , qstr_from_str_static ("led" ), rt_make_function_1 (pyb_led ));
429- rt_store_attr (m , qstr_from_str_static ("Led" ), rt_make_function_1 (pyb_Led ));
430- rt_store_attr (m , qstr_from_str_static ("gpio" ), (mp_obj_t )& pyb_gpio_obj );
431- rt_store_name (qstr_from_str_static ("pyb" ), m );
432- rt_store_name (qstr_from_str_static ("run" ), rt_make_function_1 (pyb_run ));
479+ rt_store_name (MP_QSTR_help , rt_make_function_n (0 , pyb_help ));
480+ mp_obj_t m = mp_obj_new_module (MP_QSTR_pyb );
481+ rt_store_attr (m , MP_QSTR_info , rt_make_function_n (0 , pyb_info ));
482+ rt_store_attr (m , MP_QSTR_source_dir , rt_make_function_n (1 , pyb_source_dir ));
483+ rt_store_attr (m , MP_QSTR_main , rt_make_function_n (1 , pyb_main ));
484+ rt_store_attr (m , MP_QSTR_gc , rt_make_function_n (0 , pyb_gc ));
485+ rt_store_attr (m , MP_QSTR_delay , rt_make_function_n (1 , pyb_delay ));
486+ rt_store_attr (m , MP_QSTR_led , rt_make_function_n (1 , pyb_led ));
487+ rt_store_attr (m , MP_QSTR_Led , rt_make_function_n (1 , pyb_Led ));
488+ rt_store_attr (m , MP_QSTR_analogRead , rt_make_function_n (1 , pyb_analog_read ));
489+ rt_store_attr (m , MP_QSTR_analogWrite , rt_make_function_n (2 , pyb_analog_write ));
490+ rt_store_attr (m , MP_QSTR_analogWriteResolution , rt_make_function_n (1 , pyb_analog_write_resolution ));
491+ rt_store_attr (m , MP_QSTR_analogWriteFrequency , rt_make_function_n (2 , pyb_analog_write_frequency ));
492+
493+ rt_store_attr (m , MP_QSTR_gpio , (mp_obj_t )& pyb_gpio_obj );
494+ rt_store_attr (m , MP_QSTR_Servo , rt_make_function_n (0 , pyb_Servo ));
495+ rt_store_name (MP_QSTR_pyb , m );
496+ rt_store_name (MP_QSTR_run , rt_make_function_n (1 , pyb_run ));
433497 }
434- #endif
435498
499+ printf ("About execute /boot.py\n" );
436500 if (!do_file ("/boot.py" )) {
437501 printf ("Unable to open '/boot.py'\n" );
438502 flash_error (4 );
439503 }
504+ printf ("Done executing /boot.py\n" );
440505
441506 // Turn bootup LED off
442507 led_state (PYB_LED_BUILTIN , 0 );
@@ -445,21 +510,23 @@ int main(void) {
445510 {
446511 vstr_t * vstr = vstr_new ();
447512 vstr_add_str (vstr , "/" );
448- if (pyb_config_source_dir == 0 ) {
513+ if (pyb_config_source_dir == MP_OBJ_NULL ) {
449514 vstr_add_str (vstr , "src" );
450515 } else {
451- vstr_add_str (vstr , qstr_str (pyb_config_source_dir ));
516+ vstr_add_str (vstr , mp_obj_str_get_str (pyb_config_source_dir ));
452517 }
453518 vstr_add_char (vstr , '/' );
454- if (pyb_config_main == 0 ) {
519+ if (pyb_config_main == MP_OBJ_NULL ) {
455520 vstr_add_str (vstr , "main.py" );
456521 } else {
457- vstr_add_str (vstr , qstr_str (pyb_config_main ));
522+ vstr_add_str (vstr , mp_obj_str_get_str (pyb_config_main ));
458523 }
524+ printf ("About execute '%s'\n" , vstr_str (vstr ));
459525 if (!do_file (vstr_str (vstr ))) {
460526 printf ("Unable to open '%s'\n" , vstr_str (vstr ));
461527 flash_error (3 );
462528 }
529+ printf ("Done executing '%s'\n" , vstr_str (vstr ));
463530 vstr_free (vstr );
464531 }
465532
@@ -471,16 +538,6 @@ int main(void) {
471538 goto soft_reset ;
472539}
473540
474- double __aeabi_f2d (float x ) {
475- // TODO
476- return 0.0 ;
477- }
478-
479- float __aeabi_d2f (double x ) {
480- // TODO
481- return 0.0 ;
482- }
483-
484541double sqrt (double x ) {
485542 // TODO
486543 return 0.0 ;
0 commit comments