spi oled

Dependents:   Drone_ground

Fork of TextOLED by Suga koubou

Committer:
okini3939
Date:
Thu May 19 09:02:47 2016 +0000
Revision:
3:c96e6ffd4c43
Parent:
TextOLED.cpp@2:45788bd06d4c
fix spi oled;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 3:c96e6ffd4c43 1 /* mbed SpiOLED Library, for a spi LCD based on Winstar WEH001602 series
okini3939 0:4467cf9395fd 2 * Originl: http://mbed.org/users/simon/libraries/TextLCD/latest
okini3939 0:4467cf9395fd 3 * Modified by Suga
okini3939 0:4467cf9395fd 4 */
okini3939 0:4467cf9395fd 5 /* mbed TextLCD Library, for a 4-bit LCD based on HD44780
okini3939 0:4467cf9395fd 6 * Copyright (c) 2007-2010, sford, http://mbed.org
okini3939 0:4467cf9395fd 7 *
okini3939 0:4467cf9395fd 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
okini3939 0:4467cf9395fd 9 * of this software and associated documentation files (the "Software"), to deal
okini3939 0:4467cf9395fd 10 * in the Software without restriction, including without limitation the rights
okini3939 0:4467cf9395fd 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
okini3939 0:4467cf9395fd 12 * copies of the Software, and to permit persons to whom the Software is
okini3939 0:4467cf9395fd 13 * furnished to do so, subject to the following conditions:
okini3939 0:4467cf9395fd 14 *
okini3939 0:4467cf9395fd 15 * The above copyright notice and this permission notice shall be included in
okini3939 0:4467cf9395fd 16 * all copies or substantial portions of the Software.
okini3939 0:4467cf9395fd 17 *
okini3939 0:4467cf9395fd 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
okini3939 0:4467cf9395fd 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
okini3939 0:4467cf9395fd 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
okini3939 0:4467cf9395fd 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
okini3939 0:4467cf9395fd 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
okini3939 0:4467cf9395fd 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
okini3939 0:4467cf9395fd 24 * THE SOFTWARE.
okini3939 0:4467cf9395fd 25 */
okini3939 0:4467cf9395fd 26
okini3939 3:c96e6ffd4c43 27 #include "SpiOLED.h"
okini3939 0:4467cf9395fd 28 #include "mbed.h"
okini3939 0:4467cf9395fd 29
okini3939 3:c96e6ffd4c43 30 SpiOLED::SpiOLED(PinName mosi, PinName miso, PinName sclk, PinName cs, LCDType type) :
okini3939 3:c96e6ffd4c43 31 _spi(mosi, miso, sclk), _cs(cs), _type(type) {
okini3939 0:4467cf9395fd 32
okini3939 3:c96e6ffd4c43 33 _cs = 1;
okini3939 3:c96e6ffd4c43 34 _spi.format(10, 3);
okini3939 0:4467cf9395fd 35
okini3939 0:4467cf9395fd 36 wait_ms(100); // Wait 15ms to ensure powered up
okini3939 0:4467cf9395fd 37
okini3939 3:c96e6ffd4c43 38 writeCommand(0x38); // Function set 001 BW N F - -
okini3939 0:4467cf9395fd 39 writeCommand(0x0C); // 0000 1 D C B
okini3939 0:4467cf9395fd 40 writeCommand(0x06); // Entry mode : 0000 01 I/D S
okini3939 0:4467cf9395fd 41 writeCommand(0x10); // Cursor and Display Shift 0001 S/C R/L 0 0
okini3939 0:4467cf9395fd 42 writeCommand(0x17); // DCDC on
okini3939 2:45788bd06d4c 43 wait_ms(10);
okini3939 2:45788bd06d4c 44 writeCommand(0x02); // Home
okini3939 2:45788bd06d4c 45 cls();
okini3939 0:4467cf9395fd 46 }
okini3939 0:4467cf9395fd 47
okini3939 3:c96e6ffd4c43 48 void SpiOLED::character(int column, int row, int c) {
okini3939 0:4467cf9395fd 49 int a = address(column, row);
okini3939 0:4467cf9395fd 50 writeCommand(a);
okini3939 0:4467cf9395fd 51 writeData(c);
okini3939 0:4467cf9395fd 52 }
okini3939 0:4467cf9395fd 53
okini3939 3:c96e6ffd4c43 54 void SpiOLED::cls() {
okini3939 0:4467cf9395fd 55 writeCommand(0x01); // cls, and set cursor to 0
okini3939 0:4467cf9395fd 56 wait_ms(10); // This command takes 1.64 ms
okini3939 0:4467cf9395fd 57 locate(0, 0);
okini3939 0:4467cf9395fd 58 }
okini3939 0:4467cf9395fd 59
okini3939 3:c96e6ffd4c43 60 void SpiOLED::locate(int column, int row) {
okini3939 0:4467cf9395fd 61 _column = column;
okini3939 0:4467cf9395fd 62 _row = row;
okini3939 0:4467cf9395fd 63 }
okini3939 0:4467cf9395fd 64
okini3939 3:c96e6ffd4c43 65 int SpiOLED::_putc(int value) {
okini3939 0:4467cf9395fd 66 if (value == '\n') {
okini3939 0:4467cf9395fd 67 _column = 0;
okini3939 0:4467cf9395fd 68 _row++;
okini3939 0:4467cf9395fd 69 if (_row >= rows()) {
okini3939 0:4467cf9395fd 70 _row = 0;
okini3939 0:4467cf9395fd 71 }
okini3939 0:4467cf9395fd 72 } else {
okini3939 0:4467cf9395fd 73 character(_column, _row, value);
okini3939 0:4467cf9395fd 74 _column++;
okini3939 0:4467cf9395fd 75 if (_column >= columns()) {
okini3939 0:4467cf9395fd 76 _column = 0;
okini3939 0:4467cf9395fd 77 _row++;
okini3939 0:4467cf9395fd 78 if (_row >= rows()) {
okini3939 0:4467cf9395fd 79 _row = 0;
okini3939 0:4467cf9395fd 80 }
okini3939 0:4467cf9395fd 81 }
okini3939 0:4467cf9395fd 82 }
okini3939 0:4467cf9395fd 83 return value;
okini3939 0:4467cf9395fd 84 }
okini3939 0:4467cf9395fd 85
okini3939 3:c96e6ffd4c43 86 int SpiOLED::_getc() {
okini3939 0:4467cf9395fd 87 return -1;
okini3939 0:4467cf9395fd 88 }
okini3939 0:4467cf9395fd 89
okini3939 3:c96e6ffd4c43 90 void SpiOLED::writeByte(int value, int rs) {
okini3939 3:c96e6ffd4c43 91 int dat;
okini3939 3:c96e6ffd4c43 92
okini3939 3:c96e6ffd4c43 93 dat = value & 0xff;
okini3939 3:c96e6ffd4c43 94 if (rs) dat |= 0x200;
okini3939 3:c96e6ffd4c43 95 _spi.write(dat);
okini3939 3:c96e6ffd4c43 96 }
okini3939 3:c96e6ffd4c43 97
okini3939 3:c96e6ffd4c43 98 void SpiOLED::writeCommand(int command) {
okini3939 3:c96e6ffd4c43 99 _cs = 0;
okini3939 0:4467cf9395fd 100 wait_us(1);
okini3939 3:c96e6ffd4c43 101 writeByte(command, 0);
okini3939 3:c96e6ffd4c43 102 _cs = 1;
okini3939 0:4467cf9395fd 103 }
okini3939 0:4467cf9395fd 104
okini3939 3:c96e6ffd4c43 105 void SpiOLED::writeData(int data) {
okini3939 3:c96e6ffd4c43 106 _cs = 0;
okini3939 0:4467cf9395fd 107 wait_us(1);
okini3939 3:c96e6ffd4c43 108 writeByte(data, 1);
okini3939 3:c96e6ffd4c43 109 _cs = 1;
okini3939 0:4467cf9395fd 110 }
okini3939 0:4467cf9395fd 111
okini3939 3:c96e6ffd4c43 112 int SpiOLED::isBusy() {
okini3939 3:c96e6ffd4c43 113 int dat;
okini3939 3:c96e6ffd4c43 114
okini3939 3:c96e6ffd4c43 115 _cs = 0;
okini3939 2:45788bd06d4c 116 wait_us(1);
okini3939 3:c96e6ffd4c43 117 _spi.write(0x100); // rs=0, rw=1
okini3939 3:c96e6ffd4c43 118 dat = _spi.write(0x100);
okini3939 3:c96e6ffd4c43 119 _cs = 1;
okini3939 3:c96e6ffd4c43 120 dat = dat >> 2;
okini3939 3:c96e6ffd4c43 121 return dat & 0x80;
okini3939 0:4467cf9395fd 122 }
okini3939 0:4467cf9395fd 123
okini3939 3:c96e6ffd4c43 124 int SpiOLED::address(int column, int row) {
okini3939 0:4467cf9395fd 125 switch (_type) {
okini3939 0:4467cf9395fd 126 case LCD20x4:
okini3939 0:4467cf9395fd 127 switch (row) {
okini3939 0:4467cf9395fd 128 case 0:
okini3939 0:4467cf9395fd 129 return 0x80 + column;
okini3939 0:4467cf9395fd 130 case 1:
okini3939 0:4467cf9395fd 131 return 0xc0 + column;
okini3939 0:4467cf9395fd 132 case 2:
okini3939 0:4467cf9395fd 133 return 0x94 + column;
okini3939 0:4467cf9395fd 134 case 3:
okini3939 0:4467cf9395fd 135 return 0xd4 + column;
okini3939 0:4467cf9395fd 136 }
okini3939 0:4467cf9395fd 137 case LCD16x2B:
okini3939 0:4467cf9395fd 138 return 0x80 + (row * 40) + column;
okini3939 0:4467cf9395fd 139 case LCD16x2:
okini3939 0:4467cf9395fd 140 case LCD20x2:
okini3939 0:4467cf9395fd 141 case LCD8x2:
okini3939 0:4467cf9395fd 142 default:
okini3939 0:4467cf9395fd 143 return 0x80 + (row * 0x40) + column;
okini3939 0:4467cf9395fd 144 }
okini3939 0:4467cf9395fd 145 }
okini3939 0:4467cf9395fd 146
okini3939 3:c96e6ffd4c43 147 int SpiOLED::columns() {
okini3939 0:4467cf9395fd 148 switch (_type) {
okini3939 0:4467cf9395fd 149 case LCD8x2:
okini3939 0:4467cf9395fd 150 return 8;
okini3939 0:4467cf9395fd 151 case LCD20x4:
okini3939 0:4467cf9395fd 152 case LCD20x2:
okini3939 0:4467cf9395fd 153 return 20;
okini3939 0:4467cf9395fd 154 case LCD16x2:
okini3939 0:4467cf9395fd 155 case LCD16x2B:
okini3939 0:4467cf9395fd 156 default:
okini3939 0:4467cf9395fd 157 return 16;
okini3939 0:4467cf9395fd 158 }
okini3939 0:4467cf9395fd 159 }
okini3939 0:4467cf9395fd 160
okini3939 3:c96e6ffd4c43 161 int SpiOLED::rows() {
okini3939 0:4467cf9395fd 162 switch (_type) {
okini3939 0:4467cf9395fd 163 case LCD20x4:
okini3939 0:4467cf9395fd 164 return 4;
okini3939 0:4467cf9395fd 165 case LCD16x2:
okini3939 0:4467cf9395fd 166 case LCD16x2B:
okini3939 0:4467cf9395fd 167 case LCD20x2:
okini3939 0:4467cf9395fd 168 case LCD8x2:
okini3939 0:4467cf9395fd 169 default:
okini3939 0:4467cf9395fd 170 return 2;
okini3939 0:4467cf9395fd 171 }
okini3939 0:4467cf9395fd 172 }
okini3939 1:df8722660f48 173
okini3939 3:c96e6ffd4c43 174 void SpiOLED::cursor (bool flg) {
okini3939 1:df8722660f48 175 if (flg) {
okini3939 1:df8722660f48 176 writeCommand(0x0F); // 0000 1 D C B
okini3939 1:df8722660f48 177 } else {
okini3939 1:df8722660f48 178 writeCommand(0x0C); // 0000 1 D C B
okini3939 1:df8722660f48 179 }
okini3939 1:df8722660f48 180 }