mbed base board 2016 Check program check for SD card read&write (3 switches, 6 leds, I2C LCD, SD card)
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
ACM1602NI.cpp@2:0ee1c0236a11, 2016-10-26 (annotated)
- Committer:
- tamaki
- Date:
- Wed Oct 26 08:13:51 2016 +0000
- Revision:
- 2:0ee1c0236a11
- Parent:
- 1:a41c274f95e2
mbed base board 2016 Check program check for SD card read&write (3 switches, 6 leds, I2C LCD, SD card)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tamaki | 1:a41c274f95e2 | 1 | /* An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01 |
tamaki | 1:a41c274f95e2 | 2 | * Copyright 2013, Takuo WATANABE (wtakuo) |
tamaki | 1:a41c274f95e2 | 3 | * |
tamaki | 1:a41c274f95e2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
tamaki | 1:a41c274f95e2 | 5 | * you may not use this file except in compliance with the License. |
tamaki | 1:a41c274f95e2 | 6 | * You may obtain a copy of the License at |
tamaki | 1:a41c274f95e2 | 7 | * |
tamaki | 1:a41c274f95e2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
tamaki | 1:a41c274f95e2 | 9 | * |
tamaki | 1:a41c274f95e2 | 10 | * Unless required by applicable law or agreed to in writing, software |
tamaki | 1:a41c274f95e2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
tamaki | 1:a41c274f95e2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
tamaki | 1:a41c274f95e2 | 13 | * See the License for the specific language governing permissions and |
tamaki | 1:a41c274f95e2 | 14 | * limitations under the License. |
tamaki | 1:a41c274f95e2 | 15 | */ |
tamaki | 1:a41c274f95e2 | 16 | |
tamaki | 1:a41c274f95e2 | 17 | #include "mbed.h" |
tamaki | 1:a41c274f95e2 | 18 | #include "ACM1602NI.h" |
tamaki | 1:a41c274f95e2 | 19 | |
tamaki | 1:a41c274f95e2 | 20 | #define I2C_SUCCESS 0 |
tamaki | 1:a41c274f95e2 | 21 | #define I2C_FAILURE 1 |
tamaki | 1:a41c274f95e2 | 22 | |
tamaki | 1:a41c274f95e2 | 23 | ACM1602NI::ACM1602NI(PinName sda, PinName scl) : _i2c(sda, scl) |
tamaki | 1:a41c274f95e2 | 24 | { |
tamaki | 1:a41c274f95e2 | 25 | init(); |
tamaki | 1:a41c274f95e2 | 26 | } |
tamaki | 1:a41c274f95e2 | 27 | |
tamaki | 1:a41c274f95e2 | 28 | ACM1602NI::ACM1602NI(I2C &i2c) : _i2c(i2c) |
tamaki | 1:a41c274f95e2 | 29 | { |
tamaki | 1:a41c274f95e2 | 30 | init(); |
tamaki | 1:a41c274f95e2 | 31 | } |
tamaki | 1:a41c274f95e2 | 32 | |
tamaki | 1:a41c274f95e2 | 33 | void ACM1602NI::init() |
tamaki | 1:a41c274f95e2 | 34 | { |
tamaki | 1:a41c274f95e2 | 35 | writeCommand(0x01); |
tamaki | 1:a41c274f95e2 | 36 | wait_ms(i2c_command_wait_ms); |
tamaki | 1:a41c274f95e2 | 37 | writeCommand(0x38); |
tamaki | 1:a41c274f95e2 | 38 | wait_ms(i2c_command_wait_ms); |
tamaki | 1:a41c274f95e2 | 39 | writeCommand(0x0f); |
tamaki | 1:a41c274f95e2 | 40 | wait_ms(i2c_command_wait_ms); |
tamaki | 1:a41c274f95e2 | 41 | writeCommand(0x06); |
tamaki | 1:a41c274f95e2 | 42 | wait_ms(i2c_command_wait_ms); |
tamaki | 1:a41c274f95e2 | 43 | locate(0, 0); |
tamaki | 1:a41c274f95e2 | 44 | } |
tamaki | 1:a41c274f95e2 | 45 | |
tamaki | 1:a41c274f95e2 | 46 | int ACM1602NI::writeBytes(const char *data, int length, bool repeated) |
tamaki | 1:a41c274f95e2 | 47 | { |
tamaki | 1:a41c274f95e2 | 48 | wait_us(i2c_bit_wait_us); |
tamaki | 1:a41c274f95e2 | 49 | _i2c.start(); |
tamaki | 1:a41c274f95e2 | 50 | for (int i = 0; i < length; i++) { |
tamaki | 1:a41c274f95e2 | 51 | wait_us(i2c_bit_wait_us); |
tamaki | 1:a41c274f95e2 | 52 | if (_i2c.write(data[i]) != 1) { |
tamaki | 1:a41c274f95e2 | 53 | wait_us(i2c_bit_wait_us); |
tamaki | 1:a41c274f95e2 | 54 | _i2c.stop(); |
tamaki | 1:a41c274f95e2 | 55 | return I2C_FAILURE; |
tamaki | 1:a41c274f95e2 | 56 | } |
tamaki | 1:a41c274f95e2 | 57 | } |
tamaki | 1:a41c274f95e2 | 58 | if (!repeated) { |
tamaki | 1:a41c274f95e2 | 59 | wait_us(i2c_bit_wait_us); |
tamaki | 1:a41c274f95e2 | 60 | _i2c.stop(); |
tamaki | 1:a41c274f95e2 | 61 | } |
tamaki | 1:a41c274f95e2 | 62 | return I2C_SUCCESS; |
tamaki | 1:a41c274f95e2 | 63 | } |
tamaki | 1:a41c274f95e2 | 64 | |
tamaki | 1:a41c274f95e2 | 65 | void ACM1602NI::character(int column, int row, int c) |
tamaki | 1:a41c274f95e2 | 66 | { |
tamaki | 1:a41c274f95e2 | 67 | writeCommand(address(column, row)); |
tamaki | 1:a41c274f95e2 | 68 | writeData(c); |
tamaki | 1:a41c274f95e2 | 69 | } |
tamaki | 1:a41c274f95e2 | 70 | |
tamaki | 1:a41c274f95e2 | 71 | void ACM1602NI::cls() |
tamaki | 1:a41c274f95e2 | 72 | { |
tamaki | 1:a41c274f95e2 | 73 | writeCommand(0x01); |
tamaki | 1:a41c274f95e2 | 74 | wait_ms(i2c_command_wait_ms); |
tamaki | 1:a41c274f95e2 | 75 | locate(0, 0); |
tamaki | 1:a41c274f95e2 | 76 | } |
tamaki | 1:a41c274f95e2 | 77 | |
tamaki | 1:a41c274f95e2 | 78 | void ACM1602NI::locate(int column, int row) |
tamaki | 1:a41c274f95e2 | 79 | { |
tamaki | 1:a41c274f95e2 | 80 | _column = column; |
tamaki | 1:a41c274f95e2 | 81 | _row = row; |
tamaki | 1:a41c274f95e2 | 82 | } |
tamaki | 1:a41c274f95e2 | 83 | |
tamaki | 1:a41c274f95e2 | 84 | int ACM1602NI::_putc(int value) |
tamaki | 1:a41c274f95e2 | 85 | { |
tamaki | 1:a41c274f95e2 | 86 | if (value == '\n') { |
tamaki | 1:a41c274f95e2 | 87 | _column = 0; |
tamaki | 1:a41c274f95e2 | 88 | _row = (_row + 1) % rows(); |
tamaki | 1:a41c274f95e2 | 89 | } else { |
tamaki | 1:a41c274f95e2 | 90 | character(_column, _row, value); |
tamaki | 1:a41c274f95e2 | 91 | _column++; |
tamaki | 1:a41c274f95e2 | 92 | if (_column >= columns()) { |
tamaki | 1:a41c274f95e2 | 93 | _column = 0; |
tamaki | 1:a41c274f95e2 | 94 | _row = (_row + 1) % rows(); |
tamaki | 1:a41c274f95e2 | 95 | } |
tamaki | 1:a41c274f95e2 | 96 | } |
tamaki | 1:a41c274f95e2 | 97 | return value; |
tamaki | 1:a41c274f95e2 | 98 | } |
tamaki | 1:a41c274f95e2 | 99 | |
tamaki | 1:a41c274f95e2 | 100 | int ACM1602NI::_getc() |
tamaki | 1:a41c274f95e2 | 101 | { |
tamaki | 1:a41c274f95e2 | 102 | return -1; |
tamaki | 1:a41c274f95e2 | 103 | } |
tamaki | 1:a41c274f95e2 | 104 | |
tamaki | 1:a41c274f95e2 | 105 | void ACM1602NI::writeCommand(int command) |
tamaki | 1:a41c274f95e2 | 106 | { |
tamaki | 1:a41c274f95e2 | 107 | char bs[3] = { i2c_addr, 0x00, command }; |
tamaki | 1:a41c274f95e2 | 108 | writeBytes(bs, 3); |
tamaki | 1:a41c274f95e2 | 109 | } |
tamaki | 1:a41c274f95e2 | 110 | |
tamaki | 1:a41c274f95e2 | 111 | void ACM1602NI::writeData(int data) |
tamaki | 1:a41c274f95e2 | 112 | { |
tamaki | 1:a41c274f95e2 | 113 | char bs[3] = { i2c_addr, 0x80, data }; |
tamaki | 1:a41c274f95e2 | 114 | writeBytes(bs, 3); |
tamaki | 1:a41c274f95e2 | 115 | } |
tamaki | 1:a41c274f95e2 | 116 | |
tamaki | 1:a41c274f95e2 | 117 | int ACM1602NI::address(int column, int row) |
tamaki | 1:a41c274f95e2 | 118 | { |
tamaki | 1:a41c274f95e2 | 119 | return 0x80 + row * 0x40 + column; |
tamaki | 1:a41c274f95e2 | 120 | } |
tamaki | 1:a41c274f95e2 | 121 | |
tamaki | 1:a41c274f95e2 | 122 | int ACM1602NI::columns() |
tamaki | 1:a41c274f95e2 | 123 | { |
tamaki | 1:a41c274f95e2 | 124 | return display_columns; |
tamaki | 1:a41c274f95e2 | 125 | } |
tamaki | 1:a41c274f95e2 | 126 | |
tamaki | 1:a41c274f95e2 | 127 | int ACM1602NI::rows() |
tamaki | 1:a41c274f95e2 | 128 | { |
tamaki | 1:a41c274f95e2 | 129 | return display_rows; |
tamaki | 1:a41c274f95e2 | 130 | } |