bob3.cpp 7.02 KB
Newer Older
springob's avatar
springob committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

#include "iodefs.h"
#include "analog.h"
#include "leds.h"
#include "bob3.h"
#include "utils.h"
#include "ircom.h"
#include <stdlib.h>
#include <avr/io.h>
#include <avr/boot.h>

#ifndef SIGRD
#define SIGRD 5
#endif

Bob3 bob3;

uint8_t _bob3_revision;

uint8_t _arm_mode;

void Bob3::init() {
  uint8_t sig2 = boot_signature_byte_get(4);
  if (sig2==0x0a) _bob3_revision = 102;  // V1.02
  if (sig2==0x0f) _bob3_revision = 103;  // V1.03
  leds_init();
  analog_init();
  leds_set_RGBx(1, OFF);
  leds_set_RGBx(2, OFF);
  _arm_mode = 1;
}

void Bob3::setLed(uint8_t id, uint16_t color) {
  switch (id) {
    case 1:
    case 2:
      leds_set_RGBx(id, color);
      break;
    case 3:
      set_output_bitval(IO_LED_1, color);
      break;
    case 4:
      set_output_bitval(IO_LED_2, color);
      break;
  }
}


uint16_t Bob3::getLed(uint8_t id) {
  if (id<=2) {
    return leds_get_RGBx(id);
  } else if (id==3) {
    return get_output_bit(IO_LED_1)?ON:OFF;
  } else if (id==4) {
    return get_output_bit(IO_LED_2)?ON:OFF;
  }
  return 0;
}



uint16_t Bob3::getIRSensor() {
  return analog_getValueExt(ANALOG_IR, 2);
}


uint16_t Bob3::getIRLight() {
  return analog_getValueExt(ANALOG_IR, 0);
}


void Bob3::enableIRSensor(bool enable) {
  
}


uint8_t Bob3::getArm(uint8_t id) {
  if (_arm_mode==ARMS_DETECTOR) {
    uint16_t v0;
    if (id==1) {
      v0 = analog_getValueExt(ANALOG_L0, 0);
    } else if (id==2) {
      v0 = analog_getValueExt(ANALOG_R0, 0);
    } else {
      return 0;
    }
    // 4095 = maxval
    // ca. 1100 = untouched
    // 0 = touched
    v0 /= 4; // better range...

    // 0: untouched
    // 255: touched max
    if (v0>255) return 0;
    return 255-v0;
    
  } else {
    uint8_t v0, v1, v2, v3;
    if (id==1) {
      v0 = analog_getValueExt(ANALOG_L0, 0);
      v1 = analog_getValueExt(ANALOG_L1, 0);
      v2 = analog_getValueExt(ANALOG_L2, 0);
      v3 = analog_getValueExt(ANALOG_L3, 0);
    } else if (id==2) {
      v0 = analog_getValueExt(ANALOG_R0, 0);
      v1 = analog_getValueExt(ANALOG_R1, 0);
      v2 = analog_getValueExt(ANALOG_R2, 0);
      v3 = analog_getValueExt(ANALOG_R3, 0);
    } else {
      return 0;
    }
  
    if (v0&0x01) return 1;
    if (v1&0x01) return 2;
    if (v2&0x01) return 2;
    if (v3&0x01) return 3;
    return 0;
  }
}


void Bob3::enableArms(uint8_t enable) {
  _arm_mode = enable;
}


uint16_t Bob3::getTemperature() {
  return analog_getValueExt(ANALOG_TEMP, 0)/4;
}


uint16_t Bob3::getMillivolt() {
  // ADC_BANDGAP_CHANNEL_VOLTAGE must be below 1.28 V!!!
  uint16_t voltage = analog_getValueExt(ANALOG_VOLT, 0);
  voltage = ((uint16_t)(50*ADC_BANDGAP_CHANNEL_VOLTAGE*1024))/voltage;
  voltage *= 20;
  return voltage;
}


uint8_t Bob3::getID() {
  uint8_t id=0;
  deactivate_output_bit(IO_ID_0);
  deactivate_output_bit(IO_ID_1);
  deactivate_output_bit(IO_ID_2);
  deactivate_output_bit(IO_ID_3);
  deactivate_output_bit(IO_ID_4);
  set_output_bit(IO_ID_0);
  set_output_bit(IO_ID_1);
  set_output_bit(IO_ID_2);
  set_output_bit(IO_ID_3);
  set_output_bit(IO_ID_4);
  id += get_input_bit(IO_ID_0)?0:1;
  id += get_input_bit(IO_ID_1)?0:2;
  id += get_input_bit(IO_ID_2)?0:4;
  id += get_input_bit(IO_ID_3)?0:8;
  id += get_input_bit(IO_ID_4)?0:16;
  clear_output_bit(IO_ID_0);
  clear_output_bit(IO_ID_1);
  clear_output_bit(IO_ID_2);
  clear_output_bit(IO_ID_3);
  clear_output_bit(IO_ID_4);  
  return id;
}


int16_t Bob3::receiveMessage(uint8_t carrier, uint16_t timeout) {
  int16_t code;
  uint8_t an_bak = analog_enable(false);
  code = ircom_receive(carrier, timeout);  
  analog_enable(an_bak);
  return code;
}


void Bob3::transmitMessage(uint8_t carrier, uint8_t code) {
  uint8_t an_bak = analog_enable(false);
  ircom_transmit(carrier, code);
  analog_enable(an_bak);
}


static uint16_t mixColor16(uint16_t color1, uint16_t color2, uint8_t w1, uint8_t w2) {
  uint8_t r = (((uint8_t(color1>>8))&0x0f)*w1 + ((uint8_t(color2>>8))&0x0f)*w2)/16;
  uint8_t g = (((uint8_t(color1>>4))&0x0f)*w1 + ((uint8_t(color2>>4))&0x0f)*w2)/16;
  uint8_t b = (((uint8_t(color1>>0))&0x0f)*w1 + ((uint8_t(color2>>0))&0x0f)*w2)/16;
  return rgb(r, g, b);
}


uint16_t mixColor(uint16_t color1, uint16_t color2, uint8_t w1, uint8_t w2) {
  if (w1==0) return color2;
  if (w2==0) return color1;
  uint8_t sum=w1+w2;
  if (sum==16) return mixColor16(color1, color2, w1, w2);
  uint8_t r = (((color1>>8)&0x0f)*w1 + ((color2>>8)&0x0f)*w2)/sum;
  uint8_t g = (((color1>>4)&0x0f)*w1 + ((color2>>4)&0x0f)*w2)/sum;
  uint8_t b = (((color1>>0)&0x0f)*w1 + ((color2>>0)&0x0f)*w2)/sum;
  return rgb(r, g, b);  
}



uint16_t hsvx(uint8_t h, uint8_t _s, uint8_t _v) { 
  if (_s == 0) {
    _v >>= 4;
    return rgb(_v,_v,_v);
  }  
  
  uint16_t v = _v;
  uint16_t s = _s;
  uint8_t seg, delta;

  if      (h>=213) { seg=5; delta = 6*(h-213); }
  else if (h>=170) { seg=4; delta = 6*(h-170); } 
  else if (h>=128) { seg=3; delta = 6*(h-128); }
  else if (h>=85)  { seg=2; delta = 6*(h-85); }
  else if (h>=42)  { seg=1; delta = 6*(h-42); }
  else             { seg=0; delta = 6*(h-0); }

  uint8_t p = (v * (255 - s)) >> 12;
  uint8_t q = (v * (255 - ((s * delta) >> 8))) >> 12;
  uint8_t t = (v * (255 - ((s * (255 - delta)) >> 8))) >> 12;
 
  v >>= 4;
  
  switch (seg) {
    case 0: return rgb(v, t, p);
    case 1: return rgb(q, v, p);
    case 2: return rgb(p, v, t);
    case 3: return rgb(p, q, v);
    case 4: return rgb(t, p, v);
    case 5: return rgb(v, p, q);
  }
  return 0;
}


uint16_t hsv(int16_t _h, uint8_t _s, uint8_t _v) { 
  // _h: [0..359] degree (tolarant)
  // _s: [0..100] percent
  // _v: [0..100] percent
  
  if (_s == 0) {
    _v = 2*_v + _v/2; // Range: [0...250]
    _v >>= 4;
    return rgb(_v,_v,_v);
  }  
  
  uint16_t v = 2*_v + _v/2; // Range: [0...250]
  uint16_t s = 2*_s + _s/2; // Range: [0...250]
  uint8_t seg, delta;

  while (_h<0) _h += 360;
  while (_h>=360) _h -= 360;
  
  if      (_h>=300) { seg=5; delta = _h-300; }
  else if (_h>=240) { seg=4; delta = _h-240; } 
  else if (_h>=180) { seg=3; delta = _h-180; }
  else if (_h>=120) { seg=2; delta = _h-120; }
  else if (_h>=60)  { seg=1; delta = _h-60; }
  else              { seg=0; delta = _h-0; }
  
  delta = 4*delta + delta/4; // Range: [0...250]

  uint8_t p = (v * (255 - s)) >> 12;
  uint8_t q = (v * (255 - ((s * delta) >> 8))) >> 12;
  uint8_t t = (v * (255 - ((s * (255 - delta)) >> 8))) >> 12;
 
  v >>= 4;
  
  switch (seg) {
    case 0: return rgb(v, t, p);
    case 1: return rgb(q, v, p);
    case 2: return rgb(p, v, t);
    case 3: return rgb(p, q, v);
    case 4: return rgb(t, p, v);
    case 5: return rgb(v, p, q);
  }
  return 0;
}



uint32_t random32() {
  static unsigned long ctx = 1;
  ctx += analog_getRandomSeed();
  return random_r(&ctx);
}


uint16_t randomNumber(uint16_t lo, uint16_t hi) {
  uint16_t x = random16();
  if ((lo==0x0000) && (hi==0xffff)) return x;
  if (hi<lo) return 0;
  uint16_t range = hi-lo+1;
  x = x%range;
  return x+lo;
}


uint16_t randomBits(uint8_t zeros, uint8_t ones) {
  uint16_t val = 0;
  uint8_t rounds = zeros+ones;
  while (rounds--) {
    val<<=1;
    if (randomNumber(0, rounds) < ones) {
      ones--;
      val |= 0x01;
    }
  }
  return val;
}