Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var N_LEDS = 3;
- var BRIGHTNESS_FACTOR = 25;
- var arr = new Uint8ClampedArray(N_LEDS * 3/*3 leds * 3 channels*/);
- SPI2.setup({baud:3200000, mosi:B15});
- // Setup external button
- pinMode(B1, "input_pulldown");
- A7.write(1);
- var preflashstate = false;
- function preFlash()
- {
- preflashstate = !preflashstate;
- if (preflashstate === true)
- {
- setAll(new Color(255, 255, 0));
- }
- else
- {
- setAll(new Color(0, 0, 0));
- }
- updateLeds();
- }
- function LED2Flash()
- {
- LED2.write(!LED2.read());
- }
- var flashstate;
- function flashall()
- {
- flashState = !flashState;
- if (flashState)
- {
- updateLeds();
- }
- else
- {
- SPI2.send4bit(new Uint8ClampedArray(N_LEDS * 3), 0b0001, 0b0011);
- }
- }
- var flasharr = [];
- var fprevcol = [];
- // I realize now I could just have used a length instead
- // This one is more future proof tho
- function flashsel()
- {
- flashState = !flashState;
- if (flashState)
- {
- for (var i = 0; i < flasharr.length; i++)
- {
- fprevcol[i].setToLed(flasharr[i]);
- }
- }
- else
- {
- for (var i = 0; i < flasharr.length; i++)
- {
- var n = flasharr[i] * 3;
- arr[n] = 0;
- arr[n + 1] = 0;
- arr[n + 2] = 0;
- }
- }
- updateLeds();
- }
- function updateLeds()
- {
- var col = new Uint8ClampedArray(arr.length);
- for (var i = 0; i < arr.length; i++)
- {
- col[i] = arr[i] / BRIGHTNESS_FACTOR;
- }
- SPI2.send4bit(col, 0b0001, 0b0011);
- }
- function clearAll()
- {
- for (var i = 0; i < arr.length; i++)
- {
- arr[i] = 0;
- }
- }
- function setAll(color)
- {
- for (var i = 0; i < N_LEDS; i++)
- {
- color.setToLed(i);
- }
- }
- function ran(max)
- {
- return Math.floor(Math.random() * max);
- }
- function ran(min,max)
- {
- return Math.floor(Math.random() * (max - min)) + min;
- }
- var Color = function(r,g,b)
- {
- this.r = r;
- this.g = g;
- this.b = b;
- };
- Color.prototype.setToLed = function(led)
- {
- led *= 3;
- //arr.splice(led, 3, this.r, this.g, this.b);
- arr[led ] = this.r;
- arr[led + 1] = this.g;
- arr[led + 2] = this.b;
- };
- var SlotType = function(name, prize, color)
- {
- this.name = name;
- this.prize = prize;
- this.color = color;
- };
- var SYMBOLS = [
- new SlotType("Cherry", 20, new Color(255, 0, 0)),
- new SlotType("Lemon", 50, new Color(255, 255, 0)),
- new SlotType("Melon", 100, new Color(0, 255, 0)),
- new SlotType("7", 200, new Color(255, 2, 120)),
- new SlotType("Bar", 500, new Color(0, 255, 255)),
- ];
- var Wheel = function(led)
- {
- this.led = led;
- this.fruit = ran(SYMBOLS.length);
- this.vel = 0.0;
- this.pos = 0.0;
- };
- Wheel.prototype.spin = function(stopt)
- {
- this.vel = (2 + Math.random() * 5) / 40;
- setTimeout(this.stop, stopt);
- };
- Wheel.prototype.step = function()
- {
- this.vel *= 0.9912;
- this.pos += this.vel;
- this.pos -= Math.floor(this.pos);
- this.fruit = Math.floor(this.pos * SYMBOLS.length);
- SYMBOLS[this.fruit].color.setToLed(this.led);
- };
- Wheel.prototype.stop = function()
- {
- this.vel = 0.0;
- };
- var wheels = [];
- function resetFruitmachine()
- {
- clearInterval();
- clearWatch();
- init();
- }
- function spin()
- {
- for (var i = 0; i < N_LEDS;)
- {
- wheels[i].spin((++i * 500));
- }
- setTimeout(function(){
- clearInterval();
- // Not suitable for > 3 leds
- var indexes = [];
- var winstreak = 0;
- for (var i = 0; i < N_LEDS - 1;)
- {
- if (wheels[i++].fruit === wheels[i].fruit)
- {
- winstreak++;
- indexes.push(i - 1);
- }
- }
- clearInterval();
- if (winstreak !== 0)
- {
- indexes.push(indexes[winstreak - 1] + 1);
- var fruit = SYMBOLS[wheels[indexes[0]].fruit];
- var prize = Math.pow(fruit.prize, winstreak);
- console.log("You won!(" + fruit.name + " x" + (winstreak + 1) + ") Prize: $" + prize + "!");
- flasharr = indexes;
- for (var i = 0; i < flasharr.length; i++)
- {
- fprevcol[i] = fruit.color;
- }
- setInterval(flashsel, 100, {repeat:true});
- }
- else
- {
- setInterval(flashall, 500, {repeat:true});
- }
- setInterval(LED2Flash, 250, {repeat:true});
- setWatch(function(){
- clearInterval();
- clearWatch();
- LED2.write(0);
- setWatch(resetFruitmachine, BTN, {repeat:true});
- spin();
- flashstate = false;
- }, B1, {repeat:true,edge:"rising"});
- }, (N_LEDS + 2.5) * 500);
- setInterval(step, 21);
- }
- function step()
- {
- for (var i = 0; i < N_LEDS; i++)
- {
- wheels[i].step();
- }
- updateLeds();
- }
- function init()
- {
- for (var i = 0; i < N_LEDS; i++)
- {
- wheels.push(new Wheel(i));
- }
- setInterval(preFlash, 700, {repeat:true});
- setInterval(LED2Flash, 250, {repeat:true});
- setWatch(function(){
- clearInterval();
- clearWatch();
- clearAll();
- preflashstate = false;
- LED2.write(0);
- updateLeds();
- setWatch(resetFruitmachine, BTN, {repeat:true});
- spin();
- }, B1, {repeat:true,edge:"rising"});
- }
- init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement