Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2022 Alec Delaney
3#
4# SPDX-License-Identifier: Unlicense
5
6from circuitpython_functools import cache, clear_caches
7
8
9class MathClass:
10 def __init__(self, initial_value):
11 self.value = initial_value
12
13 @cache
14 def add(self, a, b, *, c=1, d=2):
15 return self.value + a + b + c + d
16
17 @cache
18 def subtract_from(self, input_value):
19 return self.value - input_value
20
21
22math_instance = MathClass(5)
23
24print(math_instance.add(1, 1, c=2, d=3))
25print(math_instance.add(1, 1, c=2, d=3)) # Repeat call, will use cached result
26print(math_instance.add(1, 0, c=3, d=4))
27print(math_instance.subtract_from(42))
28print(math_instance.add(1, b=1, c=2, d=3))
29print(math_instance.add(1, b=0, c=2, d=3))
30
31# Clear all the caches so we don't use too much memory
32clear_caches()
33
34print(
35 math_instance.add(1, 0, c=3, d=4)
36) # Cache was cleared, so this is caclulated again
37print(math_instance.subtract_from(18))