issue172: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édente | ||
issue172:micro-ci_micro-la [2021/08/30 09:45] – créée auntiee | issue172:micro-ci_micro-la [2021/08/31 17:14] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | + | **“LADIES AND GENTLEMEN, BOYS AND GIRLS. CHILDREN OF ALL AGES” (That last part is for Ronnie [“guilty as charged” - Ronnie]). Welcome to part 6 of Micro This Micro That! | |
- | “LADIES AND GENTLEMEN, BOYS AND GIRLS. CHILDREN OF ALL AGES” (That last part is for Ronnie [“guilty as charged” - Ronnie]). Welcome to part 6 of Micro This Micro That! | + | |
This month (August 2021), we’ll take a look at using the SSD1302 128x64 I2C OLED Display with our Microcontrollers. For all intents and purposes, it’s pretty much the same between the ESP8266, ESP32 and RPi Pico with a few exceptions. Those exceptions are where we connect the wiring for the I2C device and the way we access the I2C device. For many reasons, I selected the ESP8266 for this month’s project. | This month (August 2021), we’ll take a look at using the SSD1302 128x64 I2C OLED Display with our Microcontrollers. For all intents and purposes, it’s pretty much the same between the ESP8266, ESP32 and RPi Pico with a few exceptions. Those exceptions are where we connect the wiring for the I2C device and the way we access the I2C device. For many reasons, I selected the ESP8266 for this month’s project. | ||
Ligne 11: | Ligne 10: | ||
# construct an I2C bus | # construct an I2C bus | ||
+ | |||
+ | i2c = I2C(scl=Pin(5), | ||
+ | |||
+ | « MESDAMES ET MESSIEURS, GARÇONS ET FILLES. ENFANTS DE TOUS LES ÂGES » (Cette dernière partie est pour Ronnie [« Je plaide coupable » - Ronnie] | ||
+ | |||
+ | Ce mois-ci (août 2021), nous allons examiner l' | ||
+ | |||
+ | Récemment, les microprogrammes MicroPython de l' | ||
+ | |||
+ | Pour utiliser l' | ||
+ | |||
+ | from machine import Pin, I2C | ||
+ | |||
+ | # construire un bus I2C | ||
i2c = I2C(scl=Pin(5), | i2c = I2C(scl=Pin(5), | ||
- | If, on the other hand, you want to use the software I2C support, you can do it like this… | + | |
+ | **If, on the other hand, you want to use the software I2C support, you can do it like this… | ||
from machine import Pin, SoftI2C | from machine import Pin, SoftI2C | ||
Ligne 26: | Ligne 40: | ||
i2c = I2C(0) | i2c = I2C(0) | ||
- | And use the defined pins for bus 0 (or 1). This, however, created a number of problems, since there were so many options for the I2C bus pins. So, to be safe and avoid confusion, always define your pins. | + | And use the defined pins for bus 0 (or 1). This, however, created a number of problems, since there were so many options for the I2C bus pins. So, to be safe and avoid confusion, always define your pins.** |
- | The Wiring | + | Si, par contre, vous voulez utiliser le support du logiciel I2C, vous pouvez le faire comme ceci : |
+ | |||
+ | from machine import Pin, SoftI2C | ||
+ | |||
+ | # construire un bus I2C | ||
+ | |||
+ | i2c = SoftI2C(scl=Pin(5), | ||
+ | |||
+ | Très similaire, mais, bien sûr, si vous utilisez la solution logicielle, vous devez vraiment vous assurer de définir les broches. Dans les versions précédentes du firmware MicroPython du RPi, vous pouviez simplement utiliser : | ||
+ | |||
+ | i2c = I2C(0) | ||
+ | |||
+ | et vous servir des broches définies pour le bus 0 (ou 1). Cependant, cela a créé un certain nombre de problèmes, car il y avait un grand nombre d' | ||
+ | |||
+ | |||
+ | **The Wiring | ||
In this month’s project, the wiring is really simple. There’s only 4 wires. 3 volts, ground, data and clock. On my breadboard, I used the following pins. | In this month’s project, the wiring is really simple. There’s only 4 wires. 3 volts, ground, data and clock. On my breadboard, I used the following pins. | ||
Ligne 50: | Ligne 79: | ||
We can see that there is only one I2C device in our project and it is at address 0x3c, which is correct for our display. | We can see that there is only one I2C device in our project and it is at address 0x3c, which is correct for our display. | ||
- | Before we get into the coding of this month’s project, we need to download a driver library. You can find it at https:// | + | Before we get into the coding of this month’s project, we need to download a driver library. You can find it at https:// |
- | The Basic Code | + | Le câblage |
+ | |||
+ | Dans le projet de ce mois-ci, le câblage est très simple. Il n'y a que 4 fils. 3 volts, masse, données et horloge. Sur ma plaque d' | ||
+ | |||
+ | Bien sûr, les broches rouge et noire sont la tension et la masse, la jaune est l' | ||
+ | |||
+ | L' | ||
+ | |||
+ | Scanner le port I2C | ||
+ | |||
+ | Une fois le câblage effectué et le firmware flashé, nous voulons nous assurer que tout fonctionne correctement avant d' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | print(hex(dev)) | ||
+ | |||
+ | 0x3c | ||
+ | >>> | ||
+ | |||
+ | Nous pouvons voir qu'il n'y a qu'un seul périphérique I2C dans notre projet et qu'il est à l' | ||
+ | |||
+ | Avant d' | ||
+ | |||
+ | |||
+ | **The Basic Code | ||
We will start with a simple program to talk to the display. To use the display, you’ll have to get a copy of the base driver library for the ssd1306. If you are using the Raspberry Pi Pico, you can find it at https:// | We will start with a simple program to talk to the display. To use the display, you’ll have to get a copy of the base driver library for the ssd1306. If you are using the Raspberry Pi Pico, you can find it at https:// | ||
Ligne 68: | Ligne 123: | ||
display.show() | display.show() | ||
- | Save the file as displaybasic1.py and run it. If everything is working correctly, you will see something like this. | + | Save the file as displaybasic1.py and run it. If everything is working correctly, you will see something like this.** |
- | Let’s take a closer look at a couple of the lines of code. When we create the display object, we send the driver the width and height in pixels of the display and the bus that it is on. If you are using a smaller or larger display, your values will be a bit different. | + | Le code de base |
+ | |||
+ | Nous allons commencer par un programme simple pour parler à l' | ||
+ | |||
+ | Ce programme de test va simplement vérifier que nous pouvons écrire sur l' | ||
+ | |||
+ | from machine import Pin, I2C | ||
+ | import ssd1306 | ||
+ | |||
+ | # utiliser l' | ||
+ | i2c = I2C(sda=Pin(4), | ||
+ | display = ssd1306.SSD1306_I2C(128, | ||
+ | |||
+ | display.text(' | ||
+ | display.show() | ||
+ | |||
+ | Sauvegardez le fichier en tant que displaybasic1.py et exécutez-le. Si tout fonctionne correctement, | ||
+ | |||
+ | |||
+ | **Let’s take a closer look at a couple of the lines of code. When we create the display object, we send the driver the width and height in pixels of the display and the bus that it is on. If you are using a smaller or larger display, your values will be a bit different. | ||
display = ssd1306.SSD1306_I2C(128, | display = ssd1306.SSD1306_I2C(128, | ||
Ligne 82: | Ligne 156: | ||
display.show() | display.show() | ||
- | That’s it. We’ll look at the graphics capability and other options of the “regular” driver in a later article. If you get the expected results, then we’ll look at the library we downloaded earlier. | + | That’s it. We’ll look at the graphics capability and other options of the “regular” driver in a later article. If you get the expected results, then we’ll look at the library we downloaded earlier.** |
- | Better Displays | + | Examinons de plus près quelques-unes des lignes de code. Lorsque nous créons l' |
+ | |||
+ | display = ssd1306.SSD1306_I2C(128, | ||
+ | |||
+ | La ligne suivante est la façon dont nous envoyons le texte à l' | ||
+ | |||
+ | display.text(' | ||
+ | |||
+ | Enfin, pour afficher réellement le texte, nous utilisons la méthode show. | ||
+ | |||
+ | display.show() | ||
+ | |||
+ | C'est tout. Nous examinerons la capacité graphique et les autres options du pilote « normal » dans un prochain article. Si vous obtenez les résultats escomptés, nous nous pencherons alors sur la bibliothèque que nous avons téléchargée précédemment. | ||
+ | |||
+ | |||
+ | **Better Displays | ||
If you are one of the lucky people who have great eyesight, then you can read the display from 3 feet or more without problems. However, for an old man like me, while the display is bright, trying to read anything greater than about 16 inches away is a strain. Wouldn’t it be nice to be able to use a bigger and denser font? That’s what we are here to talk about. (I know, it took me 4 pages to get to the real reason we are here. Sorry about that.) | If you are one of the lucky people who have great eyesight, then you can read the display from 3 feet or more without problems. However, for an old man like me, while the display is bright, trying to read anything greater than about 16 inches away is a strain. Wouldn’t it be nice to be able to use a bigger and denser font? That’s what we are here to talk about. (I know, it took me 4 pages to get to the real reason we are here. Sorry about that.) | ||
Ligne 101: | Ligne 190: | ||
If you are using a ssd1306 that has 128x64 resolution, then you don’t need to change anything here. However, if you are using a 128x32 ssd1306, change the height value to 32. I show it commented out here. | If you are using a ssd1306 that has 128x64 resolution, then you don’t need to change anything here. However, if you are using a 128x32 ssd1306, change the height value to 32. I show it commented out here. | ||
+ | |||
+ | WIDTH = const(128) | ||
+ | |||
+ | HEIGHT = const(64) | ||
+ | |||
+ | # HEIGHT = const(32)** | ||
+ | |||
+ | Affichages améliorés | ||
+ | |||
+ | Si vous faites partie des personnes chanceuses qui ont une excellente vue, vous pouvez lire l' | ||
+ | |||
+ | Le zip de distribution de micropython-font-to-py contient un très grand nombre de fichiers, mais nous n' | ||
+ | |||
+ | Nous allons modifier le fichier writer_demo.py ; vous pouvez donc soit le copier, soit récupérer le code dans le dépôt de ce mois-ci. Les quatre premiers fichiers sont les fichiers de police, le fichier ssd1306_setup.py vous permet de configurer votre broche I2C, et le fichier writer.py est le pilote réel qui fait toute la magie. Nous allons commencer par modifier le fichier ssd1306_setup.py pour définir notre configuration. | ||
+ | |||
+ | Comme d' | ||
+ | |||
+ | import machine | ||
+ | |||
+ | from machine import Pin | ||
+ | |||
+ | from ssd1306 import SSD1306_SPI, | ||
+ | |||
+ | Si vous utilisez un ssd1306 qui a une résolution de 128x64, vous n'avez rien à changer ici. Cependant, si vous utilisez un ssd1306 128x32, changez la valeur de la hauteur à 32. Je le montre commenté ici : | ||
WIDTH = const(128) | WIDTH = const(128) | ||
Ligne 108: | Ligne 221: | ||
# HEIGHT = const(32) | # HEIGHT = const(32) | ||
- | The other thing that needs to be changed is in the setup function, down near the end of the function. The first part allows you to use an SPI based display. I’ve just got it here for reference. Since this driver was originally written for the Pyboard, we will have to change a few things here. In order to make things easier for our three possible microcontrollers, | + | |
+ | **The other thing that needs to be changed is in the setup function, down near the end of the function. The first part allows you to use an SPI based display. I’ve just got it here for reference. Since this driver was originally written for the Pyboard, we will have to change a few things here. In order to make things easier for our three possible microcontrollers, | ||
At this point, we need to edit or create the writer_demo.py file. I’m going to leave out most of the comments here, but they will be in the file in my distribution. | At this point, we need to edit or create the writer_demo.py file. I’m going to leave out most of the comments here, but they will be in the file in my distribution. | ||
I’ll show you the main parts of the demo file and then I’ll explain how you can change it. | I’ll show you the main parts of the demo file and then I’ll explain how you can change it. | ||
+ | |||
+ | |||
+ | import machine | ||
+ | |||
+ | from ssd1306_setup import WIDTH, HEIGHT, setup | ||
+ | |||
+ | from writer import Writer | ||
+ | |||
+ | import time** | ||
+ | |||
+ | L' | ||
+ | |||
+ | A ce stade, nous devons modifier ou créer le fichier writer_demo.py. Je vais laisser de côté la plupart des commentaires ici, mais ils seront dans mon fichier de diffusion. | ||
+ | |||
+ | Je vais vous montrer les parties principales du fichier de démonstration et ensuite j' | ||
Ligne 123: | Ligne 252: | ||
import time | import time | ||
- | Again, we start with the support file imports. We also need to import the font file(s) that we want to include. Since I purposely chose to use the ESP8266, memory is at a premium, so I’m going to deal with only one font file at a time in this demo. For now, we’ll begin with just the font10.py file. I’ve commented out the other two in our code (I’ve chosen to ignore the courier20.py font, but you can certainly include it if you wish). | + | |
+ | **Again, we start with the support file imports. We also need to import the font file(s) that we want to include. Since I purposely chose to use the ESP8266, memory is at a premium, so I’m going to deal with only one font file at a time in this demo. For now, we’ll begin with just the font10.py file. I’ve commented out the other two in our code (I’ve chosen to ignore the courier20.py font, but you can certainly include it if you wish). | ||
# Font | # Font | ||
Ligne 138: | Ligne 268: | ||
# wri = Writer(ssd, | # wri = Writer(ssd, | ||
- | The base (always used) code is almost done. You can always use this code in your own project. | + | The base (always used) code is almost done. You can always use this code in your own project.** |
+ | |||
+ | Là encore, nous commençons par importer les fichiers de support. Nous devons également importer le ou les fichiers des polices que nous voulons inclure. Comme j'ai délibérément choisi d' | ||
+ | |||
+ | # Police | ||
+ | # import freesans20 | ||
+ | # import font6 | ||
+ | import font10 | ||
+ | |||
+ | Maintenant que les importations sont faites, nous allons créer une fonction de test. L' | ||
+ | |||
+ | def test(use_spi=False) : | ||
+ | ssd = setup(False, | ||
+ | wri = Writer(ssd, font10) | ||
+ | # wri = Writer(ssd, font6) | ||
+ | # wri = Writer(ssd, | ||
+ | |||
+ | Le code de base (toujours utilisé) est presque terminé. Vous pouvez toujours utiliser ce code dans votre propre projet. | ||
- | You will also want to include the next line (first line below) as well. This ensures that the display starts at row 0, column 0, at startup. The next steps are to get the current time and date (time.localtime()) and format it to show the date on the first line and the time on the second line. Finally, we use ssd.show() to actually display the information. The information that we get from the localtime call is returned as (2021, 8, 4, 8, 52, 49, 2, 216). Year, Month, Day, Hour, Minute, Second, Milliseconds. Remember the data is zero based, so the date information is dt[1], dt[2], dt[0] for the crazy americans. If you live in one of the saner countries, feel free to format it as you wish! (code shown above) | + | **You will also want to include the next line (first line below) as well. This ensures that the display starts at row 0, column 0, at startup. The next steps are to get the current time and date (time.localtime()) and format it to show the date on the first line and the time on the second line. Finally, we use ssd.show() to actually display the information. The information that we get from the localtime call is returned as (2021, 8, 4, 8, 52, 49, 2, 216). Year, Month, Day, Hour, Minute, Second, Milliseconds. Remember the data is zero based, so the date information is dt[1], dt[2], dt[0] for the crazy americans. If you live in one of the saner countries, feel free to format it as you wish! (code shown above) |
This is where I deviate (moreso) from the demo code that the author provides. I want to show how to display the time every second and what you have to do to overwrite the previous information. I chose to do this for a period of 2 minutes. If you feel that is too long or short, go ahead and change the 120 in the for loop to a different value. | This is where I deviate (moreso) from the demo code that the author provides. I want to show how to display the time every second and what you have to do to overwrite the previous information. I chose to do this for a period of 2 minutes. If you feel that is too long or short, go ahead and change the 120 in the for loop to a different value. | ||
Ligne 150: | Ligne 298: | ||
test(False) | test(False) | ||
- | Save the file as writer_demo2.py (just to make sure the original one is still there), and run the program. There is a chance that, if you are using the ESP8266, then you might get a memory error when you try to run it. The author’s documentation suggests that you should be able to use the file writer_minimal.py to replace writer.py. However, when I tried to use it, I got errors. So, if you are having memory issues, try to remove any unused files from your device and/or try a smaller font file. | + | Save the file as writer_demo2.py (just to make sure the original one is still there), and run the program. There is a chance that, if you are using the ESP8266, then you might get a memory error when you try to run it. The author’s documentation suggests that you should be able to use the file writer_minimal.py to replace writer.py. However, when I tried to use it, I got errors. So, if you are having memory issues, try to remove any unused files from your device and/or try a smaller font file.** |
- | Here is what each font looks like on my display. | + | Vous voudrez également inclure la ligne suivante (première ligne en dessous). Elle garantit que l' |
+ | |||
+ | C'est ici que je m' | ||
+ | |||
+ | Nous définissons la position du texte à la ligne 18 colonne 0 (illustrée ci-dessous) pour écraser l' | ||
+ | |||
+ | Enfin, nous appelons notre fonction de test pour commencer à nous amuser. | ||
+ | |||
+ | test(False) | ||
+ | |||
+ | Enregistrez le fichier en tant que writer_demo2.py (juste pour être sûr que l' | ||
+ | |||
+ | |||
+ | **Here is what each font looks like on my display. | ||
Font6.py | Font6.py | ||
Font10.py | Font10.py | ||
Ligne 166: | Ligne 327: | ||
You can find all of the code and image files on my github repository at https:// | You can find all of the code and image files on my github repository at https:// | ||
- | As always, until next time: stay safe, healthy, positive and creative! | + | As always, until next time: stay safe, healthy, positive and creative!** |
+ | |||
+ | Voici à quoi ressemble chaque police sur mon écran. | ||
+ | |||
+ | Font6.py | ||
+ | |||
+ | Font10.py | ||
+ | |||
+ | Freesans20.py | ||
+ | |||
+ | Et à nouveau le pilote de base pour comparaison. | ||
+ | |||
+ | Vous pouvez voir que les trois polices générées par le pilote Writer sont bien meilleures à distance que le pilote de base. Même en utilisant la police 6.py, le texte est assez facile à lire à environ 60 cm et, au moins pour moi, la police 10.py est la meilleure si l'on considère la lisibilité à distance et la taille de la mémoire. | ||
+ | |||
+ | Comme je l'ai suggéré précédemment, | ||
+ | |||
+ | Vous trouverez l' | ||
+ | |||
+ | Comme toujours, jusqu' | ||
issue172/micro-ci_micro-la.1630309546.txt.gz · Dernière modification : 2021/08/30 09:45 de auntiee