ディップスイッチによるアドレス計算


2進数くらい計算すればいいじゃんと言われそうですが、
ぱっと計算して試せるプログラムをProcessingで書いてみた。
数字の下の■をクリックすると、on / offをトグルして計算される。

final int NUM_LEDS = 150;

RectButton rect1, rect2, rect3, rect4, rect5, rect6, rect7, rect8, rect9;//, rect10;
int address = 0;

void setup() {
  size(320, 200);
  smooth();

  rect1 = new RectButton(20, 40, 10);
  rect2 = new RectButton(40, 40, 10);
  rect3 = new RectButton(60, 40, 10);
  rect4 = new RectButton(80, 40, 10);
  rect5 = new RectButton(100, 40, 10);
  rect6 = new RectButton(120, 40, 10);
  rect7 = new RectButton(140, 40, 10);
  rect8 = new RectButton(160, 40, 10);
  rect9 = new RectButton(180, 40, 10);
  //rect10 = new RectButton(200, 40, 10);
}

void draw() {
  background(0);

  fill(255);
  text("1", 20, 30);
  text("2", 40, 30);
  text("3", 60, 30);
  text("4", 80, 30);
  text("5", 100, 30);
  text("6", 120, 30);
  text("7", 140, 30);
  text("8", 160, 30);
  text("9", 180, 30);
  //text("10", 197, 30);
  text("Address : " + address + " ~ " + (address + NUM_LEDS), 60, 80);

  stroke(255);
  rect1.display();
  rect2.display();
  rect3.display();
  rect4.display();
  rect5.display();
  rect6.display();
  rect7.display();
  rect8.display();
  rect9.display();
  //rect10.display();
}

void mouseClicked() {
  address = 0;
  if (rect1.getPN()) {
    address += 1;
  }
  if (rect2.getPN()) {
    address += 2;
  }
  if (rect3.getPN()) {
    address += 4;
  }
  if (rect4.getPN()) {
    address += 8;
  }
  if (rect5.getPN()) {
    address += 16;
  }
  if (rect6.getPN()) {
    address += 32;
  }
  if (rect7.getPN()) {
    address += 64;
  }
  if (rect8.getPN()) {
    address += 128;
  }
  if (rect9.getPN()) {
    address += 256;
  }
  /*if (rect10.getPN()) {
    address += 512;
  }*/
}

class RectButton {
  int x, y;
  int size;
  boolean activated = false;

  RectButton(int ix, int iy, int isize) {
    x = ix;
    y = iy;
    size = isize;
  }

  boolean getPN() {
    if(overRect(x, y, size, size)) {
      activated = !activated;
    } 
    
    return activated;
  }

  boolean overRect(int x, int y, int width, int height) {
    if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
      return true;
    } 
    else {
      return false;
    }
  }

  void display() {
    stroke(255);
    fill(activated ? color(255, 0, 0) : color(102));
    rect(x, y, size, size);
  }
}