File: timer.py

package info (click to toggle)
giac 1.6.0.41%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 64,540 kB
  • sloc: cpp: 351,842; ansic: 105,138; python: 30,545; javascript: 8,675; yacc: 2,690; lex: 2,449; makefile: 1,243; sh: 579; perl: 314; lisp: 216; asm: 62; java: 41; sed: 16; csh: 7; pascal: 6
file content (117 lines) | stat: -rw-r--r-- 2,589 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'''
Timer test for the CC3200 based boards.
'''

from machine import Timer
import os
import time

mch = os.uname().machine
if 'LaunchPad' in mch:
    pwm_pin = ('GP24')
elif 'WiPy' in mch:
    pwm_pin = ('GP24')
else:
    raise Exception('Board not supported!')

for i in range(4):
    tim = Timer(i, mode=Timer.PERIODIC)
    print(tim)
    ch = tim.channel(Timer.A, freq=5)
    print(ch)
    ch = tim.channel(Timer.B, freq=5)
    print(ch)
    tim = Timer(i, mode=Timer.ONE_SHOT)
    print(tim)
    ch = tim.channel(Timer.A, freq=50)
    print(ch)
    ch = tim.channel(Timer.B, freq=50)
    print(ch)
    tim = Timer(i, mode=Timer.PWM)
    print(tim)
    ch = tim.channel(Timer.A, freq=50000, duty_cycle=2000, polarity=Timer.POSITIVE)
    print(ch)
    ch = tim.channel(Timer.B, freq=50000, duty_cycle=8000, polarity=Timer.NEGATIVE)
    print(ch)
    tim.deinit()
    print(tim)

for i in range(4):
    tim = Timer(i, mode=Timer.PERIODIC)
    tim.deinit()


class TimerTest:
    def __init__(self):
        self.tim = Timer(0, mode=Timer.PERIODIC)
        self.int_count = 0

    def timer_isr(self, tim_ch):
        self.int_count += 1

timer_test = TimerTest()
ch = timer_test.tim.channel(Timer.A, freq=5)
print(ch.freq() == 5)
ch.irq(handler=timer_test.timer_isr, trigger=Timer.TIMEOUT)
time.sleep_ms(1001)
print(timer_test.int_count == 5)

ch.freq(100)
timer_test.int_count = 0
time.sleep_ms(1001)
print(timer_test.int_count == 100)

ch.freq(1000)
time.sleep_ms(1500)
timer_test.int_count = 0
time.sleep_ms(2000)
print(timer_test.int_count == 2000)

timer_test.tim.deinit()
timer_test.tim.init(mode=Timer.ONE_SHOT)
ch = timer_test.tim.channel(Timer.A, period=100000)
ch.irq(handler=timer_test.timer_isr, trigger=Timer.TIMEOUT)
timer_test.int_count = 0
time.sleep_ms(101)
print(timer_test.int_count == 1)
time.sleep_ms(101)
print(timer_test.int_count == 1)
timer_test.tim.deinit()
print(timer_test.tim)

# 32 bit modes
tim = Timer(0, mode=Timer.PERIODIC, width=32)
ch = tim.channel(Timer.A | Timer.B, period=5000000)

# check for memory leaks...
for i in range(1000):
    tim = Timer(0, mode=Timer.PERIODIC)
    ch = tim.channel(Timer.A, freq=5)

# next ones must fail
try:
    tim = Timer(0, mode=12)
except:
    print('Exception')

try:
    tim = Timer(4, mode=Timer.ONE_SHOT)
except:
    print('Exception')

try:
    tim = Timer(0, mode=Timer.PWM, width=32)
except:
    print('Exception')

tim = Timer(0, mode=Timer.PWM)

try:
    ch = tim.channel(TIMER_A | TIMER_B, freq=10)
except:
    print('Exception')

try:
    ch = tim.channel(TIMER_A, freq=4)
except:
    print('Exception')