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_arduino_sw_spi.c
iforce2d 0:972874f31c98 4
iforce2d 0:972874f31c98 5 Universal 8bit Graphics Library
iforce2d 0:972874f31c98 6
iforce2d 0:972874f31c98 7 Copyright (c) 2011, olikraus@gmail.com
iforce2d 0:972874f31c98 8 All rights reserved.
iforce2d 0:972874f31c98 9
iforce2d 0:972874f31c98 10 Redistribution and use in source and binary forms, with or without modification,
iforce2d 0:972874f31c98 11 are permitted provided that the following conditions are met:
iforce2d 0:972874f31c98 12
iforce2d 0:972874f31c98 13 * Redistributions of source code must retain the above copyright notice, this list
iforce2d 0:972874f31c98 14 of conditions and the following disclaimer.
iforce2d 0:972874f31c98 15
iforce2d 0:972874f31c98 16 * Redistributions in binary form must reproduce the above copyright notice, this
iforce2d 0:972874f31c98 17 list of conditions and the following disclaimer in the documentation and/or other
iforce2d 0:972874f31c98 18 materials provided with the distribution.
iforce2d 0:972874f31c98 19
iforce2d 0:972874f31c98 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
iforce2d 0:972874f31c98 21 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
iforce2d 0:972874f31c98 22 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
iforce2d 0:972874f31c98 23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
iforce2d 0:972874f31c98 24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
iforce2d 0:972874f31c98 25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
iforce2d 0:972874f31c98 26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
iforce2d 0:972874f31c98 27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
iforce2d 0:972874f31c98 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
iforce2d 0:972874f31c98 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
iforce2d 0:972874f31c98 30 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
iforce2d 0:972874f31c98 31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
iforce2d 0:972874f31c98 32 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
iforce2d 0:972874f31c98 33
iforce2d 0:972874f31c98 34 Update for ATOMIC operation done (01 Jun 2013)
iforce2d 0:972874f31c98 35 U8G_ATOMIC_OR(ptr, val)
iforce2d 0:972874f31c98 36 U8G_ATOMIC_AND(ptr, val)
iforce2d 0:972874f31c98 37 U8G_ATOMIC_START();
iforce2d 0:972874f31c98 38 U8G_ATOMIC_END();
iforce2d 0:972874f31c98 39
iforce2d 0:972874f31c98 40
iforce2d 0:972874f31c98 41 */
iforce2d 0:972874f31c98 42
iforce2d 0:972874f31c98 43 #include "u8g.h"
iforce2d 0:972874f31c98 44
iforce2d 0:972874f31c98 45 #if defined(ARDUINO)
iforce2d 0:972874f31c98 46
iforce2d 0:972874f31c98 47 #if ARDUINO < 100
iforce2d 0:972874f31c98 48 #include <WProgram.h>
iforce2d 0:972874f31c98 49 #include "wiring_private.h"
iforce2d 0:972874f31c98 50 #include "pins_arduino.h"
iforce2d 0:972874f31c98 51
iforce2d 0:972874f31c98 52 #else
iforce2d 0:972874f31c98 53 #include <Arduino.h>
iforce2d 0:972874f31c98 54 #include "wiring_private.h"
iforce2d 0:972874f31c98 55 #endif
iforce2d 0:972874f31c98 56
iforce2d 0:972874f31c98 57 /*=========================================================*/
iforce2d 0:972874f31c98 58 /* Arduino, AVR */
iforce2d 0:972874f31c98 59
iforce2d 0:972874f31c98 60 #if defined(__AVR__)
iforce2d 0:972874f31c98 61
iforce2d 0:972874f31c98 62 uint8_t u8g_bitData, u8g_bitNotData;
iforce2d 0:972874f31c98 63 uint8_t u8g_bitClock, u8g_bitNotClock;
iforce2d 0:972874f31c98 64 volatile uint8_t *u8g_outData;
iforce2d 0:972874f31c98 65 volatile uint8_t *u8g_outClock;
iforce2d 0:972874f31c98 66
iforce2d 0:972874f31c98 67 static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
iforce2d 0:972874f31c98 68 {
iforce2d 0:972874f31c98 69 u8g_outData = portOutputRegister(digitalPinToPort(dataPin));
iforce2d 0:972874f31c98 70 u8g_outClock = portOutputRegister(digitalPinToPort(clockPin));
iforce2d 0:972874f31c98 71 u8g_bitData = digitalPinToBitMask(dataPin);
iforce2d 0:972874f31c98 72 u8g_bitClock = digitalPinToBitMask(clockPin);
iforce2d 0:972874f31c98 73
iforce2d 0:972874f31c98 74 u8g_bitNotClock = u8g_bitClock;
iforce2d 0:972874f31c98 75 u8g_bitNotClock ^= 0x0ff;
iforce2d 0:972874f31c98 76
iforce2d 0:972874f31c98 77 u8g_bitNotData = u8g_bitData;
iforce2d 0:972874f31c98 78 u8g_bitNotData ^= 0x0ff;
iforce2d 0:972874f31c98 79 }
iforce2d 0:972874f31c98 80
iforce2d 0:972874f31c98 81 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val) U8G_NOINLINE;
iforce2d 0:972874f31c98 82 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
iforce2d 0:972874f31c98 83 {
iforce2d 0:972874f31c98 84 uint8_t cnt = 8;
iforce2d 0:972874f31c98 85 uint8_t bitData = u8g_bitData;
iforce2d 0:972874f31c98 86 uint8_t bitNotData = u8g_bitNotData;
iforce2d 0:972874f31c98 87 uint8_t bitClock = u8g_bitClock;
iforce2d 0:972874f31c98 88 uint8_t bitNotClock = u8g_bitNotClock;
iforce2d 0:972874f31c98 89 volatile uint8_t *outData = u8g_outData;
iforce2d 0:972874f31c98 90 volatile uint8_t *outClock = u8g_outClock;
iforce2d 0:972874f31c98 91 U8G_ATOMIC_START();
iforce2d 0:972874f31c98 92 do
iforce2d 0:972874f31c98 93 {
iforce2d 0:972874f31c98 94 if ( val & 128 )
iforce2d 0:972874f31c98 95 *outData |= bitData;
iforce2d 0:972874f31c98 96 else
iforce2d 0:972874f31c98 97 *outData &= bitNotData;
iforce2d 0:972874f31c98 98
iforce2d 0:972874f31c98 99 *outClock |= bitClock;
iforce2d 0:972874f31c98 100 val <<= 1;
iforce2d 0:972874f31c98 101 cnt--;
iforce2d 0:972874f31c98 102 *outClock &= bitNotClock;
iforce2d 0:972874f31c98 103 } while( cnt != 0 );
iforce2d 0:972874f31c98 104 U8G_ATOMIC_END();
iforce2d 0:972874f31c98 105 }
iforce2d 0:972874f31c98 106
iforce2d 0:972874f31c98 107 /*=========================================================*/
iforce2d 0:972874f31c98 108 /* Arduino, Chipkit */
iforce2d 0:972874f31c98 109 #elif defined(__18CXX) || defined(__PIC32MX)
iforce2d 0:972874f31c98 110
iforce2d 0:972874f31c98 111 uint16_t dog_bitData, dog_bitNotData;
iforce2d 0:972874f31c98 112 uint16_t dog_bitClock, dog_bitNotClock;
iforce2d 0:972874f31c98 113 volatile uint32_t *dog_outData;
iforce2d 0:972874f31c98 114 volatile uint32_t *dog_outClock;
iforce2d 0:972874f31c98 115 volatile uint32_t dog_pic32_spi_tmp;
iforce2d 0:972874f31c98 116
iforce2d 0:972874f31c98 117 static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
iforce2d 0:972874f31c98 118 {
iforce2d 0:972874f31c98 119 dog_outData = portOutputRegister(digitalPinToPort(dataPin));
iforce2d 0:972874f31c98 120 dog_outClock = portOutputRegister(digitalPinToPort(clockPin));
iforce2d 0:972874f31c98 121 dog_bitData = digitalPinToBitMask(dataPin);
iforce2d 0:972874f31c98 122 dog_bitClock = digitalPinToBitMask(clockPin);
iforce2d 0:972874f31c98 123
iforce2d 0:972874f31c98 124 dog_bitNotClock = dog_bitClock;
iforce2d 0:972874f31c98 125 dog_bitNotClock ^= 0x0ffff;
iforce2d 0:972874f31c98 126
iforce2d 0:972874f31c98 127 dog_bitNotData = dog_bitData;
iforce2d 0:972874f31c98 128 dog_bitNotData ^= 0x0ffff;
iforce2d 0:972874f31c98 129 }
iforce2d 0:972874f31c98 130
iforce2d 0:972874f31c98 131 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
iforce2d 0:972874f31c98 132 {
iforce2d 0:972874f31c98 133 uint8_t cnt = 8;
iforce2d 0:972874f31c98 134 U8G_ATOMIC_START();
iforce2d 0:972874f31c98 135 do
iforce2d 0:972874f31c98 136 {
iforce2d 0:972874f31c98 137 if ( val & 128 )
iforce2d 0:972874f31c98 138 *dog_outData |= dog_bitData;
iforce2d 0:972874f31c98 139 else
iforce2d 0:972874f31c98 140 *dog_outData &= dog_bitNotData;
iforce2d 0:972874f31c98 141 val <<= 1;
iforce2d 0:972874f31c98 142 /*
iforce2d 0:972874f31c98 143 There must be some delay here. However
iforce2d 0:972874f31c98 144 fetching the adress dog_outClock is enough delay, so
iforce2d 0:972874f31c98 145 do not place dog_outClock in a local variable. This will
iforce2d 0:972874f31c98 146 break the procedure
iforce2d 0:972874f31c98 147 */
iforce2d 0:972874f31c98 148 *dog_outClock |= dog_bitClock;
iforce2d 0:972874f31c98 149 cnt--;
iforce2d 0:972874f31c98 150 *dog_outClock &= dog_bitNotClock;
iforce2d 0:972874f31c98 151 /*
iforce2d 0:972874f31c98 152 little additional delay after clk pulse, done by 3x32bit reads
iforce2d 0:972874f31c98 153 from I/O. Optimized for PIC32 with 80 MHz.
iforce2d 0:972874f31c98 154 */
iforce2d 0:972874f31c98 155 dog_pic32_spi_tmp = *dog_outClock;
iforce2d 0:972874f31c98 156 dog_pic32_spi_tmp = *dog_outClock;
iforce2d 0:972874f31c98 157 dog_pic32_spi_tmp = *dog_outClock;
iforce2d 0:972874f31c98 158 } while( cnt != 0 );
iforce2d 0:972874f31c98 159 U8G_ATOMIC_END();
iforce2d 0:972874f31c98 160 }
iforce2d 0:972874f31c98 161
iforce2d 0:972874f31c98 162 /*=========================================================*/
iforce2d 0:972874f31c98 163 /* Arduino Due */
iforce2d 0:972874f31c98 164 #elif defined(__arm__)
iforce2d 0:972874f31c98 165
iforce2d 0:972874f31c98 166 /* Due */
iforce2d 0:972874f31c98 167
iforce2d 0:972874f31c98 168 void u8g_digital_write_sam_high(uint8_t pin)
iforce2d 0:972874f31c98 169 {
iforce2d 0:972874f31c98 170 PIO_Set( g_APinDescription[pin].pPort, g_APinDescription[pin].ulPin) ;
iforce2d 0:972874f31c98 171 }
iforce2d 0:972874f31c98 172
iforce2d 0:972874f31c98 173 void u8g_digital_write_sam_low(uint8_t pin)
iforce2d 0:972874f31c98 174 {
iforce2d 0:972874f31c98 175 PIO_Clear( g_APinDescription[pin].pPort, g_APinDescription[pin].ulPin) ;
iforce2d 0:972874f31c98 176 }
iforce2d 0:972874f31c98 177
iforce2d 0:972874f31c98 178 static uint8_t u8g_sam_data_pin;
iforce2d 0:972874f31c98 179 static uint8_t u8g_sam_clock_pin;
iforce2d 0:972874f31c98 180
iforce2d 0:972874f31c98 181 static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
iforce2d 0:972874f31c98 182 {
iforce2d 0:972874f31c98 183 u8g_sam_data_pin = dataPin;
iforce2d 0:972874f31c98 184 u8g_sam_clock_pin = clockPin;
iforce2d 0:972874f31c98 185 }
iforce2d 0:972874f31c98 186
iforce2d 0:972874f31c98 187 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
iforce2d 0:972874f31c98 188 {
iforce2d 0:972874f31c98 189 uint8_t i = 8;
iforce2d 0:972874f31c98 190 do
iforce2d 0:972874f31c98 191 {
iforce2d 0:972874f31c98 192 if ( val & 128 )
iforce2d 0:972874f31c98 193 u8g_digital_write_sam_high(u8g_sam_data_pin);
iforce2d 0:972874f31c98 194 else
iforce2d 0:972874f31c98 195 u8g_digital_write_sam_low(u8g_sam_data_pin);
iforce2d 0:972874f31c98 196 val <<= 1;
iforce2d 0:972874f31c98 197 //u8g_MicroDelay();
iforce2d 0:972874f31c98 198 u8g_digital_write_sam_high(u8g_sam_clock_pin);
iforce2d 0:972874f31c98 199 u8g_MicroDelay();
iforce2d 0:972874f31c98 200 u8g_digital_write_sam_low(u8g_sam_clock_pin);
iforce2d 0:972874f31c98 201 u8g_MicroDelay();
iforce2d 0:972874f31c98 202 i--;
iforce2d 0:972874f31c98 203 } while( i != 0 );
iforce2d 0:972874f31c98 204 }
iforce2d 0:972874f31c98 205
iforce2d 0:972874f31c98 206
iforce2d 0:972874f31c98 207 #else
iforce2d 0:972874f31c98 208 /* empty interface */
iforce2d 0:972874f31c98 209
iforce2d 0:972874f31c98 210 static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
iforce2d 0:972874f31c98 211 {
iforce2d 0:972874f31c98 212 }
iforce2d 0:972874f31c98 213
iforce2d 0:972874f31c98 214 static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
iforce2d 0:972874f31c98 215 {
iforce2d 0:972874f31c98 216 }
iforce2d 0:972874f31c98 217
iforce2d 0:972874f31c98 218 #endif
iforce2d 0:972874f31c98 219
iforce2d 0:972874f31c98 220
iforce2d 0:972874f31c98 221 uint8_t u8g_com_arduino_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
iforce2d 0:972874f31c98 222 {
iforce2d 0:972874f31c98 223 switch(msg)
iforce2d 0:972874f31c98 224 {
iforce2d 0:972874f31c98 225 case U8G_COM_MSG_INIT:
iforce2d 0:972874f31c98 226 u8g_com_arduino_assign_pin_output_high(u8g);
iforce2d 0:972874f31c98 227 u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
iforce2d 0:972874f31c98 228 u8g_com_arduino_digital_write(u8g, U8G_PI_MOSI, LOW);
iforce2d 0:972874f31c98 229 u8g_com_arduino_init_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK]);
iforce2d 0:972874f31c98 230 break;
iforce2d 0:972874f31c98 231
iforce2d 0:972874f31c98 232 case U8G_COM_MSG_STOP:
iforce2d 0:972874f31c98 233 break;
iforce2d 0:972874f31c98 234
iforce2d 0:972874f31c98 235 case U8G_COM_MSG_RESET:
iforce2d 0:972874f31c98 236 if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
iforce2d 0:972874f31c98 237 u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
iforce2d 0:972874f31c98 238 break;
iforce2d 0:972874f31c98 239
iforce2d 0:972874f31c98 240 case U8G_COM_MSG_CHIP_SELECT:
iforce2d 0:972874f31c98 241 if ( arg_val == 0 )
iforce2d 0:972874f31c98 242 {
iforce2d 0:972874f31c98 243 /* disable */
iforce2d 0:972874f31c98 244 u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
iforce2d 0:972874f31c98 245 }
iforce2d 0:972874f31c98 246 else
iforce2d 0:972874f31c98 247 {
iforce2d 0:972874f31c98 248 /* enable */
iforce2d 0:972874f31c98 249 u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
iforce2d 0:972874f31c98 250 u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
iforce2d 0:972874f31c98 251 }
iforce2d 0:972874f31c98 252 break;
iforce2d 0:972874f31c98 253
iforce2d 0:972874f31c98 254 case U8G_COM_MSG_WRITE_BYTE:
iforce2d 0:972874f31c98 255 u8g_com_arduino_do_shift_out_msb_first( arg_val );
iforce2d 0:972874f31c98 256 //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val);
iforce2d 0:972874f31c98 257 break;
iforce2d 0:972874f31c98 258
iforce2d 0:972874f31c98 259 case U8G_COM_MSG_WRITE_SEQ:
iforce2d 0:972874f31c98 260 {
iforce2d 0:972874f31c98 261 register uint8_t *ptr = arg_ptr;
iforce2d 0:972874f31c98 262 while( arg_val > 0 )
iforce2d 0:972874f31c98 263 {
iforce2d 0:972874f31c98 264 u8g_com_arduino_do_shift_out_msb_first(*ptr++);
iforce2d 0:972874f31c98 265 // u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], *ptr++);
iforce2d 0:972874f31c98 266 arg_val--;
iforce2d 0:972874f31c98 267 }
iforce2d 0:972874f31c98 268 }
iforce2d 0:972874f31c98 269 break;
iforce2d 0:972874f31c98 270
iforce2d 0:972874f31c98 271 case U8G_COM_MSG_WRITE_SEQ_P:
iforce2d 0:972874f31c98 272 {
iforce2d 0:972874f31c98 273 register uint8_t *ptr = arg_ptr;
iforce2d 0:972874f31c98 274 while( arg_val > 0 )
iforce2d 0:972874f31c98 275 {
iforce2d 0:972874f31c98 276 u8g_com_arduino_do_shift_out_msb_first( u8g_pgm_read(ptr) );
iforce2d 0:972874f31c98 277 //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], u8g_pgm_read(ptr));
iforce2d 0:972874f31c98 278 ptr++;
iforce2d 0:972874f31c98 279 arg_val--;
iforce2d 0:972874f31c98 280 }
iforce2d 0:972874f31c98 281 }
iforce2d 0:972874f31c98 282 break;
iforce2d 0:972874f31c98 283
iforce2d 0:972874f31c98 284 case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
iforce2d 0:972874f31c98 285 u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
iforce2d 0:972874f31c98 286 break;
iforce2d 0:972874f31c98 287 }
iforce2d 0:972874f31c98 288 return 1;
iforce2d 0:972874f31c98 289 }
iforce2d 0:972874f31c98 290
iforce2d 0:972874f31c98 291 #else /* ARDUINO */
iforce2d 0:972874f31c98 292
iforce2d 0:972874f31c98 293 uint8_t u8g_com_arduino_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
iforce2d 0:972874f31c98 294 {
iforce2d 0:972874f31c98 295 return 1;
iforce2d 0:972874f31c98 296 }
iforce2d 0:972874f31c98 297
iforce2d 0:972874f31c98 298 #endif /* ARDUINO */
iforce2d 0:972874f31c98 299
iforce2d 0:972874f31c98 300