Rundbunt Mini Emulator

Aus Hackerspace Ffm
Version vom 7. Oktober 2015, 08:55 Uhr von Axl (Diskussion | Beiträge) (Quellcode mit Beispiel-Muster)

Wechseln zu: Navigation, Suche

Emulator Programm

Rundbunt mini Emulator screenshot 1.PNG
  • Programm für Processing 2.x (getestet unter Windows)
  • Das eindimensionale Array hsvbuf1[] wird durch die Funktion render_lamp() auf dem Bildschirm gerendert.
  • die Potis für para[1] und 'para[2] können am unteren Bildschirm-Rand mit der Maus gesteuert werden.


Limits

Processing unterscheidet sich in einigen Punkten von der FastLED Bibliothek in der Arduino IDE:

  • Der Hue Wert für Farben bleibt bei Werten > 255 "stehen", geht also nicht zirkulär wieder bei 0 los.
  • Einige Datentypen sind unbekannt, wie z.B. uint8_t.


Quellcode mit Beispiel-Muster

// -------------------------------------------------------------------------------
// Rundbunt Mini Emulator
// -------------------------------------------------------------------------------
// 005 - 2015-10-07 - corrected hue calculation with modulo
// -------------------------------------------------------------------------------


/*
The MIT License (MIT)

Copyright (c) 2015, Axel Föhr (axl [AT] realaxl.de)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


CHSV[] hsvbuf1 = new CHSV[64];

int[] para = new int[3];


class CHSV {
  float h, s, v;

  CHSV(float ih, float is, float iv) {
     h = ih;
     s = is;
     v = iv;
  }
 
  void set_HSV(float ih, float is, float iv) {
     h = ih;
     s = is;
     v = iv;
  }
}

void setup() {
  size(500, 500);
  background(32);
  
  colorMode(HSB, 255);
  
  
  for (int i = 0; i < 64; i ++) {
    hsvbuf1[i] = new CHSV(i * 4, 255, 255);
  }

  for (int i = 0; i < 3; i ++) {
    para[i] = 512;
  }
  draw_para(1);
  draw_para(2);
}


void draw() {
  {
        // =============================================
        // ==== rotating color dot                 =====
        // ==== para[1] - base color               =====
        // ==== para[2] - color distribution       =====
        // =============================================

        float mx, my;

        mx = 3.5 + 3.5 * sin(millis() / 1200.0);
        my = 3.5 + 3.5 * sin(millis() / 1321.0);

        float s,v,cx,cy,t,t2;
        int para1 = 0, para2 = 0;

        float round = (4 * atan(1) * 2) / 8; // 2 * PI / 8
        
        if(abs(para1 - para[1]) > 4) para1 = para[1];
        if(abs(para2 - para[2]) > 4) para2 = para[2];
        
        int pos;
        
        float dx, dy;
        
        for(int y=0; y<8; y++) {
          float tr = 3.1 + 6.3 * sin(millis() / (20.0 * para1)) * sin((millis() / 1000.0) + y * 100.0);
          float ts;
          dy = my - y;
          
          for(int x=0; x<8; x++) {
            dx = mx - x;
            
            ts = sqrt(sq(dx) + sq(dy));
            ts = exp(-sq(ts) * .133);
            pos = x * 8 + y;    
            hsvbuf1[pos].h = para1 / 4 + para2 * sin(ts); // y * 32.0 + 255 * sin(millis() / 10000.0);
            hsvbuf1[pos].s = 255 * ts;
            hsvbuf1[pos].v = ts * 255;
          }
        }
      }  
  render_lamp();
}


void mouseDragged() {
  int mx = mouseX;
  int my = height - mouseY;
  
  if (my < 80) {
    if (my < 40) {
      para[2] = min(1023, max(0, round(map(mx, 0, width, 0, 1023))));
      draw_para(2);
    } else {
      para[1] = min(1023, max(0, round(map(mx, 0, width, 0, 1023))));
      draw_para(1);
    }
  }
}


void draw_para(int n) {
  if (n == 1) {
    noStroke();
    fill(0);
    rect(0, height - 80, width, 40);
    fill(#3E484F);
    rect(0, height - 80, map(para[n], 0, 1023, 0, width), 40);
    fill(255);
    text("para[1] = " + para[1], 10, height - 55);
  }
  if (n == 2) {
    noStroke();
    fill(0);
    rect(0, height - 40, width, 40);
    fill(#4682B4);
    rect(0, height - 40, map(para[n], 0, 1023, 0, width), 40);
    fill(255);
    text("para[2] = " + para[2], 10, height - 15);
  }
}

void render_lamp() {
  noStroke();
  for (int x = 0; x < 8; x ++) {
    for (int y2 = 0; y2 < 8; y2 ++) {
      int y;
      if ((x % 2) == 0) {
        y = y2;
      } else {
        y = 7 - y2;
      }
      
      int pos = x * 8 + y;
      
      int x0 = 40  + 40 * x;
      int y0 = (8 * 40) - 40 * y;
      
      //fill(led[pos]);
      float h = hsvbuf1[pos].h % 256;
      fill(color(h, hsvbuf1[pos].s, hsvbuf1[pos].v));
      rect (x0, y0, 38, 38);
    }
  }
}