issue176:micro
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 | ||
issue176:micro [2022/01/05 08:11] – d52fr | issue176:micro [2022/01/07 16:25] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
Now, the reason for this article is to take “standard” graphics and convert them into something that can be displayed on a 2-color (black and white) SSD-1306 OLED display. It is not as straightforward as one would expect. We’ll need to use GIMP or PhotoShop to do the conversion which is not terribly difficult, but there are some steps involved that you will need to pretty much follow in the order as presented to accomplish this very important step. We’ll also need to use the mpfshell utility we discussed back in FCM#174 (part 8 of the Micro This Micro That series) to transfer the image files to the microcontroller. The version we used then was 0.9.2.** | Now, the reason for this article is to take “standard” graphics and convert them into something that can be displayed on a 2-color (black and white) SSD-1306 OLED display. It is not as straightforward as one would expect. We’ll need to use GIMP or PhotoShop to do the conversion which is not terribly difficult, but there are some steps involved that you will need to pretty much follow in the order as presented to accomplish this very important step. We’ll also need to use the mpfshell utility we discussed back in FCM#174 (part 8 of the Micro This Micro That series) to transfer the image files to the microcontroller. The version we used then was 0.9.2.** | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | Oui, nous allons parler encore d' | ||
+ | |||
+ | La raison de cet article est de prendre des fonctions graphiques « standards » et de les convertir en quelque chose qui peut être affiché sur un écran OLED SSD-1306 bicolore (noir et blanc). Ce n'est pas aussi simple qu'on pourrait le croire. Nous devrons utiliser GIMP ou PhotoShop pour effectuer la conversion, ce qui n'est pas très difficile, mais il y a quelques étapes à suivre, dans l' | ||
+ | |||
**Before we actually get started, I need to thank Martin Fitzpatrick for the wonderful writeup on using PBM files and the micropython code to support it. His website is https:// | **Before we actually get started, I need to thank Martin Fitzpatrick for the wonderful writeup on using PBM files and the micropython code to support it. His website is https:// | ||
Ligne 12: | Ligne 19: | ||
Since the image is 96x96 pixels, and our display is at best 128x64, we’ll want to pick something slightly smaller. Let’s pick 50x50. Select Image | Scale Image from the menu bar.** | Since the image is 96x96 pixels, and our display is at best 128x64, we’ll want to pick something slightly smaller. Let’s pick 50x50. Select Image | Scale Image from the menu bar.** | ||
+ | |||
+ | Avant de commencer, je dois remercier Martin Fitzpatrick pour son excellent article sur l' | ||
+ | |||
+ | La première chose dont nous avons besoin, ce sont des fichiers images. Je vais utiliser les icônes météo que j'ai trouvées sur https:// | ||
+ | |||
+ | L' | ||
+ | |||
+ | Comme l' | ||
+ | |||
**Highlight the Width value and change it to 50. Then simply press the {Tab} key to automatically set the Height value. Click on the Scale button to make the change. | **Highlight the Width value and change it to 50. Then simply press the {Tab} key to automatically set the Height value. Click on the Scale button to make the change. | ||
Ligne 20: | Ligne 36: | ||
Finally, we need to export it to our .pbm file format. Select File | Export As… from the menu bar, then, in the dialog, change the extension from .png to .pbm and then click the Export button. Another dialog pops up asking the Data formatting type. You want to make sure that Raw is selected then click the Export button.** | Finally, we need to export it to our .pbm file format. Select File | Export As… from the menu bar, then, in the dialog, change the extension from .png to .pbm and then click the Export button. Another dialog pops up asking the Data formatting type. You want to make sure that Raw is selected then click the Export button.** | ||
+ | |||
+ | Mettez en surbrillance la valeur de la Largeur et changez-la en 50. Ensuite, il suffit d' | ||
+ | |||
+ | Nous voulons maintenant inverser l' | ||
+ | |||
+ | Assurez-vous que vous avez sélectionné « Utiliser la palette noir et blanc (1 bit) » et « Supprimer les couleurs inutilisées et en double de la carte des couleurs ». Sélectionnez Floyd-Steinberg (Couleurs qui déteignent le moins) dans la liste déroulante Tramage des couleurs, puis cliquez sur le bouton Convertir. | ||
+ | |||
+ | Enfin, nous devons exporter l' | ||
+ | |||
**Your image should now look like this. | **Your image should now look like this. | ||
Ligne 28: | Ligne 53: | ||
Now we will open the icon image file and do three readline calls. This gets the Magic Number, the Creator comment, and the dimension of the image from the image file header (we’ll talk about these in a moment), and finally the data of the image itself (middle right).** | Now we will open the icon image file and do three readline calls. This gets the Magic Number, the Creator comment, and the dimension of the image from the image file header (we’ll talk about these in a moment), and finally the data of the image itself (middle right).** | ||
+ | |||
+ | Votre image devrait maintenant ressembler à ceci : | ||
+ | |||
+ | C'est tout pour cette partie. Ce n' | ||
+ | |||
+ | Comme toujours, nous devons importer certaines bibliothèques. Nous avons besoin des bibliothèques ssd1306 et framebuf, ainsi que des bibliothèques SoftI2C et Pin de la bibliothèque machine. Ensuite, nous devons définir l' | ||
+ | |||
+ | Maintenant, nous allons ouvrir le fichier image de l' | ||
+ | |||
**The last things we need to do are load the image data (without the header information) into the framebuffer, | **The last things we need to do are load the image data (without the header information) into the framebuffer, | ||
Ligne 36: | Ligne 70: | ||
Using this method is (in my mind) much better than converting images to a bytearray. It takes a few more lines of code to use the PBM file method, but I find it much easier to work with.** | Using this method is (in my mind) much better than converting images to a bytearray. It takes a few more lines of code to use the PBM file method, but I find it much easier to work with.** | ||
+ | |||
+ | Les dernières choses que nous devons faire sont de charger les données de l' | ||
+ | |||
+ | P4 est l' | ||
+ | |||
+ | Lorsque vous exécutez le programme, votre affichage devrait ressembler à ceci : | ||
+ | |||
+ | L' | ||
+ | |||
**You might not want to have to deal with GIMP everytime you want to convert a PNG file to PBM. | **You might not want to have to deal with GIMP everytime you want to convert a PNG file to PBM. | ||
Ligne 44: | Ligne 87: | ||
There is a small problem with the file we created using the PIL library. There doesn’t seem to be a way to set the Creator Comment in the header of the image file. Therefore, we need to make a small change to our micropython file to handle the lack of the comment. We need to change the with open code to the following (bottom left)…** | There is a small problem with the file we created using the PIL library. There doesn’t seem to be a way to set the Creator Comment in the header of the image file. Therefore, we need to make a small change to our micropython file to handle the lack of the comment. We need to change the with open code to the following (bottom left)…** | ||
+ | |||
+ | Vous n'avez peut-être pas envie d' | ||
+ | |||
+ | En me basant sur les étapes que nous avons suivies avec GIMP pour convertir le fichier, j'ai créé un programme Python rapide et TRÈS sale pour effectuer la conversion. Nous allons utiliser weather_icon-49.png pour cette partie du projet. Nommez votre fichier ConvertSingle.py . | ||
+ | |||
+ | Lorsque vous exécutez le programme, il ne devrait prendre qu'un instant pour se terminer et vous aurez une belle image de 32 x 32 au format PBM. | ||
+ | |||
+ | Il y a un petit problème avec le fichier que nous avons créé en utilisant la bibliothèque PIL. Il ne semble pas y avoir de moyen de définir le commentaire du créateur dans l' | ||
+ | |||
**Since the header comment (according to the specs that I can find) always starts with a #, we can simply check the value of the second line read to see if it starts with the pound symbol (or hash sign for all you social media users). If so, then we simply read the next line. If it doesn’t, we don’t want to try to read another line. There will only be two lines in the header, not three. If we try to read three and the comment doesn’t exist, the data portion of the file will be short and thereby corrupt. By using the scheme above, it will handle both cases properly. | **Since the header comment (according to the specs that I can find) always starts with a #, we can simply check the value of the second line read to see if it starts with the pound symbol (or hash sign for all you social media users). If so, then we simply read the next line. If it doesn’t, we don’t want to try to read another line. There will only be two lines in the header, not three. If we try to read three and the comment doesn’t exist, the data portion of the file will be short and thereby corrupt. By using the scheme above, it will handle both cases properly. | ||
Ligne 56: | Ligne 108: | ||
I hope that you and your family and friends have a happy and safe holiday season.** | I hope that you and your family and friends have a happy and safe holiday season.** | ||
+ | |||
+ | Puisque le commentaire d' | ||
+ | |||
+ | Puisque la nouvelle image ne fait que 32x32, assurez-vous de changer la ligne qui charge les données dans framebuffer en : | ||
+ | |||
+ | fbuf = framebuf.FrameBuffer(data, | ||
+ | |||
+ | Vous pouvez voir sur l' | ||
+ | |||
+ | J'ai placé tout le code de l' | ||
+ | |||
+ | Pour faire fonctionner le code ci-dessus sur le RPi Pico, j'ai dû faire très peu de changements, | ||
+ | |||
+ | J' | ||
+ | |||
+ | //Encart p. 35, texte en noir// | ||
+ | |||
+ | **I mentioned the Magic number, the Creator comment, and the Dimensions in the header. Here is what gets printed in the Thonny REPL when the program runs.** | ||
+ | |||
+ | J'ai mentionné le nombre magique, le commentaire du créateur et les dimensions dans l' | ||
+ | |||
+ | |||
+ | //Encart p. 37// | ||
+ | |||
+ | **NOTE ON USING MPFSHELL | ||
+ | |||
+ | When I was developing the project for this month, I was using mpfshell version 0.9.2 running under Python 3.7.4. This is also true for my environment when I wrote the article about using mpfshell in FCM #174. However, this month, I tried to use mpfshell under Python 3.8.11 and it wouldn’t connect to the ESP32 microcontroller. I was in a rush to get the article written so I just continued using Python 3.7.4 with no problems. When I passed an early version of the article and source code to my friend Halvard in Norway, he had issues with mpfshell using a more recent version of Python. I did some research on the mpfshell github repository and found that others were having problems on various versions of Linux using 0.9.2 under Python 3.8 and up, but mpfshell 0.9.1 seemed to work fine for them. I spent a couple of hours testing in virtual environments using mpfshell 0.9.1 under Python versions 3.7.4, 3.8.10, 3.9.5 and 3.10.0 (thanks to pyenv), and everything worked well and was able to connect to not only ESP8266 (the only officially supported microcontroller) but also ESP32 and RPi Pico. So, if you are having issues connecting to your microcontroller using mpfshell, you can use pip (or pip3) to uninstall mpfshell then install mpfshell version 0.9.1. It works just fine. The only thing that needs to change is the open command to connect via serial to the microcontroller. | ||
+ | |||
+ | Instead of using “open / | ||
+ | |||
+ | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | NOTE SUR L' | ||
+ | |||
+ | Lorsque j' | ||
+ | |||
+ | Au lieu d' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ |
issue176/micro.1641366676.txt.gz · Dernière modification : 2022/01/05 08:11 de d52fr