Skip to content

Instantly share code, notes, and snippets.

@ganzuul
Created July 20, 2017 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganzuul/dcf744bca43f450f0115bd971b7f5ab7 to your computer and use it in GitHub Desktop.
Save ganzuul/dcf744bca43f450f0115bd971b7f5ab7 to your computer and use it in GitHub Desktop.
WIP UI for setting a large number e.g. a frequency using only clockwise and CCW movements of a rotary encoder. ESP8266 with Arduino SDK.
struct TwoNibbles
{
byte v1:4;
byte v2:4;
};
union Nibble
{
TwoNibbles nibble;
byte Nibble[sizeof(TwoNibbles)];
};
Nibble wibble;
/*
Frequency synth with rotary encoder and OLED
*/
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <MD_REncoder.h>
#include <si5351.h>
#include "a.h"
Si5351 si5351;
// set up encoder object
MD_REncoder R = MD_REncoder(0, 2);
//set up display
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, 0, 5, 4);
//GloboVars
uint32_t max_v = 1000000000;
int32_t f = 1; // 1 Hz
uint32_t old_f = 0;
uint32_t v = 1;
int8_t y = 0;
void setup()
{
// Start serial and initialize the Si5351
Serial.begin(57600);
si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0);
// Set CLK0 to output 14 MHz
si5351.set_freq(1400000000ULL, SI5351_CLK0);
// Set CLK1 to output 175 MHz
si5351.set_ms_source(SI5351_CLK1, SI5351_PLLB);
si5351.set_freq_manual(17500000000ULL, 70000000000ULL, SI5351_CLK1);
// Query a status update and wait a bit to let the Si5351 populate the
// status flags correctly.
si5351.update_status();
delay(500);
// OLED
u8g2.setI2CAddress(0x78);
u8g2.begin();
Serial.begin(9600);
u8g2.setFont(u8g2_font_8x13_mr);
u8g2.setDrawColor(1);
u8g2.setFontMode(1);
//Rotary
R.begin();
Serial.print("Up! Up! Away!\n");
//MORE GloboVars??
wibble.nibble.v1 = 0b0000;
wibble.nibble.v2 = 0b0101;
}
void set_freq(uint32_t frq)
{
}
bool wibbler(uint8_t dir)
{
// Wiggle-detector
wibble.nibble.v1 = wibble.nibble.v1 << 1 ;
if (dir == DIR_CW)
{
wibble.nibble.v1 = wibble.nibble.v1 | 0b0001;
}
// Serial.print(wibble.nibble.v1,2);
// Serial.print("\n");
if (wibble.nibble.v1 == wibble.nibble.v2)
{
Serial.print("Wiggling!\n");
return true;
}
return false;
}
void disp()
{
uint8_t len_v = 0;
String cursor = String(v);
len_v = cursor.length();
len_v = len_v * 8;
cursor = "_";
uint8_t len_f = 0;
String freq = String(f);
len_f = freq.length();
len_f = len_f * 8;
u8g2.firstPage();
do {
u8g2.setCursor(104 - len_f, 10);
u8g2.print(f);
u8g2.setCursor(104 - len_v, 11);
u8g2.print(cursor);
u8g2.setCursor(106, 10);
u8g2.print("Hz");
u8g2.setCursor(8, 32);
u8g2.print(y);
yield();
} while ( u8g2.nextPage() );
}
void loop()
{
uint8_t x = R.read();
int32_t test = 0;
int32_t test2 = 0;
old_f = f;
if (x)
{
//TO-DO: Make wiggle modal
if (x == DIR_CW)
{
if (y == 9 && v < 1000000000)
{
v = v * 10;
y = 0;
}
if (y < 10)
{
y += 1;
}
}
if (wibbler(x))
{
if (v >= 10)
{
v = v/10;
} else
{
v = 1;
}
y = 1;
}
test = f+v;
test2 = f-v;
if ((test >= 0 && x == DIR_CW) || (test2 >= 0 && x != DIR_CW))
{
x == DIR_CW ? f=f+v : f=f-v;
}
}
if (old_f != f)
{
set_freq(f);
disp();
}
}
@ganzuul
Copy link
Author

ganzuul commented Jul 20, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment