|
| 1 | +from micropython import const |
| 2 | +from machine import I2C |
| 3 | + |
| 4 | + |
| 5 | +BH1745_I2C_address = const(0x38) |
| 6 | + |
| 7 | +BH1745_SYSTEM_CONTROL_REG = const(0x40) |
| 8 | +BH1745_MODE_CONTROL1_REG = const(0x41) |
| 9 | +BH1745_MODE_CONTROL2_REG = const(0x42) |
| 10 | +BH1745_MODE_CONTROL3_REG = const(0x44) |
| 11 | +BH1745_RED_DATA_LSBs_REG = const(0x50) |
| 12 | +BH1745_RED_DATA_MSBs_REG = const(0x51) |
| 13 | +BH1745_GREEN_DATA_LSBs_REG = const(0x52) |
| 14 | +BH1745_GREEN_DATA_MSBs_REG = const(0x53) |
| 15 | +BH1745_BLUE_DATA_LSBs_REG = const(0x54) |
| 16 | +BH1745_BLUE_DATA_MSBs_REG = const(0x55) |
| 17 | +BH1745_CLEAR_DATA_LSBs_REG = const(0x56) |
| 18 | +BH1745_CLEAR_DATA_MSBs_REG = const(0x57) |
| 19 | +BH1745_DINT_DATA_LSBs_REG = const(0x58) |
| 20 | +BH1745_DINT_DATA_MSBs_REG = const(0x59) |
| 21 | +BH1745_INTERRUPT_REG = const(0x60) |
| 22 | +BH1745_PERSISTENCE_REG = const(0x61) |
| 23 | +BH1745_TH_LSBs_REG = const(0x62) |
| 24 | +BH1745_TH_MSBs_REG = const(0x63) |
| 25 | +BH1745_TL_LSBs_REG = const(0x64) |
| 26 | +BH1745_TL_MSBs_REG = const(0x65) |
| 27 | +BH1745_MANUFACTURER_ID_REG = const(0x92) |
| 28 | + |
| 29 | +BH1745_PART_ID = const(0x0B) |
| 30 | + |
| 31 | +BH1745_SYSTEM_CONTROL_SW_RESET_ACTIVE = const(0x8B) |
| 32 | +BH1745_SYSTEM_CONTROL_SW_RESET_INACTIVE = const(0x0B) |
| 33 | +BH1745_SYSTEM_CONTROL_INT_PIN_ACTIVE = const(0x4B) |
| 34 | +BH1745_SYSTEM_CONTROL_INT_PIN_INACTIVE = const(0x0B) |
| 35 | + |
| 36 | +BH1745_MODE_CONTROL1_MEASUREMENT_TIME_160ms = const(0x00) |
| 37 | +BH1745_MODE_CONTROL1_MEASUREMENT_TIME_320ms = const(0x01) |
| 38 | +BH1745_MODE_CONTROL1_MEASUREMENT_TIME_640ms = const(0x02) |
| 39 | +BH1745_MODE_CONTROL1_MEASUREMENT_TIME_1280ms = const(0x03) |
| 40 | +BH1745_MODE_CONTROL1_MEASUREMENT_TIME_2560ms = const(0x04) |
| 41 | +BH1745_MODE_CONTROL1_MEASUREMENT_TIME_5120ms = const(0x05) |
| 42 | + |
| 43 | +BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_ACTIVE = const(0x10) |
| 44 | +BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_INACTIVE = const(0x00) |
| 45 | +BH1745_MODE_CONTROL2_ADC_GAIN_1X = const(0x00) |
| 46 | +BH1745_MODE_CONTROL2_ADC_GAIN_2X = const(0x01) |
| 47 | +BH1745_MODE_CONTROL2_ADC_GAIN_16X = const(0x02) |
| 48 | + |
| 49 | +BH1745_MODE_CONTROL3_BYTE = const(0x02) |
| 50 | + |
| 51 | +BH1745_INTERRUPT_INT_LATCHED = const(0x00) |
| 52 | +BH1745_INTERRUPT_INT_NOT_LATCHED = const(0x10) |
| 53 | +BH1745_INTERRUPT_INT_SOURCE_RED_CHANNEL = const(0x00) |
| 54 | +BH1745_INTERRUPT_INT_SOURCE_GREEN_CHANNEL = const(0x04) |
| 55 | +BH1745_INTERRUPT_INT_SOURCE_BLUE_CHANNEL = const(0x08) |
| 56 | +BH1745_INTERRUPT_INT_SOURCE_CLEAR_CHANNEL = const(0x0C) |
| 57 | +BH1745_INTERRUPT_INT_ENABLED = const(0x01) |
| 58 | +BH1745_INTERRUPT_INT_DISABLED = const(0x00) |
| 59 | + |
| 60 | +BH1745_PERSISTENCE_CASE1 = const(0x00) |
| 61 | +BH1745_PERSISTENCE_CASE2 = const(0x01) |
| 62 | +BH1745_PERSISTENCE_CASE3 = const(0x02) |
| 63 | +BH1745_PERSISTENCE_CASE4 = const(0x03) |
| 64 | + |
| 65 | +BH1745_MANUFACTURER_ID = const(0xE0) |
| 66 | + |
| 67 | + |
| 68 | +class BH1745(): |
| 69 | + |
| 70 | + def __init__(self): |
| 71 | + self.i2c = I2C(1, freq = 400000) |
| 72 | + self.init() |
| 73 | + |
| 74 | + |
| 75 | + def init(self): |
| 76 | + part_id = self.read_byte(BH1745_SYSTEM_CONTROL_REG) |
| 77 | + |
| 78 | + if(part_id != BH1745_PART_ID): |
| 79 | + print("Error! Wrong/Damaged Part!") |
| 80 | + else: |
| 81 | + print("Found BH1745....") |
| 82 | + self.write(BH1745_MODE_CONTROL1_REG, BH1745_MODE_CONTROL1_MEASUREMENT_TIME_320ms) |
| 83 | + self.write(BH1745_MODE_CONTROL2_REG, (BH1745_MODE_CONTROL2_RGBC_MEASUREMENT_ACTIVE | BH1745_MODE_CONTROL2_ADC_GAIN_1X)) |
| 84 | + self.write(BH1745_MODE_CONTROL3_REG, BH1745_MODE_CONTROL3_BYTE) |
| 85 | + self.write(BH1745_INTERRUPT_REG, (BH1745_INTERRUPT_INT_LATCHED | BH1745_INTERRUPT_INT_SOURCE_CLEAR_CHANNEL | BH1745_INTERRUPT_INT_DISABLED)) |
| 86 | + self.write(BH1745_PERSISTENCE_REG, BH1745_PERSISTENCE_CASE1) |
| 87 | + manufacturer_id = self.read_byte(BH1745_MANUFACTURER_ID_REG) |
| 88 | + |
| 89 | + if(manufacturer_id == BH1745_MANUFACTURER_ID): |
| 90 | + print("Part matched. All set.") |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + def write(self, reg, value): |
| 95 | + if not type(value) is bytearray: |
| 96 | + value = bytearray([value]) |
| 97 | + |
| 98 | + self.i2c.writeto_mem(BH1745_I2C_address, reg, value) |
| 99 | + |
| 100 | + |
| 101 | + def read_byte(self, reg): |
| 102 | + retval = self.i2c.readfrom_mem(BH1745_I2C_address, reg, 0x01) |
| 103 | + return retval[0x00] |
| 104 | + |
| 105 | + |
| 106 | + def read_word(self, reg): |
| 107 | + value = self.i2c.readfrom_mem(BH1745_I2C_address, reg, 0x02) |
| 108 | + retval = ((value[0x01] << 0x08) | value[0x00]) |
| 109 | + return retval |
| 110 | + |
| 111 | + |
| 112 | + def read_RGBC(self): |
| 113 | + r = 0 |
| 114 | + g = 0 |
| 115 | + b = 0 |
| 116 | + c = 0 |
| 117 | + rgb = 0 |
| 118 | + |
| 119 | + r = self.read_byte(BH1745_RED_DATA_LSBs_REG) |
| 120 | + g = self.read_byte(BH1745_GREEN_DATA_LSBs_REG) |
| 121 | + b = self.read_byte(BH1745_BLUE_DATA_LSBs_REG) |
| 122 | + c = self.read_byte(BH1745_CLEAR_DATA_LSBs_REG) |
| 123 | + rgb = (((r << 16) | (g << 8) | b) & 0xFFFF) |
| 124 | + |
| 125 | + return r, g, b, c, rgb |
| 126 | + |
| 127 | + |
| 128 | + def RGB_to_HSV(self): |
| 129 | + R, G, B, C, RGB = self.read_RGBC() |
| 130 | + |
| 131 | + r_ch = (R / 65535.0) |
| 132 | + g_ch = (G / 65535.0) |
| 133 | + b_ch = (B / 65535.0) |
| 134 | + |
| 135 | + max_R_vs_G = max(r_ch, g_ch) |
| 136 | + max_RG_vs_B = max(max_R_vs_G, b_ch) |
| 137 | + |
| 138 | + min_R_vs_G = min(r_ch, g_ch) |
| 139 | + min_RG_vs_B = min(max_R_vs_G, b_ch) |
| 140 | + |
| 141 | + diff = (max_RG_vs_B - min_RG_vs_B) |
| 142 | + |
| 143 | + if(diff == 0): |
| 144 | + hue = 0 |
| 145 | + |
| 146 | + elif(max_RG_vs_B == r_ch): |
| 147 | + if(g_ch >= b_ch): |
| 148 | + hue = (60 * ((g_ch - b_ch) / diff + 0)) |
| 149 | + else: |
| 150 | + hue = (60 * ((g_ch - b_ch) / diff + 6)) |
| 151 | + |
| 152 | + elif(max_RG_vs_B == g_ch): |
| 153 | + hue = (60 * ((b_ch - r_ch) / diff + 2)) |
| 154 | + |
| 155 | + elif(max_RG_vs_B == b_ch): |
| 156 | + hue = (60 * ((r_ch - g_ch) / diff + 2)) |
| 157 | + |
| 158 | + brightness = ((max_RG_vs_B + min_RG_vs_B) * 0.5) |
| 159 | + |
| 160 | + if(brightness == 0): |
| 161 | + saturation = 0 |
| 162 | + |
| 163 | + elif(brightness <= 0.5): |
| 164 | + saturation = (diff / (brightness * 2)) |
| 165 | + |
| 166 | + else: |
| 167 | + saturation = (diff / (2 - (brightness * 2))) |
| 168 | + |
| 169 | + |
| 170 | + return hue, saturation, brightness |
| 171 | + |
| 172 | + |
0 commit comments