Modifications

Sauter à la navigation Sauter à la recherche
3 625 octets ajoutés ,  20 mars 2025 à 16:09
Ligne 75 : Ligne 75 :     
<syntaxhighlight lang="python" line>
 
<syntaxhighlight lang="python" line>
−
from machine import Pin, ADC
+
from micropython import const
 +
from machine import RTC
 +
from rtplay import *
 
import time
 
import time
    +
HOUR_CHIME = const( 1 )
 +
HALF_HOUR_CHIME = const( 2 )
 +
 +
rtc = RTC()
 +
buzzer = RingTonePlayer( Pin.board.GP13 )
 +
 +
def next_chime( _now ):
 +
    """  Calculate Next 1/2 H and Next Hour chimes time
 +
 +
    Parameters:
 +
        _now : rtc datetime  (Y,M,D,wD,h,m,s,ms)
 +
   
 +
    returns:
 +
        Time in seconds (since Jan 1, 2000)
 +
    """
 +
    # mktime datetime (Y,M,D,h,m,s,wD,yD)
 +
    if _now[5]<30: # Minutes < 30 => Chimes at next 30 mins
 +
        mktime_data = ( _now[0], _now[1], _now[2], _now[4], 30, 0, _now[3], 0 )
 +
        chime_type = HALF_HOUR_CHIME
 +
        hour = _now[4]
 +
    else: # Shime next Hour
 +
        chime_type = HOUR_CHIME
 +
        if _now[4]+1 > 23: # > 23H
 +
            # Next day at 00:00
 +
            # Transform in seconds
 +
            mktime_data = ( _now[0], _now[1], _now[2], _now[4], _now[5], _now[6], _now[3], 0 )
 +
            secs = time.mktime( mktime_data )
 +
            # Add a day
 +
            next_day_secs = secs + 24*60+60 + 30*60
 +
            # Decode this new date
 +
            mktime_data = time.localtime( next_day_secs )
 +
            # Zeroing the Hours, Minutes (shime the at first second)
 +
            mktime_data = ( mktime_data[0], mktime_data[1], mktime_data[2], 0, 0, 1, 0, 0 )
 +
            hour = 0
 +
        else:
 +
            # Next Hour & Zeroing minutes, seconds
 +
            mktime_data = ( _now[0], _now[1], _now[2], _now[4]+1, 0, 0, 0, 0 )
 +
            hour = _now[4]+1
 +
   
 +
    # Return seconds, Half or full Hour chime, related Hour
 +
    return time.mktime( mktime_data ), chime_type, hour
 +
 +
 +
_now = rtc.datetime()
 +
_next_chime_secs, _chime_type, _hour = next_chime( _now )
 +
 +
in_day_time = lambda hour : 7 <= hour <= 21
 +
 +
while True:
 +
    # rtc datetime (Y,M,D,wD,h,m,s,ms)
 +
    _now = rtc.datetime()
 +
    # mktime datetime (Y,M,D,h,m,s,wD,yD)
 +
    _mktime_data = ( _now[0], _now[1], _now[2], _now[4], _now[5], _now[6], _now[3], 0 )
 +
    _now_secs = time.mktime( _mktime_data ) # Secs since Jan 1, 2000
 +
   
 +
    print( "Now = %r (%i secs)" % ( _now, _now_secs ) )
 +
    print( "Chime at %i secs" % _next_chime_secs )
 +
    print( "Chime time = %r " % ( time.localtime( _next_chime_secs ), ) )
 +
    print( "-" * 40 )
 +
   
 +
    # It is time to play a Chime
 +
    if _now_secs > _next_chime_secs:
 +
        if in_day_time( _hour ):
 +
            if _chime_type == HOUR_CHIME:
 +
                buzzer.play_str( "We'reRea:d=4,o=6,b=140:8d,8d,8e,8e,f,e,2p,2e,8d,8d,8e,8e,f,e,8d,8d,8e,8f,2p,8d5,16p,8d5,16p,16d5,32d5,32d5,8f,8p,8d,8p,8d5,16p,8d5,16p,16d5,32d5,32d5,8f,8d,8d,8a,8d5,16p,8d5,16p,16d5,16d5,8f,8p,8d" )
 +
                time.sleep( 2 )
 +
                for i in range( _hour ):
 +
                    buzzer.play_tone( 1500, 200 )
 +
                    time.sleep_ms( 800 )
 +
            else: # Half-Hour
 +
                buzzer.play_str( "SexyEyes:d=16,o=6,b=125:b5,8p,32b5,32p,b5,p,32b5,p,32p,f#,8p,32f#,32p,f#,p,f#,p,e,8p,32e,32p,e,p,32e,p,32p,g#,8p,32g#,32p,g#,p,g#,p,f#6,8p,32f#,32p,f#,p,32f#,p,32p,a#,8p,32a#,32p,a#6,p,32a#,p,32p,b,8p,32b" )
 +
        # Calculate time of the next chime
 +
        _next_chime_secs, _chime_type, _hour = next_chime( _now )
 +
         
 +
    time.sleep( 5 )
 
</syntaxhighlight>
 
</syntaxhighlight>
   −
Comment tester ce script:
+
Une fois le script lancé, celui-ci jour une mélodie toutes les heures et toutes les demi-heures. Il faut donc être patient pour le voir fonctionner.
 +
 
 +
Après la mélodie horaire, le buzzer effectue un nombre de "beep" correspondant à l'heure signalée.
   −
# Une fois en cours d'exécution, placer le montage dans des conditions d'illumination normale.
+
Le script en cours d'activité affiche également des messages toutes les 5 secondes dans la session REPL:
−
# Tourner le potentiomètre de sorte à trouver la position "frontière" juste à la limite entre allumé et éteint.
  −
# Faire en sorte de décaler sensiblement la position du potentiomètre dans le sens anti-horlogique (pour éteindre la LED).
  −
# Utiliser votre main pour couvrir / ombrager la photo-résistance, '''la LED doit s'allumer'''.
      +
<syntaxhighlight>
 +
----------------------------------------
 +
Now = (2025, 3, 20, 3, 17, 2, 22, 0) (1742490142 secs)
 +
Chime at 1742491800 secs
 +
Chime time = (2025, 3, 20, 17, 30, 0, 3, 79)
 +
----------------------------------------
 +
...
 +
...
 +
Now = (2025, 3, 20, 3, 17, 8, 37, 0) (1742490517 secs)
 +
Chime at 1742491800 secs
 +
Chime time = (2025, 3, 20, 17, 30, 0, 3, 79)
 +
----------------------------------------
 +
Now = (2025, 3, 20, 3, 17, 8, 42, 0) (1742490522 secs)
 +
Chime at 1742491800 secs
 +
Chime time = (2025, 3, 20, 17, 30, 0, 3, 79)
 +
----------------------------------------
 +
</syntaxhighlight>
 
Quelques mots d'explications:
 
Quelques mots d'explications:
 
* '''Ligne 4''' : désactivation du mode d'
 
* '''Ligne 4''' : désactivation du mode d'
    
{{P2E-TRAILER}}
 
{{P2E-TRAILER}}
32 920

modifications

Menu de navigation