Add 'timer' api docs (#339)
This commit is contained in:
		
				
					committed by
					
						
						Peli de Halleux
					
				
			
			
				
	
			
			
			
						parent
						
							d778232155
						
					
				
				
					commit
					6524b0a841
				
			@@ -111,3 +111,9 @@
 | 
			
		||||
            * [pause for light](/reference/sensors/color-sensor/pause-for-light)
 | 
			
		||||
            * [color](/reference/sensors/color-sensor/color)
 | 
			
		||||
            * [light](/reference/sensors/color-sensor/ambient-light)
 | 
			
		||||
    * [Control](/reference/control)
 | 
			
		||||
        * [Timer](/reference/control/timer)
 | 
			
		||||
            * [seconds](/reference/control/timer/seconds)
 | 
			
		||||
            * [millis](/reference/control/timer/millis)
 | 
			
		||||
            * [reset](/reference/control/timer/reset)
 | 
			
		||||
            * [pause until](/reference/control/timer/pause-until)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,10 @@
 | 
			
		||||
# Reference
 | 
			
		||||
 | 
			
		||||
```namespaces
 | 
			
		||||
brick.showMood(moods.sleeping);
 | 
			
		||||
control.runInParallel(function(){});
 | 
			
		||||
sensors.color(null);
 | 
			
		||||
motors.stopAll();
 | 
			
		||||
brick.showMood(moods.sleeping);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## See Also
 | 
			
		||||
@@ -11,5 +12,4 @@ motors.stopAll();
 | 
			
		||||
[brick](/reference/brick),
 | 
			
		||||
[sensors](/reference/sensors),
 | 
			
		||||
[motors](/reference/motors),
 | 
			
		||||
[touch sensor](/reference/sensors/touch-sensor),
 | 
			
		||||
[color sensor](/reference/sensors/color-sensor)
 | 
			
		||||
[control](/reference/control)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										33
									
								
								docs/reference/control.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								docs/reference/control.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
# Control
 | 
			
		||||
 | 
			
		||||
Program controls and events.
 | 
			
		||||
 | 
			
		||||
```cards
 | 
			
		||||
control.millis();
 | 
			
		||||
control.runInParallel(() => {
 | 
			
		||||
    
 | 
			
		||||
});
 | 
			
		||||
control.reset();
 | 
			
		||||
control.waitMicros(4);
 | 
			
		||||
control.deviceSerialNumber();
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Timer
 | 
			
		||||
 | 
			
		||||
```cards
 | 
			
		||||
control.timer1.reset()
 | 
			
		||||
control.timer1.pauseUntil(5)
 | 
			
		||||
control.timer1.millis()
 | 
			
		||||
control.timer1.seconds()
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Advanced #advanced
 | 
			
		||||
 | 
			
		||||
```cards
 | 
			
		||||
control.raiseEvent(0, 0);
 | 
			
		||||
control.onEvent(0, 0, () => {
 | 
			
		||||
    
 | 
			
		||||
});
 | 
			
		||||
control.assert(false, 0);
 | 
			
		||||
control.panic(0);
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										8
									
								
								docs/reference/control/timer.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								docs/reference/control/timer.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
# Timer
 | 
			
		||||
 | 
			
		||||
```cards
 | 
			
		||||
control.timer1.reset()
 | 
			
		||||
control.timer1.pauseUntil(5)
 | 
			
		||||
control.timer1.millis()
 | 
			
		||||
control.timer1.seconds()
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										25
									
								
								docs/reference/control/timer/millis.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								docs/reference/control/timer/millis.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
# millis
 | 
			
		||||
 | 
			
		||||
Get the amount of time counted by the timer in milliseconds.
 | 
			
		||||
 | 
			
		||||
The timer count begins from `0` when you program starts or is [reset](/reference/control/timer/reset). 
 | 
			
		||||
 | 
			
		||||
## Returns
 | 
			
		||||
 | 
			
		||||
* a [number](/types/number) that is the amount of time elapsed, in milliseconds, since the timer was started or reset.
 | 
			
		||||
 | 
			
		||||
## Example
 | 
			
		||||
 | 
			
		||||
Find out how many milliseconds go by between presses of the `down` button on the brick.
 | 
			
		||||
 | 
			
		||||
```blocks
 | 
			
		||||
brick.buttonDown.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    brick.showValue("DownButtonTime", control.timer1.millis(), 1)
 | 
			
		||||
    control.timer1.reset()
 | 
			
		||||
})
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## See also
 | 
			
		||||
 | 
			
		||||
[seconds](/reference/control/timer/seconds), [reset](/reference/control/timer/reset)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										32
									
								
								docs/reference/control/timer/pause-until.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								docs/reference/control/timer/pause-until.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
# pauseUntil
 | 
			
		||||
 | 
			
		||||
Pause until the timer counts up to a number of milliseconds.
 | 
			
		||||
 | 
			
		||||
```sig
 | 
			
		||||
control.timer1.pauseUntil(0)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
When code in a block comes to a **pauseUntil**, it will wait until the timer count reaches the number of milliseconds you say. Code in blocks like **forever** and **runInParallel** will keep running while the current code is paused.
 | 
			
		||||
 | 
			
		||||
The time number you give is the number of milliseconds past the running timer count. If the timer is currently at `25000` milliseconds and you want to pause for `10` seconds, then use a pause time of `35000`. If you want your pause time number to match the actual wait time, then [reset](/reference/control/timer/reset) the timer first.
 | 
			
		||||
 | 
			
		||||
## Parameters
 | 
			
		||||
 | 
			
		||||
* **ms**: the [number](/types/number) of milliseconds that you want the timer to count up to. For seconds, convert to milliseconds: 100 milliseconds = 1/10 second and 1000 milliseconds = 1 second.
 | 
			
		||||
 | 
			
		||||
## Example
 | 
			
		||||
 | 
			
		||||
Pause between messages on the screen by `5` seconds.
 | 
			
		||||
 | 
			
		||||
```blocks
 | 
			
		||||
brick.clearScreen()
 | 
			
		||||
brick.showString("Testing my pause...", 1)
 | 
			
		||||
let startTime = control.timer1.millis()
 | 
			
		||||
brick.showValue("StartTime", startTime, 3)
 | 
			
		||||
control.timer1.pauseUntil(startTime + 5000)
 | 
			
		||||
brick.showValue("EndTime", control.timer1.millis() - startTime, 4)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## See also
 | 
			
		||||
 | 
			
		||||
[millis](/reference/control/timer/millis), [reset](/reference/control/timer/reset)
 | 
			
		||||
							
								
								
									
										49
									
								
								docs/reference/control/timer/reset.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								docs/reference/control/timer/reset.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,49 @@
 | 
			
		||||
# reset
 | 
			
		||||
 | 
			
		||||
Reset the elapsed time of the timer back to `0`.
 | 
			
		||||
 | 
			
		||||
```sig
 | 
			
		||||
control.timer1.reset()
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
A timer starts counting from `0` when your program starts. It's time value always gets larger as your program runs. Maybe you want to meausure how long some task takes to finish or you want to do some action  only for a little while. A timer can keep track of the time it takes to do it.
 | 
			
		||||
 | 
			
		||||
Resetting the timer sets the time value to `0` so the next time you check the time it's exactly the amount of time that has _elapsed_. Otherwise, you need to remember a start time value and then subtract it from the current time. 
 | 
			
		||||
 | 
			
		||||
## Examples
 | 
			
		||||
 | 
			
		||||
### Press time
 | 
			
		||||
 | 
			
		||||
Find out how much time goes by between presses of the `enter` button on the brick.
 | 
			
		||||
 | 
			
		||||
```blocks
 | 
			
		||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    brick.showValue("PressTime", control.timer1.seconds(), 1)
 | 
			
		||||
    control.timer1.reset()
 | 
			
		||||
})
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Difference timer
 | 
			
		||||
 | 
			
		||||
Use a difference timer and compare it to a timer that resets. Use the ``left`` button to start timing and the ``right`` button to stop.
 | 
			
		||||
 | 
			
		||||
```blocks
 | 
			
		||||
let startTime = 0
 | 
			
		||||
let timing = false
 | 
			
		||||
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    brick.clearScreen()
 | 
			
		||||
    brick.showString("Starting timers...", 1)
 | 
			
		||||
    startTime = control.timer1.seconds()
 | 
			
		||||
    control.timer2.reset()
 | 
			
		||||
    timing = true
 | 
			
		||||
})
 | 
			
		||||
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    if (timing) {
 | 
			
		||||
        brick.clearScreen()
 | 
			
		||||
        brick.showString("Timer results...", 1)
 | 
			
		||||
        brick.showValue("timer 1", control.timer1.seconds() - startTime, 3)
 | 
			
		||||
        brick.showValue("timer 2", control.timer2.seconds(), 4)
 | 
			
		||||
        timing = false;
 | 
			
		||||
    }
 | 
			
		||||
})
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										25
									
								
								docs/reference/control/timer/seconds.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								docs/reference/control/timer/seconds.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
# seconds
 | 
			
		||||
 | 
			
		||||
Get the amount of time counted by the timer in seconds.
 | 
			
		||||
 | 
			
		||||
The timer count begins from `0` when you program starts or is [reset](/reference/control/timer/reset). The number of seconds returned also includes milliseconds if there is a fractional part of a second too.
 | 
			
		||||
 | 
			
		||||
## Returns
 | 
			
		||||
 | 
			
		||||
* a [number](/types/number) that is the amount of time elapsed, in seconds, since the timer was started or reset.
 | 
			
		||||
 | 
			
		||||
## Example
 | 
			
		||||
 | 
			
		||||
Find out how many seconds go by between presses of the `down` button on the brick.
 | 
			
		||||
 | 
			
		||||
```blocks
 | 
			
		||||
brick.buttonDown.onEvent(ButtonEvent.Pressed, function () {
 | 
			
		||||
    brick.showValue("DownButtonTime", control.timer1.seconds(), 1)
 | 
			
		||||
    control.timer1.reset()
 | 
			
		||||
})
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## See also
 | 
			
		||||
 | 
			
		||||
[millis](/reference/control/timer/millis), [reset](/reference/control/timer/reset)
 | 
			
		||||
 | 
			
		||||
@@ -11,36 +11,40 @@ namespace control {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Gets the elapsed time in millis since the last reset
 | 
			
		||||
         * Get the elapsed time in millis since the last reset
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId=timerMillis block="%timer|millis"
 | 
			
		||||
        //% help=control/timer/millis
 | 
			
		||||
        millis(): number {
 | 
			
		||||
            return control.millis() - this.start;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /**
 | 
			
		||||
         * Gets the elapsed time in seconds since the last reset
 | 
			
		||||
         * Get the elapsed time in seconds since the last reset
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId=timerSeconds block="%timer|seconds"
 | 
			
		||||
        //% help=control/timer/seconds
 | 
			
		||||
        seconds(): number {
 | 
			
		||||
            return this.millis() / 1000;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Resets the timer
 | 
			
		||||
         * Reset the timer
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId=timerRest block="%timer|reset"
 | 
			
		||||
        //% help=control/timer/reset
 | 
			
		||||
        reset() {
 | 
			
		||||
            this.start = control.millis();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Pauses until the timer reaches the given amount of milliseconds
 | 
			
		||||
         * Pause until the timer reaches the given amount of milliseconds
 | 
			
		||||
         * @param ms how long to pause for, eg: 5, 100, 200, 500, 1000, 2000
 | 
			
		||||
         */
 | 
			
		||||
        //% blockId=timerPauseUntil block="%timer|pause until (ms) %ms"
 | 
			
		||||
        //% help=control/timer/pause-until
 | 
			
		||||
        pauseUntil(ms: number) {
 | 
			
		||||
            const remaining = this.millis() - ms;
 | 
			
		||||
            const remaining = ms - this.millis();
 | 
			
		||||
            pause(Math.max(0, remaining));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user