44#include < scratchcpp/compiler.h>
55#include < scratchcpp/iengine.h>
66#include < scratchcpp/itimer.h>
7+ #include < scratchcpp/field.h>
78#include " sensingblocks.h"
89
910#include " ../engine/internal/clock.h"
@@ -22,7 +23,20 @@ void SensingBlocks::registerBlocks(IEngine *engine)
2223 // Blocks
2324 engine->addCompileFunction (this , " sensing_timer" , &compileTimer);
2425 engine->addCompileFunction (this , " sensing_resettimer" , &compileResetTimer);
26+ engine->addCompileFunction (this , " sensing_current" , &compileCurrent);
2527 engine->addCompileFunction (this , " sensing_dayssince2000" , &compileDaysSince2000);
28+
29+ // Fields
30+ engine->addField (this , " CURRENTMENU" , CURRENTMENU);
31+
32+ // Field values
33+ engine->addFieldValue (this , " YEAR" , YEAR);
34+ engine->addFieldValue (this , " MONTH" , MONTH);
35+ engine->addFieldValue (this , " DATE" , DATE);
36+ engine->addFieldValue (this , " DAYOFWEEK" , DAYOFWEEK);
37+ engine->addFieldValue (this , " HOUR" , HOUR);
38+ engine->addFieldValue (this , " MINUTE" , MINUTE);
39+ engine->addFieldValue (this , " SECOND" , SECOND);
2640}
2741
2842void SensingBlocks::compileTimer (Compiler *compiler)
@@ -35,6 +49,44 @@ void SensingBlocks::compileResetTimer(Compiler *compiler)
3549 compiler->addFunctionCall (&resetTimer);
3650}
3751
52+ void SensingBlocks::compileCurrent (Compiler *compiler)
53+ {
54+ int id = compiler->field (CURRENTMENU)->specialValueId ();
55+
56+ switch (id) {
57+ case YEAR:
58+ compiler->addFunctionCall (¤tYear);
59+ break ;
60+
61+ case MONTH:
62+ compiler->addFunctionCall (¤tMonth);
63+ break ;
64+
65+ case DATE:
66+ compiler->addFunctionCall (¤tDate);
67+ break ;
68+
69+ case DAYOFWEEK:
70+ compiler->addFunctionCall (¤tDayOfWeek);
71+ break ;
72+
73+ case HOUR:
74+ compiler->addFunctionCall (¤tHour);
75+ break ;
76+
77+ case MINUTE:
78+ compiler->addFunctionCall (¤tMinute);
79+ break ;
80+
81+ case SECOND:
82+ compiler->addFunctionCall (¤tSecond);
83+ break ;
84+
85+ default :
86+ break ;
87+ }
88+ }
89+
3890void SensingBlocks::compileDaysSince2000 (Compiler *compiler)
3991{
4092 compiler->addFunctionCall (&daysSince2000);
@@ -52,6 +104,69 @@ unsigned int SensingBlocks::resetTimer(VirtualMachine *vm)
52104 return 0 ;
53105}
54106
107+ unsigned int SensingBlocks::currentYear (VirtualMachine *vm)
108+ {
109+ time_t now = time (0 );
110+ tm *ltm = localtime (&now);
111+ vm->addReturnValue (ltm->tm_year + 1900 );
112+
113+ return 0 ;
114+ }
115+
116+ unsigned int SensingBlocks::currentMonth (VirtualMachine *vm)
117+ {
118+ time_t now = time (0 );
119+ tm *ltm = localtime (&now);
120+ vm->addReturnValue (ltm->tm_mon + 1 );
121+
122+ return 0 ;
123+ }
124+
125+ unsigned int SensingBlocks::currentDate (VirtualMachine *vm)
126+ {
127+ time_t now = time (0 );
128+ tm *ltm = localtime (&now);
129+ vm->addReturnValue (ltm->tm_mday );
130+
131+ return 0 ;
132+ }
133+
134+ unsigned int SensingBlocks::currentDayOfWeek (VirtualMachine *vm)
135+ {
136+ time_t now = time (0 );
137+ tm *ltm = localtime (&now);
138+ vm->addReturnValue (ltm->tm_wday + 1 );
139+
140+ return 0 ;
141+ }
142+
143+ unsigned int SensingBlocks::currentHour (VirtualMachine *vm)
144+ {
145+ time_t now = time (0 );
146+ tm *ltm = localtime (&now);
147+ vm->addReturnValue (ltm->tm_hour );
148+
149+ return 0 ;
150+ }
151+
152+ unsigned int SensingBlocks::currentMinute (VirtualMachine *vm)
153+ {
154+ time_t now = time (0 );
155+ tm *ltm = localtime (&now);
156+ vm->addReturnValue (ltm->tm_min );
157+
158+ return 0 ;
159+ }
160+
161+ unsigned int SensingBlocks::currentSecond (VirtualMachine *vm)
162+ {
163+ time_t now = time (0 );
164+ tm *ltm = localtime (&now);
165+ vm->addReturnValue (ltm->tm_sec );
166+
167+ return 0 ;
168+ }
169+
55170unsigned int SensingBlocks::daysSince2000 (VirtualMachine *vm)
56171{
57172 if (!clock)
0 commit comments