19
19
20
20
import configuration
21
21
import math
22
+ from random import random
22
23
import time
23
24
24
25
import _thread
@@ -39,14 +40,18 @@ class WaveGenerator:
39
40
WAVE_SHAPE_TRIANGLE = 2
40
41
WAVE_SHAPE_SAW = 3
41
42
WAVE_SHAPE_RAMP = 4
42
- NUM_WAVE_SHAPES = 5
43
+ WAVE_SHAPE_STEP_RANDOM = 5
44
+ WAVE_SHAPE_SMOOTH_RANDOM = 6
45
+ NUM_WAVE_SHAPES = 7
43
46
44
47
WAVE_SHAPES_NAMES = [
45
48
"Sine" ,
46
49
"Square" ,
47
50
"Triangle" ,
48
51
"Saw" ,
49
52
"Ramp" ,
53
+ "Step_Random" ,
54
+ "Smooth_Random" ,
50
55
]
51
56
52
57
WAVE_SHAPES_NAMES_TO_SHAPES = {
@@ -55,6 +60,8 @@ class WaveGenerator:
55
60
"triangle" : WAVE_SHAPE_TRIANGLE ,
56
61
"saw" : WAVE_SHAPE_SAW ,
57
62
"ramp" : WAVE_SHAPE_RAMP ,
63
+ "step_random" : WAVE_SHAPE_STEP_RANDOM ,
64
+ "smooth_random" : WAVE_SHAPE_SMOOTH_RANDOM ,
58
65
}
59
66
60
67
## 12x12 pixel images of the wave shapes
@@ -64,6 +71,8 @@ class WaveGenerator:
64
71
FrameBuffer (bytearray (b'\x06 \x00 \x06 \x00 \t \x00 \t \x00 \x10 \x80 \x10 \x80 @ @@ @ \x80 \x10 \x80 \x10 ' ), 12 , 12 , MONO_HLSB ), # TRIANGLE
65
72
FrameBuffer (bytearray (b'\x80 \x00 \xc0 \x00 \xa0 \x00 \x90 \x00 \x88 \x00 \x84 \x00 \x82 \x00 \x81 \x00 \x80 \x80 \x80 @\x80 \x80 \x10 ' ), 12 , 12 , MONO_HLSB ), # SAW
66
73
FrameBuffer (bytearray (b'\x00 \x10 \x00 0\x00 P\x00 \x90 \x01 \x10 \x02 \x10 \x04 \x10 \x08 \x10 \x10 \x10 \x10 @\x10 \x80 \x10 ' ), 12 , 12 , MONO_HLSB ), # RAMP
74
+ FrameBuffer (bytearray (b'\x00 \xe0 \x00 \xa0 \x00 \xa0 \x00 \xa0 <\xa0 $\xa0 $\xa0 \xe4 \xb0 \x04 \x80 \x04 \x80 \x04 \x80 \x07 \x80 ' ), 12 , 12 , MONO_HLSB ), # STEP_RANDOM
75
+ FrameBuffer (bytearray (b'\x00 `\x00 P\x00 P\x08 P\x14 P$PD\x90 \x82 \x80 \x02 \x80 \x02 \x80 \x02 \x80 \x01 \x80 ' ), 12 , 12 , MONO_HLSB ), # SMOOTH_RANDOM
67
76
]
68
77
69
78
IMAGE_SIZE = (12 , 12 )
@@ -79,6 +88,9 @@ def __init__(self, cv_output):
79
88
self .cycle_ticks = 1000
80
89
self .current_tick = 0
81
90
91
+ self .prev_random_goal = random () * MAX_OUTPUT_VOLTAGE
92
+ self .random_goal = random () * MAX_OUTPUT_VOLTAGE
93
+
82
94
def reset (self ):
83
95
"""Reset the wave to the beginning
84
96
"""
@@ -118,12 +130,19 @@ def tick(self):
118
130
volts = MAX_OUTPUT_VOLTAGE - self .current_tick / self .cycle_ticks * MAX_OUTPUT_VOLTAGE
119
131
elif self .shape == self .WAVE_SHAPE_RAMP :
120
132
volts = self .current_tick / self .cycle_ticks * MAX_OUTPUT_VOLTAGE
133
+ elif self .shape == self .WAVE_SHAPE_STEP_RANDOM :
134
+ volts = self .random_goal
135
+ elif self .shape == self .WAVE_SHAPE_SMOOTH_RANDOM :
136
+ slope = (self .random_goal - self .prev_random_goal ) / self .cycle_ticks
137
+ volts = slope * self .current_tick + self .prev_random_goal
121
138
else :
122
139
volts = 0
123
140
124
141
self .current_tick = self .current_tick + 1
125
142
if self .current_tick >= self .cycle_ticks :
126
143
self .current_tick = 0
144
+ self .prev_random_goal = self .random_goal
145
+ self .random_goal = random () * MAX_OUTPUT_VOLTAGE
127
146
128
147
self .cv_output .voltage (volts )
129
148
return volts
0 commit comments