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_com_io.c
iforce2d 0:972874f31c98 4
iforce2d 0:972874f31c98 5 abstraction layer for low level i/o
iforce2d 0:972874f31c98 6
iforce2d 0:972874f31c98 7 Universal 8bit Graphics Library
iforce2d 0:972874f31c98 8
iforce2d 0:972874f31c98 9 Copyright (c) 2012, 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 Update for ATOMIC operation done (01 Jun 2013)
iforce2d 0:972874f31c98 37 U8G_ATOMIC_OR(ptr, val)
iforce2d 0:972874f31c98 38 U8G_ATOMIC_AND(ptr, val)
iforce2d 0:972874f31c98 39 U8G_ATOMIC_START();
iforce2d 0:972874f31c98 40 U8G_ATOMIC_END();
iforce2d 0:972874f31c98 41
iforce2d 0:972874f31c98 42 uint8_t u8g_Pin(uint8_t port, uint8_t bitpos) Convert to internal number: AVR: port*8+bitpos, ARM: port*16+bitpos
iforce2d 0:972874f31c98 43 void u8g_SetPinOutput(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 44 void u8g_SetPinInput(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 45 void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
iforce2d 0:972874f31c98 46 uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 47
iforce2d 0:972874f31c98 48
iforce2d 0:972874f31c98 49 */
iforce2d 0:972874f31c98 50
iforce2d 0:972874f31c98 51 #include "u8g.h"
iforce2d 0:972874f31c98 52
iforce2d 0:972874f31c98 53 #if defined(__AVR__)
iforce2d 0:972874f31c98 54
iforce2d 0:972874f31c98 55 #include <avr/interrupt.h>
iforce2d 0:972874f31c98 56 #include <avr/io.h>
iforce2d 0:972874f31c98 57
iforce2d 0:972874f31c98 58 typedef volatile uint8_t * IO_PTR;
iforce2d 0:972874f31c98 59
iforce2d 0:972874f31c98 60 /* create internal pin number */
iforce2d 0:972874f31c98 61 uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)
iforce2d 0:972874f31c98 62 {
iforce2d 0:972874f31c98 63 port <<= 3;
iforce2d 0:972874f31c98 64 port += bitpos;
iforce2d 0:972874f31c98 65 return port;
iforce2d 0:972874f31c98 66 }
iforce2d 0:972874f31c98 67
iforce2d 0:972874f31c98 68 const IO_PTR u8g_avr_ddr_P[] PROGMEM = {
iforce2d 0:972874f31c98 69 #ifdef DDRA
iforce2d 0:972874f31c98 70 &DDRA,
iforce2d 0:972874f31c98 71 #else
iforce2d 0:972874f31c98 72 0,
iforce2d 0:972874f31c98 73 #endif
iforce2d 0:972874f31c98 74 &DDRB,
iforce2d 0:972874f31c98 75 #ifdef DDRC
iforce2d 0:972874f31c98 76 &DDRC,
iforce2d 0:972874f31c98 77 #ifdef DDRD
iforce2d 0:972874f31c98 78 &DDRD,
iforce2d 0:972874f31c98 79 #ifdef DDRE
iforce2d 0:972874f31c98 80 &DDRE,
iforce2d 0:972874f31c98 81 #ifdef DDRF
iforce2d 0:972874f31c98 82 &DDRF,
iforce2d 0:972874f31c98 83 #ifdef DDRG
iforce2d 0:972874f31c98 84 &DDRG,
iforce2d 0:972874f31c98 85 #ifdef DDRH
iforce2d 0:972874f31c98 86 &DDRH,
iforce2d 0:972874f31c98 87 #endif
iforce2d 0:972874f31c98 88 #endif
iforce2d 0:972874f31c98 89 #endif
iforce2d 0:972874f31c98 90 #endif
iforce2d 0:972874f31c98 91 #endif
iforce2d 0:972874f31c98 92 #endif
iforce2d 0:972874f31c98 93 };
iforce2d 0:972874f31c98 94
iforce2d 0:972874f31c98 95
iforce2d 0:972874f31c98 96 const IO_PTR u8g_avr_port_P[] PROGMEM = {
iforce2d 0:972874f31c98 97 #ifdef PORTA
iforce2d 0:972874f31c98 98 &PORTA,
iforce2d 0:972874f31c98 99 #else
iforce2d 0:972874f31c98 100 0,
iforce2d 0:972874f31c98 101 #endif
iforce2d 0:972874f31c98 102 &PORTB,
iforce2d 0:972874f31c98 103 #ifdef PORTC
iforce2d 0:972874f31c98 104 &PORTC,
iforce2d 0:972874f31c98 105 #ifdef PORTD
iforce2d 0:972874f31c98 106 &PORTD,
iforce2d 0:972874f31c98 107 #ifdef PORTE
iforce2d 0:972874f31c98 108 &PORTE,
iforce2d 0:972874f31c98 109 #ifdef PORTF
iforce2d 0:972874f31c98 110 &PORTF,
iforce2d 0:972874f31c98 111 #ifdef PORTG
iforce2d 0:972874f31c98 112 &PORTG,
iforce2d 0:972874f31c98 113 #ifdef PORTH
iforce2d 0:972874f31c98 114 &PORTH,
iforce2d 0:972874f31c98 115 #endif
iforce2d 0:972874f31c98 116 #endif
iforce2d 0:972874f31c98 117 #endif
iforce2d 0:972874f31c98 118 #endif
iforce2d 0:972874f31c98 119 #endif
iforce2d 0:972874f31c98 120 #endif
iforce2d 0:972874f31c98 121 };
iforce2d 0:972874f31c98 122
iforce2d 0:972874f31c98 123 const IO_PTR u8g_avr_pin_P[] PROGMEM = {
iforce2d 0:972874f31c98 124 #ifdef PINA
iforce2d 0:972874f31c98 125 &PINA,
iforce2d 0:972874f31c98 126 #else
iforce2d 0:972874f31c98 127 0,
iforce2d 0:972874f31c98 128 #endif
iforce2d 0:972874f31c98 129 &PINB,
iforce2d 0:972874f31c98 130 #ifdef PINC
iforce2d 0:972874f31c98 131 &PINC,
iforce2d 0:972874f31c98 132 #ifdef PIND
iforce2d 0:972874f31c98 133 &PIND,
iforce2d 0:972874f31c98 134 #ifdef PINE
iforce2d 0:972874f31c98 135 &PINE,
iforce2d 0:972874f31c98 136 #ifdef PINF
iforce2d 0:972874f31c98 137 &PINF,
iforce2d 0:972874f31c98 138 #ifdef PING
iforce2d 0:972874f31c98 139 &PING,
iforce2d 0:972874f31c98 140 #ifdef PINH
iforce2d 0:972874f31c98 141 &PINH,
iforce2d 0:972874f31c98 142 #endif
iforce2d 0:972874f31c98 143 #endif
iforce2d 0:972874f31c98 144 #endif
iforce2d 0:972874f31c98 145 #endif
iforce2d 0:972874f31c98 146 #endif
iforce2d 0:972874f31c98 147 #endif
iforce2d 0:972874f31c98 148 };
iforce2d 0:972874f31c98 149
iforce2d 0:972874f31c98 150 static volatile uint8_t *u8g_get_avr_io_ptr(const IO_PTR *base, uint8_t offset)
iforce2d 0:972874f31c98 151 {
iforce2d 0:972874f31c98 152 volatile uint8_t * tmp;
iforce2d 0:972874f31c98 153 base += offset;
iforce2d 0:972874f31c98 154 memcpy_P(&tmp, base, sizeof(volatile uint8_t * PROGMEM));
iforce2d 0:972874f31c98 155 return tmp;
iforce2d 0:972874f31c98 156 }
iforce2d 0:972874f31c98 157
iforce2d 0:972874f31c98 158 /* set direction to output of the specified pin (internal pin number) */
iforce2d 0:972874f31c98 159 void u8g_SetPinOutput(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 160 {
iforce2d 0:972874f31c98 161 *u8g_get_avr_io_ptr(u8g_avr_ddr_P, internal_pin_number>>3) |= _BV(internal_pin_number&7);
iforce2d 0:972874f31c98 162 }
iforce2d 0:972874f31c98 163
iforce2d 0:972874f31c98 164 void u8g_SetPinInput(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 165 {
iforce2d 0:972874f31c98 166 *u8g_get_avr_io_ptr(u8g_avr_ddr_P, internal_pin_number>>3) &= ~_BV(internal_pin_number&7);
iforce2d 0:972874f31c98 167 }
iforce2d 0:972874f31c98 168
iforce2d 0:972874f31c98 169 void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
iforce2d 0:972874f31c98 170 {
iforce2d 0:972874f31c98 171 volatile uint8_t * tmp = u8g_get_avr_io_ptr(u8g_avr_port_P, internal_pin_number>>3);
iforce2d 0:972874f31c98 172
iforce2d 0:972874f31c98 173 if ( level == 0 )
iforce2d 0:972874f31c98 174 {
iforce2d 0:972874f31c98 175 U8G_ATOMIC_AND(tmp, ~_BV(internal_pin_number&7));
iforce2d 0:972874f31c98 176 // *tmp &= ~_BV(internal_pin_number&7);
iforce2d 0:972874f31c98 177 }
iforce2d 0:972874f31c98 178 else
iforce2d 0:972874f31c98 179 {
iforce2d 0:972874f31c98 180 U8G_ATOMIC_OR(tmp, _BV(internal_pin_number&7));
iforce2d 0:972874f31c98 181 //*tmp |= _BV(internal_pin_number&7);
iforce2d 0:972874f31c98 182 }
iforce2d 0:972874f31c98 183
iforce2d 0:972874f31c98 184 }
iforce2d 0:972874f31c98 185
iforce2d 0:972874f31c98 186 uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 187 {
iforce2d 0:972874f31c98 188 volatile uint8_t * tmp = u8g_get_avr_io_ptr(u8g_avr_pin_P, internal_pin_number>>3);
iforce2d 0:972874f31c98 189 if ( ((*tmp) & _BV(internal_pin_number&7)) != 0 )
iforce2d 0:972874f31c98 190 return 1;
iforce2d 0:972874f31c98 191 return 0;
iforce2d 0:972874f31c98 192 }
iforce2d 0:972874f31c98 193
iforce2d 0:972874f31c98 194 #else
iforce2d 0:972874f31c98 195
iforce2d 0:972874f31c98 196 /* convert "port" and "bitpos" to internal pin number */
iforce2d 0:972874f31c98 197 uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)
iforce2d 0:972874f31c98 198 {
iforce2d 0:972874f31c98 199 port <<= 3;
iforce2d 0:972874f31c98 200 port += bitpos;
iforce2d 0:972874f31c98 201 return port;
iforce2d 0:972874f31c98 202 }
iforce2d 0:972874f31c98 203
iforce2d 0:972874f31c98 204 void u8g_SetPinOutput(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 205 {
iforce2d 0:972874f31c98 206 }
iforce2d 0:972874f31c98 207
iforce2d 0:972874f31c98 208 void u8g_SetPinInput(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 209 {
iforce2d 0:972874f31c98 210 }
iforce2d 0:972874f31c98 211
iforce2d 0:972874f31c98 212 void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
iforce2d 0:972874f31c98 213 {
iforce2d 0:972874f31c98 214 }
iforce2d 0:972874f31c98 215
iforce2d 0:972874f31c98 216 uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
iforce2d 0:972874f31c98 217 {
iforce2d 0:972874f31c98 218 return 0;
iforce2d 0:972874f31c98 219 }
iforce2d 0:972874f31c98 220
iforce2d 0:972874f31c98 221 #endif
iforce2d 0:972874f31c98 222
iforce2d 0:972874f31c98 223
iforce2d 0:972874f31c98 224 #if defined(U8G_WITH_PINLIST)
iforce2d 0:972874f31c98 225
iforce2d 0:972874f31c98 226 void u8g_SetPIOutput(u8g_t *u8g, uint8_t pi)
iforce2d 0:972874f31c98 227 {
iforce2d 0:972874f31c98 228 uint8_t pin;
iforce2d 0:972874f31c98 229 pin = u8g->pin_list[pi];
iforce2d 0:972874f31c98 230 if ( pin != U8G_PIN_NONE )
iforce2d 0:972874f31c98 231 u8g_SetPinOutput(pin);
iforce2d 0:972874f31c98 232 }
iforce2d 0:972874f31c98 233
iforce2d 0:972874f31c98 234 void u8g_SetPILevel(u8g_t *u8g, uint8_t pi, uint8_t level)
iforce2d 0:972874f31c98 235 {
iforce2d 0:972874f31c98 236 uint8_t pin;
iforce2d 0:972874f31c98 237 pin = u8g->pin_list[pi];
iforce2d 0:972874f31c98 238 if ( pin != U8G_PIN_NONE )
iforce2d 0:972874f31c98 239 u8g_SetPinLevel(pin, level);
iforce2d 0:972874f31c98 240 }
iforce2d 0:972874f31c98 241
iforce2d 0:972874f31c98 242 #else /* defined(U8G_WITH_PINLIST) */
iforce2d 0:972874f31c98 243 void u8g_SetPIOutput(u8g_t *u8g, uint8_t pi)
iforce2d 0:972874f31c98 244 {
iforce2d 0:972874f31c98 245 }
iforce2d 0:972874f31c98 246
iforce2d 0:972874f31c98 247 void u8g_SetPILevel(u8g_t *u8g, uint8_t pi, uint8_t level)
iforce2d 0:972874f31c98 248 {
iforce2d 0:972874f31c98 249 }
iforce2d 0:972874f31c98 250
iforce2d 0:972874f31c98 251 #endif /* defined(U8G_WITH_PINLIST) */
iforce2d 0:972874f31c98 252