Last active
July 6, 2024 15:52
-
-
Save jrelo/824ca5eb5e2964028930a75fc842ce43 to your computer and use it in GitHub Desktop.
shitty arduino IR scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
OLED: | |
GND -> GND | |
VCC -> 5v | |
SCK -> A5 | |
SDA -> A4 | |
IR Receiver: | |
GND -> GND | |
VCC -> 5v | |
OUT -> 11 | |
*/ | |
#include <IRremote.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#define SCREEN_WIDTH 128 | |
#define SCREEN_HEIGHT 32 | |
#define OLED_RESET -1 | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
int RECV_PIN = 11; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
void setup() { | |
Serial.begin(9600); | |
irrecv.enableIRIn(); | |
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { | |
Serial.println(F("SSD1306 failed")); | |
for (;;); | |
} | |
display.display(); | |
delay(1000); | |
display.clearDisplay(); | |
display.setTextSize(1); | |
display.setTextColor(SSD1306_WHITE); | |
display.display(); | |
} | |
void loop() { | |
if (irrecv.decode(&results)) { | |
displayWaveform(results.rawbuf, results.rawlen); | |
irrecv.resume(); | |
} | |
delay(10); | |
} | |
void displayWaveform(uint16_t* rawbuf, int rawlen) { | |
display.clearDisplay(); | |
display.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE); | |
int totalDuration = 0; | |
for (int i = 0; i < rawlen; i++) { | |
totalDuration += rawbuf[i]; | |
} | |
float scaleFactor = (float)totalDuration / (SCREEN_WIDTH * 4); | |
int rowHeight = SCREEN_HEIGHT / 4; | |
int midY = rowHeight / 2; | |
int x = 1; | |
int yOffset = 0; | |
unsigned long accumulatedTime = 0; | |
for (int i = 0; i < rawlen && yOffset < SCREEN_HEIGHT; i++) { | |
accumulatedTime += rawbuf[i]; | |
int newX = (int)(accumulatedTime / scaleFactor) % SCREEN_WIDTH; | |
if (newX >= SCREEN_WIDTH) { | |
newX = SCREEN_WIDTH - 1; | |
} | |
if (newX < x) { // move to next row if at end | |
yOffset += rowHeight; | |
x = 1; | |
} | |
if (i % 2 == 0) { | |
// high pulse | |
display.drawLine(x, yOffset + midY, x, yOffset, SSD1306_WHITE); | |
} else { | |
// low pulse | |
display.drawLine(x, yOffset + midY, x, yOffset + rowHeight - 1, SSD1306_WHITE); | |
} | |
x = newX + 1; | |
} | |
display.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment