An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01 MOD by Zak
Fork of ACM1602NI by
ACM1602NI.cpp@0:aac87cbfc229, 2013-05-13 (annotated)
- Committer:
- takuo
- Date:
- Mon May 13 12:50:42 2013 +0000
- Revision:
- 0:aac87cbfc229
- Child:
- 1:84527dcd3fcc
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takuo | 0:aac87cbfc229 | 1 | #include "mbed.h" |
takuo | 0:aac87cbfc229 | 2 | #include "ACM1602NI.h" |
takuo | 0:aac87cbfc229 | 3 | |
takuo | 0:aac87cbfc229 | 4 | #define _i2cSUCCESS 0 |
takuo | 0:aac87cbfc229 | 5 | #define _i2cFAILURE 1 |
takuo | 0:aac87cbfc229 | 6 | |
takuo | 0:aac87cbfc229 | 7 | ACM1602NI::ACM1602NI(I2C &i2c): _i2c(i2c) { |
takuo | 0:aac87cbfc229 | 8 | init(); |
takuo | 0:aac87cbfc229 | 9 | } |
takuo | 0:aac87cbfc229 | 10 | |
takuo | 0:aac87cbfc229 | 11 | void ACM1602NI::init() { |
takuo | 0:aac87cbfc229 | 12 | writeCommand(0x01); |
takuo | 0:aac87cbfc229 | 13 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 14 | writeCommand(0x38); |
takuo | 0:aac87cbfc229 | 15 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 16 | writeCommand(0x0f); |
takuo | 0:aac87cbfc229 | 17 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 18 | writeCommand(0x06); |
takuo | 0:aac87cbfc229 | 19 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 20 | locate(0, 0); |
takuo | 0:aac87cbfc229 | 21 | } |
takuo | 0:aac87cbfc229 | 22 | |
takuo | 0:aac87cbfc229 | 23 | int ACM1602NI::writeBytes(const char *data, int length, bool repeated) { |
takuo | 0:aac87cbfc229 | 24 | wait_us(i2c_bit_wait_us); |
takuo | 0:aac87cbfc229 | 25 | _i2c.start(); |
takuo | 0:aac87cbfc229 | 26 | for (int i = 0; i < length; i++) { |
takuo | 0:aac87cbfc229 | 27 | wait_us(i2c_bit_wait_us); |
takuo | 0:aac87cbfc229 | 28 | if (_i2c.write(data[i]) != 1) { |
takuo | 0:aac87cbfc229 | 29 | wait_us(i2c_bit_wait_us); |
takuo | 0:aac87cbfc229 | 30 | _i2c.stop(); |
takuo | 0:aac87cbfc229 | 31 | return _i2cFAILURE; |
takuo | 0:aac87cbfc229 | 32 | } |
takuo | 0:aac87cbfc229 | 33 | } |
takuo | 0:aac87cbfc229 | 34 | if (!repeated) { |
takuo | 0:aac87cbfc229 | 35 | wait_us(i2c_bit_wait_us); |
takuo | 0:aac87cbfc229 | 36 | _i2c.stop(); |
takuo | 0:aac87cbfc229 | 37 | } |
takuo | 0:aac87cbfc229 | 38 | return _i2cSUCCESS; |
takuo | 0:aac87cbfc229 | 39 | } |
takuo | 0:aac87cbfc229 | 40 | |
takuo | 0:aac87cbfc229 | 41 | void ACM1602NI::character(int column, int row, int c) { |
takuo | 0:aac87cbfc229 | 42 | int a = address(column, row); |
takuo | 0:aac87cbfc229 | 43 | writeCommand(a); |
takuo | 0:aac87cbfc229 | 44 | writeData(c); |
takuo | 0:aac87cbfc229 | 45 | } |
takuo | 0:aac87cbfc229 | 46 | |
takuo | 0:aac87cbfc229 | 47 | void ACM1602NI::cls() { |
takuo | 0:aac87cbfc229 | 48 | writeCommand(0x01); |
takuo | 0:aac87cbfc229 | 49 | wait_ms(i2c_command_wait_ms); |
takuo | 0:aac87cbfc229 | 50 | locate(0, 0); |
takuo | 0:aac87cbfc229 | 51 | } |
takuo | 0:aac87cbfc229 | 52 | |
takuo | 0:aac87cbfc229 | 53 | void ACM1602NI::locate(int column, int row) { |
takuo | 0:aac87cbfc229 | 54 | _column = column; |
takuo | 0:aac87cbfc229 | 55 | _row = row; |
takuo | 0:aac87cbfc229 | 56 | } |
takuo | 0:aac87cbfc229 | 57 | |
takuo | 0:aac87cbfc229 | 58 | int ACM1602NI::_putc(int value) { |
takuo | 0:aac87cbfc229 | 59 | if (value == '\n') { |
takuo | 0:aac87cbfc229 | 60 | _column = 0; |
takuo | 0:aac87cbfc229 | 61 | _row = (_row + 1) % rows(); |
takuo | 0:aac87cbfc229 | 62 | } |
takuo | 0:aac87cbfc229 | 63 | else { |
takuo | 0:aac87cbfc229 | 64 | character(_column, _row, value); |
takuo | 0:aac87cbfc229 | 65 | _column++; |
takuo | 0:aac87cbfc229 | 66 | if (_column >= columns()) { |
takuo | 0:aac87cbfc229 | 67 | _column = 0; |
takuo | 0:aac87cbfc229 | 68 | _row = (_row + 1) % rows(); |
takuo | 0:aac87cbfc229 | 69 | } |
takuo | 0:aac87cbfc229 | 70 | } |
takuo | 0:aac87cbfc229 | 71 | return value; |
takuo | 0:aac87cbfc229 | 72 | } |
takuo | 0:aac87cbfc229 | 73 | |
takuo | 0:aac87cbfc229 | 74 | int ACM1602NI::_getc() { |
takuo | 0:aac87cbfc229 | 75 | return -1; |
takuo | 0:aac87cbfc229 | 76 | } |
takuo | 0:aac87cbfc229 | 77 | |
takuo | 0:aac87cbfc229 | 78 | void ACM1602NI::writeCommand(int command) { |
takuo | 0:aac87cbfc229 | 79 | char bs[3] = { i2c_addr, 0x00, command }; |
takuo | 0:aac87cbfc229 | 80 | writeBytes(bs, 3); |
takuo | 0:aac87cbfc229 | 81 | } |
takuo | 0:aac87cbfc229 | 82 | |
takuo | 0:aac87cbfc229 | 83 | void ACM1602NI::writeData(int data) { |
takuo | 0:aac87cbfc229 | 84 | char bs[3] = { i2c_addr, 0x80, data }; |
takuo | 0:aac87cbfc229 | 85 | writeBytes(bs, 3); |
takuo | 0:aac87cbfc229 | 86 | } |
takuo | 0:aac87cbfc229 | 87 | |
takuo | 0:aac87cbfc229 | 88 | int ACM1602NI::address(int column, int row) { |
takuo | 0:aac87cbfc229 | 89 | return 0x80 + row * 0x40 + column; |
takuo | 0:aac87cbfc229 | 90 | } |
takuo | 0:aac87cbfc229 | 91 | |
takuo | 0:aac87cbfc229 | 92 | int ACM1602NI::columns() { |
takuo | 0:aac87cbfc229 | 93 | return display_columns; |
takuo | 0:aac87cbfc229 | 94 | } |
takuo | 0:aac87cbfc229 | 95 | |
takuo | 0:aac87cbfc229 | 96 | int ACM1602NI::rows() { |
takuo | 0:aac87cbfc229 | 97 | return display_rows; |
takuo | 0:aac87cbfc229 | 98 | } |
takuo | 0:aac87cbfc229 | 99 |