|
| 1 | +import "@dsboard/seeed_xiao_esp32c3" |
| 2 | +import * as ds from "@devicescript/core" |
| 3 | +import { I2CDriver, XiaoExpansionBoard } from "@devicescript/drivers" |
| 4 | +import { delay } from "@devicescript/core" |
| 5 | +import { configureHardware } from "@devicescript/servers" |
| 6 | + |
| 7 | +// Device I2C Arress |
| 8 | +const LCD_ADDRESS = 0x7c >> 1 |
| 9 | +const RGB_ADDRESS = 0xc4 >> 1 |
| 10 | +const RGB_ADDRESS_V5 = 0x30 |
| 11 | + |
| 12 | +// color define |
| 13 | +const WHITE = 0 |
| 14 | +const RED = 1 |
| 15 | +const GREEN = 2 |
| 16 | +const BLUE = 3 |
| 17 | + |
| 18 | +const REG_MODE1 = 0x00 |
| 19 | +const REG_MODE2 = 0x01 |
| 20 | +const REG_OUTPUT = 0x08 |
| 21 | + |
| 22 | +// commands |
| 23 | +const LCD_CLEARDISPLAY = 0x01 |
| 24 | +const LCD_RETURNHOME = 0x02 |
| 25 | +const LCD_ENTRYMODESET = 0x04 |
| 26 | +const LCD_DISPLAYCONTROL = 0x08 |
| 27 | +const LCD_CURSORSHIFT = 0x10 |
| 28 | +const LCD_FUNCTIONSET = 0x20 |
| 29 | +const LCD_SETCGRAMADDR = 0x40 |
| 30 | +const LCD_SETDDRAMADDR = 0x80 |
| 31 | + |
| 32 | +// flags for display entry mode |
| 33 | +const LCD_ENTRYRIGHT = 0x00 |
| 34 | +const LCD_ENTRYLEFT = 0x02 |
| 35 | +const LCD_ENTRYSHIFTINCREMENT = 0x01 |
| 36 | +const LCD_ENTRYSHIFTDECREMENT = 0x00 |
| 37 | + |
| 38 | +// flags for display on/off control |
| 39 | +const LCD_DISPLAYON = 0x04 |
| 40 | +const LCD_DISPLAYOFF = 0x00 |
| 41 | +const LCD_CURSORON = 0x02 |
| 42 | +const LCD_CURSOROFF = 0x00 |
| 43 | +const LCD_BLINKON = 0x01 |
| 44 | +const LCD_BLINKOFF = 0x00 |
| 45 | + |
| 46 | +// flags for display/cursor shift |
| 47 | +const LCD_DISPLAYMOVE = 0x08 |
| 48 | +const LCD_CURSORMOVE = 0x00 |
| 49 | +const LCD_MOVERIGHT = 0x04 |
| 50 | +const LCD_MOVELEFT = 0x00 |
| 51 | + |
| 52 | +// flags for function set |
| 53 | +const LCD_8BITMODE = 0x10 |
| 54 | +const LCD_4BITMODE = 0x00 |
| 55 | +const LCD_2LINE = 0x08 |
| 56 | +const LCD_1LINE = 0x00 |
| 57 | +const LCD_5x10DOTS = 0x04 |
| 58 | +const LCD_5x8DOTS = 0x00 |
| 59 | + |
| 60 | +// https://wiki.seeedstudio.com/Grove-16x2_LCD_Series/#specification |
| 61 | +// converted from https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight/ |
| 62 | +class RGBLCD extends I2CDriver { |
| 63 | + readonly columns: number |
| 64 | + readonly lines: number |
| 65 | + readonly dotsize: number |
| 66 | + private _displayfunction: number = 0 |
| 67 | + private _displaymode: number = 0 |
| 68 | + private _displaycontrol: number = 0 |
| 69 | + private _currline: number = 0 |
| 70 | + |
| 71 | + constructor( |
| 72 | + readonly options: { |
| 73 | + columns: number |
| 74 | + rows: number |
| 75 | + dotsize: number |
| 76 | + devAddr?: number |
| 77 | + } |
| 78 | + ) { |
| 79 | + super(options.devAddr || LCD_ADDRESS) |
| 80 | + this.columns = options.columns |
| 81 | + this.lines = options.rows |
| 82 | + this.dotsize = options.dotsize |
| 83 | + } |
| 84 | + |
| 85 | + async initDriver(): Promise<void> { |
| 86 | + if (this.lines > 1) { |
| 87 | + this._displayfunction |= LCD_2LINE |
| 88 | + } |
| 89 | + this._currline = 0 |
| 90 | + |
| 91 | + // for some 1 line displays you can select a 10 pixel high font |
| 92 | + if (this.dotsize !== 0 && this.lines === 1) { |
| 93 | + this._displayfunction |= LCD_5x10DOTS |
| 94 | + } |
| 95 | + |
| 96 | + // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! |
| 97 | + // according to datasheet, we need at least 40ms after power rises above 2.7V |
| 98 | + // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50 |
| 99 | + // delayMicroseconds(50000); |
| 100 | + await delay(5) |
| 101 | + |
| 102 | + // this is according to the hitachi HD44780 datasheet |
| 103 | + // page 45 figure 23 |
| 104 | + |
| 105 | + // Send function set command sequence |
| 106 | + await this.command(LCD_FUNCTIONSET | this._displayfunction) |
| 107 | + await delay(5) // wait more than 4.1ms |
| 108 | + |
| 109 | + // second try |
| 110 | + await this.command(LCD_FUNCTIONSET | this._displayfunction) |
| 111 | + await delay(1) |
| 112 | + |
| 113 | + // third go |
| 114 | + await this.command(LCD_FUNCTIONSET | this._displayfunction) |
| 115 | + |
| 116 | + // finally, set # lines, font size, etc. |
| 117 | + await this.command(LCD_FUNCTIONSET | this._displayfunction) |
| 118 | + |
| 119 | + // turn the display on with no cursor or blinking default |
| 120 | + this._displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF |
| 121 | + await this.display() |
| 122 | + |
| 123 | + // clear it off |
| 124 | + await this.clear() |
| 125 | + |
| 126 | + // Initialize to default text direction (for romance languages) |
| 127 | + this._displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT |
| 128 | + // set the entry mode |
| 129 | + await this.command(LCD_ENTRYMODESET | this._displaymode) |
| 130 | + } |
| 131 | + |
| 132 | + private async command(value: number): Promise<void> { |
| 133 | + await this.writeBuf(Buffer.from([0x80, value])) |
| 134 | + } |
| 135 | + |
| 136 | + private async display() { |
| 137 | + this._displaycontrol |= LCD_DISPLAYON |
| 138 | + await this.command(LCD_DISPLAYCONTROL | this._displaycontrol) |
| 139 | + } |
| 140 | + |
| 141 | + async clear() { |
| 142 | + await this.command(LCD_CLEARDISPLAY) // clear display, set cursor position to zero |
| 143 | + await delay(2) // this command takes a long time! |
| 144 | + } |
| 145 | + |
| 146 | + private async write(value: number) { |
| 147 | + await this.writeBuf(Buffer.from([0x40, value])) |
| 148 | + } |
| 149 | + |
| 150 | + async print(str: string) { |
| 151 | + for (let i = 0; i < str.length; ++i) { |
| 152 | + await this.write(str.charCodeAt(i)) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +const board = new XiaoExpansionBoard() |
| 158 | + |
| 159 | +console.log("start...") |
| 160 | +const lcd = new RGBLCD({ |
| 161 | + columns: 16, |
| 162 | + rows: 2, |
| 163 | + dotsize: 0, |
| 164 | + devAddr: LCD_ADDRESS, |
| 165 | +}) |
| 166 | +await lcd.init() |
| 167 | +setInterval(async () => { |
| 168 | + const t = ds.millis() + "" |
| 169 | + console.log(t) |
| 170 | + await lcd.clear() |
| 171 | + await lcd.print(t) |
| 172 | +}, 1000) |
0 commit comments