Architecture logicielle
Comme exposé dans un précédent article, il est préférable, voire impératif, de découper la complexité d'un projet logiciel. L'application de ce concept conduit à établir l'architecture logicielle ci-dessous.
L'architecture comporte trois niveaux, du plus haut au plus bas (au sens logiciel du terme) :
- Le sketch MAX7219 utilise la bibliothèque MAX7219 pour piloter l'affichage.
- La bibliothèque MAX7219 utilise les bibliothèques Arduino Core et Arduino SPI.
- Les bibliothèques Arduino Core et Arduino SPI sont au plus près du microcontrôleur : elles pilotent les entrées/sorties et le SPI.
Sketch de démonstration
Le sketch de démonstration ci-dessous propose un exemple d'utilisation de la bibliothèque MAX7219.
Initialisation du MAX7219
On déclare un objet de type MAX7219 intitulé max7219 et qui utilise la broche 10 pour sélectionner le MAX7219.
static MAX7219 max7219(10);
Le MAX7219 est initialisé dans la fonction setup :
- initialisation de la bibliothèque (ligne 53) ;
- configuration du nombre de digits (ligne 55) ;
- configuration du mode de décodage (ligne 57) ;
- réglage de la luminosité (ligne 59) ;
- allumage de l'affichage (ligne 61).
Les paramètres sont les suivants :
- affichage sur 8 digits ;
- décodage sur tous les digits ;
- luminosité maximale.
/**************************************************************************//** * \fn void setup() ******************************************************************************/ void setup() { // Initiliaze MAX7219. max7219.initialize(); // Set scan limit. max7219.setScanLimit(MAX7219::ScanLimit_Digit0To7); // Set decode mode. max7219.setDecodeMode(MAX7219::DecodeMode_AllDigits); // Set intensity. max7219.setIntensity(MAX7219::Intensity_Level15); // Set shutdown mode. max7219.setShutdownMode(MAX7219::ShutdownMode_NormalOperation); }
Affichage des caractères
La fonction loop ordonne respectivement l'affichage des caractères 0 à 7 sur les digits 0 à 7.
/**************************************************************************//** * \fn void loop() ******************************************************************************/ void loop() { // To write digit values, a loop may be more elegant but a bit more // complicated for this example. // Write digit 0 value. max7219.writeDigit(MAX7219::Digit_0, MAX7219::Character_Zero); // Write digit 1 value. max7219.writeDigit(MAX7219::Digit_1, MAX7219::Character_One); // Write digit 2 value. max7219.writeDigit(MAX7219::Digit_2, MAX7219::Character_Two); // Write digit 3 value. max7219.writeDigit(MAX7219::Digit_3, MAX7219::Character_Three); // Write digit 4 value. max7219.writeDigit(MAX7219::Digit_4, MAX7219::Character_Four); // Write digit 5 value. max7219.writeDigit(MAX7219::Digit_5, MAX7219::Character_Five); // Write digit 6 value. max7219.writeDigit(MAX7219::Digit_6, MAX7219::Character_Six); // Write digit 7 value. max7219.writeDigit(MAX7219::Digit_7, MAX7219::Character_Seven); // Wait a little before next loop. delay(1000); }
Sketch complet
/**************************************************************************//** * \brief MAX7219 library for Arduino - Demo program * \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com * \version 1.0 * \date 20110801 * * This file is part of the MAX7219 library for Arduino. * * This library is free software: you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see http://www.gnu.org/licenses/ ******************************************************************************/ /**************************************************************************//** * \file MAX7219.pde ******************************************************************************/ /****************************************************************************** * Header file inclusions. ******************************************************************************/ #include <WProgram.h> #include <SPI.h> #include <MAX7219.h> /****************************************************************************** * Private variable declaration. ******************************************************************************/ static MAX7219 max7219(10); /****************************************************************************** * Public function definitions. ******************************************************************************/ /**************************************************************************//** * \fn void setup() ******************************************************************************/ void setup() { // Initiliaze MAX7219. max7219.initialize(); // Set scan limit. max7219.setScanLimit(MAX7219::ScanLimit_Digit0To7); // Set decode mode. max7219.setDecodeMode(MAX7219::DecodeMode_AllDigits); // Set intensity. max7219.setIntensity(MAX7219::Intensity_Level15); // Set shutdown mode. max7219.setShutdownMode(MAX7219::ShutdownMode_NormalOperation); } /**************************************************************************//** * \fn void loop() ******************************************************************************/ void loop() { // To write digit values, a loop may be more elegant but a bit more // complicated for this example. // Write digit 0 value. max7219.writeDigit(MAX7219::Digit_0, MAX7219::Character_Zero); // Write digit 1 value. max7219.writeDigit(MAX7219::Digit_1, MAX7219::Character_One); // Write digit 2 value. max7219.writeDigit(MAX7219::Digit_2, MAX7219::Character_Two); // Write digit 3 value. max7219.writeDigit(MAX7219::Digit_3, MAX7219::Character_Three); // Write digit 4 value. max7219.writeDigit(MAX7219::Digit_4, MAX7219::Character_Four); // Write digit 5 value. max7219.writeDigit(MAX7219::Digit_5, MAX7219::Character_Five); // Write digit 6 value. max7219.writeDigit(MAX7219::Digit_6, MAX7219::Character_Six); // Write digit 7 value. max7219.writeDigit(MAX7219::Digit_7, MAX7219::Character_Seven); // Wait a little before next loop. delay(1000); }
Bibliothèque MAX7219
La bibliothèque MAX7219 exploite les fonctionnalités du contrôleur d'affichage MAX7219. Elle propose des méthodes permettant de configurer ce composant et d'afficher des caractères sur les afficheurs qu'il contrôle.
Fichier d'entête
/**************************************************************************//** * \brief MAX7219 library for Arduino * \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com * \version 1.0 * \date 20110801 * * This file is part of the MAX7219 library for Arduino. * * This library is free software: you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see http://www.gnu.org/licenses/ ******************************************************************************/ /**************************************************************************//** * \headerfile MAX7219.h ******************************************************************************/ #ifndef MAX7219_h #define MAX7219_h /****************************************************************************** * Header file inclusions. ******************************************************************************/ #include <WProgram.h> /**************************************************************************//** * \class MAX7219 ******************************************************************************/ class MAX7219 { public: /******************************************************************//** * \enum DecodeModes * \typedef DecodeMode_t **********************************************************************/ typedef enum DecodeModes { DecodeMode_NoDecode = 0x00, DecodeMode_Digit0 = 0x01, DecodeMode_Digit1 = 0x02, DecodeMode_Digit2 = 0x04, DecodeMode_Digit3 = 0x08, DecodeMode_Digit4 = 0x10, DecodeMode_Digit5 = 0x20, DecodeMode_Digit6 = 0x40, DecodeMode_Digit7 = 0x80, DecodeMode_AllDigits = 0xFF } DecodeMode_t; /******************************************************************//** * \enum DecodeModes * \typedef DecodeMode_t **********************************************************************/ typedef enum Intensities { Intensity_Level0 = 0x00, Intensity_Level1 = 0x01, Intensity_Level2 = 0x02, Intensity_Level3 = 0x03, Intensity_Level4 = 0x04, Intensity_Level5 = 0x05, Intensity_Level6 = 0x06, Intensity_Level7 = 0x07, Intensity_Level8 = 0x08, Intensity_Level9 = 0x09, Intensity_Level10 = 0x0A, Intensity_Level11 = 0x0B, Intensity_Level12 = 0x0C, Intensity_Level13 = 0x0D, Intensity_Level14 = 0x0E, Intensity_Level15 = 0x0F } Intensity_t; /******************************************************************//** * \enum ScanLimits * \typedef ScanLimit_t **********************************************************************/ typedef enum ScanLimits { ScanLimit_Digit0 = 0x00, ScanLimit_Digit0To1 = 0x01, ScanLimit_Digit0To2 = 0x02, ScanLimit_Digit0To3 = 0x03, ScanLimit_Digit0To4 = 0x04, ScanLimit_Digit0To5 = 0x05, ScanLimit_Digit0To6 = 0x06, ScanLimit_Digit0To7 = 0x07 } ScanLimit_t; /******************************************************************//** * \enum ShutdownModes * \typedef ShutdownMode_t **********************************************************************/ typedef enum ShutdownModes { ShutdownMode_Shutdown = 0x00, ShutdownMode_NormalOperation = 0x01 } ShutdownMode_t; /******************************************************************//** * \enum DisplayTestModes * \typedef DisplayTestMode_t **********************************************************************/ typedef enum DisplayTestModes { NormalOperation = 0x00, TestMode = 0x01 } DisplayTestMode_t; /******************************************************************//** * \enum Digits * \typedef Digit_t **********************************************************************/ typedef enum Digits { Digit_0 = 0x01, Digit_1 = 0x02, Digit_2 = 0x03, Digit_3 = 0x04, Digit_4 = 0x05, Digit_5 = 0x06, Digit_6 = 0x07, Digit_7 = 0x08 } Digit_t; /******************************************************************//** * \enum Characters * \typedef Character_t **********************************************************************/ typedef enum Characters { Character_Zero = 0x00, Character_One = 0x01, Character_Two = 0x02, Character_Three = 0x03, Character_Four = 0x04, Character_Five = 0x05, Character_Six = 0x06, Character_Seven = 0x07, Character_Eight = 0x08, Character_Nine = 0x09, Character_Dash = 0x0A, Character_E = 0x0B, Character_H = 0x0C, Character_L = 0x0D, Character_P = 0x0E, Character_Blank = 0x0F } Character_t; /******************************************************************//** * \enum Segments * \typedef Segment_t **********************************************************************/ typedef enum Segments { Segment_DP = 0x80, Segment_A = 0x40, Segment_B = 0x20, Segment_C = 0x10, Segment_D = 0x08, Segment_E = 0x04, Segment_F = 0x02, Segment_G = 0x01 } Segment_t; public: /******************************************************************//** * \fn MAX7219(byte csPin) * * \brief Constructor. * * \param csPin Chip select pin number. **********************************************************************/ MAX7219 ( byte csPin ); /******************************************************************//** * \fn void initialize() * * \brief Initialize SPI to drive MAX7219. **********************************************************************/ void initialize(); /******************************************************************//** * \fn void setDecodeMode(DecodeModes mode) * * \brief Set MAX7219 decode mode. * * \param mode Decode mode to set. **********************************************************************/ void setDecodeMode ( DecodeModes mode ); /******************************************************************//** * \fn void setIntensity(Intensities intensity) * * \brief Set MAX7219 intensity. * * \param itensity Intensity to set. **********************************************************************/ void setIntensity ( Intensities intensity ); /******************************************************************//** * \fn void setScanLimit(ScanLimits limit) * * \brief Set MAX7219 scan limit. * * \param limit Scan limit to set. **********************************************************************/ void setScanLimit ( ScanLimits limit ); /******************************************************************//** * \fn void setShutdownMode(ShutdownModes mode) * * \brief Set MAX7219 shutdown mode. * * \param mode Shutdown mode to set. **********************************************************************/ void setShutdownMode ( ShutdownModes mode ); /******************************************************************//** * \fn void setDisplayTestMode(DisplayTestModes mode) * * \brief Set MAX7219 display test mode. * * \param mode Display test mode to set. **********************************************************************/ void setDisplayTestMode ( DisplayTestModes mode ); /******************************************************************//** * \fn void writeDigit( * Digits digit, * Characters character, * bool decimalPoint = false) * * \brief Write character on digit. * * \param digit Digit to write. * \param character Character to write. * \param decimalPoint Display decimal point. **********************************************************************/ void writeDigit ( Digits digit, Characters character, bool decimalPoint = false ); /******************************************************************//** * \fn void writeDigit(Digits digit, Segments segments) * * \brief Set segment(s) on digit. * * \param digit Digit to write. * \param segment Segment(s) to set. **********************************************************************/ void writeDigit ( Digits digit, Segments segment ); private: byte m_csPin; /******************************************************************//** * \fn void write(byte address, byte value) * * \brief Write value into MAX7219 register. * * \param address Register address. * \param value Value to write. **********************************************************************/ void write ( byte address, byte value ); }; #endif // MAX7219_h
Fichier source
/**************************************************************************//** * \brief MAX7219 library for Arduino * \author Copyright (C) 2011 Julien Le Sech - www.idreammicro.com * \version 1.0 * \date 20110801 * * This file is part of the MAX7219 library for Arduino. * * This library is free software: you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see http://www.gnu.org/licenses/ ******************************************************************************/ /**************************************************************************//** * \file MAX7219.cpp ******************************************************************************/ /****************************************************************************** * Header file inclusions. ******************************************************************************/ #include <WProgram.h> #include <SPI.h> #include <MAX7219.h> /****************************************************************************** * Private macros. ******************************************************************************/ #define REG_NO_OP 0x00 #define REG_DIGIT_0 0x01 #define REG_DIGIT_1 0x02 #define REG_DIGIT_2 0x03 #define REG_DIGIT_3 0x04 #define REG_DIGIT_4 0x05 #define REG_DIGIT_5 0x06 #define REG_DIGIT_6 0x07 #define REG_DIGIT_7 0x08 #define REG_DECODE_MODE 0x09 #define REG_INTENSITY 0x0A #define REG_SCAN_LIMIT 0x0B #define REG_SHUTDOWN 0x0C #define REG_DISPLAY_TEST 0x0F /****************************************************************************** * Public method definitions. ******************************************************************************/ /**************************************************************************//** * \fn MAX7219::MAX7219(byte csPin) * * \brief Constructor. * * \param csPin Chip select pin number. ******************************************************************************/ MAX7219::MAX7219 ( byte csPin ){ m_csPin = csPin; } /**************************************************************************//** * \fn void MAX7219::initialize() * * \brief Initialize SPI to drive MAX7219. ******************************************************************************/ void MAX7219::initialize() { // Configure chip select pin as output. pinMode(m_csPin, OUTPUT); // Wait a little to allow MAX7219 to see a correct logic level on CS pin. delay(1); // Configure SPI. SPI.begin(); SPI.setBitOrder(MSBFIRST); SPI.setDataMode(SPI_MODE0); SPI.setClockDivider(SPI_CLOCK_DIV4); } /**************************************************************************//** * \fn void MAX7219::setDecodeMode(DecodeModes mode) * * \brief Set MAX7219 decode mode. * * \param mode Decode mode to set. ******************************************************************************/ void MAX7219::setDecodeMode ( DecodeModes mode ){ write(REG_DECODE_MODE, mode); } /**************************************************************************//** * \fn void MAX7219::setIntensity(Intensities intensity) * * \brief Set MAX7219 intensity. * * \param itensity Intensity to set. ******************************************************************************/ void MAX7219::setIntensity ( Intensities intensity ){ write(REG_INTENSITY, intensity); } /**************************************************************************//** * \fn void MAX7219::setScanLimit(ScanLimits limit) * * \brief Set MAX7219 scan limit. * * \param limit Scan limit to set. ******************************************************************************/ void MAX7219::setScanLimit ( ScanLimits limit ){ write(REG_SCAN_LIMIT, limit); } /**************************************************************************//** * \fn void MAX7219::setShutdownMode(ShutdownModes mode) * * \brief Set MAX7219 shutdown mode. * * \param mode Shutdown mode to set. ******************************************************************************/ void MAX7219::setShutdownMode ( ShutdownModes mode ){ write(REG_SHUTDOWN, mode); } /**************************************************************************//** * \fn void MAX7219::setDisplayTestMode(DisplayTestModes mode) * * \brief Set MAX7219 display test mode. * * \param mode Display test mode to set. ******************************************************************************/ void MAX7219::setDisplayTestMode ( DisplayTestModes mode ){ write(REG_DISPLAY_TEST, mode); } /**************************************************************************//** * \fn void MAX7219::writeDigit( * Digits digit, * Characters character, * bool decimalPoint) * * \brief Write character on digit. * * \param digit Digit to write. * \param character Character to write. * \param decimalPoint Display decimal point if true. ******************************************************************************/ void MAX7219::writeDigit ( Digits digit, Characters character, bool decimalPoint ){ byte value = character; if (decimalPoint) { // If decimal point must be switched on. value |= Segment_DP; } write(digit, value); } /**************************************************************************//** * \fn void MAX7219::writeDigit(Digits digit, Segments segments) * * \brief Set segment(s) on digit. * * \param digit Digit to write. * \param segment Segment(s) to set. ******************************************************************************/ void MAX7219::writeDigit ( Digits digit, Segments segments ){ write(digit, segments); } /****************************************************************************** * Private method definitions. ******************************************************************************/ /**************************************************************************//** * \fn void MAX7219::write(byte address, byte value) * * \brief Write value into MAX7219 register. * * \param address Register address. * \param value Value to write. ******************************************************************************/ void MAX7219::write ( byte address, byte value ){ // Reset chip select pin to select the device. digitalWrite(m_csPin, LOW); // Transmit data to the device, register address and value. SPI.transfer(address); SPI.transfer(value); // Set chip select pin to valid data onto the device. digitalWrite(m_csPin, HIGH); }
Utilisation de la bibliothèque
Installation de la bibliothèque :
- téléchargement de l'archive MAX7219.zip ;
- décompression de l'archive dans le dossier <sketchbook>/libraries/ où <sketchbook> désigne l'emplacement du dossier de travail Arduino.
L'archive MAX7219.zip contient les fichiers suivants :
- le sketch d'exemple examples/MAX7219.pde ;
- le fichier de licence lgpl-3.0.txt ;
- le fichier source MAX7219.cpp ;
- le fichier d'entête MAX7219.h.
Intégration de la bibliothèque :
- création d'un nouveau sketch via le menu File > New ;
- import de la bibliothèque MAX7219 via le menu Sketch > Import library > MAX7219.
L'inclusion du fichier d'entête de la bibliothèque MAX7219.h doit apparaître dans le sketch courant :
#include <MAX7219.h>
Licence
La bibliothèque MAX7219 est sous licence GNU Lesser General Public Licence (LGPL) v3.
Documentation
Le code source est documenté de manière à générer automatiquement la documentation à l'aide de Doxygen.
2 réactions
1 De Julien Le Sech - 18/02/2012, 08:41
Mise à disposition de la bibliothèque MAX7219 pour Arduino 0022/0023 dans le dépôt idreammicro-arduino.
[Parcourir avec WebSVN]
2 De Julien Le Sech - 18/02/2012, 08:42