Une horloge à afficheurs 7 segments

Dependencies:   BSP_DISCO_F746NG

Files at this revision

API Documentation at this revision

Comitter:
sol427
Date:
Mon Jun 22 16:33:43 2020 +0000
Parent:
3:9f66aabe7b3b
Commit message:
Horloge fonctionnelle

Changed in this revision

button.cpp Show annotated file Show diff for this revision Revisions of this file
button.h Show annotated file Show diff for this revision Revisions of this file
horloge.cpp Show annotated file Show diff for this revision Revisions of this file
horloge.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
view.cpp Show annotated file Show diff for this revision Revisions of this file
view.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/button.cpp	Mon Jun 22 16:33:43 2020 +0000
@@ -0,0 +1,58 @@
+#include "button.h"
+#include "mbed.h"
+#include "stm32746g_discovery_lcd.h"
+
+int Button::strlen(uint8_t *str)
+{
+    int i = 0;
+    while(str[i])
+    {
+        i++;
+    }
+    return i;
+}
+
+
+Button::Button(int x, int y, int width, int height, uint32_t bgColor, uint32_t borderWidth)
+    : m_x(x), m_y(y), m_width(width), m_height(height), m_bgColor(bgColor), m_borderWidth(borderWidth)
+{
+    
+}
+
+bool Button::contain(int x, int y)
+{
+    return (x > m_x && x < m_x + m_width && y > m_y && y < m_y + m_height);
+}
+
+void Button::draw()
+{
+    int x = m_x;
+    while (x < m_x + m_width) {
+        int y = m_y;
+        while (y < m_y + m_height) {
+            if (y - m_y < m_borderWidth)
+                BSP_LCD_DrawPixel(x, y, m_borderColor);
+            else if ( (m_y + m_height) - y <= m_borderWidth)
+                BSP_LCD_DrawPixel(x, y, m_borderColor);
+            else if (x - m_x < m_borderWidth)
+                BSP_LCD_DrawPixel(x, y, m_borderColor);
+            else if ( (m_x + m_width) - x <= m_borderWidth)
+                BSP_LCD_DrawPixel(x, y, m_borderColor);
+            else
+                BSP_LCD_DrawPixel(x, y, m_bgColor);
+            
+            y++;
+        }
+        x++;
+    }
+    BSP_LCD_SetTextColor(m_textColor);
+    BSP_LCD_SetBackColor(m_bgColor);
+    BSP_LCD_DisplayStringAt((m_x + m_width/2)- 8*(strlen(m_text)/2), (m_y + m_height/2) - 5, (uint8_t *)&m_text, LEFT_MODE);
+
+}
+
+void Button::setText(const char *str, uint32_t textColor){
+    m_textColor = textColor;
+    sprintf((char*)m_text, str);
+     
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/button.h	Mon Jun 22 16:33:43 2020 +0000
@@ -0,0 +1,25 @@
+#ifndef BUTTON_H
+#define BUTTON_H
+#include "stm32746g_discovery_lcd.h"
+class Button
+{
+
+public:
+    Button(int x = 50, int y = 50, int width = 50, int height = 30, uint32_t bgColor = LCD_COLOR_LIGHTGRAY, uint32_t borderWidth = 1);
+    void setText(const char *str, uint32_t textColor = LCD_COLOR_BLACK);
+    bool contain(int x, int y);
+    void draw();
+    int strlen(uint8_t *str);
+    
+private :
+    int16_t m_x = 0;
+    int16_t m_y = 0;
+    int16_t m_width = 50;
+    int16_t m_height = 30;
+    uint32_t m_bgColor = LCD_COLOR_LIGHTGRAY;
+    uint32_t m_borderColor = LCD_COLOR_GRAY;
+    uint32_t m_textColor = LCD_COLOR_BLACK;
+    uint32_t m_borderWidth = 1;
+    uint8_t m_text[30];
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/horloge.cpp	Mon Jun 22 16:33:43 2020 +0000
@@ -0,0 +1,131 @@
+#include "horloge.h"
+#include "mbed.h"
+#include <stdint.h>
+#include <vector>
+//#include "stm32746g_discovery_lcd.h"
+
+
+Horloge::Horloge() 
+    : m_minutes(59), m_heures(23), m_minutesPre(100), m_heuresPre(100), m_secondesPre(100)
+{
+    set_time(54);
+    dataPin = new DigitalOut(D8, 0);
+    clockPin = new DigitalOut(D9, 0);
+    latchPin = new DigitalOut(D10, 0);
+    enablePin = new PwmOut(D11);
+    enablePin->period(0.00002f);
+    enablePin->write(0.6f);
+    
+}
+
+void Horloge::setTempsPrecedent(uint8_t minutes, uint8_t heures) {
+    m_minutesPre = minutes;
+    m_heuresPre = heures;
+}
+
+vector<uint8_t> Horloge::get4Digits() {
+    vector<uint8_t> digits;
+    
+    digits.push_back(m_heures / 10);
+    digits.push_back(m_heures % 10);
+    digits.push_back(m_minutes / 10);
+    digits.push_back(m_minutes % 10);
+    
+    return digits;   
+}
+
+uint8_t Horloge::getMinutes() {
+    return m_minutes;
+}
+
+uint8_t Horloge::getHeures() {
+    return m_heures;
+}
+
+uint8_t Horloge::getMinutesPre() {
+    return m_minutesPre;
+}
+
+uint8_t Horloge::getHeuresPre() {
+    return m_heuresPre;
+}
+
+void Horloge::setRegistres(DigitalOut* dataPin, DigitalOut* clockPin, uint8_t data) {
+    for(uint8_t i = 0; i < 8; i++)
+    {
+        dataPin->write(data & (1 << i));
+        HAL_Delay(1);
+        clockPin->write(1);
+        HAL_Delay(1);
+        clockPin->write(0);
+    }
+}
+
+void Horloge::minuteP() {
+    if(m_minutes == 59) {
+        m_minutes = 0;
+        heureP();
+    } else {
+        m_minutes++;
+    }
+}
+
+void Horloge::minuteM() {
+    if(m_minutes == 0) {
+        m_minutes = 59;
+    } else {
+        m_minutes--;
+    }
+}
+
+void Horloge::heureP() {
+    if(m_heures == 23) {
+        m_heures = 0;
+    } else {
+        m_heures++;
+    }
+}
+
+void Horloge::heureM() {
+    if(m_heures == 0) {
+        m_heures = 23;
+    } else {
+        m_heures--;
+    }
+}
+
+void Horloge::update() {
+    // récupère l'heure
+    uint8_t secondes = (uint8_t)time(NULL);
+    if(secondes == 59) {
+        set_time(0);
+        minuteP();
+        
+    }
+    // affiche l'heure sur les LEDs si elle a changée
+    if (secondes != m_secondesPre) {
+        vector<uint8_t> temps = get4Digits();
+    
+        latchPin->write(1);
+        if(point == true) {
+            setRegistres(dataPin, clockPin, m_table[temps.at(0)]);
+            setRegistres(dataPin, clockPin, m_table[temps.at(1)]+128);
+            setRegistres(dataPin, clockPin, m_table3[temps.at(2)]+8);
+            setRegistres(dataPin, clockPin, m_table[temps.at(3)]);
+        } else {
+            setRegistres(dataPin, clockPin, m_table[temps.at(0)]);
+            setRegistres(dataPin, clockPin, m_table[temps.at(1)]);
+            setRegistres(dataPin, clockPin, m_table3[temps.at(2)]);
+            setRegistres(dataPin, clockPin, m_table[temps.at(3)]);
+        }
+        latchPin->write(0);
+        point = !point;
+    }
+    
+    m_secondesPre = secondes;
+}
+
+void Horloge::sonner() {
+    
+}
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/horloge.h	Mon Jun 22 16:33:43 2020 +0000
@@ -0,0 +1,70 @@
+#ifndef HORLOGE_H
+#define HORLOGE_H
+#include <stdint.h>
+#include <vector>
+#include "mbed.h"
+
+using namespace std;
+//#include "stm32746g_discovery_lcd.h"
+class Horloge
+{
+
+public:
+    Horloge();
+    void setTempsPrecedent(uint8_t minutes, uint8_t heures);
+    vector<uint8_t> get4Digits();
+    uint8_t getMinutes();
+    uint8_t getHeures();
+    uint8_t getMinutesPre();
+    uint8_t getHeuresPre();
+    void setRegistres(DigitalOut* dataPin, DigitalOut* clockPin, uint8_t data);
+    void minuteP();
+    void minuteM();
+    void heureP();
+    void heureM();
+    void update();
+    void sonner();
+    
+private :
+//    Serial pc(USBTX, USBRX, 115200)
+    DigitalOut* dataPin;
+    DigitalOut* clockPin;
+    DigitalOut* latchPin;
+    PwmOut* enablePin;
+    uint8_t m_minutes;
+    uint8_t m_heures;
+    uint8_t m_minutesPre;
+    uint8_t m_heuresPre;
+    uint8_t m_secondesPre;
+    bool m_reveilActif;
+    uint8_t m_minutesReveil;
+    uint8_t m_heuresReveil;
+    bool point = false;
+    
+    uint8_t m_table[10] = {
+        0b01110111, // 0 ABCDEF
+        0b01000001, // 1 BC
+        0b00111011, // 2 ABDEG
+        0b01101011, // 3 ABCDG
+        0b01001101, // 4 BCFG
+        0b01101110, // 5 ACDFG
+        0b01111110, // 6 ACDEFG
+        0b01000011, // 7 ABC
+        0b01111111, // 8 ABCDEFG
+        0b01101111, // 9 ABCDFG
+    };
+    uint8_t m_table3[10] = {
+        0b01110111, // 0 ABCDEF
+        0b01000001, // 1 BC
+        0b10110011, // 2 ABDEG
+        0b11100011, // 3 ABCDG
+        0b11000101, // 4 BCFG
+        0b11100110, // 5 ACDFG
+        0b11110110, // 6 ACDEFG
+        0b01000011, // 7 ABC
+        0b11110111, // 8 ABCDEFG
+        0b11100111, // 9 ABCDFG
+    };
+    
+};
+#endif
\ No newline at end of file
--- a/main.cpp	Wed Nov 20 11:49:15 2019 +0100
+++ b/main.cpp	Mon Jun 22 16:33:43 2020 +0000
@@ -1,30 +1,38 @@
 #include "mbed.h"
+#include "button.h"
+#include "horloge.h"
+#include "view.h"
 #include "stm32746g_discovery_lcd.h"
 #include "stm32746g_discovery_ts.h"
+#include "time.h"
+#include <list>
+#define SCREENWIDTH 480
+#define SCREENHEIGHT 272
 
 int main()
 {
     TS_StateTypeDef TS_State;
     uint16_t x, y;
-    uint8_t text[30];
     uint8_t status;
     uint8_t idx;
     uint8_t cleared = 0;
-    uint8_t prev_nb_touches = 0;
+    
+//    PwmOut pwmtest(D12);
+//    pwmtest.period(0.00002f);
+//    pwmtest.write(0.2f);
 
     BSP_LCD_Init();
     BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
     BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
 
-    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
-    HAL_Delay(1000);
-
+    // initialisation de l'écran
     status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
     if (status != TS_OK) {
         BSP_LCD_Clear(LCD_COLOR_RED);
         BSP_LCD_SetBackColor(LCD_COLOR_RED);
         BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
         BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
+        while (1);
     } else {
         BSP_LCD_Clear(LCD_COLOR_GREEN);
         BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
@@ -32,43 +40,32 @@
         BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
     }
 
-    HAL_Delay(1000);
+    HAL_Delay(100);
     BSP_LCD_SetFont(&Font12);
-    BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
-    BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+    BSP_LCD_SetTextColor(LCD_COLOR_LIGHTGRAY);
+    
+    // initialisation de la vue, l'horloge commence a ce moment là
+    View v;
 
     while(1) {
-
+        v.updateHorloge();
+        v.drawDynamic();
+        
         BSP_TS_GetState(&TS_State);
         if (TS_State.touchDetected) {
-            // Clear lines corresponding to old touches coordinates
-            if (TS_State.touchDetected < prev_nb_touches) {
-                for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
-                    BSP_LCD_ClearStringLine(idx);
-                }
-            }
-            prev_nb_touches = TS_State.touchDetected;
-
             cleared = 0;
-
-            sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
-            BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
-
             for (idx = 0; idx < TS_State.touchDetected; idx++) {
                 x = TS_State.touchX[idx];
                 y = TS_State.touchY[idx];
-                sprintf((char*)text, "Touch %d: x=%d y=%d    ", idx+1, x, y);
-                BSP_LCD_DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
+                v.contain(x, y);
             }
-
-            BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
         } else {
             if (!cleared) {
-                BSP_LCD_Clear(LCD_COLOR_BLUE);
-                sprintf((char*)text, "Touches: 0");
-                BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
+                v.drawStatic();
+                v.drawDynamic();
+                
                 cleared = 1;
             }
         }
     }
-}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/view.cpp	Mon Jun 22 16:33:43 2020 +0000
@@ -0,0 +1,143 @@
+#include "view.h"
+#include "stm32746g_discovery_lcd.h"
+#include "mbed.h"
+#include "button.h"
+#include <list>
+
+View::View(int width, int height, uint32_t bgColor)
+    : m_width(width), m_height(height), m_bgColor(bgColor), m_horlogeX(40), m_reveilX(200)
+{
+    
+    // creation des boutons
+    
+    // reglage de l'heure
+    Button heurePlus(m_horlogeX, 110, 35, 30);
+    Button heureMoins(m_horlogeX + 60 ,110 , 35, 30);
+    Button minutePlus(m_horlogeX, 190, 35, 30);
+    Button minuteMoins(m_horlogeX + 60 ,190 , 35, 30);
+    
+    heurePlus.setText("+" );
+    heureMoins.setText("-");
+    minutePlus.setText("+");
+    minuteMoins.setText("-");
+    
+    m_buttonList.push_back(heurePlus);
+    m_buttonList.push_back(heureMoins);
+    m_buttonList.push_back(minutePlus);
+    m_buttonList.push_back(minuteMoins);
+    
+    // reglage du reveil
+    Button reveilHeurePlus(m_reveilX, 110, 35, 30);
+    Button reveilHeureMoins(m_reveilX + 60 ,110 , 35, 30);
+    Button reveilMinutePlus(m_reveilX, 190, 35, 30);
+    Button reveilMinuteMoins(m_reveilX + 60 ,190 , 35, 30);
+    
+    reveilHeurePlus.setText("+" );
+    reveilHeureMoins.setText("-");
+    reveilMinutePlus.setText("+");
+    reveilMinuteMoins.setText("-");
+    
+    m_buttonList.push_back(reveilHeurePlus); // id = 0
+    m_buttonList.push_back(reveilHeureMoins); // id = 1
+    m_buttonList.push_back(reveilMinutePlus); // id = 2
+    m_buttonList.push_back(reveilMinuteMoins); // id = 3
+    
+    // initialisation
+    drawDynamic();
+    drawStatic();
+    
+}
+
+
+void View::contain(int x, int y)
+{
+        // s'execute lorsque l'on a touché un point de l'ecran
+        // récupère les coordonnées du point, si elles correspondent
+        // aux coordonnes d'un bouton, on execute la fonction correspondante
+        int idBut = 0;
+        for (Button it : m_buttonList) {
+            if (it.contain(x,y)) {
+                if (idBut == 0) {
+                    // bouton heurePlus
+                    h.heureP();
+                    HAL_Delay(220);
+                } else if(idBut == 1) {
+                    // bouton heureMoins
+                    h.heureM();
+                    HAL_Delay(220);
+                } else if(idBut == 2) {
+                    // bouton MinutePlus
+                    h.minuteP();
+                    HAL_Delay(100);
+                } else if(idBut == 3) {
+                    // bouton MinuteMoins
+                    h.minuteM();
+                    HAL_Delay(100);
+                }
+                
+            }
+            idBut++;
+        }
+}
+
+void View::drawStatic()
+{
+    BSP_LCD_Clear(LCD_COLOR_BLACK);
+    
+    // trace le texte
+    uint8_t text[30];
+    BSP_LCD_SetTextColor(LCD_COLOR_LIGHTGRAY);
+    
+        // horloge
+    sprintf((char*)text, "Horloge");
+    BSP_LCD_SetFont(&Font24);
+    BSP_LCD_DisplayStringAt(m_horlogeX, 20, (uint8_t *)&text, LEFT_MODE);
+    
+    sprintf((char*)text, "heures");
+    BSP_LCD_SetFont(&Font20);
+    BSP_LCD_DisplayStringAt(m_horlogeX, 80, (uint8_t *)&text, LEFT_MODE);
+    
+    sprintf((char*)text, "minutes");
+    BSP_LCD_SetFont(&Font20);
+    BSP_LCD_DisplayStringAt(m_horlogeX, 160, (uint8_t *)&text, LEFT_MODE);
+    
+        // reveil
+    sprintf((char*)text, "Reveil");
+    BSP_LCD_SetFont(&Font24);
+    BSP_LCD_DisplayStringAt(m_reveilX, 20, (uint8_t *)&text, LEFT_MODE);
+    
+    sprintf((char*)text, "heures");
+    BSP_LCD_SetFont(&Font20);
+    BSP_LCD_DisplayStringAt(m_reveilX, 80, (uint8_t *)&text, LEFT_MODE);
+    
+    sprintf((char*)text, "minutes");
+    BSP_LCD_SetFont(&Font20);
+    BSP_LCD_DisplayStringAt(m_reveilX, 160, (uint8_t *)&text, LEFT_MODE);
+    
+    // trace les boutons
+    BSP_LCD_SetFont(&Font12);
+    for (Button it : m_buttonList) {
+        it.draw();
+    }
+    
+    BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
+}
+
+void View::drawDynamic() {
+    //uint8_t text[30];
+    BSP_LCD_SetTextColor(LCD_COLOR_LIGHTGRAY);
+    BSP_LCD_SetFont(&Font20);
+    
+    // trace l'heure
+        int minutes = h.getMinutes();
+        uint8_t text[5];
+        vector<uint8_t> digits;
+        digits = h.get4Digits();
+        
+        sprintf((char*)text, "%d%d:%d%d",  digits.at(0),  digits.at(1),  digits.at(2),  digits.at(3));
+        BSP_LCD_DisplayStringAt(m_horlogeX, 50, (uint8_t *)&text, LEFT_MODE);
+}
+
+void View::updateHorloge() {
+    h.update();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/view.h	Mon Jun 22 16:33:43 2020 +0000
@@ -0,0 +1,32 @@
+#ifndef VIEW_H
+#define VIEW_H
+#include "mbed.h"
+#include "button.h"
+#include "horloge.h"
+#include "stm32746g_discovery_lcd.h"
+#include <list>
+
+#define SCREENWIDTH 480
+#define SCREENHEIGHT 272
+
+class View
+{
+public:
+    View(int width = SCREENWIDTH, int height = SCREENHEIGHT, uint32_t bgColor = LCD_COLOR_WHITE);
+    void contain(int x, int y);
+    void drawStatic();
+    void drawDynamic();
+    void updateHorloge();
+
+private :
+    int16_t m_width = SCREENWIDTH;
+    int16_t m_height = SCREENHEIGHT;
+    uint32_t m_bgColor = LCD_COLOR_BLACK;
+    list <Button> m_buttonList;
+    uint8_t m_horlogeX;
+    uint8_t m_reveilX;
+    Horloge h;
+    
+};
+
+#endif
\ No newline at end of file