Advertisement
Guest User

Espruino Pico Fruitmachine stuff hurr durr I'm a sheep

a guest
Apr 27th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var N_LEDS = 3;
  2. var BRIGHTNESS_FACTOR = 25;
  3. var arr = new Uint8ClampedArray(N_LEDS * 3/*3 leds * 3 channels*/);
  4. SPI2.setup({baud:3200000, mosi:B15});
  5.  
  6. // Setup external button
  7. pinMode(B1, "input_pulldown");
  8. A7.write(1);
  9.  
  10. var preflashstate = false;
  11.  
  12. function preFlash()
  13. {
  14.   preflashstate = !preflashstate;
  15.   if (preflashstate === true)
  16.   {
  17.     setAll(new Color(255, 255, 0));
  18.   }
  19.   else
  20.   {
  21.     setAll(new Color(0, 0, 0));
  22.   }
  23.   updateLeds();
  24. }
  25. function LED2Flash()
  26. {
  27.   LED2.write(!LED2.read());
  28. }
  29.  
  30. var flashstate;
  31.  
  32. function flashall()
  33. {
  34.   flashState = !flashState;
  35.   if (flashState)
  36.   {
  37.     updateLeds();
  38.   }
  39.   else
  40.   {
  41.     SPI2.send4bit(new Uint8ClampedArray(N_LEDS * 3), 0b0001, 0b0011);
  42.   }
  43. }
  44.  
  45. var flasharr = [];
  46. var fprevcol = [];
  47.  
  48. // I realize now I could just have used a length instead
  49. // This one is more future proof tho
  50. function flashsel()
  51. {
  52.   flashState = !flashState;
  53.   if (flashState)
  54.   {
  55.     for (var i = 0; i < flasharr.length; i++)
  56.     {
  57.       fprevcol[i].setToLed(flasharr[i]);
  58.     }
  59.   }
  60.   else
  61.   {
  62.     for (var i = 0; i < flasharr.length; i++)
  63.     {
  64.       var n = flasharr[i] * 3;
  65.       arr[n] = 0;
  66.       arr[n + 1] = 0;
  67.       arr[n + 2] = 0;
  68.     }
  69.   }
  70.   updateLeds();
  71. }
  72.  
  73. function updateLeds()
  74. {
  75.   var col = new Uint8ClampedArray(arr.length);
  76.   for (var i = 0; i < arr.length; i++)
  77.   {
  78.     col[i] = arr[i] / BRIGHTNESS_FACTOR;
  79.   }
  80.   SPI2.send4bit(col, 0b0001, 0b0011);
  81. }
  82.  
  83. function clearAll()
  84. {
  85.   for (var i = 0; i < arr.length; i++)
  86.   {
  87.     arr[i] = 0;
  88.   }
  89. }
  90. function setAll(color)
  91. {
  92.   for (var i = 0; i < N_LEDS; i++)
  93.   {
  94.     color.setToLed(i);
  95.   }
  96. }
  97.  
  98. function ran(max)
  99. {
  100.   return Math.floor(Math.random() * max);
  101. }
  102. function ran(min,max)
  103. {
  104.   return Math.floor(Math.random() * (max - min)) + min;
  105. }
  106.  
  107. var Color = function(r,g,b)
  108. {
  109.   this.r = r;
  110.   this.g = g;
  111.   this.b = b;
  112. };
  113. Color.prototype.setToLed = function(led)
  114. {
  115.   led *= 3;
  116.   //arr.splice(led, 3, this.r, this.g, this.b);
  117.   arr[led    ] = this.r;
  118.   arr[led + 1] = this.g;
  119.   arr[led + 2] = this.b;
  120. };
  121.  
  122. var SlotType = function(name, prize, color)
  123. {
  124.   this.name = name;
  125.   this.prize = prize;
  126.   this.color = color;
  127. };
  128.  
  129. var SYMBOLS = [
  130.   new SlotType("Cherry", 20, new Color(255, 0, 0)),
  131.   new SlotType("Lemon", 50, new Color(255, 255, 0)),
  132.   new SlotType("Melon", 100, new Color(0, 255, 0)),
  133.   new SlotType("7", 200, new Color(255, 2, 120)),
  134.   new SlotType("Bar", 500, new Color(0, 255, 255)),
  135. ];
  136.  
  137. var Wheel = function(led)
  138. {
  139.   this.led = led;
  140.   this.fruit = ran(SYMBOLS.length);
  141.   this.vel = 0.0;
  142.   this.pos = 0.0;
  143. };
  144. Wheel.prototype.spin = function(stopt)
  145. {
  146.   this.vel = (2 + Math.random() * 5) / 40;
  147.   setTimeout(this.stop, stopt);
  148. };
  149. Wheel.prototype.step = function()
  150. {
  151.   this.vel *= 0.9912;
  152.   this.pos += this.vel;
  153.   this.pos -= Math.floor(this.pos);
  154.   this.fruit = Math.floor(this.pos * SYMBOLS.length);
  155.   SYMBOLS[this.fruit].color.setToLed(this.led);
  156. };
  157. Wheel.prototype.stop = function()
  158. {
  159.   this.vel = 0.0;
  160. };
  161.  
  162. var wheels = [];
  163.  
  164. function resetFruitmachine()
  165. {
  166.   clearInterval();
  167.   clearWatch();
  168.   init();
  169. }
  170. function spin()
  171. {
  172.   for (var i = 0; i < N_LEDS;)
  173.   {
  174.     wheels[i].spin((++i * 500));
  175.   }
  176.  
  177.   setTimeout(function(){
  178.    
  179.     clearInterval();
  180.    
  181.     // Not suitable for > 3 leds
  182.    
  183.     var indexes = [];
  184.     var winstreak = 0;
  185.     for (var i = 0; i < N_LEDS - 1;)
  186.     {
  187.       if (wheels[i++].fruit === wheels[i].fruit)
  188.       {
  189.         winstreak++;
  190.         indexes.push(i - 1);
  191.       }
  192.     }
  193.    
  194.      clearInterval();
  195.    
  196.     if (winstreak !== 0)
  197.     {
  198.       indexes.push(indexes[winstreak - 1] + 1);
  199.       var fruit = SYMBOLS[wheels[indexes[0]].fruit];
  200.       var prize = Math.pow(fruit.prize, winstreak);
  201.       console.log("You won!(" + fruit.name + " x" + (winstreak + 1) + ") Prize: $" + prize + "!");
  202.      
  203.       flasharr = indexes;
  204.       for (var i = 0; i < flasharr.length; i++)
  205.       {
  206.         fprevcol[i] = fruit.color;
  207.       }
  208.      
  209.       setInterval(flashsel, 100, {repeat:true});
  210.     }
  211.     else
  212.     {
  213.       setInterval(flashall, 500, {repeat:true});
  214.     }
  215.    
  216.    
  217.     setInterval(LED2Flash, 250, {repeat:true});
  218.     setWatch(function(){
  219.       clearInterval();
  220.       clearWatch();
  221.       LED2.write(0);
  222.       setWatch(resetFruitmachine, BTN, {repeat:true});
  223.       spin();
  224.       flashstate = false;
  225.     }, B1, {repeat:true,edge:"rising"});
  226.    
  227.   }, (N_LEDS + 2.5) * 500);
  228.   setInterval(step, 21);
  229. }
  230.  
  231. function step()
  232. {
  233.   for (var i = 0; i < N_LEDS; i++)
  234.   {
  235.     wheels[i].step();
  236.   }
  237.   updateLeds();
  238. }
  239.  
  240. function init()
  241. {
  242.   for (var i = 0; i < N_LEDS; i++)
  243.   {
  244.     wheels.push(new Wheel(i));
  245.   }
  246.  
  247.   setInterval(preFlash, 700, {repeat:true});
  248.   setInterval(LED2Flash, 250, {repeat:true});
  249.  
  250.   setWatch(function(){
  251.     clearInterval();
  252.     clearWatch();
  253.     clearAll();
  254.     preflashstate = false;
  255.     LED2.write(0);
  256.     updateLeds();
  257.  
  258.     setWatch(resetFruitmachine, BTN, {repeat:true});
  259.     spin();
  260.   }, B1, {repeat:true,edge:"rising"});
  261. }
  262. init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement