#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// Define SPI Pins for NUCLEO-C031C6
#define TFT_DC PA9
#define TFT_CS PA10
#define TFT_RST PA8 // Reset pin (if needed)
#define BUTTON_PIN PB6 // Change if needed
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
unsigned long lastTime_CH1 = 0;
unsigned long lastTime_CH2 = 0;
unsigned long period_CH1 = 1000;
unsigned long period_CH2 = 1000;
uint16_t frequency_CH1 = 1;
uint16_t frequency_CH2 = 1;
bool paused = false;
bool lastButtonState = HIGH;
unsigned long lastButtonPressTime = 0;
const unsigned long debounceDelay = 50;
// Adaptive Bandwidth Parameters
uint16_t bandwidthFactor = 100; // Fixed-point (scaled by 100)
void splashscreen() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 80);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print("Karnil Semiconductors");
delay(2000);
}
void drawGrid() {
for (int x = 20; x < 240; x += 20) {
tft.drawFastVLine(x, 0, 240, ILI9341_WHITE);
}
for (int y = 20; y < 240; y += 20) {
tft.drawFastHLine(0, y, 240, ILI9341_WHITE);
}
}
void pausePlay() {
bool buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW && lastButtonState == HIGH) {
delay(50);
paused = !paused;
}
lastButtonState = buttonState;
}
void updateBandwidth(uint16_t signalStrength) {
bandwidthFactor = 100 / (1 + ((abs(signalStrength - 2048)) / 512));
}
void setup() {
SPI.begin();
tft.begin();
tft.setRotation(3);
splashscreen();
drawGrid();
pinMode(PA0, INPUT_ANALOG);
pinMode(PA1, INPUT_ANALOG);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
pausePlay();
if (paused) return;
static int y = 1;
static int prev_x_CH1 = 60, prev_y_CH1 = 1;
static int prev_x_CH2 = 180, prev_y_CH2 = 1;
uint16_t raw_ch1 = analogRead(PA0);
uint16_t raw_ch2 = analogRead(PA1);
updateBandwidth(raw_ch1);
updateBandwidth(raw_ch2);
unsigned long currentTime = millis();
if (raw_ch1 > 3000 && millis() - lastTime_CH1 > 5) {
period_CH1 = currentTime - lastTime_CH1;
lastTime_CH1 = currentTime;
frequency_CH1 = 100000 / period_CH1; // Scaled up by 100 for fixed-point math
}
if (raw_ch2 > 3000 && millis() - lastTime_CH2 > 5) {
period_CH2 = currentTime - lastTime_CH2;
lastTime_CH2 = currentTime;
frequency_CH2 = 100000 / period_CH2;
}
uint16_t scaleFactor_CH1 = 5000 / (frequency_CH1 * bandwidthFactor); // Fixed-point scaling
uint16_t scaleFactor_CH2 = 5000 / (frequency_CH2 * bandwidthFactor);
int x_CH1 = map(raw_ch1, 0, 4095, 60 - scaleFactor_CH1, 60 + scaleFactor_CH1);
int x_CH2 = map(raw_ch2, 0, 4095, 180 - scaleFactor_CH2, 180 + scaleFactor_CH2);
if (y > 1) {
tft.drawLine(prev_x_CH1, prev_y_CH1, x_CH1, y, ILI9341_RED);
tft.drawLine(prev_x_CH2, prev_y_CH2, x_CH2, y, ILI9341_GREEN);
}
prev_x_CH1 = x_CH1;
prev_y_CH1 = y;
prev_x_CH2 = x_CH2;
prev_y_CH2 = y;
y++;
if (y >= 238) {
y = 1;
tft.fillRect(1, 1, 239, 236, ILI9341_BLACK);
drawGrid();
}
delay(10);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6