---
---

<link rel="stylesheet" href="styles.css" type="text/css">

<span id="Initial"><br><br></span>

# <span><a href="#Plan">[&nabla;]</a></span> <span><a href="#Final">[@]</a></span> <span style="color: red;">MicroPython pour les microcontrôleurs</span>

### Version 2020-10-17
### Licence cc-by-sa ModLibre.info sauf autres indications<br><br>

#### NB : Copiez les instructions que vous voulez tester dans une version récente de MicroPython (ou de Python 3 dans certains cas)

<span id="Plan"><br><br></span>

# <span><a href="#Hello">[&nabla;]</a></span> <span><a href="#Initial">[&Delta;]</a></span> <span style="color: red;">Plan de l'introduction à MicroPython pour les microprocesseurs</span>

* [<STRONG>BBC micro:bit : Affichage séquentiel</STRONG>](#Hello)
* [<STRONG>BBC micro:bit : Affichage d'un canard</STRONG>](#Duck)
* [<STRONG>BBC micro:bit : LED clignotante</STRONG>](#BlinkLED)
* [<STRONG>Pyboard : Tableaux inspirés de Mondrian<STRONG>](#Mondrian)

<span id="Hello"><br><br></span>

# <span><a href="#Duck">[&nabla;]</a></span> <span><a href="#Plan">[&Delta;]</a></span> <span style="color: red;">BBC micro:bit : Affichage séquentiel</span>

```
H    e    l    l    o         W    o    r    l    d    !  
```
```{Python}
# Copyright (C) 2017-2018   cc-by-sa 4.0 License : SparkFun
# Version       2018-04-18  cc-by-sa 4.0 http://ModLibre.info
#
# SparkFun Electronics      https://www.sparkfun.com/microbit
# https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-microbit-experiment-guide
# Experiment 0.0 :          Display "Hello World!" on your micro:bit
#
# Circuit: None = Aucun (compatible with 4tronix Music:Box)
# 3 P0 4 5 6 7 P1 8 9 10 11 12 P2 13 14 15 16 17 3V 18 19 20 21 0V 22
#   P0         P1                                3V             0V

from microbit import *

while True:
    display.scroll("Hello World!")
    sleep(1000)
```
#### Simulations de l'écran du micro:bit : un caractère à la fois !
```
H    e    l    l    o         W    o    r    l    d    !  
```

<span id="Duck"><br><br></span>

# <span><a href="#BlinkLED">[&nabla;]</a></span> <span><a href="#Hello">[&Delta;]</a></span> <span style="color: red;">BBC micro:bit : Affichage d'un canard</span>

```
  * *
* * *
  * * * * *
    * * *
```
```{Python}
# Copyright (C) 2017-2018   cc-by-sa 4.0 License : SparkFun
# Version       2018-04-18  cc-by-sa 4.0 http://ModLibre.info
#
# SparkFun Electronics      https://www.sparkfun.com/microbit
# https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-microbit-experiment-guide
# Experiment 0.1 :          Display an image
#
# Circuit: None = Aucun (compatible with 4tronix Music:Box)
# 3 P0 4 5 6 7 P1 8 9 10 11 12 P2 13 14 15 16 17 3V 18 19 20 21 0V 22
#   P0         P1                                3V             0V

from microbit import *

while True:
    display.show(Image.DUCK)
```
#### Simulations de l'écran du micro:bit
```
  * *
* * *
  * * * * *
    * * *
```
#### &#9633;&nbsp;&#9632;&nbsp;&#9632;&nbsp;&#9633;&nbsp;&#9633;<br>&#9632;&nbsp;&#9632;&nbsp;&#9632;&nbsp;&#9633;&nbsp;&#9633;<br>&#9633;&nbsp;&#9632;&nbsp;&#9632;&nbsp;&#9632;&nbsp;&#9632;<br>&#9633;&nbsp;&#9632;&nbsp;&#9632;&nbsp;&#9632;&nbsp;&#9633;

<span id="BlinkLED"><br><br></span>

# <span><a href="#Mondrian">[&nabla;]</a></span> <span><a href="#Duck">[&Delta;]</a></span> <span style="color: red;">BBC micro:bit : LED clignotante</span>

#### Simulation de la LED : <img src="images/Single_flashing_yellow_traffic_light_0050x0050_cc0_WM.gif" style="width:5%; border:0px solid; margin-right: 0px" align="center">

```{Python}
# Copyright (C) 2017-2018   cc-by-sa 4.0 License : SparkFun
# Version       2018-04-18  cc-by-sa 4.0 http://ModLibre.info
#
# SparkFun Electronics      https://www.sparkfun.com/microbit
# https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-microbit-experiment-guide
# Experiment 1.0 :          Blinking a LED
#
# kiktronik      :          Inventor's Kit for the BBC micro:bit
# https://www.kitronik.co.uk/5603-inventors-kit-for-the-bbc-microbit.html
# Experiment 8   :          Blinking LED (+ compass)
#
# Modified circuit compatible with 4tronix Music:Box
# 3 P0 4 5 6 7 P1 8 9 10 11 12 P2 13 14 15 16 17=3V=18 19 20 21=0V=22
#                              P2                               0V
#                               <-----R1=47 Ohm----+----LED-----> 
#  R1: Yellow=Jaune=4  Purple=Violet=7  Black=Noir=1  Gold=Or=+-5%
#  https://fr.wikipedia.org/wiki/R%C3%A9sistance_(composant)

from microbit import *

while True:
    pin2.write_digital(1)
    sleep(1000)
    pin2.write_digital(0)
    sleep(1000)
```
#### Simulation de la LED : <img src="images/Single_flashing_yellow_traffic_light_0050x0050_cc0_WM.gif" style="width:5%; border:0px solid; margin-right: 0px" align="center">

<span id="Mondrian"><br><br></span>

# <span><a href="#Biblio">[&nabla;]</a></span> <span><a href="#BlinkLED">[&Delta;]</a></span> <span style="color: red;">Pyboard : Tableaux inspirés de Mondrian</span>

<img src="images/PyB20J02T1040c_2500x1600_cc-by-sa_JMT.jpg" style="width:60%; border:0px solid; margin-right: 0px" align="center">

```{Python}
# main.py -- put your code here!
#:Copyright (C) 2013-2018  MIT License : Nicholas H. TOLLERVEY...
# Ref:                                   MicroPython-Hack-1LED
# http://wiki.mchobby.be/index.php?title=MicroPython-Hack-1LED
# Version       2018-09-07 MIT License : Jean.Thiery@ModLibre.info
#
# NB: Exemple de MicroPython pour le Pyboard

import pyb
from   pyb import Pin
import lcd160cr
from   random import randint, choice, uniform

# Déclaration des broches
# pin_out = Pin('Y9', Pin.OUT_PP)
# pRed   = pyb.Pin( RED_PIN,   pyb.Pin.OUT_PP )
# pGreen = pyb.Pin( GREEN_PIN, pyb.Pin.OUT_PP )
# pBlue  = pyb.Pin( BLUE_PIN,  pyb.Pin.OUT_PP )

pyb.LED(1).on()            # Red
pyb.LED(2).on()            # Green
pyb.LED(3).on()            # Yellow
pyb.LED(4).on()            # Blue

#   while True:
for i in range(4):
    pyb.LED(1).toggle()    # Red
    pyb.delay(500)
    pyb.LED(2).toggle()    # Green
    pyb.delay(500)
    pyb.LED(3).toggle()    # Yellow
    pyb.delay(500)
    pyb.LED(4).toggle()    # Blue
    pyb.delay(500)

#   Attendre 1 secondes
pyb.delay( 1000 )    

#   Tout en noir (toutes les LEDs éteintes)
pyb.LED(1).off()           # Red
pyb.LED(2).off()           # Green
pyb.LED(3).off()           # Yellow
pyb.LED(4).off()           # Blue
 
lcd = lcd160cr.LCD160CR('X')

MAX_DEPTH = 4
RED = lcd.rgb(255, 0, 0)
YELLOW = lcd.rgb(255, 255, 0)
BLUE = lcd.rgb(0, 0, 255)


WHITE = lcd.rgb(255, 255, 255)
BLACK = lcd.rgb(0, 0, 0)
COLOURS = [RED, YELLOW, BLUE, WHITE, WHITE, WHITE]


class Node:
    """
    Un noeud d'arborescence du sujet tableau Mondrian.
    """

    def __init__(self, depth=0):
        """
        Choix de la couleur, calcul de profondeur
        et ajout de noeuds enfants si pas trop dense.
        """
        self.colour = choice(COLOURS)
        self.depth = depth + 1
        self.children = []
        if self.depth <= MAX_DEPTH:
            self.direction = choice(['h', 'v'])
            self.divide = uniform(0.1, 0.9)
            self.children.append(Node(self.depth))
            self.children.append(Node(self.depth))

    def draw(self, x, y, w, h):
        """
        Trace ce noeud et ses enfants x fois.
        """
        lcd.set_pen(BLACK, self.colour)
        lcd.rect(x, y, w, h)
        if self.children:
            if self.direction == 'h':
                self.children[0].draw(x, y, int(w * self.divide), h)
                self.children[1].draw(x + int(w * self.divide), y,
                                      int(w * (1.0 - self.divide)), h)
            else:
                self.children[0].draw(x, y, w, int(h * self.divide))
                self.children[1].draw(x, y + int(h * self.divide), w,
                                      int(h * (1.0 - self.divide)))


while True:
    # Produit un nouveau tableau Mondrian au bout de 4-8 sec.
    tree = Node()
    tree.draw(0, 0, lcd.w, lcd.h)
    pyb.delay(randint(4000, 8000))
```

<img src="images/PyB20J02T1040c_2500x1600_cc-by-sa_JMT.jpg" style="width:60%; border:0px solid; margin-right: 0px" align="center">

<span id="Biblio"><br><br></span>

# <span><a href="#Final">[&nabla;]</a></span> <span><a href="#Mondrian">[&Delta;]</a></span> <span style="color: red;">Bibliographie</span>

- [MicroPython license information](http://docs.micropython.org/en/latest/license.html)
- [MicroPython tutorial for the pyboard](http://docs.micropython.org/en/latest/pyboard/tutorial/index.html)

<span id="Final"><br><br></span>

# <span><a href="#Initial">[@]</a></span> <span><a href="#Mondrian">[&Delta;]</a></span> <span style="color: red;">Fin</span>

***
##### [2021-03-13 &nbsp;&nbsp;&nbsp; © 2020-2021 ModLibre.info](http://ModLibre.info) &nbsp;&nbsp;&nbsp; [licence cc-by-sa](http://creativecommons.org) &nbsp;&nbsp;&nbsp; [Source](MicroPython.Rmd) &nbsp;&nbsp;&nbsp; [W3C](http://validator.w3.org/) &nbsp;&nbsp;&nbsp; [HTML5](https://fr.wikipedia.org/wiki/HTML5) &nbsp;&nbsp;&nbsp; [CSS](http://jigsaw.w3.org/css-validator/)
***
