#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <Adafruit_Si5351.h>
#include "si5351.h"
#include "stdlib.h"
#include "hardware/adc.h"
#include "hardware/gpio.h"
//#include "pgmspace.h"
// User Preferences
#define IF 455
#define BAND_INIT 2
#define XT_CAL_F 33000
#define S_GAIN 303
#define tunestep 1
#define band 2
#define rx_tx 0
#define adc 26
// Encoder Pins
#define ENCODER_PIN_A 7 // GPIO2
#define ENCODER_PIN_B 8 // GPIO3
#define ENCODER_BUTTON 9 // GPIO3
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
//Adafruit_Si5351 si5351 = Adafruit_Si5351(0x60);
Si5351 si5351(0x60);
unsigned long freq, freqold, fstep;
long interfreq = IF, interfreqold = 0;
long cal = XT_CAL_F;
unsigned int smval;
long encoder = 0;
byte stp, n = 1;
byte count, x, xo;
bool sts = 0;
unsigned int period = 100;
unsigned long time_now = 0;
void setup() {
// Initialize Pico ADC
adc_init();
adc_gpio_init(adc);
// Initialize I2C
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.display();
// Set up GPIO Pins
gpio_init(tunestep);
gpio_set_dir(tunestep, GPIO_IN);
gpio_pull_up(tunestep);
gpio_init(band);
gpio_set_dir(band, GPIO_IN);
gpio_pull_up(band);
gpio_init(rx_tx);
gpio_set_dir(rx_tx, GPIO_IN);
gpio_pull_up(rx_tx);
si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0);
si5351.set_correction(cal, SI5351_PLL_INPUT_XO);
si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA);
si5351.output_enable(SI5351_CLK0, 1);
si5351.output_enable(SI5351_CLK1, 0);
si5351.output_enable(SI5351_CLK2, 0);
count = BAND_INIT;
bandpresets();
stp = 4;
setstep();
}
void loop() {
long newPosition = encoderRead(); // Custom function for encoder read on Pico
if (newPosition != encoder) {
if (newPosition > encoder) {
set_frequency(1); // Clockwise
} else {
set_frequency(-1); // Counter-clockwise
}
encoder = newPosition;
}
if (freqold != freq) {
time_now = to_ms_since_boot(get_absolute_time());
tunegen();
freqold = freq;
}
if (interfreqold != interfreq) {
time_now = to_ms_since_boot(get_absolute_time());
tunegen();
interfreqold = interfreq;
}
if (xo != x) {
time_now = to_ms_since_boot(get_absolute_time());
xo = x;
}
if (gpio_get(tunestep) == 0) {
time_now = to_ms_since_boot(get_absolute_time()) + 300;
setstep();
sleep_ms(300);
}
if (gpio_get(band) == 0) {
time_now = to_ms_since_boot(get_absolute_time()) + 300;
inc_preset();
sleep_ms(300);
}
if (gpio_get(rx_tx) == 0) {
time_now = to_ms_since_boot(get_absolute_time()) + 300;
sts = 1; // TX mode
} else {
sts = 0; // RX mode
}
if ((time_now + period) > to_ms_since_boot(get_absolute_time())) {
displayfreq();
layout();
}
sgnalread();
}
// Other functions remain unchanged with slight modifications to read GPIOs, use Pico's `sleep_ms` instead of `delay`, and handle ADC differently.
void encoderRead() {
// Implement the encoder reading logic using GPIO on Pico
}
// Other functions remain unchanged, adapted for Pico-specific GPIO and ADC calls.