shaoziyang / microbit-lib

all kinds of microbit python drives, libs, examples, etc.
MIT License
49 stars 39 forks source link

New demo -- cleaned up to display properly on Github.com #3

Open randmor opened 6 years ago

randmor commented 6 years ago
'''
    DS1302 Demo #2 (v3)
    Author: R_Moore
    Date:   9/3/2018
    Uses ShaoZiYang's DS1302 Driver from this URL:
        https://github.com/shaoziyang/microbit-lib/tree/master/misc/DS1302
        http://www.micropython.org.cn

    Improved demo/test program to be more Micro:bit user friendly...
'''
from microbit import *
import time
import DS1302

# The following function translates the 'day of week' code 
# (an integer value btw 0 & 6, inclusive) into the 
# equivalent English name for the day.

def xlateDayCode(day):
    if day == 0:
        return 'Sunday'
    elif day == 1:
        return 'Monday'
    elif day == 2:
        return 'Tuesday'
    elif day == 3:
        return 'Wednesday'
    elif day == 4:
        return 'Thursday'
    elif day == 5:
        return 'Friday'
    elif day == 6:
        return 'Saturday'
    else:
        return 'Error'

# Create an instance of a DS1302 class and call it 'ds'.
ds = DS1302.DS1302(clk=pin13, dio=pin14, cs=pin15)

# Use next line of code just one time to set the date/time of RTC module (un-comment it to use)
# ds.DateTime([2018, 3, 9, 4, 23, 0, 1])  # March 3, 2018 at 11:00:01 pm.

while True:
    # Display day of week...
    display.scroll('Today is ' + xlateDayCode(ds.Weekday()) + ', ')

    # Display current date in 'mm/dd/yyyy' format...
    display.scroll(str(ds.Month()) + '/' + str(ds.Day()) + '/' + str(ds.Year()))
    time.sleep_ms(1000)

    # Display current time in 'hh:mm:ss' format...
    display.scroll(str(ds.Hour()) + ':' + str(ds.Minute()) + ':' + str(ds.Second()))
    time.sleep_ms(10000)        # Wait 10 seconds & repeat...
randmor commented 6 years ago

Seems GitHub is not Python friendly when it comes to comments in the source code. Seems to want to interpret '#' as some kind of format character. Here's the code again stripped of comments:

` from microbit import * import time import DS1302

def xlateDayCode(day): if day == 0: return 'Sunday' elif day == 1: return 'Monday' elif day == 2: return 'Tuesday' elif day == 3: return 'Wednesday' elif day == 4: return 'Thursday' elif day == 5: return 'Friday' elif day == 6: return 'Saturday' else: return 'Error'

ds = DS1302.DS1302(clk=pin13, dio=pin14, cs=pin15)

ds.DateTime([2018, 3, 9, 4, 23, 0, 1]) # March 3, 2018 at 11:00:01 pm.

while True: display.scroll('Today is ' + xlateDayCode(ds.Weekday()) + ', ')
display.scroll(str(ds.Month()) + '/' + str(ds.Day()) + '/' + str(ds.Year())) time.sleep_ms(1000) display.scroll(str(ds.Hour()) + ':' + str(ds.Minute()) + ':' + str(ds.Second()) + ' ') time.sleep_ms(10000) # Wait 10 seconds & repeat...

`

randmor commented 6 years ago

Now tab indents are gone.