Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WaffleJS Apr '19 - CoffeeJS

WaffleJS Apr '19 - CoffeeJS

Slides for "CoffeeJS - How I hacked a coffee machine using JavaScript".
More about project: bit.ly/coffee-js

Dominik Kundel

April 03, 2019
Tweet

More Decks by Dominik Kundel

Other Decks in Programming

Transcript

  1. Coffee.js How I Hacked a Coffee Machine Using JavaScript Dominik

    Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp
  2. Dominik Kundel | @dkundel | #wafflejs | console.log(` Hi! I’m

    Dominik Kundel `); dkundel.com @dkundel [email protected] github/dkundel Developer Evangelist !&& JavaScript Hacker #coffeejs #htcpcp
  3. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp How

    I Hacked a Coffee Machine Using JavaScript
  4. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp 1.

    The last time I wrote C++ was in university Why JavaScript?
  5. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp 1.

    The last time I wrote C++ was in university 2. I know JavaScript well === faster prototyping Why JavaScript?
  6. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp 1.

    The last time I wrote C++ was in university 2. I know JavaScript well === faster prototyping 3. Writing a web server is easy Why JavaScript?
  7. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp 1.

    The last time I wrote C++ was in university 2. I know JavaScript well === faster prototyping 3. Writing a web server is easy 4. It’s more of a challenge Why JavaScript?
  8. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp 1.

    The last time I wrote C++ was in university 2. I know JavaScript well === faster prototyping 3. Writing a web server is easy 4. It’s more of a challenge 5. Hardware === eventDriven && JS === eventDriven Why JavaScript?
  9. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp The

    Facts • 8 cables • 6 switches • 7 LEDs
  10. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp The

    Facts • 8 cables • 6 switches • 7 LEDs The Assumptions • One cable for power • At least 3 pins + power for buttons • At least 3 pins + power for LEDs
  11. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp const

    five = require('johnny-five'); const Tessel = require('tessel-io'); const board = new five.Board({ io: new Tessel(), }); board.on('ready', () !=> { const p1 = new five.Pin({ pin: 'b0', mode: 2, }); !//!!... const p8 = new five.Pin({ pin: 'b7', mode: 2, }); const pins = [p1 !/* !!... !*/, , p8]; board.repl.inject({ pins: pins }); }); The Naive Approach
  12. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp board.on('ready',

    () !=> { !// PIN declaration !!... const b2 = new five.Button('b1'); const b3 = new five.Button('b2'); const b7 = new five.Button('b6'); const b8 = new five.Button('b7'); const buttons = [b2, b3, b7, b8]; buttons.forEach(btn !=> { btn.on('press', () !=> console.log('Pressed button no.%d', btn.pin) ); btn.on('release', () !=> console.log('Released button no.%d', btn.pin) ); }); }); How about Buttons? const b2 = new five.Button('b1'); const b3 = new five.Button('b2'); const b7 = new five.Button('b6'); const b8 = new five.Button('b7'); const buttons = [b2, b3, b7, b8]; buttons.forEach(btn !=> { btn.on('press', () !=> console.log('Pressed button no.%d', btn.pin) ); btn.on('release', () !=> console.log('Released button no.%d', btn.pin) ); });
  13. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp A

    Small Success • Pin 1 is power (turns LED 4,5,7) • Pin 4-6 control LEDs 4,5,7 • Pin 7-8 react on switches • Pin 2 manipulates switches 3 & 4 • Pin 3 manipulates switches 1 & 2
  14. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp •

    Switch 1 = P3 → P7 • Switch 2 = P3 → P8 • Switch 3 = P2 → P7 • Switch 4 = P2 → P8 • Switch 5 = P1 → P7 • Switch 6 = P1 → P8 • LED 7 = P1 → P6 • …
  15. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp Controlling

    Relays const espresso = new five.Relay({ pin: 'a4', type: 'NO', }); const grande = new five.Relay({ pin: 'a5', type: 'NO', }); const power = new five.Relay({ pin: 'a6', type: 'NO', }); espresso.close(); grande.close(); power.close(); board.repl.inject({ espresso, grande, power });
  16. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp •

    Built on top of HTTP • Adds BREW method • Safe and Accept-Additions Headers • HTTP code 418 • coffee:!// URI scheme • Many more things Hyper Text Coffee Pot Control Protocol
  17. Dominik Kundel | @dkundel | #wafflejs | #coffeejs #htcpcp Check

    out the Code github.com/dkundel/htcpcp-delongi