/** Private function to read luminosity on both channels */ private void getData() throws RuntimeIOException { enable(); // Wait x ms for ADC to complete */ if (integrationTime == TSL2561_INTEGRATIONTIME_13MS) { SleepUtil.sleepSeconds(0.014); } else if (integrationTime == TSL2561_INTEGRATIONTIME_101MS) { SleepUtil.sleepSeconds(0.102); } else { SleepUtil.sleepSeconds(0.403); } // Reads a two byte value from channel 0 (visible + infrared) broadband = i2cDevice.readUShort( TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, I2CConstants.SUB_ADDRESS_SIZE_1_BYTE, ByteOrder.LITTLE_ENDIAN); // Reads a two byte value from channel 1 (infrared) ir = i2cDevice.readUShort( TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, I2CConstants.SUB_ADDRESS_SIZE_1_BYTE, ByteOrder.LITTLE_ENDIAN); // Turn the device off to save power disable(); }
public void createChar(int location, byte[] charMap) { /* In the character generator RAM, the user can rewrite character patterns by program. * For 5×8 dots, eight character patterns can be written, and for 5×10 dots, * four character patterns can be written. */ if (characterFont5x8) { if (location < 0 || location >= 8) { throw new IllegalArgumentException("Invalid location (" + location + ") , must be 0..7"); } if (charMap.length != 8) { throw new IllegalArgumentException( "Invalid charMap length (" + charMap.length + ") , must be 8"); } } else { if (location < 0 || location >= 4) { throw new IllegalArgumentException("Invalid location (" + location + ") , must be 0..3"); } if (charMap.length != 10) { throw new IllegalArgumentException( "Invalid charMap length (" + charMap.length + ") , must be 10"); } } writeInstruction((byte) (INST_SET_CGRAM_ADDR | (location << 3))); SleepUtil.sleepMicros(30); for (int i = 0; i < charMap.length; i++) { writeData((byte) (charMap[i] & 0b11111)); SleepUtil.sleepMicros(40); } }
private void write4Bits(boolean instruction, byte value) { byte data = (byte) (value | (instruction ? REGISTER_SELECT_INSTRUCTION : REGISTER_SELECT_DATA) | (backlight ? BACKLIGHT_ON : BACKLIGHT_OFF)); device.writeByte((byte) (data | ENABLE), order); // 50us delay enough? SleepUtil.sleepMicros(50); device.writeByte((byte) (data & ~ENABLE), order); // 50us delay enough? SleepUtil.sleepMicros(50); }
public I2CLcd(int controller, int deviceAddress, ByteOrder order, int columns, int rows) { if (rows == 2) { rowOffsets = ROW_OFFSETS_2ROWS; } else if (rows == 4) { if (columns == 16) { rowOffsets = ROW_OFFSETS_16x4; } else if (columns == 20) { rowOffsets = ROW_OFFSETS_20x4; } } if (rowOffsets == null) { throw new IllegalArgumentException(columns + "x" + rows + " LCDs not supported"); } if (rowOffsets == null) { throw new IllegalArgumentException(columns + "x" + rows + " LCDs not supported"); } if (rows < 1 || rows > rowOffsets.length) { throw new IllegalArgumentException( "Invalid number of rows (" + rows + "), must be 1.." + rowOffsets.length); } this.order = order; this.columns = columns; this.rows = rows; backlight = DEFAULT_BACKLIGHT_STATE; characterFont5x8 = true; device = new I2CDevice( controller, deviceAddress, I2CConstants.ADDR_SIZE_7, I2CConstants.DEFAULT_CLOCK_FREQUENCY); // Initialise the display. From p45/46 of the datasheet: // https://www.sparkfun.com/datasheets/LCD/HD44780.pdf // If the power supply conditions for correctly operating the internal reset // circuit are not met, initialisation by instructions becomes necessary. // Need to do this 3 times for the 4-bit interface (p46) // Function set (Interface is 8 bits long). write4Bits(true, (byte) (INST_FUNCTION_SET | FS_DATA_LENGTH_8BIT)); // Wait for more than 4.1 ms SleepUtil.sleepMillis(4); // Function set (Interface is 8 bits long). write4Bits(true, (byte) (INST_FUNCTION_SET | FS_DATA_LENGTH_8BIT)); // Wait for more than 100us SleepUtil.sleepMicros(100); // Function set (Interface is 8 bits long). write4Bits(true, (byte) (INST_FUNCTION_SET | FS_DATA_LENGTH_8BIT)); // Now set it to 4-bit mode write4Bits(true, (byte) (INST_FUNCTION_SET | FS_DATA_LENGTH_4BIT)); // Function set: 4-bit data length, lines & character font as requested writeInstruction( (byte) (INST_FUNCTION_SET | FS_DATA_LENGTH_4BIT | (rows == 1 ? FS_DISPLAY_1LINE : FS_DISPLAY_2LINES) | (characterFont5x8 ? FS_CHAR_FONT_5X8DOTS : FS_CHAR_FONT_5X10DOTS))); // Display On, Cursor on, Blink on displayControl(true, true, true); // Cursor increment, display shift off entryModeControl(true, false); // Clear display clear(); }