I am interfacing color sensor with stm32f4 disc board.
i am facing error of "" TypeError: can't convert NoneType to int "" . can anyone help me in this.
*In the bolded line in the code , i am facing this issue. can anyone rectify this out .# main.py -- put your code here!
# /**I2C1 GPIO Configuration
# PB8 ------> I2C1_SCL
# PB9 ------> I2C1_SDA
import pyb
from pyb import I2C
#/* Control Register */
COLOR_SENSOR_REG_CTL = const(0x80)
REG_CTL_POWER = const(0x01)
REG_CTL_ADC_EN = const(0x02)
#/* Color read Register */
COLOR_SENSOR_REG_CMD = const(0x90) #The REG_BLOCK_READ and REG_GREEN_LOW direction are the same
REG_GREEN_LOW = const(0x90)
REG_GREEN_HIGH = const(0x91)
REG_RED_LOW = const(0x92)
REG_RED_HIGH = const(0x93)
REG_BLUE_LOW = const(0x94)
REG_BLUE_HIGH = const(0x95)
REG_CLEAR_LOW = const(0x96)
REG_CLEAR_HIGH = const(0x97)
GREEN = 0
RED = 1
BLUE = 2
CLEAR = 3
NUM_COLORS = 4
i2c = I2C(1)
i2c.init(I2C.MASTER,baudrate=200000)
sensor = i2c.scan()
"""
* @brief To transmit data on I2C
* @param Wr_Addr Register address on which Wr_Data will be written
* @param Wr_Data data
* @return value if data written successful, it will return 0 otherwise 1
"""
def I2C_WriteRegister(Wr_Addr, Wr_Data):
global i2c, sensor
i2c.mem_write(Wr_Data,sensor[1],Wr_Addr,timeout=1000)
def I2C_ReadRegister(Rd_Addr):
global i2c, sensor
i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)
# @brief This Function configures and Initializes I2C1
def BSP_COLOR_SENSOR_I2C1_Init():
global i2c, sensor
i2c = I2C(1)
i2c.init(I2C.MASTER,baudrate = 100000)
# @brief This function is for De-Initializing I2C1
def BSP_COLOR_SENSOR_I2C1_MspDeInit():
i2c.deinit()
def ColorSensor_Init():
global i2c, sensor
BSP_COLOR_SENSOR_I2C1_Init()
sensor = i2c.scan() #Only sensor will be detected in list
if i2c.mem_write(1,sensor[0],1,timeout=500) == False:
# if((i2c.is_ready(sensor[0])):
while(1):
print("Unable to initialize color sensor \n")
def ColorSensor_ReadColor(colour):
ColorRegistersCommand = 0
colorvalue = bytearray(2)
color = 0
ColorRegisters0Command = REG_GREEN_LOW + (2 * (colour))
colorvalue[0] = I2C_ReadRegister(ColorRegistersCommand)
colorvalue[1] = I2C_ReadRegister((ColorRegistersCommand + 1))
color = ((colorvalue[1]<<8)*256)|colorvalue[0]
return color
def main():
colour = green_ = red_ = blue_ = clear_ = None
displayvalue = bytearray(5)
tmp = maxColor = None
while True:
# for (colour = GREEN; colour <= NUM_COLORS; colour++):
#
colour = GREEN
if (colour <= NUM_COLORS):
displayvalue[colour] = ColorSensor_ReadColor(colour)
colour+=1
green_ = displayvalue[0]
red_ = displayvalue[1]
blue_ = displayvalue[2]
clear_ = displayvalue[3]
red_ = red_ * 1.70
blue_ = blue_ * 1.35
maxColor = max(red_, green_)
maxColor = max(maxColor, blue_)
if(maxColor > 255):
tmp = 250.0/maxColor
green_ *= tmp
red_ *= tmp
blue_ *= tmp
print(red_)
print(green_)
print(blue_)
pyb.delay(500)
while True:
main()