issue177:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue177:micro-ci_micro-la [2022/01/30 23:48] – d52fr | issue177:micro-ci_micro-la [2022/02/02 09:04] (Version actuelle) – d52fr | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
Back in issue Full Circle Magazine issue #172 Part 6, I introduced the Micropython Font To Py library from Peter Hinch, but only a small part of that package. | Back in issue Full Circle Magazine issue #172 Part 6, I introduced the Micropython Font To Py library from Peter Hinch, but only a small part of that package. | ||
+ | |||
+ | Cela fait presque un an que j'ai commencé cette rubrique. J' | ||
+ | |||
+ | Une fois de plus, nous allons traiter de l' | ||
+ | |||
+ | Dans le numéro 172 du Full Circle Magazine, partie 6 de la rubrique, j'ai présenté la bibliothèque Micropython Font To Py de Peter Hinch, mais seulement une petite partie de ce paquet. Nous avons utilisé quelques-unes de ses polices et sa bibliothèque writer. Si vous ne l'avez pas encore récupérée, | ||
+ | |||
**This time, we’ll be creating our own fonts for the OLED display using our normal desktop computers and regular Python. | **This time, we’ll be creating our own fonts for the OLED display using our normal desktop computers and regular Python. | ||
Ligne 12: | Ligne 19: | ||
Next, you will want to download and unpack the micropython-font-to-py package into a folder and then create a folder somewhere to serve as your work folder. | Next, you will want to download and unpack the micropython-font-to-py package into a folder and then create a folder somewhere to serve as your work folder. | ||
+ | |||
+ | Cette fois-ci, nous allons créer nos propres polices pour l' | ||
+ | |||
+ | Pour ce projet, nous utiliserons le microcontrôleur Raspberry Pi Pico et un écran OLED SSD1306 128x64. Vous trouverez ci-dessous une image montrant le câblage, au cas où vous en auriez besoin. | ||
+ | |||
+ | Et voici la grille de câblage. | ||
+ | |||
+ | Ensuite, vous devez télécharger et décompresser le paquet micropython-font-to-py dans un dossier, puis créer un dossier quelque part qui servira de dossier de travail. Vous voudrez copier (au minimum) les fichiers font_to_py.py, | ||
+ | |||
**You need to modify the ssd1306_setup.py file to make it support our Pico I2C setup. | **You need to modify the ssd1306_setup.py file to make it support our Pico I2C setup. | ||
Ligne 35: | Ligne 51: | ||
ssd = SSD1306_I2C(WIDTH, | ssd = SSD1306_I2C(WIDTH, | ||
- | return ssd*** | + | return ssd** |
+ | |||
+ | Vous devez modifier le fichier ssd1306_setup.py pour qu'il supporte la configuration I2C de notre Pico. Cependant, il n'y a que quelques endroits que vous devez changer. Vers le haut du fichier, après les commentaires, | ||
+ | |||
+ | import machine | ||
+ | |||
+ | from ssd1306 import SSD1306_SPI, | ||
+ | |||
+ | WIDTH = const(128) | ||
+ | |||
+ | HEIGHT = const(64) | ||
+ | |||
+ | Vous devez modifier WIDTH et HEIGHT pour qu' | ||
+ | |||
+ | Maintenant, au bas du fichier, vous verrez les lignes de code suivantes. | ||
+ | |||
+ | else: | ||
+ | |||
+ | # i2c = machine.I2C(2) | ||
+ | |||
+ | i2c = machine.I2C(0) | ||
+ | |||
+ | ssd = SSD1306_I2C(WIDTH, | ||
+ | |||
+ | return ssd | ||
**Save your file. Copy the Writer.py and ssd1306_setup.py file to the Pico, if they are not already there. | **Save your file. Copy the Writer.py and ssd1306_setup.py file to the Pico, if they are not already there. | ||
Ligne 54: | Ligne 95: | ||
font_to_py.py {full path to font file} {size} {-x} {file name to save to}** | font_to_py.py {full path to font file} {size} {-x} {file name to save to}** | ||
+ | |||
+ | Enregistrez votre fichier. Copiez les fichiers Writer.py et ssd1306_setup.py sur le Pico, s'ils n'y sont pas déjà. | ||
+ | |||
+ | Nous pouvons maintenant créer nos fichiers de police spéciaux. L' | ||
+ | |||
+ | Pour pouvoir utiliser l' | ||
+ | |||
+ | pip install freetype-py | ||
+ | |||
+ | ou : | ||
+ | |||
+ | pip3 install freetype-py | ||
+ | |||
+ | Maintenant que nous nous sommes occupés de toutes nos dépendances, | ||
+ | |||
+ | La ligne de commande doit être : | ||
+ | |||
+ | font_to_py.py {chemin complet du fichier de polices} {taille} {-x} {nom du fichier à sauvegarder} | ||
+ | |||
**You MUST include a fully qualified path to the font you want to use. On my system there are two font locations. | **You MUST include a fully qualified path to the font you want to use. On my system there are two font locations. | ||
Ligne 69: | Ligne 129: | ||
ubuntu12.py written successfully.** | ubuntu12.py written successfully.** | ||
- | **Now we can see that the characters are 12 pixels in height and the width is 11 pixels. | + | Il est OBLIGATOIRE d' |
+ | |||
+ | Vous devez également vous assurer d' | ||
+ | |||
+ | $ python font_to_py.py / | ||
+ | |||
+ | Writing Python font file. | ||
+ | |||
+ | Height set in 1 passes. Actual height 12 pixels. | ||
+ | |||
+ | Max character width 11 pixels. | ||
+ | |||
+ | ubuntu12.py written successfully. | ||
+ | |||
+ | |||
+ | **Now we can see that the characters are 12 pixels in height and the width is 11 pixels. This will help decide how many characters you can fit on a line and how many lines you can fit on the 128x64 display. | ||
$ python font_test.py ubuntu12 Hello | $ python font_test.py ubuntu12 Hello | ||
Ligne 98: | Ligne 173: | ||
Now we need to do the same thing but this time make the font a 16 point font. We’ll keep the font as Ubuntu-Regular.** | Now we need to do the same thing but this time make the font a 16 point font. We’ll keep the font as Ubuntu-Regular.** | ||
+ | |||
+ | Nous pouvons maintenant voir que les caractères ont une hauteur de 12 pixels et une largeur de 11 pixels. Cela nous aidera à déterminer le nombre de caractères que vous pouvez faire tenir sur une ligne et le nombre de lignes que vous pouvez faire tenir sur l' | ||
+ | |||
+ | $ python font_test.py ubuntu12 Hello | ||
+ | |||
+ | Horizontal map | ||
+ | |||
+ | Normal bit order | ||
+ | |||
+ | Proportional spacing | ||
+ | |||
+ | Dimensions height*max_width 12 * 11 | ||
+ | |||
+ | Start char " " (ord 32) end char " | ||
+ | |||
+ | ............................ | ||
+ | |||
+ | ................# | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | .###### | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | ............................ | ||
+ | ............................ | ||
+ | |||
+ | Maintenant, nous devons faire la même chose, mais cette fois-ci en utilisant une police de 16 points. Nous allons garder la police Ubuntu Regular. | ||
+ | |||
**$ python font_to_py.py / | **$ python font_to_py.py / | ||
Ligne 137: | Ligne 243: | ||
...................................... | ...................................... | ||
......................................** | ......................................** | ||
+ | |||
+ | $ python font_to_py.py / | ||
+ | |||
+ | Writing Python font file. | ||
+ | |||
+ | Height set in 1 passes. Actual height 16 pixels. | ||
+ | |||
+ | Max character width 15 pixels. | ||
+ | |||
+ | ubuntu16.py written successfully. | ||
+ | |||
+ | Maintenant, testons le fichier de police 16 points. | ||
+ | |||
+ | $ python font_test.py ubuntu16 Hello | ||
+ | |||
+ | Horizontal map | ||
+ | |||
+ | Normal bit order | ||
+ | |||
+ | Proportional spacing | ||
+ | |||
+ | Dimensions height*max_width 16 * 15 | ||
+ | |||
+ | Start char " " (ord 32) end char " | ||
+ | |||
+ | ...................................... | ||
+ | .....................# | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | .######### | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | .# | ||
+ | ...................................... | ||
+ | ...................................... | ||
+ | ...................................... | ||
+ | |||
**In case you are curious about what the font file looks like, it’s a “normal” python file that contains the font definitions in byte format. | **In case you are curious about what the font file looks like, it’s a “normal” python file that contains the font definitions in byte format. | ||
Ligne 155: | Ligne 303: | ||
import time** | import time** | ||
+ | |||
+ | Au cas où vous seriez curieux de savoir à quoi ressemble le fichier de police, il s'agit d'un fichier python « normal » qui contient les définitions des polices au format octet. Vous pouvez certainement y jeter un coup d' | ||
+ | |||
+ | Maintenant, nous devons transférer les fichiers de police et écrire notre code de test sur le microcontrôleur Pico. | ||
+ | |||
+ | J'ai connecté mon Pico au port USB et j'ai lancé Thonny. Comme j'ai travaillé avec mon ESP32 et mon ESP8266, j'ai dû cliquer sur le coin inférieur droit pour m' | ||
+ | |||
+ | J'ai utilisé la commande du menu File | Open et sélectionné « This computer ». J'ai navigué jusqu' | ||
+ | |||
+ | Maintenant, ouvrez un nouvel onglet d' | ||
+ | |||
+ | import machine | ||
+ | |||
+ | from ssd1306_setup import WIDTH, HEIGHT, setup | ||
+ | |||
+ | from writer import Writer | ||
+ | |||
+ | import time | ||
Ligne 170: | Ligne 336: | ||
Next, we’ll display the same data in the 16 point font. Because the font is 16 point, we can’t fit all of the text in one screen like we did with the 12 point font. We’ll break it into two parts, the first two lines of alphabet characters, the numbers 1 to 0 and then some shifted characters. | Next, we’ll display the same data in the 16 point font. Because the font is 16 point, we can’t fit all of the text in one screen like we did with the 12 point font. We’ll break it into two parts, the first two lines of alphabet characters, the numbers 1 to 0 and then some shifted characters. | ||
+ | |||
+ | # Font | ||
+ | |||
+ | import ubuntu12 | ||
+ | |||
+ | import ubuntu16 | ||
+ | |||
+ | Maintenant, nous allons créer une fonction très simple qui fait le gros du travail pour nous. Nous l' | ||
+ | |||
+ | Dans cette fonction, nous commencerons par appeler la fonction setup de ssd1306_setup qui fera nos connexions I2C pour nous et dira à la fonction Writer d' | ||
+ | |||
+ | Nous terminons par un appel à ssd.show() afin d' | ||
+ | |||
+ | Ensuite, nous afficherons les mêmes données dans une police de 16 points. Comme la police est de 16 points, nous ne pouvons pas faire tenir tout le texte sur un seul écran comme nous le faisions avec la police de 12 points. Nous allons le diviser en deux parties, les deux premières lignes de caractères de l' | ||
+ | |||
**wri = Writer(ssd, ubuntu16) | **wri = Writer(ssd, ubuntu16) | ||
Ligne 198: | Ligne 379: | ||
Finally we’ll send out the lower case characters in the 16 point font as we did above. | Finally we’ll send out the lower case characters in the 16 point font as we did above. | ||
+ | |||
+ | wri = Writer(ssd, ubuntu16) | ||
+ | |||
+ | Writer.set_textpos(ssd, | ||
+ | |||
+ | wri.printstring(' | ||
+ | |||
+ | Writer.set_textpos(ssd, | ||
+ | |||
+ | wri.printstring(' | ||
+ | |||
+ | Writer.set_textpos(ssd, | ||
+ | |||
+ | wri.printstring(' | ||
+ | |||
+ | Writer.set_textpos(ssd, | ||
+ | |||
+ | wri.printstring(' | ||
+ | |||
+ | ssd.show() | ||
+ | |||
+ | time.sleep(3) | ||
+ | |||
+ | ssd.fill(0) | ||
+ | |||
+ | Enfin, nous envoyons les caractères minuscules dans la police 16 points comme nous l' | ||
+ | |||
** Writer.set_textpos(ssd, | ** Writer.set_textpos(ssd, | ||
Ligne 228: | Ligne 436: | ||
Until next time, as always; stay safe, healthy, positive and creative!** | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | Writer.set_textpos(ssd, | ||
+ | |||
+ | wri.printstring(' | ||
+ | |||
+ | Writer.set_textpos(ssd, | ||
+ | |||
+ | wri.printstring(' | ||
+ | |||
+ | ssd.show() | ||
+ | |||
+ | time.sleep(3) | ||
+ | |||
+ | ssd.fill(0) | ||
+ | |||
+ | ssd.show() | ||
+ | |||
+ | Enfin, nous appelons la fonction du code pour que tout démarre : | ||
+ | |||
+ | test() | ||
+ | |||
+ | C'est tout. Enregistrez le programme sous le nom de ubuntutest.py. | ||
+ | |||
+ | Maintenant, lorsque nous exécutons le programme, vous pouvez voir le résultat de ce dur labeur. | ||
+ | |||
+ | J'ai mis les fichiers de police et les fichiers de code sur mon dépôt à https:// | ||
+ | |||
+ | À ce stade, vous devriez être en mesure de choisir une police de votre choix et de la convertir pour qu' | ||
+ | |||
+ | Jusqu' | ||
+ |
issue177/micro-ci_micro-la.1643582925.txt.gz · Dernière modification : 2022/01/30 23:48 de d52fr