-
Notifications
You must be signed in to change notification settings - Fork 3
/
PropellerRTC_Emulator.spin
188 lines (156 loc) · 14 KB
/
PropellerRTC_Emulator.spin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{{
************************************************
* Propeller RTC Emulator v1.0 *
* Author: Beau Schwabe *
* Copyright (c) 2009 Parallax *
* See end of file for terms of use. *
************************************************
}}
VAR
long Timer,Temp,ClockFlag,_TimeAddress
byte MonthDays,SS,MM,HH,AP,DD,MO,YY,LY
byte APswitch,DateTimeStamp[11]
long Stack[100]
PUB Start(TimeAddress)
_TimeAddress := TimeAddress
cognew(Run(TimeAddress),@Stack)
PUB SetSec(_SS)
SS := _SS
PUB SetMin(_MM)
MM := _MM
PUB SetHour(_HH)
HH := _HH
PUB SetAMPM(_AP)
AP := _AP
PUB SetDate(_DD)
DD := _DD
PUB SetMonth(_MO)
MO := _MO
PUB SetYear(_YY)
YY := _YY
PUB Suspend
ClockFlag := 1 '' Suspend Clock
repeat while ClockFlag == 1 '' Clock responds with a 2 when suspend received
ParseTime(_TimeAddress) '' Unpack current time variable values from 'long'
PUB Restart
UnParseTime(_TimeAddress) '' Pack current time variable values into 'long'
ClockFlag := 0 '' Restart Clock
PUB Run(TimeAddress)
'' TimeAddress variable allocation:
'' Leap Year Month Date AM/PM Hours Minutes Seconds
'' (0-1) (00-31) (1-12) (1-31) (0-1) (1-12) (00-59) (00-59)
'' 0____00000____0000___00000____0____0000____000000____000000
APswitch := 1
Timer := cnt
repeat
waitcnt(Timer += clkfreq) '' 1 Second Synchronized Delay
if ClockFlag <> 0 '' Check for request to suspend clock?
ClockFlag := 2 '' respond by acknowledging request
repeat while ClockFlag <> 0 '' Wait for the OK to resume clock
Timer := cnt
ParseTime(TimeAddress)
If YY>>2<<2 == YY '' Detect Leap Year
LY := 1
else
LY := 0
MonthDays := 28 '' Decode number of days in each month
If MO <> 2
MonthDays += 2
if MO & %0001 <> (MO & %1000)/%1000
MonthDays += 1
else
MonthDays += LY
SS += 1 '' Increment Time Calendar
if SS == 60 '' Seconds LOGIC
SS := 0
MM += 1
if MM == 60 '' Minutes LOGIC
MM := 0
HH += 1
if HH == 13 '' Hours LOGIC
HH := 1
if HH == 11 '' AM/PM LOGIC
APswitch := 0
if HH < 11
APswitch := 1
if HH == 12
if APswitch == 0
APswitch := 1
AP := 1 - AP
if AP == 0
DD += 1
if DD == MonthDays + 1 '' Days LOGIC
DD := 1
MO += 1
if MO == 13 '' Months LOGIC
MO := 1
YY += 1
if YY == 33 '' Years LOGIC
YY := 32
UnParseTime(TimeAddress) '' Pack current time variable values into 'long'
PUB UnParseTime(TimeAddress)
Result := LY<<31 | YY<<26 | MO<< 22 | DD<<17 | AP<<16 | HH<<12 | MM<<6 | SS
longmove(TimeAddress,@Result,1)
PUB ParseTime(TimeAddress)
longmove(@Temp,TimeAddress,1) 'Parse Data
SS := Temp & %111111
Temp := Temp >> 6
MM := Temp & %111111
Temp := Temp >> 6
HH := Temp & %1111
Temp := Temp >> 4
AP := Temp & %1
Temp := Temp >> 1
DD := Temp & %11111
Temp := Temp >> 5
MO := Temp & %1111
Temp := Temp >> 4
YY := Temp & %11111
Temp := Temp >> 5
LY := Temp & %1
PUB ParseDateStamp(DataAddress)
DateTimeStamp[0] := "2" ''<-Year
DateTimeStamp[1] := "0" '<-Year
DateTimeStamp[2] := $30 + YY/10 '<-Year
DateTimeStamp[3] := $30 + YY-(YY/10)*10 '<-Year
DateTimeStamp[4] := "/"
DateTimeStamp[5] := $30 + MO/10 ''<-Month
DateTimeStamp[6] := $30 + MO-(MO/10)*10 '<-Month
DateTimeStamp[7] := "/"
DateTimeStamp[8] := $30 + DD/10 ''<-Day
DateTimeStamp[9] := $30 + DD-(DD/10)*10 '<-Day
DateTimeStamp[10] := 0 ' String Terminator ALWAYS Zero
bytemove(DataAddress,@DateTimeStamp,11)
PUB ParseTimeStamp(DataAddress)
DateTimeStamp[0] := $30 + HH/10 ''<-Hour
DateTimeStamp[1] := $30 + HH-(HH/10)*10 '<-Hour
DateTimeStamp[2] := ":"
DateTimeStamp[3] := $30 + MM/10 ''<-Minute
DateTimeStamp[4] := $30 + MM-(MM/10)*10 '<-Minute
DateTimeStamp[5] := ":"
DateTimeStamp[6] := $30 + SS/10 ''<-Second
DateTimeStamp[7] := $30 + SS-(SS/10)*10 '<-Second
if AP < 1
DateTimeStamp[8] := "a" ''<-Set am
else
DateTimeStamp[8] := "p" ''<-Set pm
DateTimeStamp[9] := "m"
DateTimeStamp[10] := 0 ' String Terminator ALWAYS Zero
bytemove(DataAddress,@DateTimeStamp,11)
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TERMS OF USE: MIT License │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
│WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
│COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
}}