Initial commit
This commit is contained in:
commit
98cf0a42d2
|
|
@ -0,0 +1,2 @@
|
||||||
|
.vscode
|
||||||
|
.DS_Store
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
|
|
@ -0,0 +1 @@
|
||||||
|
0000001111111110000001010000000100000101000000010000010100000001000001110000000100000111000000010000001100000001000000010000000000000011111111100000011000000000000001100000000000000110000000000000100000000000000010000000000000000000000000000000000000000000
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
(From [Nuts and Volts](https://www.nutsvolts.com/uploads/wygwam/NV_1007_Martin_Figure09.jpg).)
|
||||||
|
|
@ -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);
|
||||||
Loading…
Reference in New Issue