#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <Fonts/FreeMono9pt7b.h>
#include "myfont.h"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define i2c_Address 0x3c
#define SDA_PIN 21
#define SCL_PIN 22
Adafruit_SH1106G lcd = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // defining the oled display (just the name is lcd ,used one is oled)
void setup() {
Serial.begin(115200);
//Wire.begin(SDA_PIN, SCL_PIN);
// if (!lcd.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// Serial.println(F("SSD1306 allocation failed"));
// while (1);
// }
lcd.begin(i2c_Address, true);
lcd.clearDisplay();
// lcd.setFont(&FreeMono9pt7b);
lcd.setFont(&Roboto_Light_14);
lcd.setTextSize(1);
lcd.setTextColor(SH110X_WHITE);
lcd.setCursor(10, 25);
lcd.print("Hello, World!");
lcd.display();
}
void display_waveforms(long freq, int level, int attenuation, int wave_number, bool toggle) {
freq = freq % 1000000000; // making frequency<=1000000000 if needed
wave_number = wave_number % 5; // wave_number defines the wave currently being used
lcd.clearDisplay(); // to clear previous screen
lcd.setCursor(0, 2 * 15);
lcd.print("Lvl:");
lcd.setCursor(0, 3 * 15);
lcd.print("Fq:");
String unit = "Hz";
long dec = 0;
if (freq >= 1000 && freq < 1000000) {
dec = freq % 1000;
freq = freq / 1000;
unit = "K" + unit; //setting units for frequency as Hz or KHz or MHz
} else if (freq >= 1000000 && freq < 1000000000) {
dec = freq % 1000000;
dec = dec / 1000;
freq = freq / 1000000;
unit = "M" + unit;
}
lcd.setCursor(6 * 8, 2 * 15);
if (level>=1000)
{
lcd.print(level/1000);
lcd.print(".");
lcd.print((level%1000)/100);
lcd.print((level%100)/10);
}
else
{
lcd.print(level);
lcd.print("m");
}
//lcd.print(level);
lcd.print("Vp-p"); // V p-p
lcd.setCursor(3 * 8, 3 * 15); //location for print
lcd.print((freq / 100) % 10);
lcd.print(((freq % 100) / 10) % 10); // making format of fff.fff units for frequency
lcd.print(freq % 10);
lcd.print(".");
lcd.print((dec / 100) % 10);
lcd.print(((dec % 100) / 10) % 10);
lcd.print(dec % 10);
//lcd.setCursor(9 * 8, 3 * 15);
lcd.print(unit);
lcd.setCursor(0, 1 * 15); // setCursor(x,y) sets cursor to the given x,y in the display (in this case 128*64 oled)
lcd.print("Synthesized FGN"); // print(variable) prints the variable to the current cursor position
lcd.setCursor(0, 4 * 15);
lcd.print("Wave: ");
lcd.setCursor(6 * 8, 4 * 15);
const char* wave_types[] = {"Sine ", "Sqr ", "Ramp", "Pulse", "Tri "}; //to show the wave according to the wave number
lcd.print(wave_types[wave_number]);
lcd.print(attenuation);
lcd.print("dB");
lcd.display(); // display the changes
}
int l=33;
int w=0;
int att=0;
long f=2100;
int s=0;
bool toggle = false;
String data="0010110";
void loop()
{
f+=20000;
delay(500);
display_waveforms(f,l,att,w, toggle);
toggle = !toggle;
l+=100;
w=(w+1)%5;
att++;
s=1-s;
}