Skip to content

Instantly share code, notes, and snippets.

@JarrettR
Last active May 11, 2024 00:00
Show Gist options
  • Save JarrettR/5a7e9dd47758e778c563967026ef297f to your computer and use it in GitHub Desktop.
Save JarrettR/5a7e9dd47758e778c563967026ef297f to your computer and use it in GitHub Desktop.
header = '''
#pragma once
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/rmt.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "esp_log.h"
const rmt_item32_t on_key[] = {
'''
footer = ''' {{{ 0, 1, 0, 0 }}}
};
'''
def check_bounds(time_signals):
short_uint_time_signals = []
for times in time_signals:
# Edge case: both 0 and 1 pulses in the same frame above 0x7FFF
if times[0] > 0x7FFF and times[1] > 0x7FFF:
print("Error: Write edge case correction for uint16 overflow")
while times[0] > 0x7FFF:
print(times)
times[0] -= 0x7FFF
short_uint_time_signals.append([0x7FFF,times[1]])
times[1] = 1
while times[1] > 0x7FFF:
print(times)
times[1] -= 0x7FFF
short_uint_time_signals.append([times[0],0x7FFF])
times[0] = 1
short_uint_time_signals.append(times)
return short_uint_time_signals
def convert_vcd_to_time_signal(input_file, output_file):
with open(input_file, 'r') as f:
lines = f.readlines()
time_signal = []
lines = lines[19:-1]
for line in lines:
parts = line.split()
time_ns = int(parts[0][1:])
value = int(parts[1][0])
time_us = time_ns // 25 # Not actually using microseconds anymore
time_signal.append((time_us, 1 - value)) # I measured the signal inverted, so it needs to be flipped
time_signal.append((time_us + 5, value))
time_signal_pairs = []
prev_time = 0
current_pair = [0,0]
for time_us, value in time_signal:
if prev_time > 0:
if value == 0:
current_pair[0] = time_us - prev_time
else:
current_pair[1] = time_us - prev_time
time_signal_pairs.append(current_pair.copy())
prev_time = time_us
time_signal_pairs = check_bounds(time_signal_pairs)
with open(output_file, 'w') as f:
f.write(header)
for pair in time_signal_pairs:
f.write(" {{{{{{ {},{},{},{} }}}}}},\n".format(pair[0], 1, pair[1], 0))
f.write(footer)
# Example usage:
input_file = "ir-on.vcd"
output_file = "output.txt"
convert_vcd_to_time_signal(input_file, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment