May 1, 2026
param (
[Parameter(Mandatory=$true)]
[string]$InputFile,
[Parameter(Mandatory=$false)]
[string]$OutputFile = "output_codes.h"
)
# Ensure the input file exists
if (-not (Test-Path $InputFile)) {
Write-Error "Input file '$InputFile' not found."
exit
}
# Read lines, ignoring empty ones
$lines = Get-Content $InputFile | Where-Object { $_.Trim() -ne "" }
$allCodesNames = @()
$codeLengths = @()
$outputContent = @()
$outputContent += "// Define each code as a separate array in PROGMEM"
$outputContent += ""
$index = 0
foreach ($line in $lines) {
# Extract only the numbers, sanitizing any weird spacing or trailing commas
$matches = [regex]::Matches($line, '\d+')
$numbers = $matches | ForEach-Object { $_.Value }
# Get the length
$length = $numbers.Count
if ($length -eq 0) { continue } # Skip lines with no numbers
$codeLengths += $length
$arrayName = "c$index"
$allCodesNames += $arrayName
# Format into comma-separated string
$formattedNumbers = $numbers -join ", "
# Append the array declaration
$outputContent += "const uint16_t ${arrayName}[] PROGMEM = {$formattedNumbers};"
$outputContent += ""
$index++
}
$outputContent += "// A master table of all codes and their lengths"
$outputContent += ""
# Format master pointer array
$allCodesStr = $allCodesNames -join ", "
$outputContent += "const uint16_t* const allCodes[] PROGMEM = {$allCodesStr};"
$outputContent += ""
# Format lengths array
$lengthsStr = $codeLengths -join ", "
$outputContent += "const uint8_t codeLengths[] = {$lengthsStr}; // Number of values in each array"
$outputContent += ""
# Format total count
$outputContent += "const uint8_t totalCodes = $index;"
# Write to the output file
$outputContent | Set-Content $OutputFile
Write-Host "Successfully generated formatted codes in '$OutputFile'!" -ForegroundColor Green