issue176:micro
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue176:micro [2022/01/02 14:28] – créée auntiee | issue176:micro [2022/01/07 16:25] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | First, I want to wish all our readers a very happy holiday season. No matter what you wish to celebrate, please do so safely! | + | **First, I want to wish all our readers a very happy holiday season. No matter what you wish to celebrate, please do so safely! |
Yes, we are going to talk about more graphics for our MicroController projects. Yet again, we will be using the SSD-1306 for the display, and, for the microcontroller this time, we’ll go back to our ESP-32 generic. | Yes, we are going to talk about more graphics for our MicroController projects. Yet again, we will be using the SSD-1306 for the display, and, for the microcontroller this time, we’ll go back to our ESP-32 generic. | ||
- | 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.** |
- | 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:// | + | 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:// | ||
The first thing that we need to have is some image files. I’m going to use weather icons that I found at https:// | The first thing that we need to have is some image files. I’m going to use weather icons that I found at https:// | ||
Ligne 11: | Ligne 18: | ||
The next step is to convert the images we wish into PBM format. We’ll use GIMP to accomplish that. Open GIMP and load the image you want. We’ll use the weather_icon-47.png file for this tutorial. | The next step is to convert the images we wish into PBM format. We’ll use GIMP to accomplish that. Open GIMP and load the image you want. We’ll use the weather_icon-47.png file for this tutorial. | ||
- | 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.** |
- | 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. | + | 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. | ||
Now we want to invert the image so the black pixels become white. Select Colors | Invert from the menu bar. Now we need to convert the image to a 1-bit-per-pixel image. To do this, select Image | Mode | Indexed from the menu bar. You’ll see a dialog pop up that looks something like this. | Now we want to invert the image so the black pixels become white. Select Colors | Invert from the menu bar. Now we need to convert the image to a 1-bit-per-pixel image. To do this, select Image | Mode | Indexed from the menu bar. You’ll see a dialog pop up that looks something like this. | ||
Ligne 19: | Ligne 35: | ||
Make sure you have Use black and white (1-bit) palette selected and Remove unused and duplicate colors from colormap selected as well. Select Floyd-Steinberg (Reduced color bleeding) in the Color dithering dropdown, then click on the Convert button. | Make sure you have Use black and white (1-bit) palette selected and Remove unused and duplicate colors from colormap selected as well. Select Floyd-Steinberg (Reduced color bleeding) in the Color dithering dropdown, then click on the Convert 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. | + | 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.** |
- | Your image should now look like this. | + | 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. | ||
That’s it for that part. It wasn’t too very painful – detailed, but not painful. Now we want to upload this image to our microcontroller. Again, I use mpfshell to do this (please see my note near the bottom of the article on using mpfshell). Next, we’ll look at our code to display it on the OLED display. We’ll name our file graphx32.py since we are using it on an ESP-32 generic microcontroller. | That’s it for that part. It wasn’t too very painful – detailed, but not painful. Now we want to upload this image to our microcontroller. Again, I use mpfshell to do this (please see my note near the bottom of the article on using mpfshell). Next, we’ll look at our code to display it on the OLED display. We’ll name our file graphx32.py since we are using it on an ESP-32 generic microcontroller. | ||
Ligne 27: | Ligne 52: | ||
As always, we need to import some libraries. We need the ssd1306 and framebuf libraries, as well as the SoftI2C and Pin libraries from the machine library. Next we need to define the display object for the display (top right). | As always, we need to import some libraries. We need the ssd1306 and framebuf libraries, as well as the SoftI2C and Pin libraries from the machine library. Next we need to define the display object for the display (top 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). | + | 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).** |
- | The last things we need to do are load the image data (without the header information) into the framebuffer, | + | 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 P4 is the identifier that the file is actually a PBM file. (There is another PBM format that uses a P1, which denotes a “Plain PBM” file). The second line as you can see is a comment that states that the file was created by GIMP. Finally you can see that the image dimensions are, as we expected, 50 x 50 pixels. This information will be important if you don’t remember what the size of the image file will be. | The P4 is the identifier that the file is actually a PBM file. (There is another PBM format that uses a P1, which denotes a “Plain PBM” file). The second line as you can see is a comment that states that the file was created by GIMP. Finally you can see that the image dimensions are, as we expected, 50 x 50 pixels. This information will be important if you don’t remember what the size of the image file will be. | ||
Ligne 35: | Ligne 69: | ||
When you run the program, your display should look like this. | When you run the program, your display should look like this. | ||
+ | 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' |
- | You might not want to have to deal with GIMP everytime you want to convert a PNG file to PBM. | + | 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. | ||
Based on the steps we followed using GIMP to convert the file, I threw together a quick and VERY dirty Python program to do the conversion. We’ll use weather_icon-49.png for this part of the project. Name your file ConvertSingle.py . | Based on the steps we followed using GIMP to convert the file, I threw together a quick and VERY dirty Python program to do the conversion. We’ll use weather_icon-49.png for this part of the project. Name your file ConvertSingle.py . | ||
Ligne 44: | Ligne 86: | ||
When you run the program, it should just take a moment to finish and you’ll have a nice 32 x 32 PBM format image. | When you run the program, it should just take a moment to finish and you’ll have a nice 32 x 32 PBM format image. | ||
- | 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)…** |
- | 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. | + | 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 new image is only 32x32, be sure to change the line that loads the data into the frame buffer to… | Since the new image is only 32x32, be sure to change the line that loads the data into the frame buffer to… | ||
Ligne 56: | Ligne 107: | ||
To run the above code on the RPi Pico, I had to make a very few changes – you can find the code file in the zipped repository as graphxPico.py. | To run the above code on the RPi Pico, I had to make a very few changes – you can find the code file in the zipped repository as graphxPico.py. | ||
- | 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.1641130124.txt.gz · Dernière modification : 2022/01/02 14:28 de auntiee