Displays distance to start location on OLED screen.

Dependencies:   mbed

Committer:
iforce2d
Date:
Wed Mar 07 12:49:14 2018 +0000
Revision:
0:972874f31c98
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iforce2d 0:972874f31c98 1 /*
iforce2d 0:972874f31c98 2
iforce2d 0:972874f31c98 3 u8g_dev_ht1632.c
iforce2d 0:972874f31c98 4
iforce2d 0:972874f31c98 5 1-Bit (BW) Driver for HT1632 controller
iforce2d 0:972874f31c98 6
iforce2d 0:972874f31c98 7 Universal 8bit Graphics Library
iforce2d 0:972874f31c98 8
iforce2d 0:972874f31c98 9 Copyright (c) 2013, olikraus@gmail.com
iforce2d 0:972874f31c98 10 All rights reserved.
iforce2d 0:972874f31c98 11
iforce2d 0:972874f31c98 12 Redistribution and use in source and binary forms, with or without modification,
iforce2d 0:972874f31c98 13 are permitted provided that the following conditions are met:
iforce2d 0:972874f31c98 14
iforce2d 0:972874f31c98 15 * Redistributions of source code must retain the above copyright notice, this list
iforce2d 0:972874f31c98 16 of conditions and the following disclaimer.
iforce2d 0:972874f31c98 17
iforce2d 0:972874f31c98 18 * Redistributions in binary form must reproduce the above copyright notice, this
iforce2d 0:972874f31c98 19 list of conditions and the following disclaimer in the documentation and/or other
iforce2d 0:972874f31c98 20 materials provided with the distribution.
iforce2d 0:972874f31c98 21
iforce2d 0:972874f31c98 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
iforce2d 0:972874f31c98 23 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
iforce2d 0:972874f31c98 24 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
iforce2d 0:972874f31c98 25 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
iforce2d 0:972874f31c98 26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
iforce2d 0:972874f31c98 27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
iforce2d 0:972874f31c98 28 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
iforce2d 0:972874f31c98 29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
iforce2d 0:972874f31c98 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
iforce2d 0:972874f31c98 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
iforce2d 0:972874f31c98 32 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
iforce2d 0:972874f31c98 33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
iforce2d 0:972874f31c98 34 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
iforce2d 0:972874f31c98 35
iforce2d 0:972874f31c98 36
iforce2d 0:972874f31c98 37
iforce2d 0:972874f31c98 38 U8G_PIN_NONE can be used as argument
iforce2d 0:972874f31c98 39
iforce2d 0:972874f31c98 40 uint8_t u8g_InitSPI(u8g_t *u8g, u8g_dev_t *dev, uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset)
iforce2d 0:972874f31c98 41 {
iforce2d 0:972874f31c98 42 ...
iforce2d 0:972874f31c98 43 u8g->pin_list[U8G_PI_SCK] = sck;
iforce2d 0:972874f31c98 44 u8g->pin_list[U8G_PI_MOSI] = mosi;
iforce2d 0:972874f31c98 45 u8g->pin_list[U8G_PI_CS] = cs;
iforce2d 0:972874f31c98 46 u8g->pin_list[U8G_PI_A0] = a0;
iforce2d 0:972874f31c98 47 u8g->pin_list[U8G_PI_RESET] = reset;
iforce2d 0:972874f31c98 48
iforce2d 0:972874f31c98 49 mapping
iforce2d 0:972874f31c98 50
iforce2d 0:972874f31c98 51 #define DATA_PIN --> U8G_PI_MOSI
iforce2d 0:972874f31c98 52 #define WR_PIN --> U8G_PI_SCK
iforce2d 0:972874f31c98 53 #define CS_PIN --> U8G_PI_CS
iforce2d 0:972874f31c98 54 U8G_PI_A0 --> not used
iforce2d 0:972874f31c98 55 U8G_PI_RESET --> not used
iforce2d 0:972874f31c98 56
iforce2d 0:972874f31c98 57 Usage:
iforce2d 0:972874f31c98 58
iforce2d 0:972874f31c98 59 u8g_InitSPI(&u8g, &u8g_dev_ht1632_24x16, WR_PIN, DATA_IN, CS_PIN, U8G_PIN_NONE, U8G_PIN_NONE)
iforce2d 0:972874f31c98 60
iforce2d 0:972874f31c98 61 */
iforce2d 0:972874f31c98 62
iforce2d 0:972874f31c98 63 #include "u8g.h"
iforce2d 0:972874f31c98 64
iforce2d 0:972874f31c98 65 #define WIDTH 24
iforce2d 0:972874f31c98 66 #define HEIGHT 16
iforce2d 0:972874f31c98 67 #define PAGE_HEIGHT 16
iforce2d 0:972874f31c98 68
iforce2d 0:972874f31c98 69 /* http://forum.arduino.cc/index.php?topic=168537.0 */
iforce2d 0:972874f31c98 70
iforce2d 0:972874f31c98 71 #define HT1632_CMD_SYSDIS 0x00 // CMD= 0000-0000-x Turn off oscil
iforce2d 0:972874f31c98 72 #define HT1632_CMD_SYSON 0x01 // CMD= 0000-0001-x Enable system oscil
iforce2d 0:972874f31c98 73 #define HT1632_CMD_LEDOFF 0x02 // CMD= 0000-0010-x LED duty cycle gen off
iforce2d 0:972874f31c98 74 #define HT1632_CMD_LEDON 0x03 // CMD= 0000-0011-x LEDs ON
iforce2d 0:972874f31c98 75 #define HT1632_CMD_BLOFF 0x08 // CMD= 0000-1000-x Blink OFF
iforce2d 0:972874f31c98 76 #define HT1632_CMD_BLON 0x09 // CMD= 0000-1001-x Blink On
iforce2d 0:972874f31c98 77 #define HT1632_CMD_SLVMD 0x10 // CMD= 0001-00xx-x Slave Mode
iforce2d 0:972874f31c98 78 #define HT1632_CMD_MSTMD 0x14 // CMD= 0001-01xx-x Master Mode
iforce2d 0:972874f31c98 79 #define HT1632_CMD_RCCLK 0x18 // CMD= 0001-10xx-x Use on-chip clock
iforce2d 0:972874f31c98 80 #define HT1632_CMD_EXTCLK 0x1C // CMD= 0001-11xx-x Use external clock
iforce2d 0:972874f31c98 81 #define HT1632_CMD_COMS00 0x20 // CMD= 0010-ABxx-x commons options
iforce2d 0:972874f31c98 82 #define HT1632_CMD_COMS01 0x24 // CMD= 0010-ABxx-x commons options
iforce2d 0:972874f31c98 83 #define HT1632_CMD_COMS10 0x28 // CMD= 0010-ABxx-x commons options
iforce2d 0:972874f31c98 84 #define HT1632_CMD_COMS11 0x2C // P-MOS OUTPUT AND 16COMMON OPTION
iforce2d 0:972874f31c98 85 #define HT1632_CMD_PWM 0xA0 // CMD= 101x-PPPP-x PWM duty cycle
iforce2d 0:972874f31c98 86
iforce2d 0:972874f31c98 87 #define HT1632_ID_CMD 4 /* ID = 100 - Commands */
iforce2d 0:972874f31c98 88 #define HT1632_ID_RD 6 /* ID = 110 - Read RAM */
iforce2d 0:972874f31c98 89 #define HT1632_ID_WR 5 /* ID = 101 - Write RAM */
iforce2d 0:972874f31c98 90
iforce2d 0:972874f31c98 91 #define HT1632_ID_LEN 3 // IDs are 3 bits
iforce2d 0:972874f31c98 92 #define HT1632_CMD_LEN 8 // CMDs are 8 bits
iforce2d 0:972874f31c98 93 #define HT1632_DATA_LEN 8 // Data are 4*2 bits
iforce2d 0:972874f31c98 94 #define HT1632_ADDR_LEN 7 // Address are 7 bits
iforce2d 0:972874f31c98 95
iforce2d 0:972874f31c98 96 #if defined(ARDUINO)
iforce2d 0:972874f31c98 97
iforce2d 0:972874f31c98 98 #if ARDUINO < 100
iforce2d 0:972874f31c98 99 #include <WProgram.h>
iforce2d 0:972874f31c98 100 #else
iforce2d 0:972874f31c98 101 #include <Arduino.h>
iforce2d 0:972874f31c98 102 #endif
iforce2d 0:972874f31c98 103
iforce2d 0:972874f31c98 104 //#define WR_PIN 3
iforce2d 0:972874f31c98 105 //#define DATA_PIN 2
iforce2d 0:972874f31c98 106 //#define CS_PIN 4
iforce2d 0:972874f31c98 107
iforce2d 0:972874f31c98 108 void ht1632_write_data_MSB(u8g_t *u8g, uint8_t cnt, uint8_t data, uint8_t extra)
iforce2d 0:972874f31c98 109 {
iforce2d 0:972874f31c98 110 int8_t i;
iforce2d 0:972874f31c98 111 uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
iforce2d 0:972874f31c98 112 uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
iforce2d 0:972874f31c98 113
iforce2d 0:972874f31c98 114 for(i = cnt - 1; i >= 0; i--)
iforce2d 0:972874f31c98 115 {
iforce2d 0:972874f31c98 116 if ((data >> i) & 1)
iforce2d 0:972874f31c98 117 {
iforce2d 0:972874f31c98 118 digitalWrite(data_pin, HIGH);
iforce2d 0:972874f31c98 119 }
iforce2d 0:972874f31c98 120 else
iforce2d 0:972874f31c98 121 {
iforce2d 0:972874f31c98 122 digitalWrite(data_pin, LOW);
iforce2d 0:972874f31c98 123 }
iforce2d 0:972874f31c98 124
iforce2d 0:972874f31c98 125 digitalWrite(wr_pin, LOW);
iforce2d 0:972874f31c98 126 u8g_MicroDelay();
iforce2d 0:972874f31c98 127 digitalWrite(wr_pin, HIGH);
iforce2d 0:972874f31c98 128 u8g_MicroDelay();
iforce2d 0:972874f31c98 129 }
iforce2d 0:972874f31c98 130
iforce2d 0:972874f31c98 131 // Send an extra bit
iforce2d 0:972874f31c98 132 if (extra)
iforce2d 0:972874f31c98 133 {
iforce2d 0:972874f31c98 134 digitalWrite(data_pin, HIGH);
iforce2d 0:972874f31c98 135 digitalWrite(wr_pin, LOW);
iforce2d 0:972874f31c98 136 u8g_MicroDelay();
iforce2d 0:972874f31c98 137 digitalWrite(wr_pin, HIGH);
iforce2d 0:972874f31c98 138 u8g_MicroDelay();
iforce2d 0:972874f31c98 139 }
iforce2d 0:972874f31c98 140 }
iforce2d 0:972874f31c98 141
iforce2d 0:972874f31c98 142 void ht1632_write_data(u8g_t *u8g, uint8_t cnt, uint8_t data)
iforce2d 0:972874f31c98 143 {
iforce2d 0:972874f31c98 144 uint8_t i;
iforce2d 0:972874f31c98 145 uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
iforce2d 0:972874f31c98 146 uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
iforce2d 0:972874f31c98 147 for (i = 0; i < cnt; i++)
iforce2d 0:972874f31c98 148 {
iforce2d 0:972874f31c98 149
iforce2d 0:972874f31c98 150 if ((data >> i) & 1) {
iforce2d 0:972874f31c98 151 digitalWrite(data_pin, HIGH);
iforce2d 0:972874f31c98 152 }
iforce2d 0:972874f31c98 153 else {
iforce2d 0:972874f31c98 154 digitalWrite(data_pin, LOW);
iforce2d 0:972874f31c98 155 }
iforce2d 0:972874f31c98 156
iforce2d 0:972874f31c98 157 digitalWrite(wr_pin, LOW);
iforce2d 0:972874f31c98 158 u8g_MicroDelay();
iforce2d 0:972874f31c98 159 digitalWrite(wr_pin, HIGH);
iforce2d 0:972874f31c98 160 u8g_MicroDelay();
iforce2d 0:972874f31c98 161 }
iforce2d 0:972874f31c98 162 }
iforce2d 0:972874f31c98 163
iforce2d 0:972874f31c98 164
iforce2d 0:972874f31c98 165 void ht1632_init(u8g_t *u8g)
iforce2d 0:972874f31c98 166 {
iforce2d 0:972874f31c98 167 //uint8_t i;
iforce2d 0:972874f31c98 168 uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI];
iforce2d 0:972874f31c98 169 uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK];
iforce2d 0:972874f31c98 170 uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
iforce2d 0:972874f31c98 171 pinMode(data_pin, OUTPUT);
iforce2d 0:972874f31c98 172 pinMode(wr_pin, OUTPUT);
iforce2d 0:972874f31c98 173 pinMode(cs_pin, OUTPUT);
iforce2d 0:972874f31c98 174
iforce2d 0:972874f31c98 175 digitalWrite(data_pin, HIGH);
iforce2d 0:972874f31c98 176 digitalWrite(wr_pin, HIGH);
iforce2d 0:972874f31c98 177 digitalWrite(cs_pin, HIGH);
iforce2d 0:972874f31c98 178
iforce2d 0:972874f31c98 179 digitalWrite(cs_pin, LOW);
iforce2d 0:972874f31c98 180 /* init display once after startup */
iforce2d 0:972874f31c98 181 ht1632_write_data_MSB(u8g, 3, HT1632_ID_CMD, false); // IDs are 3 bits
iforce2d 0:972874f31c98 182 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_SYSDIS, true); // 8 bits
iforce2d 0:972874f31c98 183 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_SYSON, true); // 8 bits
iforce2d 0:972874f31c98 184 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_COMS11, true); // 8 bits
iforce2d 0:972874f31c98 185 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_LEDON, true); // 8 bits
iforce2d 0:972874f31c98 186 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_BLOFF, true); // 8 bits
iforce2d 0:972874f31c98 187 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_PWM+15, true); // 8 bits
iforce2d 0:972874f31c98 188 digitalWrite(cs_pin, HIGH);
iforce2d 0:972874f31c98 189
iforce2d 0:972874f31c98 190 /* removed following (debug) code */
iforce2d 0:972874f31c98 191 /*
iforce2d 0:972874f31c98 192 digitalWrite(cs_pin, LOW);
iforce2d 0:972874f31c98 193 ht1632_write_data_MSB(u8g, 3, HT1632_ID_WR, false); // Send "write to display" command
iforce2d 0:972874f31c98 194 ht1632_write_data_MSB(u8g, 7, 0, false);
iforce2d 0:972874f31c98 195 for(i = 0; i<48; ++i)
iforce2d 0:972874f31c98 196 {
iforce2d 0:972874f31c98 197 ht1632_write_data(u8g, 8, 0xFF);
iforce2d 0:972874f31c98 198 }
iforce2d 0:972874f31c98 199 digitalWrite(cs_pin, HIGH);
iforce2d 0:972874f31c98 200 */
iforce2d 0:972874f31c98 201 }
iforce2d 0:972874f31c98 202
iforce2d 0:972874f31c98 203 /*
iforce2d 0:972874f31c98 204 page: 0=data contain lines 0..16, 1=data contain lines 16..32 (a 24x16 display will only have page 0)
iforce2d 0:972874f31c98 205 cnt: width of the display
iforce2d 0:972874f31c98 206 data: pointer to a buffer with 2*cnt bytes.
iforce2d 0:972874f31c98 207 */
iforce2d 0:972874f31c98 208 void ht1632_transfer_data(u8g_t *u8g, uint8_t page, uint8_t cnt, uint8_t *data)
iforce2d 0:972874f31c98 209 {
iforce2d 0:972874f31c98 210 uint8_t addr;
iforce2d 0:972874f31c98 211 uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
iforce2d 0:972874f31c98 212 /* send data to the ht1632 */
iforce2d 0:972874f31c98 213 digitalWrite(cs_pin, LOW);
iforce2d 0:972874f31c98 214 ht1632_write_data_MSB(u8g, 3, HT1632_ID_WR, false); // Send "write to display" command
iforce2d 0:972874f31c98 215 ht1632_write_data_MSB(u8g, 7, page*2*cnt, false);
iforce2d 0:972874f31c98 216
iforce2d 0:972874f31c98 217 // Operating in progressive addressing mode
iforce2d 0:972874f31c98 218 for (addr = 0; addr < cnt; addr++)
iforce2d 0:972874f31c98 219 {
iforce2d 0:972874f31c98 220 ht1632_write_data(u8g, 8, data[addr]);
iforce2d 0:972874f31c98 221 ht1632_write_data(u8g, 8, data[addr+cnt]);
iforce2d 0:972874f31c98 222 }
iforce2d 0:972874f31c98 223 digitalWrite(cs_pin, HIGH);
iforce2d 0:972874f31c98 224 }
iforce2d 0:972874f31c98 225
iforce2d 0:972874f31c98 226 /* value is between 0...15 */
iforce2d 0:972874f31c98 227 void ht1632_set_contrast(u8g_t *u8g, uint8_t value)
iforce2d 0:972874f31c98 228 {
iforce2d 0:972874f31c98 229 uint8_t cs_pin = u8g->pin_list[U8G_PI_CS];
iforce2d 0:972874f31c98 230 digitalWrite(cs_pin, LOW);
iforce2d 0:972874f31c98 231 ht1632_write_data_MSB(u8g, 3, HT1632_ID_CMD, false);
iforce2d 0:972874f31c98 232 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_PWM + value, false);
iforce2d 0:972874f31c98 233 digitalWrite(cs_pin, HIGH);
iforce2d 0:972874f31c98 234 }
iforce2d 0:972874f31c98 235
iforce2d 0:972874f31c98 236 #else
iforce2d 0:972874f31c98 237 void ht1632_init(u8g_t *u8g)
iforce2d 0:972874f31c98 238 {
iforce2d 0:972874f31c98 239 }
iforce2d 0:972874f31c98 240
iforce2d 0:972874f31c98 241 void ht1632_transfer_data(u8g_t *u8g, uint8_t page, uint8_t cnt, uint8_t *data)
iforce2d 0:972874f31c98 242 {
iforce2d 0:972874f31c98 243 }
iforce2d 0:972874f31c98 244
iforce2d 0:972874f31c98 245 void ht1632_set_contrast(u8g_t *u8g, uint8_t value)
iforce2d 0:972874f31c98 246 {
iforce2d 0:972874f31c98 247 }
iforce2d 0:972874f31c98 248
iforce2d 0:972874f31c98 249 #endif /* ARDUINO */
iforce2d 0:972874f31c98 250
iforce2d 0:972874f31c98 251
iforce2d 0:972874f31c98 252 uint8_t u8g_dev_ht1632_24x16_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
iforce2d 0:972874f31c98 253 {
iforce2d 0:972874f31c98 254 switch(msg)
iforce2d 0:972874f31c98 255 {
iforce2d 0:972874f31c98 256 case U8G_DEV_MSG_INIT:
iforce2d 0:972874f31c98 257 ht1632_init(u8g);
iforce2d 0:972874f31c98 258 break;
iforce2d 0:972874f31c98 259 case U8G_DEV_MSG_STOP:
iforce2d 0:972874f31c98 260 break;
iforce2d 0:972874f31c98 261 case U8G_DEV_MSG_PAGE_NEXT:
iforce2d 0:972874f31c98 262 {
iforce2d 0:972874f31c98 263 u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
iforce2d 0:972874f31c98 264
iforce2d 0:972874f31c98 265 /* current page: pb->p.page */
iforce2d 0:972874f31c98 266 /* ptr to the buffer: pb->buf */
iforce2d 0:972874f31c98 267 ht1632_transfer_data(u8g, pb->p.page, WIDTH, pb->buf);
iforce2d 0:972874f31c98 268 }
iforce2d 0:972874f31c98 269 break;
iforce2d 0:972874f31c98 270 case U8G_DEV_MSG_CONTRAST:
iforce2d 0:972874f31c98 271 /* values passed to SetContrast() are between 0 and 255, scale down to 0...15 */
iforce2d 0:972874f31c98 272 ht1632_set_contrast(u8g, (*(uint8_t *)arg) >> 4);
iforce2d 0:972874f31c98 273 return 1;
iforce2d 0:972874f31c98 274 }
iforce2d 0:972874f31c98 275 return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
iforce2d 0:972874f31c98 276 }
iforce2d 0:972874f31c98 277
iforce2d 0:972874f31c98 278 uint8_t u8g_dev_ht1632_24x16_buf[WIDTH*2] U8G_NOCOMMON ;
iforce2d 0:972874f31c98 279 u8g_pb_t u8g_dev_ht1632_24x16_pb = { {16, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_ht1632_24x16_buf};
iforce2d 0:972874f31c98 280 u8g_dev_t u8g_dev_ht1632_24x16 = { u8g_dev_ht1632_24x16_fn, &u8g_dev_ht1632_24x16_pb, u8g_com_null_fn };
iforce2d 0:972874f31c98 281
iforce2d 0:972874f31c98 282