commit 98cf0a42d2614dd2726542e8ce941e94a894a18e Author: n loewen Date: Wed Aug 9 18:06:34 2023 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d0ee45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +.DS_Store \ No newline at end of file diff --git a/diagram.jpg b/diagram.jpg new file mode 100644 index 0000000..f295c6c Binary files /dev/null and b/diagram.jpg differ diff --git a/example--fill-display-program.txt b/example--fill-display-program.txt new file mode 100644 index 0000000..2f66a26 --- /dev/null +++ b/example--fill-display-program.txt @@ -0,0 +1 @@ +0000001111111110000001010000000100000101000000010000010100000001000001110000000100000111000000010000001100000001000000010000000000000011111111100000011000000000000001100000000000000110000000000000100000000000000010000000000000000000000000000000000000000000 \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..55206d6 --- /dev/null +++ b/readme.md @@ -0,0 +1,44 @@ +# Create square wave patterns from binary strings + +This script turns a binary string into a square-wave pattern. + +It is intended for creating embroidery patterns based on programs for my paper computer. + +The output is formatted as if the data is being sent over an 8-bit data bus — see the [reference image below](#format-reference-image). + +## Use + +``` +./square-wave-from-binary.js bin.txt +``` + +The input must be a text file containing nothing but 0s and 1s (no spaces, newlines, etc). It must be an even number of 8-bit bytes in length. + +## Example output + +``` +01234567 01234567 01234567 01234567 +▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ ▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ + +▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ ▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ + +▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ ▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ + +▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ ▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ + +▄▀▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ ▄▀▄▄▄▄▄▄ ▀▄▀▄▄▄▄▄ + +▄▀▀▄▀▄▀▄ ▀▄▀▄▄▄▄▄ ▄▀▀▄▀▄▀▄ ▄▄▄▄▄▄▄▄ + +▀▀▄▄▄▄▄▄ ▀▄▀▄▀▄▄▄ ▀▀▀▄▀▄▀▄ ▄▄▄▄▄▄▄▄ + +▀▄▀▀▀▀▀▀ ▀▀▀▀▀▀▀▄ ▀▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄ + +01234567 01234567 01234567 01234567 +``` + +## Format reference image + +![](https://www.nutsvolts.com/uploads/wygwam/NV_1007_Martin_Figure09.jpg) + +(From [Nuts and Volts](https://www.nutsvolts.com/uploads/wygwam/NV_1007_Martin_Figure09.jpg).) diff --git a/square-wave-from-binary.js b/square-wave-from-binary.js new file mode 100644 index 0000000..c4817f9 --- /dev/null +++ b/square-wave-from-binary.js @@ -0,0 +1,51 @@ +#!/usr/bin/env node + +// Input file must contain a binary string with no spaces, linebreaks, etc. + +const fs = require('fs'); +const filename = process.argv[2]; +const inputFile_str = fs.readFileSync(filename, 'utf8'); + +const create_square_wave_string = (s) => { + s = s.replace(/1/g, '▀'); + return s = s.replace(/0/g, '▄'); +} + +const number_hex_string = (quantity) => { + let output = ''; + let current_digit = 0; + for (let i = 0; i < quantity; i++) { + current_digit = i % 8; + output = output + current_digit; + } + return output; +} + +const binary_string_to_bus_strings = (input) => { + let output = ['','','','','','','','']; + let input_bytes = input.match(/.{1,8}/g); + + input_bytes.forEach( (byte) => { + let byte_array = byte.split(''); + for (let i = 0; i < 8; i++) { + output[i] = output[i] + byte_array[i]; + } + }); + + + // Print output + let nums = number_hex_string(output[0].length); + let spaced_nums = nums.match(/.{1,8}/g).join(' '); + console.log(); + console.log(spaced_nums); + + output.forEach( (s) => { + let spaced = s.match(/.{1,8}/g).join(' '); + console.log(create_square_wave_string(spaced)); + console.log(); + }); + console.log(spaced_nums); + console.log(); +} + +binary_string_to_bus_strings(inputFile_str); \ No newline at end of file