项目作者: mruby-esp32

项目描述 :
I2C library for mruby-esp32.
高级语言: C
项目地址: git://github.com/mruby-esp32/mruby-esp32-i2c.git
创建时间: 2017-05-24T06:29:38Z
项目社区:https://github.com/mruby-esp32/mruby-esp32-i2c

开源协议:

下载


mruby-esp32-i2c

I2C library for mruby-esp32.

Installation

Add the line below to your build_config.rb:

  1. conf.gem :github => 'mruby-esp32/mruby-esp32-i2c'

Example

Example of controlling the LCD display with ST7032 controller.

Demo movie

movie

Code

  1. include ESP32
  2. class ST7032
  3. def initialize(i2c, addr=0x3e, cols=8)
  4. @i2c = i2c
  5. @addr = addr
  6. @cols = cols
  7. end
  8. def init
  9. # see controller data sheet
  10. @i2c.send("\x00\x38\x39\x14\x70\x56\x6c", @addr)
  11. System.delay(200)
  12. @i2c.send("\x00\x38\x0c\x01", @addr)
  13. System.delay(1)
  14. end
  15. def ready?
  16. @i2c.ready?(@addr)
  17. end
  18. def contrast(n)
  19. c1 = (0x70 | (n & 0x0f)).chr
  20. c2 = (0x54 | (n >> 4) & 0x03).chr
  21. @i2c.send(["\x00\x39", c1, c2, "\x38"].join, @addr)
  22. end
  23. def clear
  24. @i2c.send("\x00\x01", @addr)
  25. end
  26. def print(str)
  27. clear
  28. @i2c.send("\x00\x80", @addr)
  29. @i2c.send(["\x40", str[0,@cols]].join, @addr)
  30. if str.length > @cols
  31. @i2c.send("\x00\xc0", @addr);
  32. @i2c.send(["\x40", str[@cols,@cols]].join, @addr)
  33. end
  34. end
  35. end
  36. i2c = I2C.new(I2C::PORT0).init(I2C::MASTER)
  37. p i2c.scan
  38. lcd = ST7032.new(i2c)
  39. until lcd.ready?
  40. System.delay(1000)
  41. end
  42. lcd.init
  43. lcd.print("Hello, world!")
  44. System.delay(3000)
  45. (0..63).each {|c|
  46. lcd.contrast(c)
  47. lcd.print("c=#{c}")
  48. System.delay(100)
  49. }
  50. lcd.contrast(32)
  51. lcd.clear
  52. i2c.deinit

License

Copyright (c) 2017 YAMAMOTO Masaya

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.