Skip to content

Instantly share code, notes, and snippets.

@pwillard
Created September 6, 2022 17:19
Show Gist options
  • Save pwillard/ee75b0e9d8f8238bc000d90decbf5bdb to your computer and use it in GitHub Desktop.
Save pwillard/ee75b0e9d8f8238bc000d90decbf5bdb to your computer and use it in GitHub Desktop.
Crappy Time Alarm
'Program: WAVESHARE-CLOCK.bas
'Author: Pete Willard
'Version: 0.10
'Target: picomite version
'Date: 2022/06/23
'Updated: 2022/09/06
'Time: 08:34 AM
'Notes: USING PIMORONI LIPO BOARD WITH WAVESHARE Pico-ResTouch-LCD-3.5
' and DS3231 I2C Real Time Clock (generic module)
' DATE format has support for USA style ;-P
' *** MUCH IS COMMENTED OUT AT THE MOMENT ***
'
'Reference: Too numerous to list
'
' EDT = Time in NYC is UTC-4
' EST = Time in NYC is UTC-5
'
' Eastern Daylight Time - (EDT) From the second Sunday in March 2:00AM until the
' first Sunday in November 2:00A (also known as Daylight Savings Time - DST)
'
' Eastern Standard Time - (EST) From the first Sunday in November 2:00AM until the
' second Sunday in March 2:00 AM
'
'
'=====[ CONFIGURATION ]==========================================================
'OPTION SYSTEM SPI GP10,GP11,GP12
'OPTION SYSTEM I2C GP4,GP5
'OPTION AUTORUN ON
'OPTION COLOURCODE ON
'OPTION DISPLAY 25, 132
'OPTION LCDPANEL ILI9488W, RLANDSCAPE,GP8,GP15,GP9,GP13 ' To place the USB topside
'OPTION GUI CONTROLS 75
'OPTION TOUCH GP16,GP17
'GUI CALIBRATE 0, 3998, 211, -1268, 884
'OPTION SDCARD GP22
'===
Option DEFAULT INTEGER
Option EXPLICIT
'=====[ CONSTANTS ]==============================================================
Const Background = RGB(BLACK)
Const LIGHT = RGB(WHITE)
Const AMBER = RGB(211,191,0)
Const L.GREEN = RGB(48, 112, 30)
Const OptionDisplayDate = 1
Const MAIN_SWITCH = 5
Const ALARM_LED = 6
Const ALARM_ON = 15
Const ALARM_OFF = 16
Const AL1 = 1
Const AL2 = 2
Const TIM = 3
Const DAT = 4
Const opt = 5
Const DAE = 6
Const alm1 = 10
Const alm2 = 20
Const MP3_ADDR = &H37
'THESE ARE THE COMMANDS WE CAN SEND
Const CMD_STOP = &H00
Const CMD_PLAY_TRACK = &H01 'PLAY A TRACK NUMBER LIKE ON A CD: REGARDLESS OF
' FILE NAMES PLAYS 2ND FILE IN DIR.
Const CMD_PLAY_FILENUMBER = &H02 'PLAY A FILE # BASED ON DIRECTORY SORT
Const CMD_PAUSE = &H03 'PAUSE - TOGGLE
Const CMD_PLAY_NEXT = &H04
Const CMD_PLAY_PREVIOUS = &H05
Const CMD_SET_EQ = &H06
Const CMD_SET_VOLUME = &H07
Const CMD_GET_SONG_COUNT = &H08 'NOTE THIS CAUSES SONG TO STOP PLAYING
Const CMD_GET_SONG_NAME = &H09 'FILL ARRAY WITH 8 CHARS OF SONG NAME
Const CMD_GET_PLAY_STATUS = &H0A
Const CMD_GET_CARD_STATUS = &H0B
Const CMD_GET_VERSION = &H0C
Const CMD_CLEAR_INTERRUPTS = &H0D
Const CMD_GET_VOLUME = &H0E
Const CMD_GET_EQ = &H0F
Const CMD_GET_ID = &H10
Const CMD_SET_ADDRESS = &HC7
Dim integer TIMEZONE = -4 ' STANDARD TIME
'=====[ Variables ]=============================================================
Dim string FullTime$
Dim string SP$(10)
Dim INTEGER SONGNUM
Dim integer Sunday.Count
Dim INTEGEr DST = 0 ' True or False
Dim INTEGER A1,A2
Dim STRING LastTime$ = ""
Dim STRING HHMM$
Dim INTEGER AMPM
Dim STRING MyDate$
Dim STRING ALARM1$,ALARM2$
Dim INTEGER SHOWTIME = 1
Dim SETALARMS = 0
' fonts
Dim integer f1=3,f2=1
Dim INTEGER Alarm1_H, Alarm1_M,Alarm1_On
Dim integer Alarm2_H, Alarm2_m,Alarm2_On
Dim integer clock.hour, clock.minute, clock.day
Dim INTEGER USA = 1 ' 1 = USA Date Format, 0 = format for everyone else
'VAR restore
'Print "ALARM 1 SETTING",A1 ' Alarm State OFF/ON
'Print "ALARM 2 SETTING",A2 ' Alarm State OFF/ON
'====[ PINS]
GUI INTERRUPT MyInt
'GUI Interrupt TouchDown, TouchUp
'=====[ SETUP ]==================================================================
' on boot, get time from RTC and setup a regular update inteval
Sub STARTUP
'RTC GETTIME
rtcsettime
SetTick 7200000, RtcSetTime ' 2 hours
Print "UTC Time:"; Time$
Print "UTC Date:";Date$
Pause 1000
I2C write MP3_addr, 0, 1, &H00
If MM.I2C=0 Then
Print "MP3-TRIGGER IS ONLINE"
Else
Print "MP3-TRIGGER IS OFFLINE": End
EndIf
End Sub
'================================================================================
'=====[ MAIN CODE ]==============================================================
CLS
STARTUP
' Default Text Color and Font Size
Colour AMBER,Background
' Draw Alarm State buttons
' Alarm 1 = Weekday Alarm
' Alarm 2 = Weekend Alarm
Font f2
Backlight 60
'GUI CAPTION AL1,"Alarm 1",70,255,"CM"
'GUI Switch alm1, "OFF|ON", 10, 268, 115, 50, RGB(BLACK),AMBER
'CtrlVal(alm1) = A1
'GUI CAPTION AL2,"Alarm 2",410,255,"CM"
'GUI Switch alm2, "OFF|ON", 350, 268, 115, 50, RGB(Black),AMBER
'CtrlVal(alm2) = A2
GUI BUTTON OPT,"OPTION",MM.HRes/2 -75,268,150,50,RGB(CYAN),RGB(64,64,64)
' Tight Loop
' Update Time on screen only when HH:MM changes, every 60 seconds
Do
Font f1,2
HHMM$ = Left$(Time$,5)
If LastTime$ <> HHMM$ Then UpdateClock
Loop
'================================================================================
'=====[ SUBROUTINES ]============================================================
Sub MyInt
Print Touch(x) Touch(y)
SHOWTIME = 1
UPDATEClock
' On Screen Touch
'GUI ENABLE ALM1,AL1
'GUI ENABLE ALM2,AL2
'GUI ENABLE opt
'Select Case Touch(ref)
'Case alm1
'A1 = CtrlVal(alm1)
'Print "ALARM 1 SETTING",A1
'VAR save A1
'Case alm2
'A2 = CtrlVal(alm2)
'Print "ALARM2 SETTING",A2
'VAR save A2
'End Select
SONGNUM = 6
PLAYSONG SONGNUM
End Sub
'=====
Sub RTCSETTIME
' retreive time from the Real Time Clock
' Sets TIME$ and DATE$ variables
SETDST
RTC GETTIME
Local STRING TheTime$ = Left$(Time$,5)
Local hour = Val(Left$(TheTime$,2))
Local second = Val(Right$(TheTime$,2))
Local minute = Val(Mid$(TheTime$,4,2))
Print "UTC Hour= ";hour
If DST = 0 Then TIMEZONE = TIMEZONE -1
hour = hour + TIMEZONE
Print "Adjusted Hour= ";hour
Time$ = Str$(hour) + ":" + Str$(minute) +":"+ Str$(Second)
Print "Adjusted to ";Time$
End Sub
'=====
Sub UpdateClock
' Note: ALARMS are always set with 24 Hour time.
' Display is always 12 Hr AM/PM
Print "Update time: " Time$
LastTime = Left$(Time$,5)
Local hour = Val(Left$(lastTime$,2))
Local minute = Val(Right$(lastTime$,2))
Local meridian = hour
Local AmPm$
Local HH$,MM$
' Check if AM or PM
If meridian >= 12 Then
meridian = meridian - 12
AMPM$ = "PM"
Else
AMPM$ = "AM"
EndIf
' Handle 12 AM, avoid 0 O'clock.
If meridian = 0 Then meridian = 12
' Reassemble the TIME variables back into a string value
' with leading zero blanking
HH$ = Str$(meridian)
If Meridian < 10 Then HH$ = " " + HH$
HH$ = HH$ + ":"
MM$ = Str$(Minute)
If minute < 10 Then MM$ = "0" + MM$
HHMM$ = HH$ + MM$
' Update the screen
' with ability to blank the time.
If SHOWTIME = 1 Then
Text (MM.HRes/2 -50), MM.VRes/4, HHMM$, "CM", 6, 2 ' TIME
Text MM.HRes -50, MM.VRes/4 +10, AMPM$, "R" ' AM/PM
GET_DATE
EndIf
' Dim alarm on/off switches
'GUI DISABLE ALM1,AL1
'GUI DISABLE ALM2,AL2
End Sub
'===
Sub SETDST
' New York, Usa, Time Zone
' Figure Out Eastern Daylight Time By Finding
' The Day Of The Month Of The Second Sunday In March
' and the Day of the month in the first Sunday in November
' Then determine if the current date is within the DST range
' Set DST to 1 or 0 accordingly
Local Myyear$ = Mid$(Date$,7)
Local Dst.Addday = 0 ' In the loop, We Start With An Increment, So Begin With 0
Local Dst.Start$ = "" ' Will contain the DST Start Date
Local DST.End$ = "" ' Will contain the DST End Date
Local Dst.Sunday$ = "" ' Sunday String matching
Local Dst.Countsunday = 0 ' Looking for sundays as we increment days
Local Day1, Day2 ' Day of the week
' Compute DST Start
Do
Inc Dst.Addday
' Pad Day To Make Day$() Happy if needed
If Dst.Addday < 10 Then
Dst.Start$ = "0" +Str$(Dst.Addday) + "-03-" + Myyear$
Else
Dst.Start$ = Str$(Dst.Addday) + "-03-" + Myyear$
EndIf
Dst.Sunday$ = Day$(Dst.Start$)
If Dst.Sunday$ = "Sunday" Then
Inc Dst.Countsunday
EndIf
Loop Until Dst.Countsunday = 2
Day1 = DST.AddDay
'compute DST End
DST.AddDay = 0
DST.CountSunday = 0
Do
Inc Dst.Addday
' Pad Day To make Day$() happy since day will always be a low number
Dst.End$ = "0" +Str$(Dst.Addday) + "-11-" + Myyear$
Dst.Sunday$ = Day$(Dst.End$)
If Dst.Sunday$ = "Sunday" Then
Inc Dst.Countsunday
EndIf
Loop Until Dst.Countsunday = 1
Day2 = DST.AddDay
'' Print Results
'Print "Second Sunday In March Is Day:" Day1
'Print "First Sunday In November Is Day:" Day2
Dst.Start$ = Str$(Day1) + "-03-" + Myyear$ +" 02:00:00"
'print "Current Epoch Time " EPOCH(NOW)
'print "DST START Value " DST.Start$
'print "DST START Epoch Time " EPOCH(DST.Start$)
Dst.End$ = "0" +Str$(DAY2) + "-11-" + Myyear$ + " 02:00:00"
'print "DST END Value " DST.END$
'print "DST END Epoch Time " EPOCH(DST.END$)
' Set DST STATE
If Epoch(NOW) < Epoch(DST.START$) Then DST = 0
If Epoch(NOW) > Epoch(DST.START$) Then DST = 1
If Epoch(NOW) > Epoch(DST.END$) Then DST = 0
Print "DST STATE = " DST
End Sub
'===
Sub GET_DATE
' Uses DAY$() to get weekday
' Fix the date notation for the weird Americans
Local MyDay$ = Left$(Date$,2)
Local MyMonth$ = Mid$(Date$,4,2)
Local MyYear$ = Mid$(Date$,7)
' Avengers Assemble
If USA Then
MyDate$ = MyMonth$ +"/"+ MyDay$ +"/"+ MyYear$
Else
MyDate$ = MyDay$ +"/"+ MyMonth$ +"/"+ MyYear$
EndIf
' Pad the Weekday string so we always clear what was already there
' on the screen
Text MM.HRes/2,MM.VRes/2+15,PadString$(10,Day$(NOW)),"CM"
Text MM.HRes/2, MM.VRes*3/4-15, MyDate$, "CM"
End Sub
'===
' Add leading and trailing spaces to a string for a given size
Function PadString$(size,S$)
Local pad = size/2
PadString$ = s$
If Len(padstring$) >= size Then
padstring$ = s$
Else
padstring$ = String$(pad," ") + S$ + String$(pad," ")
EndIf
End Function
'===
' x=Constrain(TriggerTemp,7,10)
Function Constrain(v As Integer,Llim As Integer, Ulim As Integer) As Integer
Constrain=Min(Max(v,Llim ),Ulim )
End Function
Sub PLAYSONG SONGNUM
'I2C OPEN 100,200
Print "play song"
I2C WRITE MP3_ADDR,0,2,CMD_SET_VOLUME,4
Pause 1000
I2C WRITE MP3_ADDR,0,2,&H01,SONGNUM
'I2C CLOSE
End Sub
'================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment