import { IMatrix } from "matrix-display-store" ; import { setRenderer , start , nextView , getCurrentView , getViews , setCTA , } from "../runner" ; import throttle = require ( "lodash/throttle" ) ; const polka = require ( "polka" ) ; const serve = require ( "sirv" ) ( "public" ) ; const rpio = require ( "rpio" ) ; const cors = require ( "cors" ) ( ) ; let __currentFrameData = [ ] ; polka ( ) . use ( cors ) . use ( serve ) . get ( "/api/data" , ( _ , res ) => { res . end ( JSON . stringify ( { data : __currentFrameData , currentView : getCurrentView ( ) , } ) ) ; } ) . get ( "/api/views" , ( _ , res ) => { res . end ( JSON . stringify ( getViews ( ) ) ) ; } ) . get ( "/api/view" , ( _ , res ) => { res . end ( JSON . stringify ( getCurrentView ( ) ) ) ; } ) . post ( "/api/view/:index" , ( req , res ) => { nextView ( Number ( req . params . index ) ) ; res . end ( "OK" ) ; } ) . post ( "/api/view" , ( _ , res ) => { nextView ( ) ; res . end ( "OK" ) ; } ) . post ( "/api/cta" , ( _ , res ) => { setCTA ( ) ; res . end ( "OK" ) ; } ) . listen ( 80 , ( err ) => { if ( err ) throw err ; console . log ( `> Running on localhost:8000` ) ; } ) ; const pin = 37 ; const throttledNextView = throttle ( nextView , 1500 ) ; rpio . open ( pin , rpio . INPUT , rpio . PULL_UP ) ; rpio . poll ( pin , ( cbpin ) => { if ( ! rpio . read ( cbpin ) ) { throttledNextView ( ) ; } } ) ; const LedMatrix = require ( "node-rpi-rgb-led-matrix" ) ; const led = new LedMatrix ( 16 ) ; function render ( data ) { __currentFrameData = data ; for ( let i = 0 ; i < data . length ; i += 1 ) { const dy = Math . floor ( i / 32 ) ; const dx = i - dy * 32 ; const { on , color } = data [ i ] ; if ( on && color ) { led . setPixel ( dx , dy , color . r , color . g , color . b ) ; } else { led . setPixel ( dx , dy , 0 , 0 , 0 ) ; } } } setRenderer ( render ) ; start ( ) ;