Timer field

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

Timer field

Postby Alberto FUL » Tue Feb 20, 2018 11:56 pm

Is it possible to create new user timer fields ?
Alberto FUL
 
Posts: 39
Joined: Thu Jan 12, 2017 1:48 pm
Location: Italy

Re: Timer field

Postby dezsoe » Wed Feb 21, 2018 9:34 am

Yes, but write some details. What precision, what is does it have to do, etc.

E.g. I have an AutoReset timer with adjustable time-out:

autoreset.png
autoreset.png (5.45 KiB) Viewed 11169 times
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Timer field

Postby Alberto FUL » Wed Feb 21, 2018 11:41 am

I'd like toi have clone of field 989 (worktimer).
I'd like to be able to read worktimer field and copy and store the value in another field.
Is also possible to format an user field with specific format like th eaxis DROS (I mean decimal positions).
Alberto FUL
 
Posts: 39
Joined: Thu Jan 12, 2017 1:48 pm
Location: Italy

Re: Timer field

Postby dezsoe » Wed Feb 21, 2018 12:28 pm

Ok, your worktimer clone should work as the original, so run only when cycle start is active?

You can format a field by writing text to it in a macro:

Code: Select all
AS3.Setfieldtext(mynumber.ToString("F4"), 20000); // field 20000 will be formatted to 4 decimals, e.g. 15.0010
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Timer field

Postby Alberto FUL » Wed Feb 21, 2018 1:38 pm

Yes, only when in cycle.
Alberto FUL
 
Posts: 39
Joined: Thu Jan 12, 2017 1:48 pm
Location: Italy

Re: Timer field

Postby dezsoe » Sat Feb 24, 2018 12:33 am

Hi Alberto,

Here is the macro you need. It contains more than you need, but I don't know programming skills, so I left all stuff collected and you just have to make small changes as you need. If you need, I can make the code custumization for you. If you have any question, feel free to ask.

Code: Select all
// ================================================================================================
// 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;
}

// ================================================================================================


Timer counts on the bottom right:

timer2.png
timer2.png (29.87 KiB) Viewed 11085 times
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Timer field

Postby Alberto FUL » Sat Mar 03, 2018 10:11 am

WOW.!
Thks
Alberto FUL
 
Posts: 39
Joined: Thu Jan 12, 2017 1:48 pm
Location: Italy


Return to Macros

Who is online

Users browsing this forum: No registered users and 2 guests