forked from mikeckennedy/talk-python-transcripts
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path017_micropython.txt
More file actions
1326 lines (663 loc) · 52 KB
/
017_micropython.txt
File metadata and controls
1326 lines (663 loc) · 52 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
00:00:00 How many layers of abstraction and indirection are between your Python code and machine instructions?
00:00:05 What if that number could be one and Python itself was the operating system?
00:00:08 That would be so amazing, right?
00:00:10 In fact, it is amazing, and it's called MicroPython.
00:00:13 This is show number 17.
00:00:15 It's MicroPython with guest Damian George.
00:00:18 You're listening to Talk Python To Me, and this was recorded on Thursday, June 25, 2015.
00:00:24 I'm a developer in many senses of the word, because I make these applications, but I also use these verbs to make this music.
00:00:35 I construct it line by line, just like when I'm coding another software design.
00:00:39 In both cases, it's about design patterns.
00:00:42 Anyone can get the job done.
00:00:44 It's the execution that matters.
00:00:45 I have many interests.
00:00:47 Sometimes it can flit.
00:00:48 My creativity can usually be...
00:00:50 Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
00:00:57 This is your host, Michael Kennedy.
00:00:59 Follow me on Twitter, where I'm @mkennedy.
00:01:01 Keep up with the show and listen to past episodes at talkpythontome.com.
00:01:06 And follow the show on Twitter, where we're at Talk Python.
00:01:09 This show will be talking to Damian George about rethinking the Python runtime for embedded microcontrollers and his implementation, MicroPython.
00:01:18 This episode is brought to you by CodeShip and Hired.
00:01:21 Thank them for supporting the show on Twitter via at CodeShip and at Hired underscore HQ.
00:01:27 Before we get to the interview with Damian, I'd like to give a shout out to Ganesh Kupan.
00:01:32 Ganesh suggested this topic for the show, and I think it's a great one.
00:01:37 Ganesh, thank you for sending that in and making this happen.
00:01:40 Now, let me introduce Damian.
00:01:43 Damian George is a theoretical physicist at the University of Cambridge and the creator of MicroPython.
00:01:48 Damian, welcome to the show.
00:01:50 Hi, Michael.
00:01:52 Thanks.
00:01:52 Yeah, this is really great.
00:01:53 We've talked a lot about software, and we've even talked a little bit about robots on the show before.
00:01:58 And now we're going to talk about sort of the most low-level stuff you can do with Python that I know of.
00:02:05 We're going to talk about running Python directly on microchips and microcontrollers, right?
00:02:10 Yes, that's right.
00:02:12 Yeah.
00:02:12 Your project, MicroPython, at micropython.org is really cool.
00:02:16 Thank you.
00:02:17 Yeah, you're welcome.
00:02:18 And some of the listeners said, hey, you really need to have Damian on your show and talk about this stuff because what you guys are doing, it's really amazing.
00:02:24 And so we're definitely going to do that.
00:02:26 We're going to talk all about that.
00:02:28 But before we do, maybe you could tell me a bit about your background, how you got into programming, how you got into Python, that kind of thing.
00:02:33 Okay.
00:02:33 Well, I mean, I've always liked computers and using computers and programming since I can remember.
00:02:41 I know that I've written many languages, toy languages, ever since I could start programming.
00:02:47 It's always been sort of a passion.
00:02:50 I think anyone who gets into programming at some stage tries writing their own language.
00:02:55 That's why there's so many of them.
00:02:57 But it's a really great way to understand how a computer works is by inventing your own language or at least implementing something that already exists.
00:03:07 That's sort of something that I did in my spare time.
00:03:11 But actually, formally, I also did a computer engineering degree at university, which was based around microcontrollers and designing computers and designing instruction sets and writing operating systems and those kinds of low-level things.
00:03:30 So that was really good to get an actual formal training so that you're forced to be exposed to all the things that maybe you didn't want to study.
00:03:40 Absolutely.
00:03:41 Was that mostly in C or C++?
00:03:43 And what languages and stuff did you use in that field?
00:03:47 We learned a lot of languages like Haskell and Java and C and C++.
00:03:53 So, yeah, but I mean, it was a bit about learning languages, but also you learn algorithms like hash tables and things like that.
00:04:02 And then, you know, you learn, I mean, you learn how to write a compiler.
00:04:07 And it's not so much about the language.
00:04:09 It's about the concepts and the algorithms.
00:04:12 Right, like the abstract syntax trees and things like this, yeah?
00:04:15 Yeah, that's right.
00:04:17 That's right.
00:04:17 So, I mean, this is what I studied.
00:04:20 I actually also studied theoretical physics and pure math.
00:04:25 So, I did a lot of extra stuff.
00:04:29 And all these things, I think, sort of tie in, in the end, to the same kind of way of thinking, is that you think very methodically and you try and solve problems.
00:04:40 And you use computers a lot in maths and physics to solve problems because they're so difficult nowadays that you can't just do it with a pen and paper.
00:04:49 Yeah, I think you're totally right.
00:04:50 My background is I was working on my PhD in math as well, and I got into computers to trying to do research with math.
00:04:57 And, like, you're going to have to learn to program this thing.
00:04:59 And it turned out I liked computers better, so I never did get a PhD.
00:05:03 But I totally know where you're coming from.
00:05:05 That's awesome.
00:05:05 In physics, if you're an experimental physicist, a lot of the time you end up doing just a lot of programming, like Monte Carlo simulations and data analysis and things like that.
00:05:16 And, I mean, yeah, if you like the kind of stuff, it's really good.
00:05:20 But if you don't, you know, maybe if you want to build an actual apparatus, then you probably need to be an engineer.
00:05:27 I think, yeah, my background is all these things combined.
00:05:31 And, yeah, I just really like the technical side of things.
00:05:35 That's cool.
00:05:37 Both programming and hardware.
00:05:38 Yeah, so how did you get into Python?
00:05:40 You started out in this sort of what to me feels more like, you know, the majority of the work is done in C, C++, Fortran, that kind of space with physics and operating systems and so on.
00:05:51 And then somehow you ended up in Python, right?
00:05:53 Yeah, I mean, I think it's good if you know more than one language or more than two or more than five languages.
00:06:01 And Python is such a popular language and for a good reason.
00:06:04 It's a very nice language.
00:06:07 And so, yeah, it's just one of the ones that you will, I think, eventually learn if you're into programming.
00:06:13 And, yeah, I mean, yeah, I just, I guess just stumbled across it like all the other languages that people learn, like C and C++.
00:06:25 Because, yeah, I mean, C is a great language and I love writing code in C.
00:06:31 I mean, I know it's a very old language, but it allows you to be really close to the hardware.
00:06:36 I mean, a line of C translates to a few lines of assembly and you sort of know what's going on.
00:06:42 And, yeah, that means you have to write more code to do the same sort of thing as you would in a higher level language.
00:06:47 But it gives you more power and when you need it, speed.
00:06:51 But then, of course, you want something like Python to say when you don't, you're not so worried about the efficiency and you're more worried about writing something quickly and expressing your idea in a very quick way and sort of a very clean and clear way.
00:07:09 So a Python program is, you know, you think it and then you sort of put your thoughts on it in code and it's sort of the same thing.
00:07:18 Yeah, that's a lovely thing about Python, isn't it?
00:07:21 Yeah, yeah, exactly.
00:07:22 You just, you know, you don't have to allocate lists and make links in the list.
00:07:27 You know, it just works having a list or it just works having a hash table, a dictionary.
00:07:31 And, you know, you have lambdas and you have list comprehension and you have generators.
00:07:36 And these things allow you to think in a certain way.
00:07:40 And if you're writing in C, you don't think about generators and you don't think about sort of closures because they're really hard.
00:07:48 But they're really good concepts.
00:07:49 And a language that allows you to express those concepts easily is a good language.
00:07:55 I agree.
00:07:56 And when I think of, you know, microcontrollers and embedded devices, I typically think of people using C because they want to squeeze that extra 5% of energy or performance or, you know, those are more or less the same thing in some sense.
00:08:10 Right.
00:08:11 You've taken the opposite perspective.
00:08:13 You've made Python suitable for microcontrollers rather than saying we're going to use a language that already sort of matched to the metal.
00:08:21 That's really awesome.
00:08:21 So you call this project MicroPython, right?
00:08:24 Yeah, that's right.
00:08:25 MicroPython.
00:08:26 I think the name says it all.
00:08:29 That's awesome.
00:08:31 If you want Python on a microcontroller, then that's what you need.
00:08:36 I did it as a sort of, to begin with, it was more of a can it be done question.
00:08:41 Because, you know, I just, it's technically interesting to try and put a language as big and as high level and as dynamic and as memory hungry as Python on a really small microcontroller, which has very, very, very little memory.
00:08:58 So that was kind of what started it for me was just the technical interest.
00:09:03 But at the same time, I also thought, you know, why not pick another language or why not just, I mean, C is already on microcontrollers and stuff, but why even choose a high level language to start with?
00:09:16 And my reason is that because microcontrollers now are a little, you know, are more powerful than they were a few years ago when they had almost no memory at all, you know, like 100 bytes or something.
00:09:28 Now they have 100 kilobytes.
00:09:31 And that's starting to be interesting.
00:09:35 They also have really sophisticated peripherals.
00:09:38 So you buy, you know, a $1 chip and it has all these UART and SPI and I2C and ADCs and DACs and all these peripherals that do great things and are very, very powerful.
00:09:49 It has DMA to do quick transfers and this kind of stuff.
00:09:54 When you start writing it in C to control all of these peripherals, it gets really, really hard really quickly because you've got to, you've got so many configuration options.
00:10:04 You've got so many things to think about, interrupts and interrupt levels and it becomes very quickly sort of, I mean, it can become a bit of a mess to get something to run quickly and easily.
00:10:18 So if you just want to flash an LED and read some sensor, you don't need to sort of write really efficient code that you would need to or C would allow you to.
00:10:31 So to do a simple thing, but a low level simple thing, like turn a light on, I mean, what you're doing is putting a voltage, five volts or three volts on a pin.
00:10:44 It's a pretty basic operation.
00:10:46 It's something that you can do in Python quite efficiently.
00:10:50 And then once you, you can do, you know, turn up an LED on with a Python command, you can think about having a list or a dictionary of pins and then you can iterate over them or you can pass them along as objects.
00:11:03 So you can start to really abstract the low level hardware.
00:11:09 And it's still relatively efficient because, well, the way MicroPython is implemented, you still have running relatively efficiently on the microcontroller.
00:11:18 You know, I've seen your API and it looks super clean.
00:11:22 I saw your Kickstarter video and the one that you have on micropython.org is sort of the opener video to introduce people.
00:11:28 That's really cool.
00:11:29 Maybe let's take a step back and just real quickly tell people what MicroPython is and tell them about your board and how it came into existence.
00:11:36 Sure.
00:11:37 Well, so MicroPython is a rewrite, a complete re-implementation from scratch of Python sort of version three, as it were, 3.4, actually.
00:11:51 So complete rewrite, no components whatsoever that are in common with CPython.
00:11:59 And the aim in rewriting it was to make sure that every operation was done as efficiently and used as little and, if possible, no RAM at all.
00:12:10 So that was sort of the guiding principle is use as little RAM as possible.
00:12:15 So MicroPython, the software, is this re-implementation of Python.
00:12:21 And it includes all of your favorites, lists and dictionaries and sets.
00:12:26 And, I mean, it's complete Python 3.4 in terms of the language specification.
00:12:31 Probably not the same standard library, though.
00:12:34 Probably things like dbapi2 is not there or something like that.
00:12:37 Right, yeah.
00:12:38 So there's the language in terms of the syntax and the semantics, you know, classes and function definitions.
00:12:44 And then there's the library.
00:12:46 So import numpy or whatever.
00:12:48 Import this, import that.
00:12:51 Python famously comes with batteries included with all of the standard library and everything you need, you know, statistics to compute the mean of something and things like that.
00:13:02 And these things, some of them are not sensible to run on a microcontroller and some of them won't fit on a microcontroller.
00:13:10 And so not all, well, not that many of the standard libraries are available.
00:13:16 But the ones that are useful are there, like JSON string parsing, for example, and regular expressions are there in a sort of cut down form.
00:13:25 And, I mean, we're always looking to add more.
00:13:27 It's just that the manpower to write all of these things in a micro way is not that easy.
00:13:33 Do you have to re-implement every standard library package or module that you're using?
00:13:39 Some of them we have, for example, JSON printing and parsing is rewritten because you want to make it efficient.
00:13:48 But other things actually import directly.
00:13:52 We can just copy over the CPython standard library and use that code directly.
00:13:58 So, yeah, it depends what you want to do.
00:14:01 I mean, the idea really is to provide micropython as the core language.
00:14:06 And then if people need some extra feature, they can copy it from normal CPython and just see if it works and it should work.
00:14:15 And, you know, if not, maybe it needs a few little tweaks here and there.
00:14:21 But, yeah, we concentrate mostly on the core language and providing a very robust and hopefully efficient core language.
00:14:33 So that's, but that's the software side of things.
00:14:36 Then there's the hardware side of things, which is, so the Pyboard, which is this little microcontroller that runs micropython.
00:14:45 And all the peripherals on this Pyboard are well supported, like the I2C bus or the UART, for example.
00:14:53 And the aim with this was, well, to be able to sort of kickstart this whole idea.
00:15:00 We went to Kickstarter.
00:15:01 Yeah.
00:15:03 Why don't you tell everybody a little bit about your Kickstarter?
00:15:05 It was super, super successful.
00:15:07 I saw you originally were going out for 15,000 pounds.
00:15:10 Yes.
00:15:11 And you raised 97,000 pounds or 153,000 US dollars.
00:15:16 That's, you must have been super thrilled.
00:15:18 Yeah.
00:15:19 Well, it was, it was a great reception.
00:15:21 We had a lot of people get on board.
00:15:24 And yeah, I mean, it was, it was really good.
00:15:27 I didn't expect it to be that big.
00:15:29 I mean, as I said, it started out as a sort of technical interest to be like, you know, it'd be really great to be able to see if this would fit.
00:15:36 And then, you know, getting it running, making a little board.
00:15:40 So the idea with the board was that I could give something to the Kickstarter backers.
00:15:45 If they back, you know, it's nice to actually have something physical as a backer instead of a piece of free software, which is going to be free for everyone.
00:15:53 So the hardware was a way to sort of, to give an incentive to say, look, there's actually something physical it can run on and you can do cool stuff with it.
00:16:01 That's cool.
00:16:02 The work of creating it, was it mostly actually writing the reimplementation of Python or was it, how much of it was hardware work and how much of it was software work?
00:16:11 I'd say it was about 50, 50 to begin with.
00:16:15 Up until when I launched the campaign, it was about 50, 50.
00:16:19 The software, I mean, yeah, I didn't write the entire thing before the Kickstarter campaign started.
00:16:25 I wrote enough to be able to convince myself and others that it would actually work.
00:16:29 Like you have a REPL and you can turn lights on and off and so on.
00:16:33 So there were bits of the language missing at the Kickstarter stage.
00:16:37 And probably 50% of the time was spent actually building the hardware.
00:16:42 So I had to learn about these microcontrollers and how they work.
00:16:47 The hardest thing is getting a tool chain up and running to cross compile stuff and download it and then, you know, debug it.
00:16:52 And why doesn't my USB thing work and so on?
00:16:55 There's a lot of really technical and difficult things to sort of get your head around when you first get into microcontrollers.
00:17:04 And I mean, these days with their 32-bit ARM chips, they're very powerful things.
00:17:09 And there's lots of little things that can go wrong.
00:17:14 So, yeah, I liked it.
00:17:16 It was a good balance of software and hardware.
00:17:18 Then after the Kickstarter, when the hardware was finalized, it shifted more into software.
00:17:23 And since then, it's been, you know, pretty much purely just software, improving the software both for the core language and also to control the Pyboard in a nice way and support all the features.
00:17:37 Yeah, it looks really rich.
00:17:39 Maybe you could tell everyone a little bit about the features of the board because when I learned about it, I was surprised at all the things it has.
00:17:45 You know, an accelerometer, for example, storage, all sorts of stuff.
00:17:50 It seems pretty much like I could take that, put it into something with a power source and use it.
00:17:57 Yeah, well, for sure.
00:17:58 I mean, the aim is you plug it into USB and it works straight away.
00:18:01 I just need to power source and it's got so that the accelerometer is not built into the microcontroller.
00:18:07 That's a separate little chip that's on there.
00:18:09 But the idea of putting that on was so that people could, you know, have sort of some input to play with straight away instead of buttons or switches or something.
00:18:19 An accelerometer is like a lot more fun to play with.
00:18:23 On your video, you have a little app you wrote where there's a robot that can lean forwards, backwards, left, right.
00:18:30 And as you tilt the board, the robot kind of leans in the way.
00:18:33 And you're doing that with an accelerometer and Python.
00:18:37 That's right.
00:18:37 That was a really cool example.
00:18:38 Yeah, well, I mean, that's only, you know, a handful of lines.
00:18:43 It's just an infinite loop while true.
00:18:45 And then you read the accelerometer, which is like one line in Python.
00:18:50 And then you set the servo angle based on that accelerometer reading.
00:18:54 So, you know, it really is just a few lines to do that stuff.
00:18:58 Okay, cool.
00:18:59 So maybe you could walk the listeners through what it's like from going, you know, file new project to having, what was it?
00:19:06 A main.py that runs when you turn on the board?
00:19:09 Yeah.
00:19:10 What's the lifecycle look like there?
00:19:12 So there's actually a few different ways of using it.
00:19:16 Okay.
00:19:18 So the simplest way is you just plug it in to USB and then it becomes a serial port, like a COM port, which you can log into.
00:19:29 It depends if you use, you know, Linux or Windows or Mac.
00:19:33 But you just use a terminal program and then you can connect to the serial port of the board and you have a Python prompt, a REPL right there.
00:19:43 And that runs directly on the board.
00:19:45 So you can type commands in straight away and they will be executed and do whatever you ask on the board.
00:19:52 So that's a good way to sort of just play around with things, debug things, test things.
00:19:57 The other way of programming it is to write your code, your script in a file and call it main.py.
00:20:07 And you copy that to the little mini file system that's on the board.
00:20:11 And then whenever it turns on, whenever it gets power, it runs that main.py script straight away and just does whatever you have in there.
00:20:20 So you could have an infinite loop, you know, reading a sensor and outputting some LEDs or servo angle or something like that.
00:20:26 And, of course, it's not just a single file.
00:20:29 You've got the full import machinery of Python.
00:20:32 So if you also copy some libraries to the little file system on the board, then you can import them as well.
00:20:39 So you can do lots and lots of different things.
00:20:42 That's cool.
00:20:42 And how much storage space do I have?
00:20:44 CodeShip is a hosted continuous delivery service focused on speed, security, and customizability.
00:21:03 You can set up continuous integration in a matter of seconds and automatically deploy when your tests have passed.
00:21:09 CodeShip supports your GitHub and Bitbucket projects.
00:21:12 You can get started with CodeShip's free plan today.
00:21:15 Should you decide to go with a premium plan, Talk Python listeners can save 20% off any plan for the next three months by using the code TALKPYTHON.
00:21:23 All caps, no spaces.
00:21:25 Check them out at CodeShip.com and tell them thanks for sponsoring the show on Twitter where they're at CodeShip.
00:21:38 It has about just under 200 or 100 kilobytes file system, which is not very big.
00:21:46 So you can put a few scripts and maybe log some data.
00:21:50 But it does have expansion with an SD card.
00:21:54 So you can put a little micro SD card in.
00:21:56 For a few dollars, you can buy something that has a gigabyte of storage and then you can run scripts from there.
00:22:01 So you can put everything there and log lots of data, do whatever you like.
00:22:06 So it's expandable in that sense.
00:22:10 Yeah, that's excellent.
00:22:11 So you should be able to put a lot of code on there for 200K.
00:22:17 Yeah, yeah.
00:22:17 And yeah, the other, there's actually, well, the third way to use it is you can actually sort of, you can write your script on your PC and then run it remotely by using a small, actually another Python script, which sort of downloads your script onto the board and runs it straight away.
00:22:37 So that's a quick way to sort of prototype your code.
00:22:41 Yeah, yeah, that's cool.
00:22:42 How do you get files on there other than this prototyping way?
00:22:46 Can I, does it like show up as a drive when I connect it or?
00:22:50 Yeah, it shows up as a USB drive and you just, it's just like a USB stick.
00:22:54 That's it.
00:22:57 Yeah, cool.
00:22:58 Can I set up networking if I want to do a little internet of thing type stuff?
00:23:01 Yes.
00:23:03 So the Pyboard itself has drivers for a wireless chip.
00:23:09 So you have to buy an extra wireless chip and connect it up and then it will work as a, it'll work as sort of a Wi-Fi extension.
00:23:18 So you have, you have your normal Python sockets.
00:23:21 So you can create a socket and you can, you bind it and connect it to a server.
00:23:26 You can either be a server or a client.
00:23:28 So you can do that kind of, the same Python stuff you can do on your PC on, on the board.
00:23:34 It's a bit restricted.
00:23:36 Yeah.
00:23:36 Could I run a web server on it?
00:23:37 Yes, you can.
00:23:39 Yes, you can.
00:23:39 It's like probably one of the micro, yeah, like one of the micro frameworks, like a Flask or Pyramid or something like that might be better.
00:23:45 Yeah.
00:23:46 Flask, even Flask might be a little bit big.
00:23:48 I mean, you'd be surprised.
00:23:50 Flask is, I mean, these, when you load up a full Python app, there's a lot of stuff that actually gets imported that you don't think about.
00:23:58 So in micro Python, which we have to think about all that and, and really strip it back.
00:24:04 But the other thing, so there, there's been a few other Kickstarters based around micro Python since mine,
00:24:12 a year and a half, yeah, almost two years ago now started.
00:24:18 So just recently there was this Wi-Fi Kickstarter, which is actually a Wi-Fi module that runs micro Python.
00:24:29 So it's one ship and it has Wi-Fi built into it.
00:24:32 So it really is an Internet of Things toy.
00:24:34 Well, it's more than a toy.
00:24:35 It's a cool Internet of Things device that you, that is like the Pyboard in that you, it runs micro Python.
00:24:44 And you can tell net in to get a REPL prompt and you can FTP in to copy your main dot pyscript across.
00:24:52 So it's, it's really a proper wireless from the, from the ground micro Python device.
00:24:59 And on that you can make a server or a client and you, you can sort of attach a battery, put it behind your sofa.
00:25:05 And then there's this little Python wireless thing running behind your sofa.
00:25:10 That is really cool.
00:25:11 Yeah.
00:25:11 Just Google WIPY Kickstarter.
00:25:15 Yeah.
00:25:16 Wi-Fi and you'll find it.
00:25:17 That's awesome.
00:25:17 And it looks like it, it also really exceeded its, its Kickstarter goal.
00:25:23 You, they were going for 30, 30,000 pounds and they got 75,000 pounds.
00:25:27 That's great.
00:25:27 Yeah.
00:25:28 I think they're in euros there.
00:25:29 Yes.
00:25:30 Oh yeah, that's right.
00:25:31 It's euros.
00:25:31 So yeah, more like dollars.
00:25:32 Yeah.
00:25:33 Right now with the exchange rate.
00:25:34 Okay.
00:25:34 Very cool.
00:25:35 Several times I've had the thought as you're talking about this, you're like,
00:25:40 Oh, we've really got to squeeze the performance out of this framework or that framework is actually kind of heavy in this way.
00:25:45 Or I rewrote these things to be more efficient.
00:25:48 Is there a place for micro Python in the data center?
00:25:53 Well, maybe, maybe in the future.
00:25:56 Yeah.
00:25:57 So what I'm thinking is, you know, if you've got like these little Docker containers and if you're looking for server density, you know, maybe having something that was focused on a micro controller that uses so few things.
00:26:09 So few resources, you might be able to run really, really dense sort of cloud computing systems and do interesting things with it.
00:26:16 What are your thoughts?
00:26:17 Yeah.
00:26:17 No, definitely.
00:26:19 That's actually one of the directions of the project as well.
00:26:23 So it's so that the soft micro Python software runs on the Pyboard and the Wi-Fi and other pieces of hardware, but it also runs on your PC, Linux, Mac or Windows.
00:26:35 And it's basically Python, but unbloated.
00:26:39 So it tries to be a really efficient version of Python and as compatible as it can be.
00:26:45 So it's very, very quick to start up and it uses, you know, almost no memory.
00:26:49 And I've actually tried sort of spawning, you know, a thousand micro Pythons on my computer and it's fine.
00:26:56 It runs a thousand micro Pythons.
00:26:58 And I tried spawning a thousand Python, normal Pythons, and then my computer just grounds to a halt.
00:27:04 So, okay, that's just a sort of a fun game.
00:27:09 But it shows you that, yeah, as you were saying, if you wanted to, you know, have a container and put in some app and you needed it to be efficient because you wanted to run lots of them, then maybe Macro Python can do what you need.
00:27:23 So, I mean, there is a movement.
00:27:26 There are some people who are helping to, there's quite a lot of people on GitHub working on Macro Python, which is really great.
00:27:34 Yeah, that's cool.
00:27:35 And that's just github.com/micropython, all our case, right?
00:27:39 Yes, that's right.
00:27:39 Yep.
00:27:40 But one of the things they're doing is implementing as much of the Python standard library as can be done in order to run some real-life applications like a web server, for example.
00:27:56 And, yeah, I mean, there's some other interesting applications as well.
00:28:03 Running a web server is sort of, yeah, the number one thing that you think about when you're thinking about, you know, Python and running lots of instances.
00:28:11 So that's definitely something that we're working towards.
00:28:15 I mean, it already can run web server for sure.
00:28:20 But I don't know of anyone who's doing that in production just yet.
00:28:25 Yeah, actually doing it.
00:28:26 Well, maybe we're giving some people some ideas out there.
00:28:28 Yeah.
00:28:29 It seems like an interesting thing to be the sort of foundation of some lightweight, restful services that you want to just have lots of horizontal scale on.
00:28:39 Yeah, yeah.
00:28:40 Yeah, there's definitely lots of scope for this.
00:28:43 Lots and lots of scope.
00:28:44 Okay.
00:28:44 So the actual operating system on the microcontroller, that's written in Python?
00:28:49 Python is the operating system.
00:28:51 That's what – I think that's a good way to describe it.
00:28:56 That's really awesome.
00:28:56 On the microcontroller, there is no operating system.
00:29:01 It's just bare metal.
00:29:02 So the first thing that happens when it boots up is you have to set the stack pointer and you have to copy the data from ROM to RAM.
00:29:10 And then you've got sort of free reign.
00:29:14 You can do whatever you like.
00:29:15 So, you know, it jumps into some initialization, loads up the Python runtime, and then jumps to the REPL.
00:29:21 So Python essentially is the operating system, and it's giving you full control over the microcontroller.
00:29:28 There's nothing – there's no context switching.
00:29:31 I mean, every cycle is yours to spend on Python code.
00:29:35 That's fantastic.
00:29:36 Yeah.
00:29:38 So you can – I mean, you can do things like disable interrupts with a Python call, and then, you know, everything's nice and quiet.
00:29:45 You can do things in cycle count and then turn them back on again.
00:29:49 You can set interrupts to trigger, so you can have a timer which counts and say, you know, 10 times a second fires an interrupt.
00:29:57 And when that interrupt fires, you can call a Lambda function in Python to do something, and then it will return.
00:30:03 So you can call Python on an interrupt.
00:30:06 That's pretty awesome to hook a Lambda function straight to like a hardware interrupt.
00:30:11 That's super cool.
00:30:11 Yeah.
00:30:12 So you can do that.
00:30:14 And the other, I think, very nice thing – well, the thing that I think is quite cool is you can write inline assembler code within Python.
00:30:22 So just like in C, how you can say, you know, inline assembler, and you write your functions there.
00:30:27 In Python, you can write a function, and you use the inline assembler decorator for that function.
00:30:34 It's a special decorator.
00:30:36 And then the compiler knows that instead of compiling Python, it compiles the, you know, the arm assembly.
00:30:43 It assembles it.
00:30:45 And then it turns that into a normal function that you can call from Python.
00:30:49 You can even pass arguments to that function.
00:30:53 And then it can do whatever you want to do in assembler, and it can even return stuff back to Python land.
00:30:57 So this way, if you really need to do some low-level things, then it's at your fingertips.
00:31:04 That's very cool.
00:31:05 That's kind of your escape hatch from – if you haven't thought of it in Python, if you guys haven't pre-made this feature available, that's the way to do it, right?
00:31:15 That's right.
00:31:16 Yeah.
00:31:17 You can access the direct hardware however you like.
00:31:21 Now, I'm not sure that I would be super comfortable doing much in direct assembler, but I would be in C.
00:31:26 Is there a possibility of having, like, inline C in the future?
00:31:30 There already is such a thing.
00:31:33 It's not inline C, but it's a mode of a compiler, the Python compiler, that is.
00:31:42 So the Python compiler in MicroPython is a bytecode compiler.
00:31:46 It reads your script, and it turns it into a parse tree, and then it compiles the parse tree into some bytecode.
00:31:54 But there's a sort of an option to – instead of emitting bytecodes, it can emit direct machine code.
00:32:01 So this is kind of like just-in-time compiling, although it's ahead of time compiling.
00:32:06 But this is how the V8 JavaScript engine works.
00:32:09 Instead of targeting a virtual machine with bytecode, it just targets the direct hardware by emitting.
00:32:16 So if I wanted to load an integer onto the stack, then instead of calling a bytecode, I can just do the actual machine code.
00:32:24 And then if I wanted to call a function or call a method, then I just implement the machine code to actually do that.
00:32:32 So this makes your code run faster, but it also allows you to declare types to say, okay, using Python's type annotations, I'm going to say that this argument is an integer and this one's an integer.
00:32:43 And then MicroPython can optimize that when you do an addition.
00:32:46 And instead of calling lots and lots of calls to do an addition to these two numbers, it knows that it can use a single hardware add, and it just adds them.
00:32:57 And in this way, you essentially turn your Python code into sort of C-like code.
00:33:06 But it still looks Python, but it's compiled to something that would have happened if it was written in C.
00:33:13 So you've got pointers, and you've got integers, and you can do loops.
00:33:17 So you can write much faster code that way if you really need to.
00:33:23 Yeah, that sounds really interesting.
00:33:26 One thing I'd like to sort of think about with you is what's the tradeoff in terms of performance if I were to, say, work with embedded C on a regular, like, say, embedded Linux or something versus Python on MicroPython on this board?
00:33:44 So versus, say, Python on a regular, like, Adreno or something in terms of relative performance.
00:33:52 What's the tradeoffs here?
00:33:55 So, okay, yeah, if you want a number, it's something like a factor of 100 slower when you're running Python's bytecode.
00:34:02 So each, so, yeah, this is a very rough number.
00:34:06 So, you know, if you wanted to do a loop, which was flashing an LED on and off, then if you've written that loop in Python, you can get maybe 100 kilohertz out of that loop.
00:34:19 But if you're writing it in directly in C, you'll probably get 10 megahertz out of the loop.
00:34:24 Now, I mean, yeah, look, MicroPython is not meant to replace C in any form.
00:34:33 It's, I mean, it's supposed to be there for doing high-level control of all these sophisticated components.
00:34:40 And if you really need to run a 10 megahertz loop, then, well, you could use the inline assembler, maybe, or you could use this other mode I was talking about before.
00:34:50 Or you can write your own C function and then recompile MicroPython and use that C function as sort of an extension to MicroPython if you need to.
00:34:59 So you have lots of different options.
00:35:03 But there is, of course, going to be some kind of overhead in terms of the speed.
00:35:07 I mean, you can't escape that.
00:35:08 Yeah, no, that's totally reasonable.
00:35:10 I guess one of the things I was trying to get a sense of is how does MicroPython compare to CPython on the same hardware?
00:35:18 Ah, I see.
00:35:30 This episode is brought to you by Hired.
00:35:32 Hired is a two-sided, curated marketplace that connects the world's knowledge workers to the best opportunities.
00:35:39 Each offer you receive has salary and equity presented right up front, and you can view the offers to accept or reject them before you even talk to the company.
00:35:48 Typically, candidates receive five or more offers in just the first week, and there are no obligations ever.
00:35:54 Sounds pretty awesome, doesn't it?
00:35:56 Well, did I mention there's a signing bonus?
00:35:59 Everyone who accepts a job from Hired gets a $2,000 signing bonus.
00:36:02 And as Talk Python listeners, it gets way sweeter.
00:36:07 Use the link Hired.com slash Talk Python To Me, and Hired will double the signing bonus to $4,000.
00:36:14 Opportunity's knocking.
00:36:16 Visit Hired.com slash Talk Python To Me and answer the call.
00:36:20 On the same hardware, it's impossible to measure because C-Python won't run on a Pyboard.
00:36:36 That's really not working.
00:36:38 If you want to try and compare apples with apples, you can run MicroPython on your PC, and you can run C-Python on your PC, and you can compare the two using some benchmark that you like.
00:36:51 Actually, MicroPython is slightly faster than C-Python for a reasonable range of benchmarks that we have.
00:37:01 For example, the Pystone benchmark, MicroPython runs a little bit faster than C-Python.
00:37:06 And that's because it uses less resources, and the way it handles integers is a bit more efficient.
00:37:11 So, you know, not having to allocate RAM to do certain things makes it faster.
00:37:16 But, of course, there are other things that C-Python is quicker at.
00:37:19 But, actually, on par, they're about on par when you take your own overall sort of benchmark.
00:37:27 Okay, that's very interesting.
00:37:29 So, it's more like they're on par, but C-Python is kind of unsuitable for these microcontrollers because it's so big and memory hungry, yeah?
00:37:38 One thing this might be worth talking about is where is MicroPython used?
00:37:43 Are there some notable, I guess, I'm not sure if deployment's the right word, but products or devices with MicroPython?
00:37:50 Well, it's been used, I think, in quite a lot of places.
00:37:55 Some of them that I don't know about and some of them I do know about.
00:37:59 I think it's used a lot in robotics.
00:38:04 There's been a few other Kickstarters as well.
00:38:07 To do with MicroPython.
00:38:10 There was this Robocore Kickstarter where they made a little sort of robot brain that ran MicroPython so you could make robots.
00:38:17 And then the Wi-Fi, obviously, for Internet of Things.
00:38:20 It's been used in teaching.
00:38:23 There's some people that will be using it for teaching university classes because it's sort of a good mix of, you know,
00:38:31 in university course you don't have all this time to go into the details of getting cross-compilers working and all this stuff.
00:38:37 You just want to teach some basics about low-level interrupts and controlling an I2C bus.
00:38:43 And doing that in Python, yeah, it seems like a really great way because Python is so simple.
00:38:47 You totally understand what's going on and so it lets you focus on the concept, right?
00:38:52 Yeah, exactly.
00:38:52 And turn an LED on and off and sort of get some hands-on use of some hardware straight away.
00:39:00 So, I mean, in the UK here there's a big push for students at primary school even to start learning languages.
00:39:09 So, they have to learn two languages and mostly Python is one of the languages they learn.
00:39:15 And they also do sort of some hardware in their sort of design or science class.
00:39:21 So, if these two things go together, if MicroPython, because they already know Python and they need to do some sort of simple exercises,
00:39:30 it's a very easy path for them to take.
00:39:35 So, it's been used in education and I think it will grow a bit more.
00:39:40 One other very exciting thing is that the European Space Agency has actually expressed interest in using MicroPython for space-based applications.
00:39:50 That's awesome.
00:39:51 Because the hardware that runs in space is very limited in resources because it has to be proven that, you know,
00:40:00 it takes a long time to prove that a piece of hardware is actually run in space given its radiation hardening and so on.
00:40:09 So, yes, the European Space Agency has agreed to fund some further development of MicroPython to see if it can be made robust and deterministic
00:40:21 so that it can be put in a space-based application.
00:40:26 So, there's more than, yeah, more than just the Internet of Things.
00:40:30 Yeah, that's, yeah, space.