Skip to content

Instantly share code, notes, and snippets.

@jsomers
Last active October 17, 2017 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsomers/bcf76e724bcc8f6efcbe3640c34fcf24 to your computer and use it in GitHub Desktop.
Save jsomers/bcf76e724bcc8f6efcbe3640c34fcf24 to your computer and use it in GitHub Desktop.
DocWriter code
# Dependencies: ruby-full, watir, xvfb, iceweasel, geckodriver
# Run `Xvfb :10 -ac &` before running this script
require 'watir'
NEW_DOC_URL = "https://docs.google.com/document/create"
EMAIL_ADDRESS = ENV['DOCWRITER_EMAIL']
PASSWORD = ENV['DOCWRITER_PASSWORD']
SHARE_WITH_EMAIL = ENV['DOCWRITER_SHARETO']
browser = Watir::Browser.new(:firefox)
browser.goto NEW_DOC_URL
el = browser.input(id: "identifierId")
el.wait_until_present
el.send_keys EMAIL_ADDRESS
btn = browser.div(id: "identifierNext")
btn.wait_until_present
btn.click
pswd = browser.input(name: "password")
pswd.wait_until_present
pswd.send_keys PASSWORD
btn = browser.div(id: "passwordNext")
btn.wait_until_present
btn.click
# Write the title
el = browser.input(class: "docs-title-input")
el.wait_until_present
el.click
until el.value.empty?
el.send_keys :backspace
end
el.send_keys DateTime.now.new_offset(Rational(-4, 24)).strftime("%h %d, %Y - %I:%M%p")
el.send_keys :return
# Share the doc
el = browser.div(class: "jfk-button-action")
el.wait_until_present
el.click
sleep 2
iframe = browser.iframes.find {|i| i.class_name == "share-client-content-iframe"}
sleep 1
iframe.textarea(class: "apps-share-chips-input").send_keys SHARE_WITH_EMAIL
sleep 1
iframe.div(class: "simple-sharing-primary-button").click
cursor = browser.div(class: 'kix-cursor-caret')
cursor.wait_until_present
puts "READY"
$stdout.flush
table = {
"0,0" => ",",
"0,1" => "/",
"0,2" => "1",
"0,3" => "3",
"0,4" => "7",
"0,5" => "5",
"0,6" => "-",
"0,7" => "9",
"1,0" => ".",
"1,1" => "PLUSMINUS",
"1,2" => "2",
"1,3" => "4",
"1,4" => "8",
"1,5" => "6",
"1,6" => "=",
"1,7" => "0",
"2,0" => "HALF",
"2,1" => "q",
"2,2" => "e",
"2,3" => "t",
"2,4" => "o",
"2,5" => "u",
"2,6" => "n",
"2,7" => "v",
"3,0" => ";",
"3,1" => "z",
"3,2" => "f",
"3,3" => "h",
"3,4" => "s",
"3,5" => "k",
"3,6" => "x",
"3,7" => "c",
"4,0" => "]",
"4,1" => "w",
"4,2" => "r",
"4,3" => "y",
"4,4" => "p",
"4,5" => "i",
"4,6" => "m",
"4,7" => "b",
"5,0" => "'",
"5,1" => "a",
"5,2" => "g",
"5,3" => "j",
"5,4" => "d",
"5,5" => "l",
"5,6" => "_SWITCH",
"5,7" => "\t",
"6,0" => " ",
"6,1" => "\n",
"6,2" => "W_OUT",
"6,3" => "RELOC",
"6,4" => "ALT",
"6,5" => "\b",
"6,6" => "CORRECT",
"6,7" => "CODE",
"7,0" => "SHIFT",
"7,1" => "SHIFT_LOCK",
"7,2" => "NC",
"7,3" => "NC",
"7,4" => "NC",
"7,5" => "NC",
"7,6" => "NC",
"7,7" => "NC"
}
shiftTable = {
"0,0" => ",",
"0,1" => "?",
"0,2" => "!",
"0,3" => "#",
"0,4" => "&",
"0,5" => "%",
"0,6" => "_",
"0,7" => "(",
"1,0" => ".",
"1,1" => "CIRCLE",
"1,2" => "@",
"1,3" => "$",
"1,4" => "*",
"1,5" => "CENT",
"1,6" => "+",
"1,7" => ")",
"2,0" => "QUARTER",
"2,1" => "Q",
"2,2" => "E",
"2,3" => "T",
"2,4" => "O",
"2,5" => "U",
"2,6" => "N",
"2,7" => "V",
"3,0" => ":",
"3,1" => "Z",
"3,2" => "F",
"3,3" => "H",
"3,4" => "S",
"3,5" => "K",
"3,6" => "X",
"3,7" => "C",
"4,0" => "}",
"4,1" => "W",
"4,2" => "R",
"4,3" => "Y",
"4,4" => "P",
"4,5" => "I",
"4,6" => "M",
"4,7" => "B",
"5,0" => "\"",
"5,1" => "A",
"5,2" => "G",
"5,3" => "J",
"5,4" => "D",
"5,5" => "L",
"5,6" => "_SWITCH",
"5,7" => "\t",
"6,0" => " ",
"6,1" => "\n",
"6,2" => "W_OUT",
"6,3" => "RELOC",
"6,4" => "ALT",
"6,5" => "\b",
"6,6" => "CORRECT",
"6,7" => "CODE",
"7,0" => "SHIFT",
"7,1" => "SHIFT_LOCK",
"7,2" => "NC",
"7,3" => "NC",
"7,4" => "NC",
"7,5" => "NC",
"7,6" => "NC",
"7,7" => "NC"
}
consecutive_enter_count = 0
$stdin.each do |io|
if io == "6,1\n"
if consecutive_enter_count >= 1
cursor.send_keys :return
cursor.send_keys :return
consecutive_enter_count = 0
else
cursor.send_keys " "
consecutive_enter_count += 1
end
else
cursor.send_keys io.include?("s") ? shiftTable[io.chomp.sub(/s/, '')] : table[io.chomp]
consecutive_enter_count = 0
end
end
// Compile with `gcc -Wall -o read_typewriter_signals read_typewriter_signals.c -lwiringPi`
#include <wiringPi.h>
#include <stdio.h>
int main(void) {
wiringPiSetup();
setbuf(stdout, NULL);
int scanPins[] = {5, 22, 10, 11, 26, 27, 28, 29};
int signalPins[] = {13, 12, 3, 2, 0, 7, 24, 23};
int i = 0;
int j = 0;
for (i=0; i<8; i++) {
pinMode(scanPins[i], INPUT);
pinMode(signalPins[i], INPUT);
pullUpDnControl(scanPins[i], PUD_UP);
pullUpDnControl(signalPins[i], PUD_UP);
}
int keyDown[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
int lastI;
int lastJ;
int sameKeyCount = 0;
for (;;) {
for (i=0; i<8; i++) {
for (j=0; j<8; j++) {
if (digitalRead(scanPins[i]) == LOW && digitalRead(signalPins[j]) == LOW) {
(i == lastI && j == lastJ) ? sameKeyCount++ : sameKeyCount = 0;
if (sameKeyCount > 50 && keyDown[i][j] <= 0) {
printf("%d,%d\n", i, j);
keyDown[i][j] = 50;
}
lastI = i;
lastJ = j;
}
if (digitalRead(scanPins[i]) == LOW && digitalRead(signalPins[j]) == HIGH) {
keyDown[i][j] = (keyDown[i][j] - 1);
}
}
}
}
}
#!/bin/bash
STATE="error";
while [ $STATE == "error" ]; do
STATE=$(ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error)
sleep 1
done
./read_typewriter_signals | ssh -t -t $CLOUD_USERNAME@$REMOTE_IP "DISPLAY=:10 ruby drive_google_doc.rb"
@jsomers
Copy link
Author

jsomers commented Oct 17, 2017

Run "startup.sh" by putting this line in /etc/rc.local per https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/:

sudo /home/pi/startup.sh &

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment