issue177:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédenteProchaine révisionLes deux révisions suivantes | ||
issue177:micro-ci_micro-la [2022/01/29 14:41] – créée auntiee | issue177:micro-ci_micro-la [2022/02/01 17:07] – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Well, it’s been almost a year since I’ve started this column. | + | **Well, it’s been almost a year since I’ve started this column. |
Once again, we will deal with the SSD1306 display, but this time we will be talking about creating custom fonts. | Once again, we will deal with the SSD1306 display, but this time we will be talking about creating custom fonts. | ||
- | 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. |
- | This time, we’ll be creating our own fonts for the OLED display using our normal desktop computers and regular Python. | + | 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. | ||
For this project, we’ll use the Raspberry Pi Pico microcontroller and a SSD1306 128x64 OLED Display. | For this project, we’ll use the Raspberry Pi Pico microcontroller and a SSD1306 128x64 OLED Display. | ||
Ligne 11: | Ligne 18: | ||
And here is the wiring grid. | And here is the wiring grid. | ||
- | 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. |
import machine | import machine | ||
Ligne 26: | Ligne 42: | ||
Now at the bottom of the file, you will see the following lines of code. Make sure you change the i2c = machine.I2C(2) to i2c = machine.I2C(0) | Now at the bottom of the file, you will see the following lines of code. Make sure you change the i2c = machine.I2C(2) to i2c = machine.I2C(0) | ||
+ | |||
+ | else: | ||
+ | |||
+ | # i2c = machine.I2C(2) | ||
+ | |||
+ | i2c = machine.I2C(0) | ||
+ | |||
+ | ssd = SSD1306_I2C(WIDTH, | ||
+ | |||
+ | 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: | else: | ||
Ligne 37: | Ligne 77: | ||
return ssd | 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. | ||
Now we can create our special font files. | Now we can create our special font files. | ||
Ligne 53: | Ligne 94: | ||
The command line should be | The command line should be | ||
- | 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. |
You also need to be sure to include the -x parameter when you create the font file. This makes sure that the font is horizontally mapped. | You also need to be sure to include the -x parameter when you create the font file. This makes sure that the font is horizontally mapped. | ||
+ | |||
+ | $ 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.** | ||
+ | |||
+ | Il est OBLIGATOIRE d' | ||
+ | |||
+ | Vous devez également vous assurer d' | ||
$ python font_to_py.py / | $ python font_to_py.py / | ||
Ligne 69: | Ligne 143: | ||
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. | + | |
+ | **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 97: | Ligne 172: | ||
............................ | ............................ | ||
- | 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.** |
- | $ python font_to_py.py / | + | 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 / | ||
Writing Python font file. | Writing Python font file. | ||
Ligne 136: | Ligne 242: | ||
...................................... | ...................................... | ||
...................................... | ...................................... | ||
+ | ......................................** | ||
+ | |||
+ | $ 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. |
Now we need to transfer the font files and write our test code on the Pico Microcontroller. | Now we need to transfer the font files and write our test code on the Pico Microcontroller. | ||
Ligne 147: | Ligne 295: | ||
Now open a new editor tab in Thonny and we’ll start writing the code for the Pico. First, we’ll (as always) setup the imports. | Now open a new editor tab in Thonny and we’ll start writing the code for the Pico. First, we’ll (as always) setup the imports. | ||
+ | |||
+ | import machine | ||
+ | |||
+ | from ssd1306_setup import WIDTH, HEIGHT, setup | ||
+ | |||
+ | from writer import Writer | ||
+ | |||
+ | 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 | import machine | ||
Ligne 157: | Ligne 323: | ||
- | # Font | + | **# Font |
import ubuntu12 | import ubuntu12 | ||
Ligne 169: | Ligne 335: | ||
We finish up with making a call to ssd.show() in order to actually display the data we just sent and then we’ll sleep for 3 seconds and after the 3 second delay, we’ll clear the display with ssd.fill(0) which turns off all the pixels. | We finish up with making a call to ssd.show() in order to actually display the data we just sent and then we’ll sleep for 3 seconds and after the 3 second delay, we’ll clear the display with ssd.fill(0) which turns off all the pixels. | ||
- | 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. |
- | wri = Writer(ssd, ubuntu16) | + | # 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) | ||
Writer.set_textpos(ssd, | Writer.set_textpos(ssd, | ||
Ligne 197: | Ligne 378: | ||
ssd.fill(0) | ssd.fill(0) | ||
- | 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' | ||
+ | |||
+ | |||
+ | ** | ||
| | ||
Ligne 227: | Ligne 435: | ||
At this point, you should be able to pick a font of your choice and convert it to run on your microcontroller to support your own programs. | At this point, you should be able to pick a font of your choice and convert it to run on your microcontroller to support your own programs. | ||
- | 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.txt · Dernière modification : 2022/02/02 09:04 de d52fr