iforce2d Chris
/
ubxDistanceMeter
Displays distance to start location on OLED screen.
u8g_state.c@0:972874f31c98, 2018-03-07 (annotated)
- Committer:
- iforce2d
- Date:
- Wed Mar 07 12:49:14 2018 +0000
- Revision:
- 0:972874f31c98
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iforce2d | 0:972874f31c98 | 1 | /* |
iforce2d | 0:972874f31c98 | 2 | |
iforce2d | 0:972874f31c98 | 3 | u8g_state.c |
iforce2d | 0:972874f31c98 | 4 | |
iforce2d | 0:972874f31c98 | 5 | backup and restore hardware state |
iforce2d | 0:972874f31c98 | 6 | |
iforce2d | 0:972874f31c98 | 7 | Universal 8bit Graphics Library |
iforce2d | 0:972874f31c98 | 8 | |
iforce2d | 0:972874f31c98 | 9 | Copyright (c) 2011, 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 | state callback: backup env U8G_STATE_MSG_BACKUP_ENV |
iforce2d | 0:972874f31c98 | 38 | device callback: DEV_MSG_INIT |
iforce2d | 0:972874f31c98 | 39 | state callback: backup u8g U8G_STATE_MSG_BACKUP_U8G |
iforce2d | 0:972874f31c98 | 40 | state callback: restore env U8G_STATE_MSG_RESTORE_ENV |
iforce2d | 0:972874f31c98 | 41 | |
iforce2d | 0:972874f31c98 | 42 | state callback: backup env U8G_STATE_MSG_BACKUP_ENV |
iforce2d | 0:972874f31c98 | 43 | state callback: retore u8g U8G_STATE_MSG_RESTORE_U8G |
iforce2d | 0:972874f31c98 | 44 | DEV_MSG_PAGE_FIRST or DEV_MSG_PAGE_NEXT |
iforce2d | 0:972874f31c98 | 45 | state callback: restore env U8G_STATE_MSG_RESTORE_ENV |
iforce2d | 0:972874f31c98 | 46 | |
iforce2d | 0:972874f31c98 | 47 | */ |
iforce2d | 0:972874f31c98 | 48 | |
iforce2d | 0:972874f31c98 | 49 | #include <stddef.h> |
iforce2d | 0:972874f31c98 | 50 | #include "u8g.h" |
iforce2d | 0:972874f31c98 | 51 | |
iforce2d | 0:972874f31c98 | 52 | void u8g_state_dummy_cb(uint8_t msg) |
iforce2d | 0:972874f31c98 | 53 | { |
iforce2d | 0:972874f31c98 | 54 | /* the dummy procedure does nothing */ |
iforce2d | 0:972874f31c98 | 55 | } |
iforce2d | 0:972874f31c98 | 56 | |
iforce2d | 0:972874f31c98 | 57 | void u8g_SetHardwareBackup(u8g_t *u8g, u8g_state_cb backup_cb) |
iforce2d | 0:972874f31c98 | 58 | { |
iforce2d | 0:972874f31c98 | 59 | u8g->state_cb = backup_cb; |
iforce2d | 0:972874f31c98 | 60 | /* in most cases the init message was already sent, so this will backup the */ |
iforce2d | 0:972874f31c98 | 61 | /* current u8g state */ |
iforce2d | 0:972874f31c98 | 62 | backup_cb(U8G_STATE_MSG_BACKUP_U8G); |
iforce2d | 0:972874f31c98 | 63 | } |
iforce2d | 0:972874f31c98 | 64 | |
iforce2d | 0:972874f31c98 | 65 | |
iforce2d | 0:972874f31c98 | 66 | /*===============================================================*/ |
iforce2d | 0:972874f31c98 | 67 | /* register variable for restoring interrupt state */ |
iforce2d | 0:972874f31c98 | 68 | |
iforce2d | 0:972874f31c98 | 69 | #if defined(__AVR__) |
iforce2d | 0:972874f31c98 | 70 | uint8_t global_SREG_backup; |
iforce2d | 0:972874f31c98 | 71 | #endif |
iforce2d | 0:972874f31c98 | 72 | |
iforce2d | 0:972874f31c98 | 73 | |
iforce2d | 0:972874f31c98 | 74 | |
iforce2d | 0:972874f31c98 | 75 | /*===============================================================*/ |
iforce2d | 0:972874f31c98 | 76 | /* AVR */ |
iforce2d | 0:972874f31c98 | 77 | |
iforce2d | 0:972874f31c98 | 78 | #if defined(__AVR__) |
iforce2d | 0:972874f31c98 | 79 | #define U8G_ATMEGA_HW_SPI |
iforce2d | 0:972874f31c98 | 80 | |
iforce2d | 0:972874f31c98 | 81 | /* remove the definition for attiny */ |
iforce2d | 0:972874f31c98 | 82 | #if __AVR_ARCH__ == 2 |
iforce2d | 0:972874f31c98 | 83 | #undef U8G_ATMEGA_HW_SPI |
iforce2d | 0:972874f31c98 | 84 | #endif |
iforce2d | 0:972874f31c98 | 85 | #if __AVR_ARCH__ == 25 |
iforce2d | 0:972874f31c98 | 86 | #undef U8G_ATMEGA_HW_SPI |
iforce2d | 0:972874f31c98 | 87 | #endif |
iforce2d | 0:972874f31c98 | 88 | #endif |
iforce2d | 0:972874f31c98 | 89 | |
iforce2d | 0:972874f31c98 | 90 | #if defined(U8G_ATMEGA_HW_SPI) |
iforce2d | 0:972874f31c98 | 91 | #include <avr/interrupt.h> |
iforce2d | 0:972874f31c98 | 92 | static uint8_t u8g_state_avr_spi_memory[2]; |
iforce2d | 0:972874f31c98 | 93 | |
iforce2d | 0:972874f31c98 | 94 | void u8g_backup_spi(uint8_t msg) |
iforce2d | 0:972874f31c98 | 95 | { |
iforce2d | 0:972874f31c98 | 96 | if ( U8G_STATE_MSG_IS_BACKUP(msg) ) |
iforce2d | 0:972874f31c98 | 97 | { |
iforce2d | 0:972874f31c98 | 98 | u8g_state_avr_spi_memory[U8G_STATE_MSG_GET_IDX(msg)] = SPCR; |
iforce2d | 0:972874f31c98 | 99 | } |
iforce2d | 0:972874f31c98 | 100 | else |
iforce2d | 0:972874f31c98 | 101 | { |
iforce2d | 0:972874f31c98 | 102 | uint8_t tmp = SREG; |
iforce2d | 0:972874f31c98 | 103 | cli(); |
iforce2d | 0:972874f31c98 | 104 | SPCR = 0; |
iforce2d | 0:972874f31c98 | 105 | SPCR = u8g_state_avr_spi_memory[U8G_STATE_MSG_GET_IDX(msg)]; |
iforce2d | 0:972874f31c98 | 106 | SREG = tmp; |
iforce2d | 0:972874f31c98 | 107 | } |
iforce2d | 0:972874f31c98 | 108 | } |
iforce2d | 0:972874f31c98 | 109 | |
iforce2d | 0:972874f31c98 | 110 | #elif defined(ARDUINO) && defined(__arm__) // Arduino Due, maybe we should better check for __SAM3X8E__ |
iforce2d | 0:972874f31c98 | 111 | |
iforce2d | 0:972874f31c98 | 112 | #include "sam.h" |
iforce2d | 0:972874f31c98 | 113 | |
iforce2d | 0:972874f31c98 | 114 | struct sam_backup_struct |
iforce2d | 0:972874f31c98 | 115 | { |
iforce2d | 0:972874f31c98 | 116 | uint32_t mr; |
iforce2d | 0:972874f31c98 | 117 | uint32_t sr; |
iforce2d | 0:972874f31c98 | 118 | uint32_t csr[4]; |
iforce2d | 0:972874f31c98 | 119 | } sam_backup[2]; |
iforce2d | 0:972874f31c98 | 120 | |
iforce2d | 0:972874f31c98 | 121 | void u8g_backup_spi(uint8_t msg) |
iforce2d | 0:972874f31c98 | 122 | { |
iforce2d | 0:972874f31c98 | 123 | uint8_t idx = U8G_STATE_MSG_GET_IDX(msg); |
iforce2d | 0:972874f31c98 | 124 | if ( U8G_STATE_MSG_IS_BACKUP(msg) ) |
iforce2d | 0:972874f31c98 | 125 | { |
iforce2d | 0:972874f31c98 | 126 | sam_backup[idx].mr = SPI0->SPI_MR; |
iforce2d | 0:972874f31c98 | 127 | sam_backup[idx].sr = SPI0->SPI_SR; |
iforce2d | 0:972874f31c98 | 128 | sam_backup[idx].csr[0] = SPI0->SPI_CSR[0]; |
iforce2d | 0:972874f31c98 | 129 | sam_backup[idx].csr[1] = SPI0->SPI_CSR[1]; |
iforce2d | 0:972874f31c98 | 130 | sam_backup[idx].csr[2] = SPI0->SPI_CSR[2]; |
iforce2d | 0:972874f31c98 | 131 | sam_backup[idx].csr[3] = SPI0->SPI_CSR[3]; |
iforce2d | 0:972874f31c98 | 132 | } |
iforce2d | 0:972874f31c98 | 133 | else |
iforce2d | 0:972874f31c98 | 134 | { |
iforce2d | 0:972874f31c98 | 135 | SPI0->SPI_MR = sam_backup[idx].mr; |
iforce2d | 0:972874f31c98 | 136 | SPI0->SPI_CSR[0] = sam_backup[idx].csr[0]; |
iforce2d | 0:972874f31c98 | 137 | SPI0->SPI_CSR[1] = sam_backup[idx].csr[1]; |
iforce2d | 0:972874f31c98 | 138 | SPI0->SPI_CSR[2] = sam_backup[idx].csr[2]; |
iforce2d | 0:972874f31c98 | 139 | SPI0->SPI_CSR[3] = sam_backup[idx].csr[3]; |
iforce2d | 0:972874f31c98 | 140 | } |
iforce2d | 0:972874f31c98 | 141 | } |
iforce2d | 0:972874f31c98 | 142 | |
iforce2d | 0:972874f31c98 | 143 | #else |
iforce2d | 0:972874f31c98 | 144 | |
iforce2d | 0:972874f31c98 | 145 | void u8g_backup_spi(uint8_t msg) |
iforce2d | 0:972874f31c98 | 146 | { |
iforce2d | 0:972874f31c98 | 147 | } |
iforce2d | 0:972874f31c98 | 148 | |
iforce2d | 0:972874f31c98 | 149 | #endif |
iforce2d | 0:972874f31c98 | 150 | |
iforce2d | 0:972874f31c98 | 151 |