bob3.h 3.06 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
#ifndef _BOB3_H_
#define _BOB3_H_

#include <avr/io.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <bob3/utils.h>

void setup();
void loop();

enum {
  ARM_NONE,
  ARM_TOP,
  ARM_MID_TOP,
  ARM_MID_BOT,
  ARM_BOT
};

enum {
  ARMS_OFF      = 0,
  ARMS_TOUCH    = 1,
  ARMS_DETECTOR = 2
};


enum {
  OFF            = 0x000,
  WHITE          = 0xfff,
  ON             = 0xfff,
  RED            = 0xf00,
  GREEN          = 0x0f0,
  BLUE           = 0x00f,
  YELLOW         = 0xff0,
  CYAN           = 0x0ff,
  FUCHSIA        = 0xf0f,
  
  ORANGE         = 0xf80,
  KABARED        = 0xd42,
  PURPLE         = 0xf08,
  VIOLET         = 0x63a,
  AQUAMARINE     = 0x7fd,
  BROWN          = 0xa33,
  CORAL          = 0xf75,
  CORNFLOWERBLUE = 0x69e,
  STEELBLUE      = 0x48a,
  ROYALBLUE      = 0x46e,
  FORESTGREEN    = 0x282,
  SEAGREEN       = 0x5f9,
  COLERED        = 0x100,
  UNICORN        = 0x609
};


enum {
  ARM_1  = 1,
  ARM_2  = 2,
  EYE_1  = 1,
  EYE_2  = 2,
  LED_3 = 3,
  LED_4 = 4
};


class Bob3 {
  public:
    void init();
    void setLed(uint8_t id, uint16_t color);
    uint16_t getLed(uint8_t id);
    void setEyes(uint16_t eye1, uint16_t eye2) {setLed(1, eye1); setLed(2, eye2);};
    void setWhiteLeds(uint16_t wled1, uint16_t wled2) {setLed(3, wled1); setLed(4, wled2);};
    uint16_t getIRSensor();
    uint16_t getIRLight();
    void enableIRSensor(bool enable);
    uint8_t getArm(uint8_t id);
    void enableArms(uint8_t enable);
    uint16_t getTemperature();
    uint16_t getMillivolt();
    uint8_t getID();
    int16_t receiveMessage(uint8_t carrier, uint16_t timeout);
    void transmitMessage(uint8_t carrier, uint8_t message);
    int16_t receiveMessage(uint16_t timeout) {return receiveMessage(0, timeout);}
    void transmitMessage(uint8_t message) {transmitMessage(0, message);}

    // compatibility:
    int16_t receiveIRCode(uint8_t carrier, uint16_t timeout) {return receiveMessage(carrier, timeout);}
    void transmitIRCode(uint8_t carrier, uint8_t message) {transmitMessage(carrier, message);}
    int16_t receiveIRCode(uint16_t timeout) {return receiveMessage(0, timeout);}
    void transmitIRCode(uint8_t message) {transmitMessage(0, message);}
};



extern Bob3 bob3;

//#ifndef ARDUINO
inline static 
void delay(uint16_t ms) {while (ms--) _delay_ms(1);}
//#endif

inline static uint16_t rgb(uint8_t r, uint8_t g, uint8_t b) __attribute__((const));

inline static 
uint16_t rgb(uint8_t r, uint8_t g, uint8_t b) {return ((uint16_t)r<<8)|(g<<4)|b;}

uint16_t mixColor(uint16_t color1, uint16_t color2, uint8_t w1, uint8_t w2);

uint16_t hsv(int16_t h, uint8_t s, uint8_t v);

uint16_t hsvx(uint8_t h, uint8_t s, uint8_t v);

uint16_t randomNumber(uint16_t lo, uint16_t hi);

uint16_t randomBits(uint8_t zeros, uint8_t ones);

uint32_t random32();


static inline 
uint8_t random8() {
  return random32() & 0xff;
}


static inline 
uint16_t random16() {
  return random32() & 0xffff;
}

static inline 
void remember(int16_t value) {eeprom_update_word ((uint16_t *)(E2END-1), ~value);}

static inline 
int16_t recall() {return ~eeprom_read_word ((const uint16_t *)(E2END-1));}

#endif