TypeError: can't convert bytearray to int

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

TypeError: can't convert bytearray to int

Post by nikhiledutech » Thu Apr 26, 2018 11:52 am

hi i am currently using I2C to interface color sensor. But facing issue in the following line.

Code: Select all

import pyb
from machine import Pin, I2C

#I2C 2 Initialisation
i2c = I2C(scl = ('PB8'), sda = ('PB9'))


#Color Sensor adrress
COLOR_SENSOR_ADDR  =  (0x39<<1)


COLOR_SENSOR_REG_CTL	=	0x80
REG_CTL_POWER    =    0x01
REG_CTL_ADC_EN   =    0x02

#Color read Register 
#COLOR_SENSOR_REG_CMD 0x90         //The REG_BLOCK_READ and REG_GREEN_LOW direction are the same

REG_GREEN_LOW    =    0x90
REG_GREEN_HIGH   =    0x91
REG_RED_LOW      =    0x92
REG_RED_HIGH     =    0x93
REG_BLUE_LOW     =    0x94
REG_BLUE_HIGH    =    0x95
REG_CLEAR_LOW    =    0x96
REG_CLEAR_HIGH   =    0x97

#Color read Register */
COLOR_SENSOR_REG_CMD	=	 0xB0

def min(X,Y):
	if ((X) < (Y)):
		return (X)
	else:
		return (Y)

def max(X,Y):
	if ((X) > (Y)):
		print(X)
		return (X)
	else:
		print(Y)
		return (Y)
"""
# Color Sensor Type
ColorSense_TypeDef = {'GREEN' : 0, 
			 		  'RED' : 1, 
			  		  'BLUE' : 2, 
			          'CLEAR' : 3, 
			          'NUM_COLORS' : 4 }

"""
GREEN = 0 
RED = 1 
BLUE = 2 
CLEAR = 3
NUM_COLORS = 4 

#Buffer to store data from Read function
buf = bytearray(1)
init_buf = bytearray(1)
buf_write = bytearray(1)   

buf2 = bytearray(2)                
buf3 = bytearray(3)
buf6 = bytearray(6)



#COLOR_SENSOR_ADDR  =  (0x39<<1)

COLOR_SENSOR = 57
""" /**
 * @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):
	buf_write[0] = Wr_Data
	i2c.writeto_mem(COLOR_SENSOR, Wr_Addr, buf_write)



# Read from device
def I2C_ReadRegister(Rd_Addr):  
	I2C_Tx_Buf = bytearray(1)
	I2C_Rx_Buf = bytearray(2)

	I2C_Tx_Buf[0] = Rd_Addr	

[b]	if(i2c.readfrom_mem_into(COLOR_SENSOR, I2C_Tx_Buf, I2C_Rx_Buf) == None):[/b]
		return(I2C_Rx_Buf[0])



#Color Sensor Initialistaion
def ColorSensor_Init():
	if(I2C_WriteRegister(COLOR_SENSOR_REG_CTL, REG_CTL_POWER | REG_CTL_ADC_EN) != None):
		print("Unable to initialize color sensor")
	else:
		print(" Done initialize color sensor")




#read colour sensor data
def ColorSensor_ReadColor (colour):

	ColorRegistersCommand = 0
	color_value = bytearray(2)
	color_name = 0
	ColorRegistersCommand = REG_GREEN_LOW + (2 * (colour))

	color_value[0] = I2C_ReadRegister(ColorRegistersCommand)
	color_value[1] = I2C_ReadRegister((ColorRegistersCommand + 1))

	color_name = ((color_value[1]<<8)*256)|color_value[0];
#	print (color_name)
#	print(color_value)
	return color_name
	



def main():

	colour = 0
	green_ = red_ = blue_ = clear_ = 0
	displayvalue = bytearray(5)
	tmp = 0
	maxColor = 0

#	Initialize color sensor 
	ColorSensor_Init()

	#Infinite Loop
	while True:
		z = 0
  		list_colour = [GREEN,RED, BLUE, CLEAR]
		colour = list_colour[z]
		if (colour <= NUM_COLORS):
			displayvalue[colour] = ColorSensor_ReadColor(colour)
			z += 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", red_, "Green : ", green_ , "Blue : ", blue_)
		pyb.delay(500)



main()
		


In the Read register function i am facing the following error of "TypeError: can't convert bytearray to int" . Can anyone help me out.

User avatar
Roberthh
Posts: 1046
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: TypeError: can't convert bytearray to int

Post by Roberthh » Thu Apr 26, 2018 12:09 pm

The second argument of i2c.readfrom_mem_into is expected to be an int, not an array. So no need to copy the address into a bytearry first.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: TypeError: can't convert bytearray to int

Post by nikhiledutech » Fri Apr 27, 2018 4:47 am

Okay. Thank you Sir.

Post Reply