Skip to content

I2C: writes work but reads return undefined #237

@nodebotanist

Description

@nodebotanist

I have been working with the SparkFun Qwiic Twist and have run into an issue where I2C writes work but reads return undefined. This has been replicated on a Moddable Two and an M5Stack.

Source code:

import I2C from 'pins/i2c'
import Timer from 'timer'

const registers = {
	VERSION: 0x02,
	TWIST_COUNT: 0x05,
	STATUS: 0x01,
	RED: 0x0D,
  GREEN: 0x0E,
	BLUE: 0x0F,
	CONNECT_RED: 0x10,
  CONNECT_GREEN: 0x12,
  CONNECT_BLUE: 0x14
}

const qwiic_status = {
	BUTTON_PRESSED: 1
}

class Qwiic_RGB_Rotary {
	constructor(dictionary) {
    this.I2C = new I2C({address: 0x3F})
	}

	getVersion() {
    this.I2C.write(0x02)
		let version = this.I2C.read(2)
		Timer.delay(2)	
    version = (version[0] << 8) | version[1]
		trace(`Version: ${version}\n\n`)
	}

	qwiic_twist.write(registers.RED, 0x66)
	qwiic_twist.write(registers.GREEN, 0x33,)
	qwiic_twist.write(registers.BLUE, 0x99)

	qwiic_twist.write(registers.CONNECT_BLUE, 0x00)
	qwiic_twist.write(registers.CONNECT_BLUE, 0x10)
	qwiic_twist.write(registers.CONNECT_GREEN, 0x00)
	qwiic_twist.write(registers.CONNECT_GREEN, -10 & 0xFF)
	qwiic_twist.write(registers.CONNECT_RED, 0x00)
	qwiic_twist.write(registers.CONNECT_RED, 0x05)
}

export default Qwiic_RGB_Rotary

main.js

import Timer from 'timer'
import Qwiic_RGB_Rotary from 'qwiic-rgb-rotary'

export default function () {
	let rotary = new Qwiic_RGB_Rotary()
	const version = rotary.getVersion()
	trace(`Version: ${version}`)
}

I have tried with and without the Timer.delay() and no matter what I get:
C:\Users\Nodebotanist\Projects\moddable\examples\drivers\qwiic-twist\qwiic-rgb-rotary.js (43) # Break: Qwiic_RGB_Rotary.prototype.getVersion: cannot coerce undefined to object!

I2C writes on the same bus work as expected (rotary device lights up correctly), and I checked and double-checked the registers from the source material

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions