tes ir atas semua
Dependencies: mbed ADS1115 StepperMotor SRF05 TPA81new
AMG8833/AMG8833.cpp@42:3b682b7d0388, 2019-02-11 (annotated)
- Committer:
- hisyamfs
- Date:
- Mon Feb 11 12:49:36 2019 +0000
- Revision:
- 42:3b682b7d0388
Ganti TPA81 dengan AMG8833
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hisyamfs | 42:3b682b7d0388 | 1 | #include "AMG8833.h" |
hisyamfs | 42:3b682b7d0388 | 2 | |
hisyamfs | 42:3b682b7d0388 | 3 | //#define I2C_DEBUG |
hisyamfs | 42:3b682b7d0388 | 4 | |
hisyamfs | 42:3b682b7d0388 | 5 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 6 | /*! |
hisyamfs | 42:3b682b7d0388 | 7 | @brief Setups the I2C interface and hardware |
hisyamfs | 42:3b682b7d0388 | 8 | @param addr Optional I2C address the sensor can be found on. Default is 0x69 |
hisyamfs | 42:3b682b7d0388 | 9 | @returns True if device is set up, false on any failure |
hisyamfs | 42:3b682b7d0388 | 10 | */ |
hisyamfs | 42:3b682b7d0388 | 11 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 12 | Adafruit_AMG88xx::Adafruit_AMG88xx(PinName mysda, PinName myscl) : _i2c(mysda, myscl) |
hisyamfs | 42:3b682b7d0388 | 13 | { |
hisyamfs | 42:3b682b7d0388 | 14 | |
hisyamfs | 42:3b682b7d0388 | 15 | } |
hisyamfs | 42:3b682b7d0388 | 16 | |
hisyamfs | 42:3b682b7d0388 | 17 | int Adafruit_AMG88xx::begin(char addr) |
hisyamfs | 42:3b682b7d0388 | 18 | { |
hisyamfs | 42:3b682b7d0388 | 19 | _i2caddr = addr; |
hisyamfs | 42:3b682b7d0388 | 20 | |
hisyamfs | 42:3b682b7d0388 | 21 | //enter normal mode |
hisyamfs | 42:3b682b7d0388 | 22 | _pctl.PCTL = AMG88xx_NORMAL_MODE; |
hisyamfs | 42:3b682b7d0388 | 23 | write8(AMG88xx_PCTL, _pctl.get()); |
hisyamfs | 42:3b682b7d0388 | 24 | |
hisyamfs | 42:3b682b7d0388 | 25 | //software reset |
hisyamfs | 42:3b682b7d0388 | 26 | _rst.RST = AMG88xx_INITIAL_RESET; |
hisyamfs | 42:3b682b7d0388 | 27 | write8(AMG88xx_RST, _rst.get()); |
hisyamfs | 42:3b682b7d0388 | 28 | |
hisyamfs | 42:3b682b7d0388 | 29 | //disable interrupts by default |
hisyamfs | 42:3b682b7d0388 | 30 | disableInterrupt(); |
hisyamfs | 42:3b682b7d0388 | 31 | |
hisyamfs | 42:3b682b7d0388 | 32 | //set to 10 FPS |
hisyamfs | 42:3b682b7d0388 | 33 | _fpsc.FPS = AMG88xx_FPS_10; |
hisyamfs | 42:3b682b7d0388 | 34 | write8(AMG88xx_FPSC, _fpsc.get()); |
hisyamfs | 42:3b682b7d0388 | 35 | |
hisyamfs | 42:3b682b7d0388 | 36 | wait_ms(100); |
hisyamfs | 42:3b682b7d0388 | 37 | |
hisyamfs | 42:3b682b7d0388 | 38 | return 1; |
hisyamfs | 42:3b682b7d0388 | 39 | } |
hisyamfs | 42:3b682b7d0388 | 40 | |
hisyamfs | 42:3b682b7d0388 | 41 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 42 | /*! |
hisyamfs | 42:3b682b7d0388 | 43 | @brief Set the moving average mode. |
hisyamfs | 42:3b682b7d0388 | 44 | @param mode if True is passed, output will be twice the moving average |
hisyamfs | 42:3b682b7d0388 | 45 | */ |
hisyamfs | 42:3b682b7d0388 | 46 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 47 | void Adafruit_AMG88xx::setMovingAverageMode(int mode) |
hisyamfs | 42:3b682b7d0388 | 48 | { |
hisyamfs | 42:3b682b7d0388 | 49 | _ave.MAMOD = mode; |
hisyamfs | 42:3b682b7d0388 | 50 | write8(AMG88xx_AVE, _ave.get()); |
hisyamfs | 42:3b682b7d0388 | 51 | } |
hisyamfs | 42:3b682b7d0388 | 52 | |
hisyamfs | 42:3b682b7d0388 | 53 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 54 | /*! |
hisyamfs | 42:3b682b7d0388 | 55 | @brief Set the interrupt levels. The hysteresis value defaults to .95 * high |
hisyamfs | 42:3b682b7d0388 | 56 | @param high the value above which an interrupt will be triggered |
hisyamfs | 42:3b682b7d0388 | 57 | @param low the value below which an interrupt will be triggered |
hisyamfs | 42:3b682b7d0388 | 58 | */ |
hisyamfs | 42:3b682b7d0388 | 59 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 60 | void Adafruit_AMG88xx::setInterruptLevels(float high, float low) |
hisyamfs | 42:3b682b7d0388 | 61 | { |
hisyamfs | 42:3b682b7d0388 | 62 | setInterruptLevels(high, low, high * .95); |
hisyamfs | 42:3b682b7d0388 | 63 | } |
hisyamfs | 42:3b682b7d0388 | 64 | |
hisyamfs | 42:3b682b7d0388 | 65 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 66 | /*! |
hisyamfs | 42:3b682b7d0388 | 67 | @brief Set the interrupt levels |
hisyamfs | 42:3b682b7d0388 | 68 | @param high the value above which an interrupt will be triggered |
hisyamfs | 42:3b682b7d0388 | 69 | @param low the value below which an interrupt will be triggered |
hisyamfs | 42:3b682b7d0388 | 70 | @param hysteresis the hysteresis value for interrupt detection |
hisyamfs | 42:3b682b7d0388 | 71 | */ |
hisyamfs | 42:3b682b7d0388 | 72 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 73 | void Adafruit_AMG88xx::setInterruptLevels(float high, float low, float hysteresis) |
hisyamfs | 42:3b682b7d0388 | 74 | { |
hisyamfs | 42:3b682b7d0388 | 75 | int highConv = high / AMG88xx_PIXEL_TEMP_CONVERSION; |
hisyamfs | 42:3b682b7d0388 | 76 | highConv = constrain(highConv, -4095, 4095); |
hisyamfs | 42:3b682b7d0388 | 77 | _inthl.INT_LVL_H = highConv & 0xFF; |
hisyamfs | 42:3b682b7d0388 | 78 | _inthh.INT_LVL_H = (highConv & 0xF) >> 4; |
hisyamfs | 42:3b682b7d0388 | 79 | this->write8(AMG88xx_INTHL, _inthl.get()); |
hisyamfs | 42:3b682b7d0388 | 80 | this->write8(AMG88xx_INTHH, _inthh.get()); |
hisyamfs | 42:3b682b7d0388 | 81 | |
hisyamfs | 42:3b682b7d0388 | 82 | int lowConv = low / AMG88xx_PIXEL_TEMP_CONVERSION; |
hisyamfs | 42:3b682b7d0388 | 83 | lowConv = constrain(lowConv, -4095, 4095); |
hisyamfs | 42:3b682b7d0388 | 84 | _intll.INT_LVL_L = lowConv & 0xFF; |
hisyamfs | 42:3b682b7d0388 | 85 | _intlh.INT_LVL_L = (lowConv & 0xF) >> 4; |
hisyamfs | 42:3b682b7d0388 | 86 | this->write8(AMG88xx_INTLL, _intll.get()); |
hisyamfs | 42:3b682b7d0388 | 87 | this->write8(AMG88xx_INTLH, _intlh.get()); |
hisyamfs | 42:3b682b7d0388 | 88 | |
hisyamfs | 42:3b682b7d0388 | 89 | int hysConv = hysteresis / AMG88xx_PIXEL_TEMP_CONVERSION; |
hisyamfs | 42:3b682b7d0388 | 90 | hysConv = constrain(hysConv, -4095, 4095); |
hisyamfs | 42:3b682b7d0388 | 91 | _ihysl.INT_HYS = hysConv & 0xFF; |
hisyamfs | 42:3b682b7d0388 | 92 | _ihysh.INT_HYS = (hysConv & 0xF) >> 4; |
hisyamfs | 42:3b682b7d0388 | 93 | this->write8(AMG88xx_IHYSL, _ihysl.get()); |
hisyamfs | 42:3b682b7d0388 | 94 | this->write8(AMG88xx_IHYSH, _ihysh.get()); |
hisyamfs | 42:3b682b7d0388 | 95 | } |
hisyamfs | 42:3b682b7d0388 | 96 | |
hisyamfs | 42:3b682b7d0388 | 97 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 98 | /*! |
hisyamfs | 42:3b682b7d0388 | 99 | @brief enable the interrupt pin on the device. |
hisyamfs | 42:3b682b7d0388 | 100 | */ |
hisyamfs | 42:3b682b7d0388 | 101 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 102 | void Adafruit_AMG88xx::enableInterrupt() |
hisyamfs | 42:3b682b7d0388 | 103 | { |
hisyamfs | 42:3b682b7d0388 | 104 | _intc.INTEN = 1; |
hisyamfs | 42:3b682b7d0388 | 105 | this->write8(AMG88xx_INTC, _intc.get()); |
hisyamfs | 42:3b682b7d0388 | 106 | } |
hisyamfs | 42:3b682b7d0388 | 107 | |
hisyamfs | 42:3b682b7d0388 | 108 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 109 | /*! |
hisyamfs | 42:3b682b7d0388 | 110 | @brief disable the interrupt pin on the device |
hisyamfs | 42:3b682b7d0388 | 111 | */ |
hisyamfs | 42:3b682b7d0388 | 112 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 113 | void Adafruit_AMG88xx::disableInterrupt() |
hisyamfs | 42:3b682b7d0388 | 114 | { |
hisyamfs | 42:3b682b7d0388 | 115 | _intc.INTEN = 0; |
hisyamfs | 42:3b682b7d0388 | 116 | this->write8(AMG88xx_INTC, _intc.get()); |
hisyamfs | 42:3b682b7d0388 | 117 | } |
hisyamfs | 42:3b682b7d0388 | 118 | |
hisyamfs | 42:3b682b7d0388 | 119 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 120 | /*! |
hisyamfs | 42:3b682b7d0388 | 121 | @brief Set the interrupt to either absolute value or difference mode |
hisyamfs | 42:3b682b7d0388 | 122 | @param mode passing AMG88xx_DIFFERENCE sets the device to difference mode, AMG88xx_ABSOLUTE_VALUE sets to absolute value mode. |
hisyamfs | 42:3b682b7d0388 | 123 | */ |
hisyamfs | 42:3b682b7d0388 | 124 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 125 | void Adafruit_AMG88xx::setInterruptMode(uint8_t mode) |
hisyamfs | 42:3b682b7d0388 | 126 | { |
hisyamfs | 42:3b682b7d0388 | 127 | _intc.INTMOD = mode; |
hisyamfs | 42:3b682b7d0388 | 128 | this->write8(AMG88xx_INTC, _intc.get()); |
hisyamfs | 42:3b682b7d0388 | 129 | } |
hisyamfs | 42:3b682b7d0388 | 130 | |
hisyamfs | 42:3b682b7d0388 | 131 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 132 | /*! |
hisyamfs | 42:3b682b7d0388 | 133 | @brief Read the state of the triggered interrupts on the device. The full interrupt register is 8 chars in length. |
hisyamfs | 42:3b682b7d0388 | 134 | @param buf the pointer to where the returned data will be stored |
hisyamfs | 42:3b682b7d0388 | 135 | @param size Optional number of chars to read. Default is 8 chars. |
hisyamfs | 42:3b682b7d0388 | 136 | @returns up to 8 chars of data in buf |
hisyamfs | 42:3b682b7d0388 | 137 | */ |
hisyamfs | 42:3b682b7d0388 | 138 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 139 | void Adafruit_AMG88xx::getInterrupt(uint8_t *buf, uint8_t size) |
hisyamfs | 42:3b682b7d0388 | 140 | { |
hisyamfs | 42:3b682b7d0388 | 141 | uint8_t charsToRead = min(size, (uint8_t)8); |
hisyamfs | 42:3b682b7d0388 | 142 | |
hisyamfs | 42:3b682b7d0388 | 143 | this->read(AMG88xx_INT_OFFSET, buf, charsToRead); |
hisyamfs | 42:3b682b7d0388 | 144 | } |
hisyamfs | 42:3b682b7d0388 | 145 | |
hisyamfs | 42:3b682b7d0388 | 146 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 147 | /*! |
hisyamfs | 42:3b682b7d0388 | 148 | @brief Clear any triggered interrupts |
hisyamfs | 42:3b682b7d0388 | 149 | */ |
hisyamfs | 42:3b682b7d0388 | 150 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 151 | void Adafruit_AMG88xx::clearInterrupt() |
hisyamfs | 42:3b682b7d0388 | 152 | { |
hisyamfs | 42:3b682b7d0388 | 153 | _rst.RST = AMG88xx_FLAG_RESET; |
hisyamfs | 42:3b682b7d0388 | 154 | write8(AMG88xx_RST, _rst.get()); |
hisyamfs | 42:3b682b7d0388 | 155 | } |
hisyamfs | 42:3b682b7d0388 | 156 | |
hisyamfs | 42:3b682b7d0388 | 157 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 158 | /*! |
hisyamfs | 42:3b682b7d0388 | 159 | @brief read the onboard thermistor |
hisyamfs | 42:3b682b7d0388 | 160 | @returns a the floating point temperature in degrees Celsius |
hisyamfs | 42:3b682b7d0388 | 161 | */ |
hisyamfs | 42:3b682b7d0388 | 162 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 163 | float Adafruit_AMG88xx::readThermistor() |
hisyamfs | 42:3b682b7d0388 | 164 | { |
hisyamfs | 42:3b682b7d0388 | 165 | uint8_t raw[2]; |
hisyamfs | 42:3b682b7d0388 | 166 | this->read(AMG88xx_TTHL, raw, 2); |
hisyamfs | 42:3b682b7d0388 | 167 | uint16_t recast = ((uint16_t) raw[1] << 8) | ((uint16_t) raw[0]); |
hisyamfs | 42:3b682b7d0388 | 168 | |
hisyamfs | 42:3b682b7d0388 | 169 | return signedMag12ToFloat(recast) * AMG88xx_THERMISTOR_CONVERSION; |
hisyamfs | 42:3b682b7d0388 | 170 | } |
hisyamfs | 42:3b682b7d0388 | 171 | |
hisyamfs | 42:3b682b7d0388 | 172 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 173 | /*! |
hisyamfs | 42:3b682b7d0388 | 174 | @brief Read Infrared sensor values |
hisyamfs | 42:3b682b7d0388 | 175 | @param buf the array to place the pixels in |
hisyamfs | 42:3b682b7d0388 | 176 | @param size Optionsl number of chars to read (up to 64). Default is 64 chars. |
hisyamfs | 42:3b682b7d0388 | 177 | @return up to 64 chars of pixel data in buf |
hisyamfs | 42:3b682b7d0388 | 178 | */ |
hisyamfs | 42:3b682b7d0388 | 179 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 180 | void Adafruit_AMG88xx::readPixels(float *buf, uint8_t size) |
hisyamfs | 42:3b682b7d0388 | 181 | { |
hisyamfs | 42:3b682b7d0388 | 182 | uint16_t recast; |
hisyamfs | 42:3b682b7d0388 | 183 | float converted; |
hisyamfs | 42:3b682b7d0388 | 184 | uint8_t charsToRead = min((uint8_t)(size << 1), (uint8_t)(AMG88xx_PIXEL_ARRAY_SIZE << 1)); |
hisyamfs | 42:3b682b7d0388 | 185 | uint8_t rawArray[charsToRead]; |
hisyamfs | 42:3b682b7d0388 | 186 | this->read(AMG88xx_PIXEL_OFFSET, rawArray, charsToRead); |
hisyamfs | 42:3b682b7d0388 | 187 | |
hisyamfs | 42:3b682b7d0388 | 188 | for(int i=0; i<size; i++){ |
hisyamfs | 42:3b682b7d0388 | 189 | uint8_t pos = i << 1; |
hisyamfs | 42:3b682b7d0388 | 190 | recast = ((uint16_t)rawArray[pos + 1] << 8) | ((uint16_t)rawArray[pos]); |
hisyamfs | 42:3b682b7d0388 | 191 | |
hisyamfs | 42:3b682b7d0388 | 192 | converted = int12ToFloat(recast) * AMG88xx_PIXEL_TEMP_CONVERSION; |
hisyamfs | 42:3b682b7d0388 | 193 | buf[i] = converted; |
hisyamfs | 42:3b682b7d0388 | 194 | } |
hisyamfs | 42:3b682b7d0388 | 195 | } |
hisyamfs | 42:3b682b7d0388 | 196 | |
hisyamfs | 42:3b682b7d0388 | 197 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 198 | /*! |
hisyamfs | 42:3b682b7d0388 | 199 | @brief write one char of data to the specified register |
hisyamfs | 42:3b682b7d0388 | 200 | @param reg the register to write to |
hisyamfs | 42:3b682b7d0388 | 201 | @param value the value to write |
hisyamfs | 42:3b682b7d0388 | 202 | */ |
hisyamfs | 42:3b682b7d0388 | 203 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 204 | void Adafruit_AMG88xx::write8(uint8_t reg, uint8_t value) |
hisyamfs | 42:3b682b7d0388 | 205 | { |
hisyamfs | 42:3b682b7d0388 | 206 | this->write(reg, &value, 1); |
hisyamfs | 42:3b682b7d0388 | 207 | } |
hisyamfs | 42:3b682b7d0388 | 208 | |
hisyamfs | 42:3b682b7d0388 | 209 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 210 | /*! |
hisyamfs | 42:3b682b7d0388 | 211 | @brief read one char of data from the specified register |
hisyamfs | 42:3b682b7d0388 | 212 | @param reg the register to read |
hisyamfs | 42:3b682b7d0388 | 213 | @returns one char of register data |
hisyamfs | 42:3b682b7d0388 | 214 | */ |
hisyamfs | 42:3b682b7d0388 | 215 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 216 | uint8_t Adafruit_AMG88xx::read8(uint8_t reg) |
hisyamfs | 42:3b682b7d0388 | 217 | { |
hisyamfs | 42:3b682b7d0388 | 218 | uint8_t ret; |
hisyamfs | 42:3b682b7d0388 | 219 | this->read(reg, &ret, 1); |
hisyamfs | 42:3b682b7d0388 | 220 | return ret; |
hisyamfs | 42:3b682b7d0388 | 221 | } |
hisyamfs | 42:3b682b7d0388 | 222 | |
hisyamfs | 42:3b682b7d0388 | 223 | int Adafruit_AMG88xx::read(uint8_t reg, uint8_t *buf, uint8_t num) |
hisyamfs | 42:3b682b7d0388 | 224 | { |
hisyamfs | 42:3b682b7d0388 | 225 | // char value; |
hisyamfs | 42:3b682b7d0388 | 226 | // char pos = 0; |
hisyamfs | 42:3b682b7d0388 | 227 | |
hisyamfs | 42:3b682b7d0388 | 228 | // //on arduino we need to read in AMG_I2C_CHUNKSIZE char chunks |
hisyamfs | 42:3b682b7d0388 | 229 | // while(pos < num){ |
hisyamfs | 42:3b682b7d0388 | 230 | // char read_now = min((char)AMG_I2C_CHUNKSIZE, (char)(num - pos)); |
hisyamfs | 42:3b682b7d0388 | 231 | // Wire.beginTransmission((char)_i2caddr); |
hisyamfs | 42:3b682b7d0388 | 232 | // Wire.write((char)reg + pos); |
hisyamfs | 42:3b682b7d0388 | 233 | // Wire.endTransmission(); |
hisyamfs | 42:3b682b7d0388 | 234 | // Wire.requestFrom((char)_i2caddr, read_now); |
hisyamfs | 42:3b682b7d0388 | 235 | |
hisyamfs | 42:3b682b7d0388 | 236 | // #ifdef I2C_DEBUG |
hisyamfs | 42:3b682b7d0388 | 237 | // Serial.print("[$"); Serial.print(reg + pos, HEX); Serial.print("] -> "); |
hisyamfs | 42:3b682b7d0388 | 238 | // #endif |
hisyamfs | 42:3b682b7d0388 | 239 | // for(int i=0; i<read_now; i++){ |
hisyamfs | 42:3b682b7d0388 | 240 | // buf[pos] = Wire.read(); |
hisyamfs | 42:3b682b7d0388 | 241 | // #ifdef I2C_DEBUG |
hisyamfs | 42:3b682b7d0388 | 242 | // Serial.print("0x"); Serial.print(buf[pos], HEX); Serial.print(", "); |
hisyamfs | 42:3b682b7d0388 | 243 | // #endif |
hisyamfs | 42:3b682b7d0388 | 244 | // pos++; |
hisyamfs | 42:3b682b7d0388 | 245 | // } |
hisyamfs | 42:3b682b7d0388 | 246 | // #ifdef I2C_DEBUG |
hisyamfs | 42:3b682b7d0388 | 247 | // Serial.println(); |
hisyamfs | 42:3b682b7d0388 | 248 | // #endif |
hisyamfs | 42:3b682b7d0388 | 249 | // } |
hisyamfs | 42:3b682b7d0388 | 250 | char reg_dest = reg; |
hisyamfs | 42:3b682b7d0388 | 251 | char rx_buf[num]; |
hisyamfs | 42:3b682b7d0388 | 252 | int nack = _i2c.write(_i2caddr, ®_dest, 1, 1); // no stop |
hisyamfs | 42:3b682b7d0388 | 253 | nack = _i2c.read(_i2caddr, rx_buf, num); |
hisyamfs | 42:3b682b7d0388 | 254 | for (int i = 0; i < num; i++) { |
hisyamfs | 42:3b682b7d0388 | 255 | buf[i] = (uint8_t) rx_buf[i]; |
hisyamfs | 42:3b682b7d0388 | 256 | } |
hisyamfs | 42:3b682b7d0388 | 257 | return nack; |
hisyamfs | 42:3b682b7d0388 | 258 | } |
hisyamfs | 42:3b682b7d0388 | 259 | |
hisyamfs | 42:3b682b7d0388 | 260 | int Adafruit_AMG88xx::write(uint8_t reg, uint8_t *buf, uint8_t num) |
hisyamfs | 42:3b682b7d0388 | 261 | { |
hisyamfs | 42:3b682b7d0388 | 262 | // #ifdef I2C_DEBUG |
hisyamfs | 42:3b682b7d0388 | 263 | // Serial.print("[$"); Serial.print(reg, HEX); Serial.print("] <- "); |
hisyamfs | 42:3b682b7d0388 | 264 | // #endif |
hisyamfs | 42:3b682b7d0388 | 265 | // Wire.beginTransmission((char)_i2caddr); |
hisyamfs | 42:3b682b7d0388 | 266 | // Wire.write((char)reg); |
hisyamfs | 42:3b682b7d0388 | 267 | // for (int i=0; i<num; i++) { |
hisyamfs | 42:3b682b7d0388 | 268 | // Wire.write(buf[i]); |
hisyamfs | 42:3b682b7d0388 | 269 | // #ifdef I2C_DEBUG |
hisyamfs | 42:3b682b7d0388 | 270 | // Serial.print("0x"); Serial.print(buf[i], HEX); Serial.print(", "); |
hisyamfs | 42:3b682b7d0388 | 271 | // #endif |
hisyamfs | 42:3b682b7d0388 | 272 | // } |
hisyamfs | 42:3b682b7d0388 | 273 | // Wire.endTransmission(); |
hisyamfs | 42:3b682b7d0388 | 274 | // #ifdef I2C_DEBUG |
hisyamfs | 42:3b682b7d0388 | 275 | // Serial.println(); |
hisyamfs | 42:3b682b7d0388 | 276 | // #endif |
hisyamfs | 42:3b682b7d0388 | 277 | char reg_dest = reg; |
hisyamfs | 42:3b682b7d0388 | 278 | char tx_buf[num]; |
hisyamfs | 42:3b682b7d0388 | 279 | for (int i = 0; i < num; i++) { |
hisyamfs | 42:3b682b7d0388 | 280 | tx_buf[i] = (char) buf[i]; |
hisyamfs | 42:3b682b7d0388 | 281 | } |
hisyamfs | 42:3b682b7d0388 | 282 | int nack = _i2c.write(_i2caddr, ®_dest, 1, 1); // no stop |
hisyamfs | 42:3b682b7d0388 | 283 | nack = _i2c.write(_i2caddr, tx_buf, num); |
hisyamfs | 42:3b682b7d0388 | 284 | return nack; |
hisyamfs | 42:3b682b7d0388 | 285 | } |
hisyamfs | 42:3b682b7d0388 | 286 | |
hisyamfs | 42:3b682b7d0388 | 287 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 288 | /*! |
hisyamfs | 42:3b682b7d0388 | 289 | @brief convert a 12-bit signed magnitude value to a floating point number |
hisyamfs | 42:3b682b7d0388 | 290 | @param val the 12-bit signed magnitude value to be converted |
hisyamfs | 42:3b682b7d0388 | 291 | @returns the converted floating point value |
hisyamfs | 42:3b682b7d0388 | 292 | */ |
hisyamfs | 42:3b682b7d0388 | 293 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 294 | float Adafruit_AMG88xx::signedMag12ToFloat(uint16_t val) |
hisyamfs | 42:3b682b7d0388 | 295 | { |
hisyamfs | 42:3b682b7d0388 | 296 | //take first 11 bits as absolute val |
hisyamfs | 42:3b682b7d0388 | 297 | uint16_t absVal = (val & 0x7FF); |
hisyamfs | 42:3b682b7d0388 | 298 | |
hisyamfs | 42:3b682b7d0388 | 299 | return (val & 0x800) ? 0 - (float)absVal : (float)absVal ; |
hisyamfs | 42:3b682b7d0388 | 300 | } |
hisyamfs | 42:3b682b7d0388 | 301 | |
hisyamfs | 42:3b682b7d0388 | 302 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 303 | /*! |
hisyamfs | 42:3b682b7d0388 | 304 | @brief convert a 12-bit integer two's complement value to a floating point number |
hisyamfs | 42:3b682b7d0388 | 305 | @param val the 12-bit integer two's complement value to be converted |
hisyamfs | 42:3b682b7d0388 | 306 | @returns the converted floating point value |
hisyamfs | 42:3b682b7d0388 | 307 | */ |
hisyamfs | 42:3b682b7d0388 | 308 | /**************************************************************************/ |
hisyamfs | 42:3b682b7d0388 | 309 | float Adafruit_AMG88xx::int12ToFloat(uint16_t val) |
hisyamfs | 42:3b682b7d0388 | 310 | { |
hisyamfs | 42:3b682b7d0388 | 311 | uint16_t sVal = (val << 4); //shift to left so that sign bit of 12 bit integer number is placed on sign bit of 16 bit signed integer number |
hisyamfs | 42:3b682b7d0388 | 312 | return sVal >> 4; //shift back the signed number, return converts to float |
hisyamfs | 42:3b682b7d0388 | 313 | } |
hisyamfs | 42:3b682b7d0388 | 314 | |
hisyamfs | 42:3b682b7d0388 | 315 | /** |
hisyamfs | 42:3b682b7d0388 | 316 | * @brief helper function, constraints the value |
hisyamfs | 42:3b682b7d0388 | 317 | * @param value variable to be constrained |
hisyamfs | 42:3b682b7d0388 | 318 | * @param min minimum value of the variable |
hisyamfs | 42:3b682b7d0388 | 319 | */ |
hisyamfs | 42:3b682b7d0388 | 320 | int Adafruit_AMG88xx::constrain(int value, int min, int max) |
hisyamfs | 42:3b682b7d0388 | 321 | { |
hisyamfs | 42:3b682b7d0388 | 322 | if (value > max) { |
hisyamfs | 42:3b682b7d0388 | 323 | return max; |
hisyamfs | 42:3b682b7d0388 | 324 | } |
hisyamfs | 42:3b682b7d0388 | 325 | else if (value < min) { |
hisyamfs | 42:3b682b7d0388 | 326 | return min; |
hisyamfs | 42:3b682b7d0388 | 327 | } |
hisyamfs | 42:3b682b7d0388 | 328 | else { |
hisyamfs | 42:3b682b7d0388 | 329 | return value; |
hisyamfs | 42:3b682b7d0388 | 330 | } |
hisyamfs | 42:3b682b7d0388 | 331 | } |
hisyamfs | 42:3b682b7d0388 | 332 | |
hisyamfs | 42:3b682b7d0388 | 333 | int Adafruit_AMG88xx::min(int val1, int val2) { |
hisyamfs | 42:3b682b7d0388 | 334 | if (val1 < val2) return val1; |
hisyamfs | 42:3b682b7d0388 | 335 | else return val2; |
hisyamfs | 42:3b682b7d0388 | 336 | } |
hisyamfs | 42:3b682b7d0388 | 337 | |
hisyamfs | 42:3b682b7d0388 | 338 | int Adafruit_AMG88xx::max(int val1, int val2) { |
hisyamfs | 42:3b682b7d0388 | 339 | if (val1 > val2) return val1; |
hisyamfs | 42:3b682b7d0388 | 340 | else return val2; |
hisyamfs | 42:3b682b7d0388 | 341 | } |
hisyamfs | 42:3b682b7d0388 | 342 | |
hisyamfs | 42:3b682b7d0388 | 343 | uint8_t Adafruit_AMG88xx::min(uint8_t val1, uint8_t val2) { |
hisyamfs | 42:3b682b7d0388 | 344 | if (val1 < val2) return val1; |
hisyamfs | 42:3b682b7d0388 | 345 | else return val2; |
hisyamfs | 42:3b682b7d0388 | 346 | } |
hisyamfs | 42:3b682b7d0388 | 347 | |
hisyamfs | 42:3b682b7d0388 | 348 | uint8_t Adafruit_AMG88xx::max(uint8_t val1, uint8_t val2) { |
hisyamfs | 42:3b682b7d0388 | 349 | if (val1 > val2) return val1; |
hisyamfs | 42:3b682b7d0388 | 350 | else return val2; |
hisyamfs | 42:3b682b7d0388 | 351 | } |