Skip to content

Scheduler

Joachim Stolberg edited this page Feb 4, 2023 · 6 revisions

The following framework is used to execute functions regularly but with different cycle times. The function time_slot calculates values according the pattern: 0, 1, 0, 2, 0, 3, 0, 1, 0, 2, 0, 4, ...

E.g. the value 0 occurs every second time. Code in section slot0 will be executed every second call of the loop function.

The following table shows all time slots:

Value Every Cycle time
0 2nd call 400 ms
1 4th call 800 ms
2 8th call 1.6 s
3 16th call 3.2 s
4 32th call 6.4 s
5 64th call 12.8 s
6 128th call 25.6 s
import "sys/stdio.asm"
import "sys/math.asm"

static var cycle = 0;
static var jump_tbl[] = {&&slot0, &&slot1, &&slot2, &&slot3, 
                         &&slot4, &&slot5, &&slot6, &&default};
func time_slot(cycle) {
  _asm_ {
    move A, [SP+1]
    add  A, #1
    xor  A, [SP+1]
    msb  A, A
    dec  A
  }
}

func init() {
  setstdout(1);  // use terminal windows for stdout
  putchar('\b'); // clear entire screen
}

func loop() {
  var idx;
 
  cycle = cycle + 1;
  idx = time_slot(cycle);
  idx = min(idx, sizeof(jump_tbl) - 1);
  goto *jump_tbl[idx];

  while(0) {

    slot0:
      putstr("Every 2nd times\n");
      break;

    slot1:
      putstr("Every 4th times\n");
      break;

    slot2:
      putstr("Every 8th times\n");
      break;

    slot3:
      putstr("Every 16th times\n");
      break;

    slot4:
      putstr("Every 32th times\n");
      break;

    slot5:
      putstr("Every 64th times\n");
      break;

    slot6:
      putstr("Every 128th times\n");
      break;

    default:
      putstr("Default\n");
      break;
  }

  // This line should be enabled for this 'putstr' example,
  //but not for a real case application.
  //sleep(2); 
}
Clone this wiki locally