Code Monkey home page Code Monkey logo

tikitanktwo's Introduction

Tiki Tank Two

Effect Display Devices

Currently we have three output devices for the effects:

  • Threads: 480 pixels in circle
  • Barrel: 77 pixels straight line
  • Panels: 10 LED strips on each side of the tank, represented by the array of 10 pixels

Creating effects

Every effect implements simple IEffect interface, with following signature.

public interface IEffect
{
    void Activate(System.Drawing.Color[] pixels);
    void Deactivate(System.Drawing.Color[] pixels);
    void FrameUpdate(System.Drawing.Color[] pixels);
    bool WouldUpdate();
    void Tick();

    bool IsSensorDriven { get; set; }
    string Argument { get; set; }
    System.Drawing.Color Color { get; set; }
}

void Activate(System.Drawing.Color[] pixels)

Method is called when the effect is activated for showing. Array of colors pixels[] contains current content of the display device (last frame). Make changes to this array in order to prepare your first frame, or to make a copy of the current frame to blend it with your effect.

void Deactivate(System.Drawing.Color[] pixels)

Method is called when the effect is being deactivated. Array of colors pixels[] contains current content of the display device (last frame). Make changes to this array in order to create a last frame. You can use this method to free the additional resources that effect was using.

int FrameUpdate(System.Drawing.Color[] pixels)

This is the main method for the effect which is called by the frame scheduler. Array of colors pixels[] contains current content of the display device (last frame). Modify this array to create a new frame to be displayed.

bool WouldUpdate()

Method is called regularly by frame scheduler to determine whether the effect needs frame update or not. If the method returns true then FrameUpdate() is called.

void Tick()

Method is called based on the Vehicle Speed Sensor output. When the vehicle is not moving the method is not called.

bool IsSensorDriven

Property is True when user requested this effect to be driven by the Vehicle Speed Sensor. When this property is set and vehicle is moving, method Tick() is called based on the vehicle speed.

string Argument

Property contains user changeable parameter for the effect.

System.Drawing.Color Color

Property contain user changeable color parameter for the effect.

Sample Effect

This is simple effect that blinks one pixel. Position of the blinking pixel is changing based on vehicle speed.

class BlinkingPoint : IEffect
{
    public BlinkingPoint()
    {
        this.Color = System.Drawing.Color.Red;             
    }

    public void Activate(System.Drawing.Color[] pixels)
    {
        startTime = DateTime.Now;     
        length = pixels.Length;
        position = 0;
        pointVisible = true;
    }

    public void Deactivate(System.Drawing.Color[] pixels)
    {

    }

    public void FrameUpdate(System.Drawing.Color[] pixels)
    {
        // Clean the strip
        for (int i = 0; i < length; i++)
            pixels[i] = System.Drawing.Color.Black;

        // Show the point
        if (pointVisible)
            pixels[position] = this.Color;

        pointVisible = !pointVisible;
    }

    public bool WouldUpdate()
    {
        // Determine if it's time to update based (every 100 millisecond)
        TimeSpan delta = DateTime.Now - startTime;
        if (delta.TotalMilliseconds > 100)
        {
            startTime = DateTime.Now;
            return true;
        }

        return false;
    }

    public void Tick()
    {
        // Move the point on tick
        position++;
        if (position >= length)
            position = 0;
    }

    public bool IsSensorDriven { get; set; }

    public string Argument { get; set; }

    public System.Drawing.Color Color { get; set; }

    private int position;
    private int length;
    private bool pointVisible;
    private DateTime startTime;
}	

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.