[ create a new paste ] login | about

Link: http://codepad.org/CCZMMko0    [ raw code | fork ]

NeoCat - C++, pasted on May 22:
#define ID 1
#define _NUM2STR(m) #m
#define NUM2STR(m) _NUM2STR(m)

const char led[6] = {9,11,10,3,6,5};
unsigned char auto_idle = ID == 3 ? 0 : 1;
unsigned char mode = 1; /* 0:idle 1:control */
unsigned long idle = millis();
unsigned char inputs[6];
unsigned char target[6] = {0, 0, 0, 0, 0, 0};
unsigned char orig[6] = {0, 0, 0, 0, 0, 0};
unsigned char cs[6] = {0, 0, 0, 0, 0, 0};
const unsigned char ts[][6] = {{255, 0, 0, 128, 127, 0},
                               {128, 127, 0, 0, 255, 0},
                               {0, 255, 0, 0, 128, 127},
                               {0, 128, 127, 0, 0, 255},
                               {0, 0, 255, 127, 0, 128},
                               {127, 0, 128, 255, 0, 0}};
unsigned char phase = (ID - 1)*2;
unsigned char cnt = 0;
unsigned char input_cnt = 0;
unsigned char id_match = 0;
int tmp = -1;
const char id[] = "boxel00" NUM2STR(ID);

int hex2int(char hex, int pre)
{
  return (hex >= '0' && hex <= '9' ? hex - '0' :
          hex >= 'A' && hex <= 'F' ? hex - 'A' + 10 :
          hex >= 'a' && hex <= 'f' ? hex - 'a' + 10 :
          -256) + pre * 16;
}

void copy(unsigned char dst[6], const unsigned char src[6])
{
  for (int i = 0; i < 6; i++)
    dst[i] = src[i];
}

void calcNextColor(const unsigned char *org, const unsigned char *tgt,
                   unsigned char cnt)
{
  for (int i = 0; i < 6; i++)
    cs[i] = ((int)org[i] * (255-cnt) + (int)tgt[i] * cnt) / 255;
}

void applyColor()
{
  for (int i = 0; i < 6; i++)
    analogWrite(led[i], 255-cs[i]);
}


void setup()
{
  for (int i = 0; i < 6; i++) {
    pinMode(led[i], OUTPUT);
    digitalWrite(led[i], HIGH);
  }
  Serial.begin(9600);
  Serial.print(id);
  Serial.print(" start\n");
}

void loop()
{
  if (auto_idle && mode == 1 && millis() - idle > 15000) {
    copy(orig, cs);
    mode = 0;
    cnt = 0;
  }

  while (Serial.available()) {
    char dat = Serial.read();
    if (!dat || dat == '\n') {
      id_match = 0;
      input_cnt = 0;
      tmp = -1;
    }
    else if (id_match < 8) {
      if (dat == id[id_match])
        id_match++;
      else
        id_match = 0;
    }
    else if (dat == 'I' || dat == 'i' || dat == 'X' || dat == 'x') {
      if (dat == 'I' || dat == 'i') {
        cnt = 0;
        phase = (ID - 1)*2;
        mode = 0;
        auto_idle = 1;
        copy(orig, cs);
      }
      if (dat == 'X' || dat == 'x') {
        auto_idle = 0;
      }
      id_match = 0;
      input_cnt = 0;
      tmp = -1;
    }
    else if (tmp < 0) {
      tmp = hex2int(dat, 0);
    } else {
      int col = hex2int(dat, tmp);
      if (col >= 0) {
        inputs[input_cnt++] = col;
        tmp = -1;
      }
    }
    if (input_cnt == 6) {
      id_match = 0;
      input_cnt = 0;
      copy(orig, cs);
      copy(target, inputs);
      cnt = 0;
      mode = 1;
      idle = millis();
    }
  }
  
  if (mode == 0) {
    calcNextColor(orig, ts[phase], cnt++);
    if (!cnt) {
      copy(orig, cs);
      phase = (phase + 1) % 6;
    }
    applyColor();
    delay(10);
  } else {
    calcNextColor(orig, target, cnt);
    cnt += 4;
    if (!cnt)
      copy(orig,cs);
    applyColor();
    delay(5);
  }
}


Create a new paste based on this one


Comments: