Advertisement
cymplecy

piGPIOExtension.js

Jun 27th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. new (function() {
  2.     var ext = this;
  3.     var fs = require('fs');
  4.     var websocket
  5.     var sensorSplit
  6.     var sensorDict = {}
  7.     var pinLookup = {'3' : '2', '5' : '3', '7' : '4', '8' : '14', '10' : '15' , '11' : '17', '12' : '18', '13' : '27','15' : '22', '16' : '23', '18' : '24', '19' : '10', '21' : '9', '22' : '25', '23': '11', '24' : '8', '26' : '7', '27' : '0' , '28' : '1', '29' : '5', '31' : '6', '32' : '12', '33' : '13', '35' : '19', '36' : '16', '37' : '26', '38' : '20', '40' : '21'}
  8.  
  9.     function doConnect()
  10.     {
  11.       websocket = new WebSocket('ws://localhost:8000/')
  12.       websocket.onopen = function(evt) { onOpen(evt) }
  13.       websocket.onclose = function(evt) { onClose(evt) }
  14.       websocket.onmessage = function(evt) { onMessage(evt) }
  15.       websocket.onerror = function(evt) { onError(evt) }
  16.       console.log('websocket connected from piGPIOExtension')
  17.     }
  18.  
  19.     function onOpen(evt) {
  20.       console.log('websocket opened')
  21.     }
  22.  
  23.     function onClose(evt) {
  24.       console.log('websocket closed')
  25.     }
  26.  
  27.     function onMessage(evt) {
  28.       var data = evt.data
  29.       console.log('msg from sgh:' + data)
  30.       sensorSplit = data.split(":");
  31.       sensorDict[sensorSplit[0]] = sensorSplit[1];
  32.     }
  33.  
  34.     function onError(evt) {
  35.       var error = evt.data
  36.       console.log('websocket error', error);
  37.      
  38.       websocket.close();
  39.     }
  40.  
  41.     function sendMessage(message) {
  42.       websocket.send(message);
  43.       console.log('msg to sgh:' + message)
  44.     }
  45.  
  46.     function doDisconnect() {
  47.       websocket.close();
  48.      }
  49.    
  50.     doConnect();
  51.  
  52.     // Cleanup function when the extension is unloaded
  53.     ext._shutdown = function ()
  54.     {
  55.         for (pin = 2; pin < 28; pin++)
  56.         {
  57.             if (fs.existsSync("/sys/class/gpio/gpio" + pin))
  58.                 fs.writeFileSync("/sys/class/gpio/unexport", pin, "utf8");
  59.         }
  60.     };
  61.  
  62.     // Status reporting code
  63.     // Use this to report missing hardware, plugin or unsupported browser
  64.     ext._getStatus = function ()
  65.     {
  66.         return {status: 2, msg: 'Ready'};
  67.     };
  68.  
  69.     ext.set_gpio = function (pin, val)
  70.     {
  71.         if (pin === '' || pin < 0 || pin > 27) return;
  72.  
  73.         var dir = 0, lev;
  74.         if (val == 'output high') lev = 1;
  75.         else if (val == 'output low') lev = 0;
  76.         else dir = 1;
  77.  
  78.         // check the pin is exported
  79.         if (!fs.existsSync("/sys/class/gpio/gpio" + pin))
  80.  
  81.             fs.writeFileSync("/sys/class/gpio/export", pin, "utf8");
  82.  
  83.         // the ownership of direction takes time to establish, so try this until it succeeds
  84.         while (true)
  85.         {
  86.             try {
  87.                 fs.writeFileSync("/sys/class/gpio/gpio" + pin + "/direction", dir == 0 ? "out" : "in", "utf8");
  88.                 break;
  89.             }
  90.             catch (error) {
  91.                 continue;
  92.             }
  93.         }
  94.  
  95.         // set the output value
  96.         if (dir == 0)
  97.             sendMessage('pin ' + pin + ' = ' + (lev == 1 ? "1" : "0"));
  98.             fs.writeFileSync("/sys/class/gpio/gpio" + pin + "/value", lev == 1 ? "1" : "0", "utf8");
  99.     };
  100.  
  101.     ext.get_gpio = function (pin)
  102.     {
  103.         if (pin === '' || pin < 0 || pin > 27) return;
  104.  
  105.         // check the pin is exported
  106.         if (!fs.existsSync("/sys/class/gpio/gpio" + pin))
  107.             fs.writeFileSync("/sys/class/gpio/export", pin);
  108.  
  109.         // read the pin value
  110.         var data = fs.readFileSync ("/sys/class/gpio/gpio" + pin + "/value", 'utf8');
  111.  
  112.         if (data.slice(0,1) == "1") return true;
  113.         else return false;
  114.     };
  115.    
  116. //my code
  117. ext.send_broadcast = function (bmsg)
  118.     {
  119.         sendMessage('broadcast "' + bmsg + '"');
  120.  
  121.     };
  122. ext.send_joinedBroadcast = function (bmsg1, bmsg2, bmsg3, bmsg4 = '', bmsg5 = '')
  123.     {
  124.         sendMessage('broadcast "' + bmsg1  + bmsg2  + bmsg3  + bmsg4  + bmsg5 + '"');
  125.  
  126.     };    
  127.    
  128. ext.send_variable = function (sgh_var,val)
  129.     {
  130.         sendMessage('sensor-update "' + sgh_var + '" ' + val);
  131.  
  132.     };    
  133. ext.set_pin = function (pin,val)
  134.     {
  135.         sendMessage('broadcast "pin' + pin + val +'"');
  136.     };    
  137. ext.get_pin = function (ppin)
  138.     {
  139.         //console.log('get_pin:' + ppin)
  140.         if (!(("pin" + ppin) in sensorDict)) {
  141.             sendMessage('broadcast "config' + ppin + 'in"');
  142.             //console.log('configin sent for ' + ppin)
  143.             pin = pinLookup[ppin];
  144.  
  145.             if (pin === '' || pin < 0 || pin > 27) return;
  146.  
  147.             // check the pin is exported
  148.             if (!fs.existsSync("/sys/class/gpio/gpio" + pin))
  149.                 fs.writeFileSync("/sys/class/gpio/export", pin);
  150.  
  151.             // read the pin value
  152.             var data = fs.readFileSync ("/sys/class/gpio/gpio" + pin + "/value", 'utf8');
  153.  
  154.             if (data.slice(0,1) == "1") return "1";
  155.             else return "0";
  156.            
  157.         } else {
  158.             return sensorDict[("pin" + ppin)];
  159.         }
  160.  
  161.     };    
  162. ext.set_pixel = function (x,y,val)
  163.     {
  164.         sendMessage('broadcast "pixel' + x + ',' + y + val +'"');
  165.     };      
  166. ext.set_pixels = function (val)
  167.     {
  168.         sendMessage('broadcast "pixels' + val +'"');
  169.     };    
  170. ext.get_cheerlights = function (val)
  171.     {
  172.         sendMessage('broadcast "get cheerlights"');
  173.     };    
  174.    
  175.  
  176. ext.get_sensorMsg = function (sensorName)
  177.     {
  178.     return sensorDict[sensorName];
  179.     };    
  180. ext.get_cheerlightsSensor = function ()
  181.     {
  182.     return sensorDict['cheerlights'];
  183.     };        
  184.    
  185.          
  186.      
  187.    
  188.     // Block and block menu descriptions
  189.     var descriptor = {
  190.         blocks: [
  191.            [' ', 'set pin %m.pin_numbers to %m.pin_outputs', 'set_pin', '11', 'On'],
  192.             ['r', 'pin %m.pin_numbers sensor value', 'get_pin', '7'],  
  193.             [' ', 'broadcast %s', 'send_broadcast', ' '],
  194.             [' ', 'broadcast %s %s %s', 'send_joinedBroadcast', ' ', ' ', ' '],            
  195.             [' ', 'broadcast %s %s %s %s %s', 'send_joinedBroadcast', ' ', ' ', ' ', ' ', ' '],
  196.             [' ', 'set %m.sgh_variables to %s', 'send_variable', '', '0'],      
  197.             [' ', 'set %s to %s', 'send_variable', '', ''],
  198.             ['r', '%s sensor value', 'get_sensorMsg', ''],
  199.             [' ', 'set pixel x:%s y:%s to %s', 'set_pixel', '0', '0', 'red'],  
  200.             [' ', 'set all pixels to %s', 'set_pixels', 'red'],
  201.             [' ', 'get cheerlight colour', 'get_cheerlights'],
  202.             [' ', 'set gpio %n to %m.outputs', 'set_gpio', '', 'output high'],
  203.             ['b', 'gpio %n is high?', 'get_gpio', ''],
  204.             ['r', 'cheerlight colour', 'get_cheerlightsSensor'],            
  205.  
  206.         ],
  207.         menus: {
  208.             outputs: ['output high', 'output low', 'input'],
  209.             pin_outputs: ['On', 'Off', 'High','Low', '1','0'],    
  210.             pin_numbers: ['3', '5', '7', '8', '10' , '11', '12', '13','15', '16', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29', '31', '32', '33', '35', '36', '37', '38', '40'],  
  211.             sgh_variables: ['AddOn', 'MotorA', 'MotorB','Motor1','Motor2'],              
  212.         }
  213.     };
  214.  
  215.     // Register the extension
  216.     ScratchExtensions.register('Pi GPIO', descriptor, ext);
  217. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement