Pré-requis

Le système d'exploitation utilisé est Ubuntu 10.04.2 LTS x64.

Paquets

Les paquets suivants sont requis :

Ils sont installées depuis les dépôts Ubuntu, par exemple à l'aide de la commande :

~$ sudo apt-get install python python-serial

Bibliothèque pyBusPirateLite

Des bibliothèques permettant de piloter le Bus Pirate en mode binaire sont disponibles sur sa forge, et plus particulièrement la bibliothèque pyBusPirateLite. La version utilisée dans cet article est la release 598. Il est possible de la récupérer sur la forge ou en téléchargeant cette archive.

Pilotage du MAX7219

Une présentation du MAX7219 a déjà été proposée dans cet article.

Bibliothèque MAX7219

Le meilleur moyen de produire du logiciel modulaire et réutilisable est de développer une bibliothèque. La bibliothèque MAX7219 est composée de deux fichiers :

Le fichier max7219.py constitue l'API de la bibliothèque.

#!/usr/bin/env python
# encoding: utf-8

"""
Copyright (C) 2011  Julien Le Sech - www.idreammicro.com

The MAX7219 Bus Pirate library is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.

The MAX7219 Bus Pirate library is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/
"""

from serial.serialutil import SerialException

from pyBusPirateLite.SPI import *

from max7219_constants import *

class ShutdownMode:
    """
    Shutdown modes.
    """
    Shutdown		= 0x00
    NormalOperation	= 0x01

class DisplayTest:
    """
    Display test modes.
    """
    NormalOperation	= 0x00
    TestMode		= 0x01

class ScanLimit:
    """
    Scan limits.
    """
    Digit0		= 0x00
    Digit0To1	= 0x01
    Digit0To2	= 0x02
    Digit0To3	= 0x03
    Digit0To4	= 0x04
    Digit0To5	= 0x05
    Digit0To6	= 0x06
    Digit0To7	= 0x07

class Intensity:
    """
    Intensities.
    """
    Level0	= 0x00
    Level1	= 0x01
    Level2	= 0x02
    Level3	= 0x03
    Level4	= 0x04
    Level5	= 0x05
    Level6	= 0x06
    Level7	= 0x07
    Level8	= 0x08
    Level9	= 0x09
    Level10	= 0x0A
    Level11	= 0x0B
    Level12	= 0x0C
    Level13	= 0x0D
    Level14	= 0x0E
    Level15 = 0x0F

class DecodeMode:
    """
    Decode modes.
    """
    NoDecode	= 0x00
    Digit0		= 0x01
    Digit1      = 0x02
    Digit2      = 0x04
    Digit3      = 0x08
    Digit4      = 0x10       
    Digit5      = 0x20
    Digit6      = 0x40
    Digit7      = 0x80
    AllDigits   = 0xFF

class Digit:
    """
    Digits.
    """
    Digit0	= 0x01
    Digit1	= 0x02
    Digit2	= 0x03
    Digit3	= 0x04
    Digit4	= 0x05
    Digit5	= 0x06
    Digit6	= 0x07
    Digit7	= 0x08

class Character:
    """
    Characters.
    """
    Zero	= 0x00
    One		= 0x01
    Two		= 0x02
    Three	= 0x03
    Four	= 0x04
    Five	= 0x05
    Six		= 0x06
    Seven	= 0x07
    Eight	= 0x08
    Nine	= 0x09
    Dash	= 0x0A
    E		= 0x0B
    H		= 0x0C
    L		= 0x0D
    P		= 0x0E
    Blank	= 0x0F

class Segment:
    """
    Segments.
    """
    DP	= 0x80
    A	= 0x40
    B	= 0x20
    C	= 0x10
    D	= 0x08
    E	= 0x04
    F	= 0x02
    G	= 0x01

class Max7219:
    """
    This class allow to drive a MAX7219 using Bus Pirate.
    """
    
    def __init__(self, port, baudrate):
        self.port = port
        self.baudrate = baudrate
        self.spi = SPI(self.port, self.baudrate)

    def initialize(self):
        """
        Initialize and configure Bus Pirate to drive a MAX7219.
        """
        # Enter BitBang mode.
        self.spi.BBmode()
        # Enter SPI mode.
        self.spi.enter_SPI()
        # Set SPI speed.
        self.spi.set_speed(SPISpeed._1MHZ)
        # Configure pins.
        self.spi.cfg_pins(PinCfg.POWER | PinCfg.CS)
        # Configure SPI.
        self.spi.cfg_spi(SPICfg.OUT_TYPE | SPICfg.CLK_EDGE)
    
    def set_shutdown_mode(self, shutdown_mode):
        """
        Set shutdown mode.
        """
        self.write(REG_SHUTDOWN, shutdown_mode)
    
    def set_display_test(self, display_test):
        """
        Set display test mode.
        """
        self.write(REG_DISPLAY_TEST, display_test)

    def set_scan_limit(self, scan_limit):
        """
        Set scan limit.
        """
        self.write(REG_SCAN_LIMIT, scan_limit)
        
    def set_intensity(self, intensity):
        """
        Set intensity.
        """
        self.write(REG_INTENSITY, intensity)
        
    def set_decode_mode(self, decode_mode):
        """
        Set decode mode.
        """
        self.write(REG_DECODE_MODE, decode_mode)
    
    def write_digit(self, digit, value, decimal_point = False):
        """
        Write digit value.
        """
        if decimal_point == True:
            value = value | 0x80
        self.write(digit, value)
        
    def write(self, address, value):
        """
        Write register value.
        """
        self.spi.CS_Low()
        self.spi.bulk_trans(2, [address, value])
        self.spi.CS_High()

Ses différents membres sont importés à l'aide de la directive :

from max7219 import *

Les adresses des registres du MAX7219 sont contenues dans le fichier max7219_constants.py.

#!/usr/bin/env python
# encoding: utf-8

"""
Copyright (C) 2011  Julien Le Sech - www.idreammicro.com

This file is part of the MAX7219 Bus Pirate library.

The MAX7219 Bus Pirate library is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.

The MAX7219 Bus Pirate library is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/
"""

REG_NO_OP           = 0x00
REG_DIGIT_0         = 0x01
REG_DIGIT_1         = 0x02
REG_DIGIT_2         = 0x03
REG_DIGIT_3         = 0x04
REG_DIGIT_4         = 0x05
REG_DIGIT_5         = 0x06
REG_DIGIT_6         = 0x07
REG_DIGIT_7         = 0x08
REG_DECODE_MODE     = 0x09
REG_INTENSITY       = 0x0A
REG_SCAN_LIMIT      = 0x0B
REG_SHUTDOWN        = 0x0C
REG_DISPLAY_TEST    = 0x0F

Interpréteur Python

Piloter le MAX7219 via l'interpréteur Python est sans doute le meilleur moyen de le découvrir et de comprendre son fonctionnement.

On se place dans le dossier de la bibliothèque.

~$ cd pyBusPirateLite/

On lance le shell Python.

~/pyBusPirateLite$ python

La console affiche le prompt.

Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

On pilote le MAX7219.

>>> from max7219 import *
>>> device = Max7219('/dev/ttyUSB0', 115200)
>>> device.initialize()
>>> device.set_scan_limit(ScanLimit.Digit0To7)
>>> device.set_intensity(Intensity.Level15)
>>> device.set_decode_mode(DecodeMode.AllDigits)
>>> device.write_digit(Digit.Digit0, Character.Zero)
>>> device.write_digit(Digit.Digit1, Character.One)
>>> device.write_digit(Digit.Digit2, Character.Two)
>>> device.write_digit(Digit.Digit3, Character.Three)
>>> device.write_digit(Digit.Digit4, Character.Four)
>>> device.write_digit(Digit.Digit5, Character.Five)
>>> device.write_digit(Digit.Digit6, Character.Six)
>>> device.write_digit(Digit.Digit7, Character.Seven)
>>> device.set_shutdown_mode(ShutdownMode.NormalOperation)

Logiciel de démonstration

Une fois le fonctionnement du MAX7219 assimilé, il peut être intéressant d'en garder une trace qui sera consultable ultérieurement. C'est pourquoi on écrit maintenant un logiciel de démonstration exploitant la bibliothèque développée précédemment.

#!/usr/bin/env python
# encoding: utf-8

"""
Copyright (C) 2011  Julien Le Sech - www.idreammicro.com

This file is part of the MAX7219 library for Bus Pirate.

This library is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option)b any later
version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for moredetails.

You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/
"""

import argparse
import sys 

from max7219 import *

def main():
    """
    This program drives a MAX7219 using Bus Pirate in binary mode.
    """
    
    # Parse arguments.
    parser = argparse.ArgumentParser()
    parser.add_argument(
		"-p", "--port", dest = "port", default = "/dev/ttyUSB0",
		help = "Serial port to use.")
    parser.add_argument(
		"-b", "--baudrate", dest = "baudrate", default = "115200",
		help = "Baudrate")
    args = parser.parse_args()
    
    print "Serial port = " + args.port
    print "Baudrate = " + args.baudrate
    
	# Create MAX7219 object.
    device = Max7219(args.port, args.baudrate)
    
	# Initialize MAX7219.
    device.initialize()
    
	# Set scan limit.
    device.set_scan_limit(ScanLimit.Digit0To7)

	# Set intensity.
    device.set_intensity(Intensity.Level15)

	# Set decode mode.
    device.set_decode_mode(DecodeMode.AllDigits)

	# Write digits.
    device.write_digit(Digit.Digit0, Character.Zero)
    device.write_digit(Digit.Digit1, Character.One)
    device.write_digit(Digit.Digit2, Character.Two)
    device.write_digit(Digit.Digit3, Character.Three)
    device.write_digit(Digit.Digit4, Character.Four)
    device.write_digit(Digit.Digit5, Character.Five)
    device.write_digit(Digit.Digit6, Character.Six)
    device.write_digit(Digit.Digit7, Character.Seven)
    
    # Enable display.
    device.set_shutdown_mode(ShutdownMode.NormalOperation)

if __name__ == '__main__':
    main()

Licence

La bibliothèque MAX7219 pour le Bus Pirate est placée, tout comme la bibliothèque pyBusPirateLite, sous licence GNU General Public License (GPL) v3.

Conclusion

Au travers de cet article, on vient d'expérimenter le pilotage du contrôleur d'affichage MAX7219 qui a précédemment fait l'objet d'un article purement théorique. Mais on vient également de découvrir un outil conçu et développé pour ce genre d'expérimentation : le Bus Pirate et son mode binaire.

Sujets relatifs