-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVFO_stm32_audio.ino
187 lines (165 loc) · 5.13 KB
/
VFO_stm32_audio.ino
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Wire.h>
#include <si5351.h>
Si5351 si5351;
#define TFT_CS PA4
#define TFT_CLK PA5
#define TFT_MISO PA6
#define TFT_MOSI PA7
#define TFT_RST PB0
#define TFT_DC PB1
#define AUDIO_IN PA0
#define SCALE_X_OFFSET 20
#define AUDIO_BAR_Y_OFFSET 70 // Ajusta la posición vertical de la barra de audio
#define SCALE_WIDTH 200
#define SCALE_HEIGHT 20
#define NUM_SEGMENTS 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
unsigned long currentFrequency = 13990000;
bool change = false;
int segment = 0; // Variable para el número de segmentos
String oldFrequency_string;
unsigned long lastAudioUpdate = 0; // Última actualización de la barra de audio
unsigned long audioUpdateInterval = 50; // Intervalo de actualización en milisegundos (10 veces por segundo)
String shz;
String skhz;
String smhz;
String frequency_string;
void setup() {
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
pinMode(PB12, INPUT_PULLUP);
pinMode(PB13, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PB12), clock1_ISR, FALLING);
attachInterrupt(digitalPinToInterrupt(PB13), clock1_ISR2, FALLING);
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
////////////////////////Setting up I2C Comms //////////////////////////
Wire.begin();
Wire.setClock(400000);
//////////////////////////////////////////////////////////////////////
si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0);
si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA);
si5351.output_enable(SI5351_CLK0, 1);
drawScale();
}
void clock1_ISR() {
bool dir = digitalRead(PB13);
if (dir == true) {
currentFrequency += 10;
}
if (dir == false) {
currentFrequency -= 10;
}
change = true;
}
void clock1_ISR2() {
bool dir = digitalRead(PB12);
if (dir == true) {
currentFrequency -= 10;
}
if (dir == false) {
currentFrequency += 10;
}
change = true;
}
void drawScale() {
tft.drawRect(SCALE_X_OFFSET - 2, AUDIO_BAR_Y_OFFSET - 2, SCALE_WIDTH + 4, SCALE_HEIGHT + 4, ILI9341_WHITE);
int segmentWidth = SCALE_WIDTH / NUM_SEGMENTS;
int scaleValue = 0;
tft.drawRect(SCALE_X_OFFSET, AUDIO_BAR_Y_OFFSET, SCALE_WIDTH, SCALE_HEIGHT, ILI9341_WHITE);
for (int i = 0; i <= NUM_SEGMENTS; i++) {
int x = SCALE_X_OFFSET + i * segmentWidth;
tft.drawFastVLine(x, AUDIO_BAR_Y_OFFSET - 5, SCALE_HEIGHT + 10, ILI9341_WHITE);
tft.setCursor(x - 10, AUDIO_BAR_Y_OFFSET + SCALE_HEIGHT + 5);
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE);
tft.print("S" + String(scaleValue));
scaleValue += 9 / NUM_SEGMENTS;
}
}
void loop() {
if (change == true) {
clock_update();
}
unsigned long currentMillis = millis();
if (currentMillis - lastAudioUpdate >= audioUpdateInterval) {
audio_peek(); // Actualiza la barra de medición de audio
lastAudioUpdate = currentMillis;
}
}
void clock_update() {
f_string();
si5351.set_freq((currentFrequency ) * SI5351_FREQ_MULT, SI5351_CLK0);
// tft.fillRect(1, 30, 290, 28, ILI9341_BLACK);
tft.setCursor(20, 30);
tft.setTextSize(4);
tft.setTextColor(ILI9341_BLACK);
tft.print(oldFrequency_string);
tft.setCursor(20, 30);
tft.setTextSize(4);
tft.setTextColor(ILI9341_GREEN);
tft.print(frequency_string);
oldFrequency_string = frequency_string;
change = false;
}
void audio_peek() {
int audioValue = analogRead(AUDIO_IN);
int newSegment = audioValue / (4095 / NUM_SEGMENTS);
if (newSegment != segment) {
tft.fillRect(SCALE_X_OFFSET, AUDIO_BAR_Y_OFFSET, SCALE_WIDTH, SCALE_HEIGHT, ILI9341_BLACK);
int segmentWidth = SCALE_WIDTH / NUM_SEGMENTS;
segment = newSegment;
for (int i = 0; i <= segment; i++) {
int x = SCALE_X_OFFSET + i * segmentWidth;
if (i >= NUM_SEGMENTS - 2) {
tft.fillRect(x, AUDIO_BAR_Y_OFFSET, segmentWidth, SCALE_HEIGHT, ILI9341_RED);
} else if (i == 6 || i == 7) {
tft.fillRect(x, AUDIO_BAR_Y_OFFSET, segmentWidth, SCALE_HEIGHT, ILI9341_YELLOW);
} else {
tft.fillRect(x, AUDIO_BAR_Y_OFFSET, segmentWidth, SCALE_HEIGHT, ILI9341_GREEN);
}
}
}
}
void f_string() {
int mhz = currentFrequency / 1000000;
int khz = (currentFrequency % 1000000) / 1000;
int hz = currentFrequency - ((mhz * 1000000) + (khz * 1000));
// skhz = String(khz);
smhz = String(mhz);
if (mhz < 10 && mhz >= 1) {
smhz = " " + String(mhz);
}
//############## HZ
if (hz >= 100) {
shz = "," + String(hz);
}
if (hz < 100 && hz > 10) {
shz = ",0" + String(hz);
}
if (hz < 10 && hz >= 1) {
shz = ",00" + String(hz);
}
if (hz == 0) {
shz = ",000";
}
//#################### KHZ
if (khz >= 100) {
skhz = "," + String(khz);
}
if (khz < 100 && khz > 10) {
skhz = ",0" + String(khz);
}
if (khz < 10 and khz >= 1) {
skhz = ",00" + String(khz);
}
if (khz == 0) {
skhz = ",000";
}
frequency_string = smhz + skhz + shz;
}