AS3.Setfieldtext(mynumber.ToString("F4"), 20000); // field 20000 will be formatted to 4 decimals, e.g. 15.0010
// ================================================================================================
// Run Timer v1.0
// ================================================================================================
// !! DO NOT CHANGE THIS LINE !! [ID:RunTimer#1#0]
// ================================================================================================
if (exec.GetLED(CycleStartLED))
++LoopCnt;
if (AS3.Getbuttonstate(TimerResetSw))
{
LoopCnt = 0;
AS3.Switchbutton(false, TimerResetSw);
}
// Use one of the following blocks
/*
int CntPer20 = (int)(LoopCnt / 20);
if (CntPer20 != LastCnt) // Update 1 times per sec.
{
LastCnt = CntPer20;
AS3.Setfieldtext(timeint2str(CntPer20), TimerField);
}
*/
// OR
if (LoopCnt != LastCnt) // Update 20 times per sec.
{
LastCnt = LoopCnt;
AS3.Setfieldtext(timedbl2str(LoopCnt / 20.0), TimerField);
}
// ================================================================================================
#Events
// ================================================================================================
static int LoopCnt = 0; // Counts 0.05s
static int LastCnt = -1;
const int TimerField = 21001; // Timer display field
// In screenset or M99998.txt
// AS3.Addfield("", "Arial", "left", 12, 0, 1030, 665, 140, "textfield", Double.MinValue, Double.MaxValue, 21001, 1); // text test field
// AS3.Setfieldtext("", 21001);
const int TimerResetSw = 4010; // Switch type button to reset timer
// In screenset or M99998.txt
// AS3.Addbutton(xxx, yyy, width, height, true, false, picturenumber, 4010, 1);
// This needs the updated SwLED plugin, you can find it in the plugins thread or
// you can write a macro to switch the button, but that will not run while cycle
// is started, so you can only reset the counter when no g-code is running. If
// you use a macro to reset then replace 4010 with 20000 to 21999 to call your
// switching macro. The macro will be only one line:
// AS3.Switchbutton(true, buttonnumber);
const int CycleStartLED = 54;
// ================================================================================================
// =============== Function samples
// Set field 21001 to time formatted with 3 decimals with the value of field 21000
// 123.456 -> 00:02:03.456
// AS3.Setfieldtext(timedbl2str(AS3.Getfielddouble(21000)), 21001);
// Increment formatted time with 3 decimals in field 21001 with 1s
// AS3.Setfieldtext(timedbl2str(timestr2dbl(AS3.Getfield(21001)) + 1), 21001);
// Set field 21001 to time formatted value of field 21000
// 123 -> 00:02:03
// AS3.Setfieldtext(timeint2str(AS3.Getfieldint(21000)), 21001);
// Increment formatted time in field 21001 with 1s
// AS3.Setfieldtext(timeint2str(timestr2int(AS3.Getfield(21001)) + 1), 21001);
// =============== Seconds (int) to time string 00:00:00
string timeint2str(int s)
{
int secs = s;
int hours = (int)(secs / 3600);
secs %= 3600;
int mins = (int)(secs / 60);
secs %= 60;
return (hours.ToString("D2") + ":" + mins.ToString("D2") + ":" + secs.ToString("D2"));
}
// =============== Time string to seconds (int)
int timestr2int(string s)
{
string[] tags = ("0" + s).Split(':');
int ret = 0;
int ptr = 0;
while (ptr < tags.Length)
ret = ret * 60 + Convert.ToInt32(tags[ptr++]);
return ret;
}
// =============== Seconds (double) to time string 00:00:00.000
string timedbl2str(double s)
{
double secs = s;
int hours = (int)(secs / 3600);
secs %= 3600;
int mins = (int)(secs / 60);
secs %= 60;
return (hours.ToString("D2") + ":" + mins.ToString("D2") + ":" + (secs < 10 ? "0" : "") + secs.ToString("F3"));
}
// =============== Time string to seconds (double)
double timestr2dbl(string s)
{
string[] tags = ("0" + s).Split(':');
double ret = 0.0;
int ptr = 0;
while (ptr < tags.Length)
Console.WriteLine(ptr.ToString() + ": '" + tags[ptr++] + "'");
ptr = 0;
while (ptr < tags.Length)
ret = ret * 60 + Convert.ToDouble(tags[ptr++]);
return ret;
}
// ================================================================================================
Users browsing this forum: No registered users and 0 guests