LCDモジュール AQM0802 を制御するクラス

Committer:
orivant
Date:
Sun Dec 18 08:12:40 2016 +0000
Revision:
0:ea6cbd4e50ce
LCD?????AQM0802?????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
orivant 0:ea6cbd4e50ce 1 /**
orivant 0:ea6cbd4e50ce 2 * @file LCDモジュール AQM0802 を制御するクラスの定義
orivant 0:ea6cbd4e50ce 3 */
orivant 0:ea6cbd4e50ce 4 #include "LcdAqm0802.h"
orivant 0:ea6cbd4e50ce 5
orivant 0:ea6cbd4e50ce 6 const uint8_t LcdAqm0802::LINE_HEAD_ADDR_TBL[LINE_COUNT] = {0x00, 0x40}; //!< 行先頭アドレス
orivant 0:ea6cbd4e50ce 7
orivant 0:ea6cbd4e50ce 8 /** コンストラクタ
orivant 0:ea6cbd4e50ce 9 */
orivant 0:ea6cbd4e50ce 10 LcdAqm0802::LcdAqm0802(PinName pinReset, PinName pinSDA, PinName pinSCL)
orivant 0:ea6cbd4e50ce 11 :m_pinReset(pinReset)
orivant 0:ea6cbd4e50ce 12 ,m_pinSDA(pinSDA)
orivant 0:ea6cbd4e50ce 13 ,m_pinSCL(pinSCL)
orivant 0:ea6cbd4e50ce 14 ,m_resetOut(pinReset)
orivant 0:ea6cbd4e50ce 15 ,m_i2c(pinSDA, pinSCL)
orivant 0:ea6cbd4e50ce 16 ,m_displayVisible(true)
orivant 0:ea6cbd4e50ce 17 ,m_cursorVisible(false)
orivant 0:ea6cbd4e50ce 18 ,m_blinkEnable(false)
orivant 0:ea6cbd4e50ce 19 ,m_contrast(DEFAULT_CONTRAST)
orivant 0:ea6cbd4e50ce 20 ,m_currentLineNo(0)
orivant 0:ea6cbd4e50ce 21 ,m_currentCharaNo(0)
orivant 0:ea6cbd4e50ce 22 {
orivant 0:ea6cbd4e50ce 23 m_resetOut = 1;
orivant 0:ea6cbd4e50ce 24 m_i2c.frequency(I2C_MAX_FREQUENCY);
orivant 0:ea6cbd4e50ce 25 }
orivant 0:ea6cbd4e50ce 26 /** コンストラクタ
orivant 0:ea6cbd4e50ce 27 */
orivant 0:ea6cbd4e50ce 28 LcdAqm0802::LcdAqm0802(PinName pinReset, I2C& i2c)
orivant 0:ea6cbd4e50ce 29 :m_pinReset(pinReset)
orivant 0:ea6cbd4e50ce 30 ,m_pinSDA(NC)
orivant 0:ea6cbd4e50ce 31 ,m_pinSCL(NC)
orivant 0:ea6cbd4e50ce 32 ,m_resetOut(pinReset)
orivant 0:ea6cbd4e50ce 33 ,m_i2c(i2c)
orivant 0:ea6cbd4e50ce 34 ,m_displayVisible(true)
orivant 0:ea6cbd4e50ce 35 ,m_cursorVisible(false)
orivant 0:ea6cbd4e50ce 36 ,m_blinkEnable(false)
orivant 0:ea6cbd4e50ce 37 ,m_contrast(DEFAULT_CONTRAST)
orivant 0:ea6cbd4e50ce 38 ,m_currentLineNo(0)
orivant 0:ea6cbd4e50ce 39 ,m_currentCharaNo(0)
orivant 0:ea6cbd4e50ce 40 {
orivant 0:ea6cbd4e50ce 41 m_resetOut = 1;
orivant 0:ea6cbd4e50ce 42 }
orivant 0:ea6cbd4e50ce 43 /** デストラクタ
orivant 0:ea6cbd4e50ce 44 */
orivant 0:ea6cbd4e50ce 45 LcdAqm0802::~LcdAqm0802()
orivant 0:ea6cbd4e50ce 46 {
orivant 0:ea6cbd4e50ce 47 }
orivant 0:ea6cbd4e50ce 48
orivant 0:ea6cbd4e50ce 49
orivant 0:ea6cbd4e50ce 50 /** 初期化
orivant 0:ea6cbd4e50ce 51 */
orivant 0:ea6cbd4e50ce 52 void LcdAqm0802::initialize()
orivant 0:ea6cbd4e50ce 53 {
orivant 0:ea6cbd4e50ce 54 static const int32_t COMMAND_MARGINE_WAIT_US = 27;
orivant 0:ea6cbd4e50ce 55 uint8_t bufByte;
orivant 0:ea6cbd4e50ce 56
orivant 0:ea6cbd4e50ce 57 // Function set
orivant 0:ea6cbd4e50ce 58 writeCommand(FUNC_SET_BYTE_NRM);
orivant 0:ea6cbd4e50ce 59 wait_us(COMMAND_MARGINE_WAIT_US);
orivant 0:ea6cbd4e50ce 60 // Function set
orivant 0:ea6cbd4e50ce 61 writeCommand(FUNC_SET_BYTE_EX);
orivant 0:ea6cbd4e50ce 62 wait_us(COMMAND_MARGINE_WAIT_US);
orivant 0:ea6cbd4e50ce 63 // Internal OSC frequency
orivant 0:ea6cbd4e50ce 64 writeCommand(0x14);
orivant 0:ea6cbd4e50ce 65 wait_us(COMMAND_MARGINE_WAIT_US);
orivant 0:ea6cbd4e50ce 66 // Contrast set
orivant 0:ea6cbd4e50ce 67 bufByte = calcContrastSettingByteLowwer();
orivant 0:ea6cbd4e50ce 68 writeCommand(bufByte);
orivant 0:ea6cbd4e50ce 69 wait_us(COMMAND_MARGINE_WAIT_US);
orivant 0:ea6cbd4e50ce 70 // Power/ICON/Contrast control
orivant 0:ea6cbd4e50ce 71 bufByte = calcContrastSettingByteUpper();
orivant 0:ea6cbd4e50ce 72 writeCommand(bufByte);
orivant 0:ea6cbd4e50ce 73 wait_us(COMMAND_MARGINE_WAIT_US);
orivant 0:ea6cbd4e50ce 74 // Follower control
orivant 0:ea6cbd4e50ce 75 writeCommand(0x6c);
orivant 0:ea6cbd4e50ce 76 wait_ms(200);
orivant 0:ea6cbd4e50ce 77 // Function set
orivant 0:ea6cbd4e50ce 78 writeCommand(FUNC_SET_BYTE_NRM);
orivant 0:ea6cbd4e50ce 79 wait_us(COMMAND_MARGINE_WAIT_US);
orivant 0:ea6cbd4e50ce 80 // Display ON/OFF control
orivant 0:ea6cbd4e50ce 81 bufByte = calcDisplayControlByteData();
orivant 0:ea6cbd4e50ce 82 writeCommand(bufByte);
orivant 0:ea6cbd4e50ce 83 wait_us(COMMAND_MARGINE_WAIT_US);
orivant 0:ea6cbd4e50ce 84 // Clear Display
orivant 0:ea6cbd4e50ce 85 writeCommand(0x01);
orivant 0:ea6cbd4e50ce 86 wait_us(1080);
orivant 0:ea6cbd4e50ce 87 }
orivant 0:ea6cbd4e50ce 88 /** リセット
orivant 0:ea6cbd4e50ce 89 */
orivant 0:ea6cbd4e50ce 90 void LcdAqm0802::reset()
orivant 0:ea6cbd4e50ce 91 {
orivant 0:ea6cbd4e50ce 92 // リセット信号を送信
orivant 0:ea6cbd4e50ce 93 m_resetOut = 0;
orivant 0:ea6cbd4e50ce 94 // ちょっと待つ
orivant 0:ea6cbd4e50ce 95 wait_us(5);
orivant 0:ea6cbd4e50ce 96 // リセット信号を止める
orivant 0:ea6cbd4e50ce 97 m_resetOut = 1;
orivant 0:ea6cbd4e50ce 98 }
orivant 0:ea6cbd4e50ce 99
orivant 0:ea6cbd4e50ce 100
orivant 0:ea6cbd4e50ce 101 /** 1文字描画
orivant 0:ea6cbd4e50ce 102 */
orivant 0:ea6cbd4e50ce 103 void LcdAqm0802::putc(char c)
orivant 0:ea6cbd4e50ce 104 {
orivant 0:ea6cbd4e50ce 105 writeData(c);
orivant 0:ea6cbd4e50ce 106 bool ret = incCursorPos();
orivant 0:ea6cbd4e50ce 107 if (ret) {
orivant 0:ea6cbd4e50ce 108 // 改行が必要なら、カーソル位置を変更
orivant 0:ea6cbd4e50ce 109 uint8_t bufByte = calcCursorAddr(m_currentLineNo, m_currentCharaNo);
orivant 0:ea6cbd4e50ce 110 writeCommand(0x80 | bufByte);
orivant 0:ea6cbd4e50ce 111 }
orivant 0:ea6cbd4e50ce 112 }
orivant 0:ea6cbd4e50ce 113 /** 文字列描画
orivant 0:ea6cbd4e50ce 114 */
orivant 0:ea6cbd4e50ce 115 void LcdAqm0802::puts(const char* pStr)
orivant 0:ea6cbd4e50ce 116 {
orivant 0:ea6cbd4e50ce 117 // 文字を判定しながら入力する処理
orivant 0:ea6cbd4e50ce 118 uint8_t addr;
orivant 0:ea6cbd4e50ce 119 bool stayLocate = false;
orivant 0:ea6cbd4e50ce 120 bool chainPut = false;
orivant 0:ea6cbd4e50ce 121 const char* pt = pStr;
orivant 0:ea6cbd4e50ce 122 bool ret;
orivant 0:ea6cbd4e50ce 123 // LCD操作開始
orivant 0:ea6cbd4e50ce 124 m_i2c.start();
orivant 0:ea6cbd4e50ce 125 m_i2c.write(ADDR_BYTE_WRITE);
orivant 0:ea6cbd4e50ce 126 while (*pt) {
orivant 0:ea6cbd4e50ce 127 const char &c = *pt;
orivant 0:ea6cbd4e50ce 128 if (c == '\r') {
orivant 0:ea6cbd4e50ce 129 // 復帰コードの場合、行の先頭にカーソルを移動する
orivant 0:ea6cbd4e50ce 130 m_currentCharaNo = 0;
orivant 0:ea6cbd4e50ce 131 // 実際の移動処理は後で行う
orivant 0:ea6cbd4e50ce 132 stayLocate = true;
orivant 0:ea6cbd4e50ce 133 }
orivant 0:ea6cbd4e50ce 134 else if (c == '\n') {
orivant 0:ea6cbd4e50ce 135 // 改行コードの場合、次の行にカーソルを移動する
orivant 0:ea6cbd4e50ce 136 m_currentLineNo = (m_currentLineNo + 1) % LINE_COUNT;
orivant 0:ea6cbd4e50ce 137 // 実際の移動処理は後で行う
orivant 0:ea6cbd4e50ce 138 stayLocate = true;
orivant 0:ea6cbd4e50ce 139 }
orivant 0:ea6cbd4e50ce 140 else {
orivant 0:ea6cbd4e50ce 141 // 文字入力処理
orivant 0:ea6cbd4e50ce 142 if (stayLocate) {
orivant 0:ea6cbd4e50ce 143 // 復帰・改行の予約がある場合は先に処理
orivant 0:ea6cbd4e50ce 144 if (chainPut) {
orivant 0:ea6cbd4e50ce 145 // 文字入力中の場合は一旦通信を止めないとコマンドを受け付けない
orivant 0:ea6cbd4e50ce 146 m_i2c.stop();
orivant 0:ea6cbd4e50ce 147 m_i2c.start();
orivant 0:ea6cbd4e50ce 148 m_i2c.write(ADDR_BYTE_WRITE);
orivant 0:ea6cbd4e50ce 149 }
orivant 0:ea6cbd4e50ce 150 addr = calcCursorAddr(m_currentLineNo, m_currentCharaNo);
orivant 0:ea6cbd4e50ce 151 m_i2c.write(CTRL_BYTE_SINGLE_COMMAND);
orivant 0:ea6cbd4e50ce 152 m_i2c.write(0x80 | addr);
orivant 0:ea6cbd4e50ce 153 stayLocate = false;
orivant 0:ea6cbd4e50ce 154 chainPut = false;
orivant 0:ea6cbd4e50ce 155 }
orivant 0:ea6cbd4e50ce 156 if (!chainPut) {
orivant 0:ea6cbd4e50ce 157 // 文字入力が途切れた後はデータ入力コントロールを行う
orivant 0:ea6cbd4e50ce 158 m_i2c.write(CTRL_BYTE_CHAIN_DATA);
orivant 0:ea6cbd4e50ce 159 chainPut = true;
orivant 0:ea6cbd4e50ce 160 }
orivant 0:ea6cbd4e50ce 161 // 文字入力
orivant 0:ea6cbd4e50ce 162 m_i2c.write(c);
orivant 0:ea6cbd4e50ce 163 // カーソル位置管理処理
orivant 0:ea6cbd4e50ce 164 ret = incCursorPos();
orivant 0:ea6cbd4e50ce 165 if (ret) {
orivant 0:ea6cbd4e50ce 166 // 改行が発生したのでカーソル位置変更
orivant 0:ea6cbd4e50ce 167 // 実際の移動処理は後で行う
orivant 0:ea6cbd4e50ce 168 stayLocate = true;
orivant 0:ea6cbd4e50ce 169 }
orivant 0:ea6cbd4e50ce 170 }
orivant 0:ea6cbd4e50ce 171 pt++;
orivant 0:ea6cbd4e50ce 172 }
orivant 0:ea6cbd4e50ce 173 if (stayLocate) {
orivant 0:ea6cbd4e50ce 174 // 復帰・改行の予約が残っている場合は処理する
orivant 0:ea6cbd4e50ce 175 if (chainPut) {
orivant 0:ea6cbd4e50ce 176 // 文字入力中の場合は一旦通信を止めないとコマンドを受け付けない
orivant 0:ea6cbd4e50ce 177 m_i2c.stop();
orivant 0:ea6cbd4e50ce 178 m_i2c.start();
orivant 0:ea6cbd4e50ce 179 m_i2c.write(ADDR_BYTE_WRITE);
orivant 0:ea6cbd4e50ce 180 }
orivant 0:ea6cbd4e50ce 181 addr = calcCursorAddr(m_currentLineNo, m_currentCharaNo);
orivant 0:ea6cbd4e50ce 182 m_i2c.write(CTRL_BYTE_SINGLE_COMMAND);
orivant 0:ea6cbd4e50ce 183 m_i2c.write(0x80 | addr);
orivant 0:ea6cbd4e50ce 184 }
orivant 0:ea6cbd4e50ce 185 // LCD操作終了
orivant 0:ea6cbd4e50ce 186 m_i2c.stop();
orivant 0:ea6cbd4e50ce 187 }
orivant 0:ea6cbd4e50ce 188 /** フォーマット指定描画
orivant 0:ea6cbd4e50ce 189 */
orivant 0:ea6cbd4e50ce 190 void LcdAqm0802::printf(const char* pFormat, ...)
orivant 0:ea6cbd4e50ce 191 {
orivant 0:ea6cbd4e50ce 192 va_list ap;
orivant 0:ea6cbd4e50ce 193 va_start(ap, pFormat);
orivant 0:ea6cbd4e50ce 194 char* allocatedBuffer;
orivant 0:ea6cbd4e50ce 195 int size = vasprintf(&allocatedBuffer, pFormat, ap);
orivant 0:ea6cbd4e50ce 196 va_end(ap);
orivant 0:ea6cbd4e50ce 197 puts(allocatedBuffer);
orivant 0:ea6cbd4e50ce 198 free(allocatedBuffer);
orivant 0:ea6cbd4e50ce 199 }
orivant 0:ea6cbd4e50ce 200
orivant 0:ea6cbd4e50ce 201
orivant 0:ea6cbd4e50ce 202 /** カーソル位置変更
orivant 0:ea6cbd4e50ce 203 */
orivant 0:ea6cbd4e50ce 204 void LcdAqm0802::locate(uint8_t lineNo, uint8_t charaNo)
orivant 0:ea6cbd4e50ce 205 {
orivant 0:ea6cbd4e50ce 206 // カーソル位置クランプ
orivant 0:ea6cbd4e50ce 207 if (lineNo >= LINE_COUNT) lineNo = LINE_COUNT - 1;
orivant 0:ea6cbd4e50ce 208 if (charaNo >= LINE_CHARA_COUNT) charaNo = LINE_CHARA_COUNT - 1;
orivant 0:ea6cbd4e50ce 209 m_currentLineNo = lineNo;
orivant 0:ea6cbd4e50ce 210 m_currentCharaNo = charaNo;
orivant 0:ea6cbd4e50ce 211 uint8_t addr = calcCursorAddr(m_currentLineNo, m_currentCharaNo);
orivant 0:ea6cbd4e50ce 212 writeCommand(0x80 | addr);
orivant 0:ea6cbd4e50ce 213 }
orivant 0:ea6cbd4e50ce 214 /** カーソル位置取得:行番号
orivant 0:ea6cbd4e50ce 215 */
orivant 0:ea6cbd4e50ce 216 uint8_t LcdAqm0802::getCurrentLineNo() const
orivant 0:ea6cbd4e50ce 217 {
orivant 0:ea6cbd4e50ce 218 return m_currentLineNo;
orivant 0:ea6cbd4e50ce 219 }
orivant 0:ea6cbd4e50ce 220 /** カーソル位置取得:文字番号
orivant 0:ea6cbd4e50ce 221 */
orivant 0:ea6cbd4e50ce 222 uint8_t LcdAqm0802::getCurrentCharaNo() const
orivant 0:ea6cbd4e50ce 223 {
orivant 0:ea6cbd4e50ce 224 return m_currentCharaNo;
orivant 0:ea6cbd4e50ce 225 }
orivant 0:ea6cbd4e50ce 226
orivant 0:ea6cbd4e50ce 227
orivant 0:ea6cbd4e50ce 228 /** 画面クリア
orivant 0:ea6cbd4e50ce 229 */
orivant 0:ea6cbd4e50ce 230 void LcdAqm0802::clearDisplay()
orivant 0:ea6cbd4e50ce 231 {
orivant 0:ea6cbd4e50ce 232 writeCommand(0x01);
orivant 0:ea6cbd4e50ce 233 m_currentLineNo = 0;
orivant 0:ea6cbd4e50ce 234 m_currentCharaNo = 0;
orivant 0:ea6cbd4e50ce 235 wait_ms(1);
orivant 0:ea6cbd4e50ce 236 }
orivant 0:ea6cbd4e50ce 237 /** 行クリア
orivant 0:ea6cbd4e50ce 238 */
orivant 0:ea6cbd4e50ce 239 void LcdAqm0802::clearLine(uint8_t lineNo)
orivant 0:ea6cbd4e50ce 240 {
orivant 0:ea6cbd4e50ce 241 if (lineNo >= LINE_COUNT) return;
orivant 0:ea6cbd4e50ce 242 // クリアする行の先頭に移動
orivant 0:ea6cbd4e50ce 243 uint8_t addr = calcCursorAddr(lineNo, 0);
orivant 0:ea6cbd4e50ce 244 writeCommand(0x80 | addr);
orivant 0:ea6cbd4e50ce 245 // 1行全体に' '書き込み
orivant 0:ea6cbd4e50ce 246 uint8_t data[LINE_CHARA_COUNT];
orivant 0:ea6cbd4e50ce 247 for (uint8_t i = 0; i < LINE_CHARA_COUNT; i++) data[i] = ' ';
orivant 0:ea6cbd4e50ce 248 writeData(data, LINE_CHARA_COUNT);
orivant 0:ea6cbd4e50ce 249 // カーソル位置をクリアした行の先頭に移動
orivant 0:ea6cbd4e50ce 250 m_currentLineNo = lineNo;
orivant 0:ea6cbd4e50ce 251 m_currentCharaNo = 0;
orivant 0:ea6cbd4e50ce 252 addr = calcCursorAddr(m_currentLineNo, m_currentCharaNo);
orivant 0:ea6cbd4e50ce 253 writeCommand(0x80 | addr);
orivant 0:ea6cbd4e50ce 254 }
orivant 0:ea6cbd4e50ce 255 /** 範囲クリア
orivant 0:ea6cbd4e50ce 256 */
orivant 0:ea6cbd4e50ce 257 void LcdAqm0802::clearRange(uint8_t startLineNo, uint8_t startCharaNo, uint8_t endLineNo, uint8_t endCharaNo)
orivant 0:ea6cbd4e50ce 258 {
orivant 0:ea6cbd4e50ce 259 if (startLineNo >= LINE_COUNT) return;
orivant 0:ea6cbd4e50ce 260 if (startCharaNo >= LINE_CHARA_COUNT) return;
orivant 0:ea6cbd4e50ce 261 if (startLineNo > endLineNo) return;
orivant 0:ea6cbd4e50ce 262 if (startCharaNo > endCharaNo) return;
orivant 0:ea6cbd4e50ce 263 if (endLineNo >= LINE_COUNT) endLineNo = LINE_COUNT - 1;
orivant 0:ea6cbd4e50ce 264 if (endCharaNo >= LINE_CHARA_COUNT) endCharaNo = LINE_CHARA_COUNT - 1;
orivant 0:ea6cbd4e50ce 265 uint8_t addr;
orivant 0:ea6cbd4e50ce 266 const uint8_t eraseCount = endCharaNo - startCharaNo + 1;
orivant 0:ea6cbd4e50ce 267 uint8_t eraseData[eraseCount];
orivant 0:ea6cbd4e50ce 268 for (uint8_t i = 0; i < eraseCount; i++) eraseData[i] = ' ';
orivant 0:ea6cbd4e50ce 269 for (uint8_t lineNo = startLineNo; lineNo <= endLineNo; lineNo++) {
orivant 0:ea6cbd4e50ce 270 // 対象行の削除開始位置にカーソル移動
orivant 0:ea6cbd4e50ce 271 addr = calcCursorAddr(lineNo, startCharaNo);
orivant 0:ea6cbd4e50ce 272 writeCommand(0x80 | addr);
orivant 0:ea6cbd4e50ce 273 // 消す範囲に' 'を書き込み
orivant 0:ea6cbd4e50ce 274 writeData(eraseData, eraseCount);
orivant 0:ea6cbd4e50ce 275 }
orivant 0:ea6cbd4e50ce 276 // クリアした範囲の開始位置にカーソルを移動
orivant 0:ea6cbd4e50ce 277 m_currentLineNo = startLineNo;
orivant 0:ea6cbd4e50ce 278 m_currentCharaNo = startCharaNo;
orivant 0:ea6cbd4e50ce 279 addr = calcCursorAddr(m_currentLineNo, m_currentCharaNo);
orivant 0:ea6cbd4e50ce 280 writeCommand(0x80 | addr);
orivant 0:ea6cbd4e50ce 281 }
orivant 0:ea6cbd4e50ce 282
orivant 0:ea6cbd4e50ce 283
orivant 0:ea6cbd4e50ce 284 /** 画面の表示設定
orivant 0:ea6cbd4e50ce 285 */
orivant 0:ea6cbd4e50ce 286 void LcdAqm0802::setDisplayVisible(bool visible)
orivant 0:ea6cbd4e50ce 287 {
orivant 0:ea6cbd4e50ce 288 if (m_displayVisible == visible) return;
orivant 0:ea6cbd4e50ce 289 m_displayVisible = visible;
orivant 0:ea6cbd4e50ce 290 uint8_t command = calcDisplayControlByteData();
orivant 0:ea6cbd4e50ce 291 writeCommand(command);
orivant 0:ea6cbd4e50ce 292 }
orivant 0:ea6cbd4e50ce 293 /** 画面の表示設定取得
orivant 0:ea6cbd4e50ce 294 */
orivant 0:ea6cbd4e50ce 295 bool LcdAqm0802::getDisplayVisible() const
orivant 0:ea6cbd4e50ce 296 {
orivant 0:ea6cbd4e50ce 297 return m_displayVisible;
orivant 0:ea6cbd4e50ce 298 }
orivant 0:ea6cbd4e50ce 299 /** カーソルの表示設定
orivant 0:ea6cbd4e50ce 300 */
orivant 0:ea6cbd4e50ce 301 void LcdAqm0802::setCursorVisible(bool visible)
orivant 0:ea6cbd4e50ce 302 {
orivant 0:ea6cbd4e50ce 303 if (m_cursorVisible == visible) return;
orivant 0:ea6cbd4e50ce 304 m_cursorVisible = visible;
orivant 0:ea6cbd4e50ce 305 uint8_t command = calcDisplayControlByteData();
orivant 0:ea6cbd4e50ce 306 writeCommand(command);
orivant 0:ea6cbd4e50ce 307 }
orivant 0:ea6cbd4e50ce 308 /** カーソルの表示設定取得
orivant 0:ea6cbd4e50ce 309 */
orivant 0:ea6cbd4e50ce 310 bool LcdAqm0802::getCursorVisible() const
orivant 0:ea6cbd4e50ce 311 {
orivant 0:ea6cbd4e50ce 312 return m_cursorVisible;
orivant 0:ea6cbd4e50ce 313 }
orivant 0:ea6cbd4e50ce 314 /** カーソルの点滅設定
orivant 0:ea6cbd4e50ce 315 */
orivant 0:ea6cbd4e50ce 316 void LcdAqm0802::setBlinkEnable(bool enable)
orivant 0:ea6cbd4e50ce 317 {
orivant 0:ea6cbd4e50ce 318 if (m_blinkEnable == enable) return;
orivant 0:ea6cbd4e50ce 319 m_blinkEnable = enable;
orivant 0:ea6cbd4e50ce 320 uint8_t command = calcDisplayControlByteData();
orivant 0:ea6cbd4e50ce 321 writeCommand(command);
orivant 0:ea6cbd4e50ce 322 }
orivant 0:ea6cbd4e50ce 323 /** カーソルの点滅設定取得
orivant 0:ea6cbd4e50ce 324 */
orivant 0:ea6cbd4e50ce 325 bool LcdAqm0802::getBlinkEnable() const
orivant 0:ea6cbd4e50ce 326 {
orivant 0:ea6cbd4e50ce 327 return m_blinkEnable;
orivant 0:ea6cbd4e50ce 328 }
orivant 0:ea6cbd4e50ce 329
orivant 0:ea6cbd4e50ce 330
orivant 0:ea6cbd4e50ce 331 /** コントラスト設定
orivant 0:ea6cbd4e50ce 332 */
orivant 0:ea6cbd4e50ce 333 void LcdAqm0802::setContrast(uint8_t val)
orivant 0:ea6cbd4e50ce 334 {
orivant 0:ea6cbd4e50ce 335 if (val > MAX_CONTRAST) val = MAX_CONTRAST;
orivant 0:ea6cbd4e50ce 336 if (m_contrast == val) return;
orivant 0:ea6cbd4e50ce 337
orivant 0:ea6cbd4e50ce 338 m_contrast = val;
orivant 0:ea6cbd4e50ce 339
orivant 0:ea6cbd4e50ce 340 // LCD操作開始
orivant 0:ea6cbd4e50ce 341 m_i2c.start();
orivant 0:ea6cbd4e50ce 342 m_i2c.write(ADDR_BYTE_WRITE);
orivant 0:ea6cbd4e50ce 343 // 拡張コマンド使用設定
orivant 0:ea6cbd4e50ce 344 m_i2c.write(CTRL_BYTE_SINGLE_COMMAND);
orivant 0:ea6cbd4e50ce 345 m_i2c.write(FUNC_SET_BYTE_EX);
orivant 0:ea6cbd4e50ce 346 // コントラスト設定
orivant 0:ea6cbd4e50ce 347 unsigned char bufByte = calcContrastSettingByteLowwer();
orivant 0:ea6cbd4e50ce 348 m_i2c.write(CTRL_BYTE_SINGLE_COMMAND);
orivant 0:ea6cbd4e50ce 349 m_i2c.write(bufByte);
orivant 0:ea6cbd4e50ce 350 bufByte = calcContrastSettingByteUpper();
orivant 0:ea6cbd4e50ce 351 m_i2c.write(CTRL_BYTE_SINGLE_COMMAND);
orivant 0:ea6cbd4e50ce 352 m_i2c.write(bufByte);
orivant 0:ea6cbd4e50ce 353 // 拡張コマンド使用解除
orivant 0:ea6cbd4e50ce 354 m_i2c.write(CTRL_BYTE_SINGLE_COMMAND);
orivant 0:ea6cbd4e50ce 355 m_i2c.write(FUNC_SET_BYTE_NRM);
orivant 0:ea6cbd4e50ce 356 // LCD操作終了
orivant 0:ea6cbd4e50ce 357 m_i2c.stop();
orivant 0:ea6cbd4e50ce 358 }
orivant 0:ea6cbd4e50ce 359 /** コントラスト取得
orivant 0:ea6cbd4e50ce 360 */
orivant 0:ea6cbd4e50ce 361 unsigned char LcdAqm0802::getContrast() const
orivant 0:ea6cbd4e50ce 362 {
orivant 0:ea6cbd4e50ce 363 return m_contrast;
orivant 0:ea6cbd4e50ce 364 }
orivant 0:ea6cbd4e50ce 365
orivant 0:ea6cbd4e50ce 366
orivant 0:ea6cbd4e50ce 367 /** LCDコマンド単発書き込み
orivant 0:ea6cbd4e50ce 368 */
orivant 0:ea6cbd4e50ce 369 void LcdAqm0802::writeCommand(uint8_t commandByte)
orivant 0:ea6cbd4e50ce 370 {
orivant 0:ea6cbd4e50ce 371 m_i2c.start(); // I2C通信開始
orivant 0:ea6cbd4e50ce 372 m_i2c.write(ADDR_BYTE_WRITE); // LCDへ書き込み指定
orivant 0:ea6cbd4e50ce 373 m_i2c.write(CTRL_BYTE_SINGLE_COMMAND); // 単発コマンド指定
orivant 0:ea6cbd4e50ce 374 m_i2c.write(commandByte); // コマンド書き込み
orivant 0:ea6cbd4e50ce 375 m_i2c.stop(); // I2C通信終了
orivant 0:ea6cbd4e50ce 376 }
orivant 0:ea6cbd4e50ce 377 /** LCDコマンド連続書き込み
orivant 0:ea6cbd4e50ce 378 */
orivant 0:ea6cbd4e50ce 379 void LcdAqm0802::writeCommand(uint8_t commandBytes[], uint8_t commandCount)
orivant 0:ea6cbd4e50ce 380 {
orivant 0:ea6cbd4e50ce 381 m_i2c.start(); // I2C通信開始
orivant 0:ea6cbd4e50ce 382 m_i2c.write(ADDR_BYTE_WRITE); // LCDへ書き込み指定
orivant 0:ea6cbd4e50ce 383 m_i2c.write(CTRL_BYTE_CHAIN_COMMAND); // 連続コマンド指定
orivant 0:ea6cbd4e50ce 384 for (uint8_t i = 0; i < commandCount; i++) {
orivant 0:ea6cbd4e50ce 385 m_i2c.write(commandBytes[i]); // コマンド書き込み
orivant 0:ea6cbd4e50ce 386 }
orivant 0:ea6cbd4e50ce 387 m_i2c.stop(); // I2C通信終了
orivant 0:ea6cbd4e50ce 388 }
orivant 0:ea6cbd4e50ce 389 /** LCDデータ単発書き込み
orivant 0:ea6cbd4e50ce 390 */
orivant 0:ea6cbd4e50ce 391 void LcdAqm0802::writeData(uint8_t dataByte)
orivant 0:ea6cbd4e50ce 392 {
orivant 0:ea6cbd4e50ce 393 m_i2c.start(); // I2C通信開始
orivant 0:ea6cbd4e50ce 394 m_i2c.write(ADDR_BYTE_WRITE); // LCDへ書き込み指定
orivant 0:ea6cbd4e50ce 395 m_i2c.write(CTRL_BYTE_SINGLE_DATA); // 単発データ指定
orivant 0:ea6cbd4e50ce 396 m_i2c.write(dataByte); // データ書き込み
orivant 0:ea6cbd4e50ce 397 m_i2c.stop(); // I2C通信終了
orivant 0:ea6cbd4e50ce 398 }
orivant 0:ea6cbd4e50ce 399 /** LCDデータ連続書き込み
orivant 0:ea6cbd4e50ce 400 */
orivant 0:ea6cbd4e50ce 401 void LcdAqm0802::writeData(uint8_t dataBytes[], uint8_t dataCount)
orivant 0:ea6cbd4e50ce 402 {
orivant 0:ea6cbd4e50ce 403 m_i2c.start(); // I2C通信開始
orivant 0:ea6cbd4e50ce 404 m_i2c.write(ADDR_BYTE_WRITE); // LCDへ書き込み指定
orivant 0:ea6cbd4e50ce 405 m_i2c.write(CTRL_BYTE_CHAIN_DATA); // 連続データ指定
orivant 0:ea6cbd4e50ce 406 for (uint8_t i = 0; i < dataCount; i++) {
orivant 0:ea6cbd4e50ce 407 m_i2c.write(dataBytes[i]); // データ書き込み
orivant 0:ea6cbd4e50ce 408 }
orivant 0:ea6cbd4e50ce 409 m_i2c.stop(); // I2C通信終了
orivant 0:ea6cbd4e50ce 410 }
orivant 0:ea6cbd4e50ce 411
orivant 0:ea6cbd4e50ce 412
orivant 0:ea6cbd4e50ce 413 /** コントラスト設定用バイトデータ生成(上位ビット)
orivant 0:ea6cbd4e50ce 414 */
orivant 0:ea6cbd4e50ce 415 uint8_t LcdAqm0802::calcContrastSettingByteUpper() const
orivant 0:ea6cbd4e50ce 416 {
orivant 0:ea6cbd4e50ce 417 uint8_t ret = 0x54;
orivant 0:ea6cbd4e50ce 418 uint8_t contrast = (m_contrast >> 4) & 0x03;
orivant 0:ea6cbd4e50ce 419 ret = ret | contrast;
orivant 0:ea6cbd4e50ce 420 return ret;
orivant 0:ea6cbd4e50ce 421 }
orivant 0:ea6cbd4e50ce 422 /** コントラスト設定用バイトデータ生成(下位ビット)
orivant 0:ea6cbd4e50ce 423 */
orivant 0:ea6cbd4e50ce 424 uint8_t LcdAqm0802::calcContrastSettingByteLowwer() const
orivant 0:ea6cbd4e50ce 425 {
orivant 0:ea6cbd4e50ce 426 uint8_t ret = 0x70;
orivant 0:ea6cbd4e50ce 427 uint8_t contrast = m_contrast & 0x0f;
orivant 0:ea6cbd4e50ce 428 ret = ret | contrast;
orivant 0:ea6cbd4e50ce 429 return ret;
orivant 0:ea6cbd4e50ce 430 }
orivant 0:ea6cbd4e50ce 431
orivant 0:ea6cbd4e50ce 432
orivant 0:ea6cbd4e50ce 433 /** ディスプレイ制御コマンド指定バイトデータの生成
orivant 0:ea6cbd4e50ce 434 */
orivant 0:ea6cbd4e50ce 435 uint8_t LcdAqm0802::calcDisplayControlByteData(bool displayVisible, bool cursorVisible, bool blinkEnable) const
orivant 0:ea6cbd4e50ce 436 {
orivant 0:ea6cbd4e50ce 437 uint8_t ret = 0x08;
orivant 0:ea6cbd4e50ce 438 if (displayVisible) {
orivant 0:ea6cbd4e50ce 439 ret = ret | 0x04;
orivant 0:ea6cbd4e50ce 440 }
orivant 0:ea6cbd4e50ce 441 if (cursorVisible) {
orivant 0:ea6cbd4e50ce 442 ret = ret | 0x02;
orivant 0:ea6cbd4e50ce 443 }
orivant 0:ea6cbd4e50ce 444 if (blinkEnable) {
orivant 0:ea6cbd4e50ce 445 ret = ret | 0x01;
orivant 0:ea6cbd4e50ce 446 }
orivant 0:ea6cbd4e50ce 447 return ret;
orivant 0:ea6cbd4e50ce 448 }
orivant 0:ea6cbd4e50ce 449
orivant 0:ea6cbd4e50ce 450
orivant 0:ea6cbd4e50ce 451 /** カーソル位置移動管理
orivant 0:ea6cbd4e50ce 452 */
orivant 0:ea6cbd4e50ce 453 bool LcdAqm0802::incCursorPos()
orivant 0:ea6cbd4e50ce 454 {
orivant 0:ea6cbd4e50ce 455 m_currentCharaNo++;
orivant 0:ea6cbd4e50ce 456 if (m_currentCharaNo >= LINE_CHARA_COUNT) {
orivant 0:ea6cbd4e50ce 457 m_currentCharaNo = 0;
orivant 0:ea6cbd4e50ce 458 m_currentLineNo = (m_currentLineNo + 1) % LINE_COUNT;
orivant 0:ea6cbd4e50ce 459 return true;
orivant 0:ea6cbd4e50ce 460 }
orivant 0:ea6cbd4e50ce 461 return false;
orivant 0:ea6cbd4e50ce 462 }
orivant 0:ea6cbd4e50ce 463
orivant 0:ea6cbd4e50ce 464
orivant 0:ea6cbd4e50ce 465 /** カーソル位置のアドレス計算
orivant 0:ea6cbd4e50ce 466 */
orivant 0:ea6cbd4e50ce 467 uint8_t LcdAqm0802::calcCursorAddr(uint8_t lineNo, uint8_t charaNo)
orivant 0:ea6cbd4e50ce 468 {
orivant 0:ea6cbd4e50ce 469 if (lineNo >= LINE_COUNT) lineNo = LINE_COUNT - 1;
orivant 0:ea6cbd4e50ce 470 if (charaNo >= LINE_CHARA_COUNT) charaNo = LINE_CHARA_COUNT - 1;
orivant 0:ea6cbd4e50ce 471 unsigned char addr = LINE_HEAD_ADDR_TBL[lineNo] + charaNo;
orivant 0:ea6cbd4e50ce 472 return addr;
orivant 0:ea6cbd4e50ce 473 }