/**
* Pi Pico PIO driving a 4-digit seven segment display example.
*
* Copyright (C) 2021, Uri Shaked
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/irq.h"
#include "hardware/timer.h"
#define IR_PIN 15 // Change this to the GPIO pin you're using for the IR receiver
volatile uint32_t ir_code = 0;
volatile bool ir_code_ready = false;
volatile uint32_t last_edge_time = 0;
volatile int bit_count = 0;
volatile bool header_received = false;
void gpio_callback(uint gpio, uint32_t events) {
uint32_t current_time = time_us_32();
uint32_t duration = current_time - last_edge_time;
last_edge_time = current_time;
Serial1.printf("Duration: %i\n", duration);
if (events & GPIO_IRQ_EDGE_FALL) {
if (!header_received) {
if (duration > 4400 && duration < 5500) {
// Start bit detected
header_received = true;
ir_code = 0;
bit_count = 0;
}
} else if (bit_count < 32) {
ir_code <<= 1;
if (duration > 1600) {
ir_code |= 1;
}
bit_count++;
if (bit_count == 32) {
ir_code_ready = true;
header_received = false;
}
}
}
}
void setup() {
Serial1.begin(115200);
Serial1.println("Raspberry Pi Pico PIO 7-Segment Example\n");
gpio_init(IR_PIN);
gpio_set_dir(IR_PIN, GPIO_IN);
gpio_pull_up(IR_PIN);
gpio_set_irq_enabled_with_callback(IR_PIN, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &gpio_callback);
}
int i = 0;
void loop() {
sleep_ms(50); // Simulating other tasks
if (ir_code_ready) {
Serial1.printf("Received IR code: 0x%08lX\n", (ir_code & 0x0000FF00) );
if ((ir_code & 0x0000FF00) != (~(ir_code << 8) & 0x0000FF00)) {
Serial1.printf("Error Detected! 0x%08lX 0x%08lX\n", (ir_code & 0x0000FF00), (~(ir_code << 8) & 0x0000FF00));
}
ir_code_ready = false;
}
}