stopwatch.pp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. program stopwatch;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9;
  5. //the speed of the timer when using ClockDivider_1024
  6. const
  7. TIMER_SPEED = (BUS_CLOCK div 1024);
  8. type
  9. TTimerStates = (timerState_Stop, timerState_Pause, timerState_Running);
  10. var
  11. ticks: cuint = 0;
  12. state: TTimerStates = timerState_Stop;
  13. down: cint;
  14. begin
  15. consoleDemoInit();
  16. down := keysDown();
  17. while (down and KEY_START) = 0 do
  18. begin
  19. swiWaitForVBlank();
  20. consoleClear();
  21. scanKeys();
  22. down := keysDown();
  23. if (state = timerState_Running) then
  24. ticks := ticks + timerElapsed(0);
  25. if (down and KEY_A) <> 0 then
  26. begin
  27. if (state = timerState_Stop) then
  28. begin
  29. timerStart(0, ClockDivider_1024, 0, nil);
  30. state := timerState_Running;
  31. end else
  32. if (state = timerState_Pause) then
  33. begin
  34. timerUnpause(0);
  35. state := timerState_Running;
  36. end else
  37. if (state = timerState_Running) then
  38. begin
  39. ticks := ticks + timerPause(0);
  40. state := timerState_Pause;
  41. end;
  42. end else
  43. if (down and KEY_B) <> 0 then
  44. begin
  45. timerStop(0);
  46. ticks := 0;
  47. state := timerState_Stop;
  48. end;
  49. iprintf('Press A to start and pause the '#10'timer, B to clear the timer '#10'and start to quit the program.'#10#10);
  50. iprintf('ticks: %u'#10, ticks);
  51. iprintf('second: %u:%u'#10, ticks div TIMER_SPEED, ((ticks mod TIMER_SPEED) * 1000) div TIMER_SPEED);
  52. end;
  53. if (state <> timerState_Stop) then
  54. timerStop(0);
  55. end.