Maxim Integrated
/
MAXREFDES99_demo
Demo for MAXREFDES99
maxrefdes99.cpp@0:6b0161c3e440, 2016-01-12 (annotated)
- Committer:
- j3
- Date:
- Tue Jan 12 23:44:43 2016 +0000
- Revision:
- 0:6b0161c3e440
- Child:
- 2:9fdd01cf0f88
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:6b0161c3e440 | 1 | /******************************************************************//** |
j3 | 0:6b0161c3e440 | 2 | * @file maxrefdes99.cpp |
j3 | 0:6b0161c3e440 | 3 | * |
j3 | 0:6b0161c3e440 | 4 | * @author Justin Jordan |
j3 | 0:6b0161c3e440 | 5 | * |
j3 | 0:6b0161c3e440 | 6 | * @version 0.0 |
j3 | 0:6b0161c3e440 | 7 | * |
j3 | 0:6b0161c3e440 | 8 | * Started: 08JAN16 |
j3 | 0:6b0161c3e440 | 9 | * |
j3 | 0:6b0161c3e440 | 10 | * Updated: |
j3 | 0:6b0161c3e440 | 11 | * |
j3 | 0:6b0161c3e440 | 12 | * @brief Source file for maxrefdes99 demo |
j3 | 0:6b0161c3e440 | 13 | *********************************************************************** |
j3 | 0:6b0161c3e440 | 14 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:6b0161c3e440 | 15 | * |
j3 | 0:6b0161c3e440 | 16 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:6b0161c3e440 | 17 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:6b0161c3e440 | 18 | * to deal in the Software without restriction, including without limitation |
j3 | 0:6b0161c3e440 | 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:6b0161c3e440 | 20 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:6b0161c3e440 | 21 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:6b0161c3e440 | 22 | * |
j3 | 0:6b0161c3e440 | 23 | * The above copyright notice and this permission notice shall be included |
j3 | 0:6b0161c3e440 | 24 | * in all copies or substantial portions of the Software. |
j3 | 0:6b0161c3e440 | 25 | * |
j3 | 0:6b0161c3e440 | 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:6b0161c3e440 | 27 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:6b0161c3e440 | 28 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:6b0161c3e440 | 29 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:6b0161c3e440 | 30 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:6b0161c3e440 | 31 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:6b0161c3e440 | 32 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:6b0161c3e440 | 33 | * |
j3 | 0:6b0161c3e440 | 34 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:6b0161c3e440 | 35 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:6b0161c3e440 | 36 | * Products, Inc. Branding Policy. |
j3 | 0:6b0161c3e440 | 37 | * |
j3 | 0:6b0161c3e440 | 38 | * The mere transfer of this software does not imply any licenses |
j3 | 0:6b0161c3e440 | 39 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:6b0161c3e440 | 40 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:6b0161c3e440 | 41 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:6b0161c3e440 | 42 | * ownership rights. |
j3 | 0:6b0161c3e440 | 43 | **********************************************************************/ |
j3 | 0:6b0161c3e440 | 44 | |
j3 | 0:6b0161c3e440 | 45 | |
j3 | 0:6b0161c3e440 | 46 | #include "maxrefdes99.h" |
j3 | 0:6b0161c3e440 | 47 | |
j3 | 0:6b0161c3e440 | 48 | |
j3 | 0:6b0161c3e440 | 49 | //5x7 character bit map |
j3 | 0:6b0161c3e440 | 50 | static const uint8_t font_5x7[95][6] = { |
j3 | 0:6b0161c3e440 | 51 | {0x00,0x00,0x00,0x00,0x00,0x00}, // SPACE |
j3 | 0:6b0161c3e440 | 52 | {0x00,0x00,0x00,0x4F,0x00,0x00}, // ! |
j3 | 0:6b0161c3e440 | 53 | {0x00,0x00,0x07,0x00,0x07,0x00}, // " |
j3 | 0:6b0161c3e440 | 54 | {0x00,0x14,0x7F,0x14,0x7F,0x14}, // # |
j3 | 0:6b0161c3e440 | 55 | {0x00,0x24,0x2A,0x7F,0x2A,0x12}, // $ |
j3 | 0:6b0161c3e440 | 56 | {0x00,0x23,0x13,0x08,0x64,0x62}, // % |
j3 | 0:6b0161c3e440 | 57 | {0x00,0x36,0x49,0x55,0x22,0x50}, // & |
j3 | 0:6b0161c3e440 | 58 | {0x00,0x00,0x05,0x03,0x00,0x00}, // ' |
j3 | 0:6b0161c3e440 | 59 | {0x00,0x00,0x1C,0x22,0x41,0x00}, // ( |
j3 | 0:6b0161c3e440 | 60 | {0x00,0x00,0x41,0x22,0x1C,0x00}, // ) |
j3 | 0:6b0161c3e440 | 61 | {0x00,0x14,0x08,0x3E,0x08,0x14}, // * |
j3 | 0:6b0161c3e440 | 62 | {0x00,0x08,0x08,0x3E,0x08,0x08}, // + |
j3 | 0:6b0161c3e440 | 63 | {0x00,0x00,0x50,0x30,0x00,0x00}, // , |
j3 | 0:6b0161c3e440 | 64 | {0x00,0x08,0x08,0x08,0x08,0x08}, // - |
j3 | 0:6b0161c3e440 | 65 | {0x00,0x00,0x60,0x60,0x00,0x00}, // . |
j3 | 0:6b0161c3e440 | 66 | {0x00,0x20,0x10,0x08,0x04,0x02}, // / |
j3 | 0:6b0161c3e440 | 67 | {0x00,0x3E,0x51,0x49,0x45,0x3E}, // 0 |
j3 | 0:6b0161c3e440 | 68 | {0x00,0x00,0x42,0x7F,0x40,0x00}, // 1 |
j3 | 0:6b0161c3e440 | 69 | {0x00,0x42,0x61,0x51,0x49,0x46}, // 2 |
j3 | 0:6b0161c3e440 | 70 | {0x00,0x21,0x41,0x45,0x4B,0x31}, // 3 |
j3 | 0:6b0161c3e440 | 71 | {0x00,0x18,0x14,0x12,0x7F,0x10}, // 4 |
j3 | 0:6b0161c3e440 | 72 | {0x00,0x27,0x45,0x45,0x45,0x39}, // 5 |
j3 | 0:6b0161c3e440 | 73 | {0x00,0x3C,0x4A,0x49,0x49,0x30}, // 6 |
j3 | 0:6b0161c3e440 | 74 | {0x00,0x01,0x71,0x09,0x05,0x03}, // 7 |
j3 | 0:6b0161c3e440 | 75 | {0x00,0x36,0x49,0x49,0x49,0x36}, // 8 |
j3 | 0:6b0161c3e440 | 76 | {0x00,0x06,0x49,0x49,0x29,0x1E}, // 9 |
j3 | 0:6b0161c3e440 | 77 | {0x00,0x36,0x36,0x00,0x00,0x00}, // : |
j3 | 0:6b0161c3e440 | 78 | {0x00,0x56,0x36,0x00,0x00,0x00}, // ; |
j3 | 0:6b0161c3e440 | 79 | {0x00,0x08,0x14,0x22,0x41,0x00}, // < |
j3 | 0:6b0161c3e440 | 80 | {0x00,0x14,0x14,0x14,0x14,0x14}, // = |
j3 | 0:6b0161c3e440 | 81 | {0x00,0x00,0x41,0x22,0x14,0x08}, // > |
j3 | 0:6b0161c3e440 | 82 | {0x00,0x02,0x01,0x51,0x09,0x06}, // ? |
j3 | 0:6b0161c3e440 | 83 | {0x00,0x30,0x49,0x79,0x41,0x3E}, // @ |
j3 | 0:6b0161c3e440 | 84 | {0x00,0x7E,0x11,0x11,0x11,0x7E}, // A |
j3 | 0:6b0161c3e440 | 85 | {0x00,0x7F,0x49,0x49,0x49,0x36}, // B |
j3 | 0:6b0161c3e440 | 86 | {0x00,0x3E,0x41,0x41,0x41,0x22}, // C |
j3 | 0:6b0161c3e440 | 87 | {0x00,0x7F,0x41,0x41,0x22,0x1C}, // D |
j3 | 0:6b0161c3e440 | 88 | {0x00,0x7F,0x49,0x49,0x49,0x41}, // E |
j3 | 0:6b0161c3e440 | 89 | {0x00,0x7F,0x09,0x09,0x09,0x01}, // F |
j3 | 0:6b0161c3e440 | 90 | {0x00,0x3E,0x41,0x49,0x49,0x7A}, // G |
j3 | 0:6b0161c3e440 | 91 | {0x00,0x7F,0x08,0x08,0x08,0x7F}, // H |
j3 | 0:6b0161c3e440 | 92 | {0x00,0x00,0x41,0x7F,0x41,0x00}, // I |
j3 | 0:6b0161c3e440 | 93 | {0x00,0x20,0x40,0x41,0x3F,0x01}, // J |
j3 | 0:6b0161c3e440 | 94 | {0x00,0x7F,0x08,0x14,0x22,0x41}, // K |
j3 | 0:6b0161c3e440 | 95 | {0x00,0x7F,0x40,0x40,0x40,0x40}, // L |
j3 | 0:6b0161c3e440 | 96 | {0x00,0x7F,0x02,0x0C,0x02,0x7F}, // M |
j3 | 0:6b0161c3e440 | 97 | {0x00,0x7F,0x04,0x08,0x10,0x7F}, // N |
j3 | 0:6b0161c3e440 | 98 | {0x00,0x3E,0x41,0x41,0x41,0x3E}, // O |
j3 | 0:6b0161c3e440 | 99 | {0x00,0x7F,0x09,0x09,0x09,0x06}, // P |
j3 | 0:6b0161c3e440 | 100 | {0x00,0x3E,0x41,0x51,0x21,0x5E}, // Q |
j3 | 0:6b0161c3e440 | 101 | {0x00,0x7F,0x09,0x19,0x29,0x46}, // R |
j3 | 0:6b0161c3e440 | 102 | {0x00,0x46,0x49,0x49,0x49,0x31}, // S |
j3 | 0:6b0161c3e440 | 103 | {0x00,0x01,0x01,0x7F,0x01,0x01}, // T |
j3 | 0:6b0161c3e440 | 104 | {0x00,0x3F,0x40,0x40,0x40,0x3F}, // U |
j3 | 0:6b0161c3e440 | 105 | {0x00,0x1F,0x20,0x40,0x20,0x1F}, // V |
j3 | 0:6b0161c3e440 | 106 | {0x00,0x3F,0x40,0x30,0x40,0x3F}, // W |
j3 | 0:6b0161c3e440 | 107 | {0x00,0x63,0x14,0x08,0x14,0x63}, // X |
j3 | 0:6b0161c3e440 | 108 | {0x00,0x07,0x08,0x70,0x08,0x07}, // Y |
j3 | 0:6b0161c3e440 | 109 | {0x00,0x61,0x51,0x49,0x45,0x43}, // Z |
j3 | 0:6b0161c3e440 | 110 | {0x00,0x00,0x7F,0x41,0x41,0x00}, // [ |
j3 | 0:6b0161c3e440 | 111 | {0x00,0x02,0x04,0x08,0x10,0x20}, // backslash |
j3 | 0:6b0161c3e440 | 112 | {0x00,0x00,0x41,0x41,0x7F,0x00}, // ] |
j3 | 0:6b0161c3e440 | 113 | {0x00,0x04,0x02,0x01,0x02,0x04}, // ^ |
j3 | 0:6b0161c3e440 | 114 | {0x00,0x40,0x40,0x40,0x40,0x40}, // _ |
j3 | 0:6b0161c3e440 | 115 | {0x00,0x00,0x01,0x02,0x04,0x00}, // ` |
j3 | 0:6b0161c3e440 | 116 | {0x00,0x20,0x54,0x54,0x54,0x78}, // a |
j3 | 0:6b0161c3e440 | 117 | {0x00,0x7F,0x50,0x48,0x48,0x30}, // b |
j3 | 0:6b0161c3e440 | 118 | {0x00,0x38,0x44,0x44,0x44,0x20}, // c |
j3 | 0:6b0161c3e440 | 119 | {0x00,0x38,0x44,0x44,0x48,0x7F}, // d |
j3 | 0:6b0161c3e440 | 120 | {0x00,0x38,0x54,0x54,0x54,0x18}, // e |
j3 | 0:6b0161c3e440 | 121 | {0x00,0x08,0x7E,0x09,0x01,0x02}, // f |
j3 | 0:6b0161c3e440 | 122 | {0x00,0x0C,0x52,0x52,0x52,0x3E}, // g |
j3 | 0:6b0161c3e440 | 123 | {0x00,0x7F,0x08,0x04,0x04,0x78}, // h |
j3 | 0:6b0161c3e440 | 124 | {0x00,0x00,0x44,0x7D,0x40,0x00}, // i |
j3 | 0:6b0161c3e440 | 125 | {0x00,0x20,0x40,0x44,0x3D,0x00}, // j |
j3 | 0:6b0161c3e440 | 126 | {0x00,0x7F,0x10,0x28,0x44,0x00}, // k |
j3 | 0:6b0161c3e440 | 127 | {0x00,0x00,0x41,0x7F,0x40,0x00}, // l |
j3 | 0:6b0161c3e440 | 128 | {0x00,0x78,0x04,0x58,0x44,0x78}, // m |
j3 | 0:6b0161c3e440 | 129 | {0x00,0x7C,0x08,0x04,0x04,0x78}, // n |
j3 | 0:6b0161c3e440 | 130 | {0x00,0x38,0x44,0x44,0x44,0x38}, // o |
j3 | 0:6b0161c3e440 | 131 | {0x00,0x7C,0x14,0x14,0x14,0x08}, // p |
j3 | 0:6b0161c3e440 | 132 | {0x00,0x08,0x14,0x14,0x18,0x7C}, // q |
j3 | 0:6b0161c3e440 | 133 | {0x00,0x7C,0x08,0x04,0x04,0x08}, // r |
j3 | 0:6b0161c3e440 | 134 | {0x00,0x48,0x54,0x54,0x54,0x20}, // s |
j3 | 0:6b0161c3e440 | 135 | {0x00,0x04,0x3F,0x44,0x40,0x20}, // t |
j3 | 0:6b0161c3e440 | 136 | {0x00,0x3C,0x40,0x40,0x20,0x7C}, // u |
j3 | 0:6b0161c3e440 | 137 | {0x00,0x1C,0x20,0x40,0x20,0x1C}, // v |
j3 | 0:6b0161c3e440 | 138 | {0x00,0x3C,0x40,0x30,0x40,0x3C}, // w |
j3 | 0:6b0161c3e440 | 139 | {0x00,0x44,0x28,0x10,0x28,0x44}, // x |
j3 | 0:6b0161c3e440 | 140 | {0x00,0x0C,0x50,0x50,0x50,0x3C}, // y |
j3 | 0:6b0161c3e440 | 141 | {0x00,0x44,0x64,0x54,0x4C,0x44}, // z |
j3 | 0:6b0161c3e440 | 142 | {0x00,0x00,0x08,0x36,0x41,0x00}, // { |
j3 | 0:6b0161c3e440 | 143 | {0x00,0x00,0x00,0x7F,0x00,0x00}, // | |
j3 | 0:6b0161c3e440 | 144 | {0x00,0x00,0x41,0x36,0x08,0x00}, // } |
j3 | 0:6b0161c3e440 | 145 | {0x00,0x0C,0x02,0x0C,0x10,0x0C} // ~ |
j3 | 0:6b0161c3e440 | 146 | }; |
j3 | 0:6b0161c3e440 | 147 | |
j3 | 0:6b0161c3e440 | 148 | |
j3 | 0:6b0161c3e440 | 149 | static uint8_t display_buff[256]; |
j3 | 0:6b0161c3e440 | 150 | static uint8_t temp_buff[256]; |
j3 | 0:6b0161c3e440 | 151 | |
j3 | 0:6b0161c3e440 | 152 | static const uint8_t DISPLAY_BUFF_START = 112; |
j3 | 0:6b0161c3e440 | 153 | static const uint8_t DISPLAY_BUFF_END = 143; |
j3 | 0:6b0161c3e440 | 154 | |
j3 | 0:6b0161c3e440 | 155 | static const uint32_t MAX_STRING_LENGTH = 32; |
j3 | 0:6b0161c3e440 | 156 | static char str[MAX_STRING_LENGTH]; |
j3 | 0:6b0161c3e440 | 157 | |
j3 | 0:6b0161c3e440 | 158 | |
j3 | 0:6b0161c3e440 | 159 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 160 | void get_5x7_character(char c, uint8_t *char_buff) |
j3 | 0:6b0161c3e440 | 161 | { |
j3 | 0:6b0161c3e440 | 162 | uint16_t temp_data; |
j3 | 0:6b0161c3e440 | 163 | uint8_t idx, idy; |
j3 | 0:6b0161c3e440 | 164 | |
j3 | 0:6b0161c3e440 | 165 | for(idx = 0; idx < 6; idx++) |
j3 | 0:6b0161c3e440 | 166 | { |
j3 | 0:6b0161c3e440 | 167 | //get byte of char from map and flip it, bit 0 becomes bit 7 and so on |
j3 | 0:6b0161c3e440 | 168 | temp_data = (0x00FF & (font_5x7[c - 0x20][idx])); |
j3 | 0:6b0161c3e440 | 169 | for(idy = 0; idy < 8; idy++) |
j3 | 0:6b0161c3e440 | 170 | { |
j3 | 0:6b0161c3e440 | 171 | temp_data = (temp_data | (((temp_data & (1 << idy)) << (15 - (idy*2))))); |
j3 | 0:6b0161c3e440 | 172 | } |
j3 | 0:6b0161c3e440 | 173 | temp_data = ((temp_data >> 8) & 0x00FF); |
j3 | 0:6b0161c3e440 | 174 | |
j3 | 0:6b0161c3e440 | 175 | char_buff[idx] = ((uint8_t) temp_data); |
j3 | 0:6b0161c3e440 | 176 | } |
j3 | 0:6b0161c3e440 | 177 | } |
j3 | 0:6b0161c3e440 | 178 | |
j3 | 0:6b0161c3e440 | 179 | |
j3 | 0:6b0161c3e440 | 180 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 181 | void print_char(Max7219 *p_display, uint8_t position, char c) |
j3 | 0:6b0161c3e440 | 182 | { |
j3 | 0:6b0161c3e440 | 183 | uint8_t idx; |
j3 | 0:6b0161c3e440 | 184 | uint8_t wrap_device; |
j3 | 0:6b0161c3e440 | 185 | uint8_t wrap_idx; |
j3 | 0:6b0161c3e440 | 186 | uint8_t char_buff[6]; |
j3 | 0:6b0161c3e440 | 187 | uint8_t device; |
j3 | 0:6b0161c3e440 | 188 | uint8_t display_buff_idx = 0; |
j3 | 0:6b0161c3e440 | 189 | |
j3 | 0:6b0161c3e440 | 190 | get_5x7_character(c, char_buff); |
j3 | 0:6b0161c3e440 | 191 | |
j3 | 0:6b0161c3e440 | 192 | //get device and device_position |
j3 | 0:6b0161c3e440 | 193 | if(position > 8) |
j3 | 0:6b0161c3e440 | 194 | { |
j3 | 0:6b0161c3e440 | 195 | if(position%8) |
j3 | 0:6b0161c3e440 | 196 | { |
j3 | 0:6b0161c3e440 | 197 | device = ((position/8) + 1); |
j3 | 0:6b0161c3e440 | 198 | position = position%8; |
j3 | 0:6b0161c3e440 | 199 | } |
j3 | 0:6b0161c3e440 | 200 | else |
j3 | 0:6b0161c3e440 | 201 | { |
j3 | 0:6b0161c3e440 | 202 | device = position/8; |
j3 | 0:6b0161c3e440 | 203 | position = 8; |
j3 | 0:6b0161c3e440 | 204 | } |
j3 | 0:6b0161c3e440 | 205 | } |
j3 | 0:6b0161c3e440 | 206 | else |
j3 | 0:6b0161c3e440 | 207 | { |
j3 | 0:6b0161c3e440 | 208 | device = 1; |
j3 | 0:6b0161c3e440 | 209 | } |
j3 | 0:6b0161c3e440 | 210 | |
j3 | 0:6b0161c3e440 | 211 | //check for dispaly wrap around |
j3 | 0:6b0161c3e440 | 212 | //(moving to next 8x8 grid cause character doesn't fit on current one) |
j3 | 0:6b0161c3e440 | 213 | if(position > 3) |
j3 | 0:6b0161c3e440 | 214 | { |
j3 | 0:6b0161c3e440 | 215 | wrap_idx = (9 - position); |
j3 | 0:6b0161c3e440 | 216 | if(device < 4) |
j3 | 0:6b0161c3e440 | 217 | { |
j3 | 0:6b0161c3e440 | 218 | wrap_device = (device + 1); |
j3 | 0:6b0161c3e440 | 219 | } |
j3 | 0:6b0161c3e440 | 220 | else |
j3 | 0:6b0161c3e440 | 221 | { |
j3 | 0:6b0161c3e440 | 222 | wrap_device = 1; |
j3 | 0:6b0161c3e440 | 223 | } |
j3 | 0:6b0161c3e440 | 224 | } |
j3 | 0:6b0161c3e440 | 225 | else |
j3 | 0:6b0161c3e440 | 226 | { |
j3 | 0:6b0161c3e440 | 227 | wrap_idx = 6; //make bigger than idx, not used |
j3 | 0:6b0161c3e440 | 228 | wrap_device = 0; //not used |
j3 | 0:6b0161c3e440 | 229 | } |
j3 | 0:6b0161c3e440 | 230 | |
j3 | 0:6b0161c3e440 | 231 | //print character to position |
j3 | 0:6b0161c3e440 | 232 | for(idx = 0; idx < 6; idx++) |
j3 | 0:6b0161c3e440 | 233 | { |
j3 | 0:6b0161c3e440 | 234 | //check for wrap |
j3 | 0:6b0161c3e440 | 235 | if(idx >= wrap_idx) |
j3 | 0:6b0161c3e440 | 236 | { |
j3 | 0:6b0161c3e440 | 237 | p_display->write_digit(wrap_device, ((idx - wrap_idx) + 1), char_buff[idx]); |
j3 | 0:6b0161c3e440 | 238 | } |
j3 | 0:6b0161c3e440 | 239 | else |
j3 | 0:6b0161c3e440 | 240 | { |
j3 | 0:6b0161c3e440 | 241 | p_display->write_digit(device, (position + idx), char_buff[idx]); |
j3 | 0:6b0161c3e440 | 242 | } |
j3 | 0:6b0161c3e440 | 243 | |
j3 | 0:6b0161c3e440 | 244 | //get index for display buffer, keeps image of display for shifting |
j3 | 0:6b0161c3e440 | 245 | display_buff_idx = (((device - 1) * 8) + (position - 1) + idx + DISPLAY_BUFF_START); |
j3 | 0:6b0161c3e440 | 246 | //wrap buff back around |
j3 | 0:6b0161c3e440 | 247 | if(display_buff_idx > 255) |
j3 | 0:6b0161c3e440 | 248 | { |
j3 | 0:6b0161c3e440 | 249 | display_buff_idx = display_buff_idx - 256; |
j3 | 0:6b0161c3e440 | 250 | } |
j3 | 0:6b0161c3e440 | 251 | display_buff[display_buff_idx] = char_buff[idx]; |
j3 | 0:6b0161c3e440 | 252 | } |
j3 | 0:6b0161c3e440 | 253 | } |
j3 | 0:6b0161c3e440 | 254 | |
j3 | 0:6b0161c3e440 | 255 | |
j3 | 0:6b0161c3e440 | 256 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 257 | void print_string(Max7219 *p_display, uint8_t position, const char *s) |
j3 | 0:6b0161c3e440 | 258 | { |
j3 | 0:6b0161c3e440 | 259 | uint8_t idx = 0; |
j3 | 0:6b0161c3e440 | 260 | |
j3 | 0:6b0161c3e440 | 261 | while(s[idx] != '\0') |
j3 | 0:6b0161c3e440 | 262 | { |
j3 | 0:6b0161c3e440 | 263 | print_char(p_display, (position + (idx*6)), s[idx]); |
j3 | 0:6b0161c3e440 | 264 | idx++; |
j3 | 0:6b0161c3e440 | 265 | } |
j3 | 0:6b0161c3e440 | 266 | } |
j3 | 0:6b0161c3e440 | 267 | |
j3 | 0:6b0161c3e440 | 268 | |
j3 | 0:6b0161c3e440 | 269 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 270 | void shift_display_right(Max7219 *p_display, uint8_t count, uint8_t delay) |
j3 | 0:6b0161c3e440 | 271 | { |
j3 | 0:6b0161c3e440 | 272 | uint16_t idx, idy; |
j3 | 0:6b0161c3e440 | 273 | uint8_t position; |
j3 | 0:6b0161c3e440 | 274 | |
j3 | 0:6b0161c3e440 | 275 | for(idx = 0; idx < count; idx++) |
j3 | 0:6b0161c3e440 | 276 | { |
j3 | 0:6b0161c3e440 | 277 | for(idy = 0; idy < 256; idy++) |
j3 | 0:6b0161c3e440 | 278 | { |
j3 | 0:6b0161c3e440 | 279 | if(idy == 255) |
j3 | 0:6b0161c3e440 | 280 | { |
j3 | 0:6b0161c3e440 | 281 | temp_buff[0] = display_buff[idy]; |
j3 | 0:6b0161c3e440 | 282 | } |
j3 | 0:6b0161c3e440 | 283 | else |
j3 | 0:6b0161c3e440 | 284 | { |
j3 | 0:6b0161c3e440 | 285 | temp_buff[idy + 1] = display_buff[idy]; |
j3 | 0:6b0161c3e440 | 286 | } |
j3 | 0:6b0161c3e440 | 287 | } |
j3 | 0:6b0161c3e440 | 288 | |
j3 | 0:6b0161c3e440 | 289 | for(idy = 0; idy < 256; idy++) |
j3 | 0:6b0161c3e440 | 290 | { |
j3 | 0:6b0161c3e440 | 291 | //save shifted display in buffer |
j3 | 0:6b0161c3e440 | 292 | display_buff[idy] = temp_buff[idy]; |
j3 | 0:6b0161c3e440 | 293 | |
j3 | 0:6b0161c3e440 | 294 | if((idy >= DISPLAY_BUFF_START) && ( idy <= DISPLAY_BUFF_END)) |
j3 | 0:6b0161c3e440 | 295 | { |
j3 | 0:6b0161c3e440 | 296 | position = ((idy - DISPLAY_BUFF_START) + 1); |
j3 | 0:6b0161c3e440 | 297 | if(position > 24) |
j3 | 0:6b0161c3e440 | 298 | { |
j3 | 0:6b0161c3e440 | 299 | if(position % 8) |
j3 | 0:6b0161c3e440 | 300 | { |
j3 | 0:6b0161c3e440 | 301 | position = position % 8; |
j3 | 0:6b0161c3e440 | 302 | } |
j3 | 0:6b0161c3e440 | 303 | else |
j3 | 0:6b0161c3e440 | 304 | { |
j3 | 0:6b0161c3e440 | 305 | position = 8; |
j3 | 0:6b0161c3e440 | 306 | } |
j3 | 0:6b0161c3e440 | 307 | p_display->write_digit(4, position, display_buff[idy]); |
j3 | 0:6b0161c3e440 | 308 | } |
j3 | 0:6b0161c3e440 | 309 | else if(position > 16) |
j3 | 0:6b0161c3e440 | 310 | { |
j3 | 0:6b0161c3e440 | 311 | if(position % 8) |
j3 | 0:6b0161c3e440 | 312 | { |
j3 | 0:6b0161c3e440 | 313 | position = position % 8; |
j3 | 0:6b0161c3e440 | 314 | } |
j3 | 0:6b0161c3e440 | 315 | else |
j3 | 0:6b0161c3e440 | 316 | { |
j3 | 0:6b0161c3e440 | 317 | position = 8; |
j3 | 0:6b0161c3e440 | 318 | } |
j3 | 0:6b0161c3e440 | 319 | p_display->write_digit(3, position, display_buff[idy]); |
j3 | 0:6b0161c3e440 | 320 | } |
j3 | 0:6b0161c3e440 | 321 | else if(position > 8) |
j3 | 0:6b0161c3e440 | 322 | { |
j3 | 0:6b0161c3e440 | 323 | if(position % 8) |
j3 | 0:6b0161c3e440 | 324 | { |
j3 | 0:6b0161c3e440 | 325 | position = position % 8; |
j3 | 0:6b0161c3e440 | 326 | } |
j3 | 0:6b0161c3e440 | 327 | else |
j3 | 0:6b0161c3e440 | 328 | { |
j3 | 0:6b0161c3e440 | 329 | position = 8; |
j3 | 0:6b0161c3e440 | 330 | } |
j3 | 0:6b0161c3e440 | 331 | p_display->write_digit(2, position, display_buff[idy]); |
j3 | 0:6b0161c3e440 | 332 | } |
j3 | 0:6b0161c3e440 | 333 | else |
j3 | 0:6b0161c3e440 | 334 | { |
j3 | 0:6b0161c3e440 | 335 | p_display->write_digit(1, position, display_buff[idy]); |
j3 | 0:6b0161c3e440 | 336 | } |
j3 | 0:6b0161c3e440 | 337 | } |
j3 | 0:6b0161c3e440 | 338 | } |
j3 | 0:6b0161c3e440 | 339 | |
j3 | 0:6b0161c3e440 | 340 | wait_ms(delay); |
j3 | 0:6b0161c3e440 | 341 | } |
j3 | 0:6b0161c3e440 | 342 | } |
j3 | 0:6b0161c3e440 | 343 | |
j3 | 0:6b0161c3e440 | 344 | |
j3 | 0:6b0161c3e440 | 345 | void shift_display_left(Max7219 *p_display, uint8_t count, uint8_t delay) |
j3 | 0:6b0161c3e440 | 346 | { |
j3 | 0:6b0161c3e440 | 347 | uint16_t idx, idy; |
j3 | 0:6b0161c3e440 | 348 | uint8_t position; |
j3 | 0:6b0161c3e440 | 349 | |
j3 | 0:6b0161c3e440 | 350 | for(idx = 0; idx < count; idx++) |
j3 | 0:6b0161c3e440 | 351 | { |
j3 | 0:6b0161c3e440 | 352 | for(idy = 0; idy < 256; idy++) |
j3 | 0:6b0161c3e440 | 353 | { |
j3 | 0:6b0161c3e440 | 354 | if(idy == 0) |
j3 | 0:6b0161c3e440 | 355 | { |
j3 | 0:6b0161c3e440 | 356 | temp_buff[255] = display_buff[idy]; |
j3 | 0:6b0161c3e440 | 357 | } |
j3 | 0:6b0161c3e440 | 358 | else |
j3 | 0:6b0161c3e440 | 359 | { |
j3 | 0:6b0161c3e440 | 360 | temp_buff[idy - 1] = display_buff[idy]; |
j3 | 0:6b0161c3e440 | 361 | } |
j3 | 0:6b0161c3e440 | 362 | } |
j3 | 0:6b0161c3e440 | 363 | |
j3 | 0:6b0161c3e440 | 364 | for(idy = 0; idy < 256; idy++) |
j3 | 0:6b0161c3e440 | 365 | { |
j3 | 0:6b0161c3e440 | 366 | //save shifted display in buffer |
j3 | 0:6b0161c3e440 | 367 | display_buff[idy] = temp_buff[idy]; |
j3 | 0:6b0161c3e440 | 368 | |
j3 | 0:6b0161c3e440 | 369 | if((idy >= DISPLAY_BUFF_START) && ( idy <= DISPLAY_BUFF_END)) |
j3 | 0:6b0161c3e440 | 370 | { |
j3 | 0:6b0161c3e440 | 371 | position = ((idy - DISPLAY_BUFF_START) + 1); |
j3 | 0:6b0161c3e440 | 372 | if(position > 24) |
j3 | 0:6b0161c3e440 | 373 | { |
j3 | 0:6b0161c3e440 | 374 | if(position % 8) |
j3 | 0:6b0161c3e440 | 375 | { |
j3 | 0:6b0161c3e440 | 376 | position = position % 8; |
j3 | 0:6b0161c3e440 | 377 | } |
j3 | 0:6b0161c3e440 | 378 | else |
j3 | 0:6b0161c3e440 | 379 | { |
j3 | 0:6b0161c3e440 | 380 | position = 8; |
j3 | 0:6b0161c3e440 | 381 | } |
j3 | 0:6b0161c3e440 | 382 | p_display->write_digit(4, position, display_buff[idy]); |
j3 | 0:6b0161c3e440 | 383 | } |
j3 | 0:6b0161c3e440 | 384 | else if(position > 16) |
j3 | 0:6b0161c3e440 | 385 | { |
j3 | 0:6b0161c3e440 | 386 | if(position % 8) |
j3 | 0:6b0161c3e440 | 387 | { |
j3 | 0:6b0161c3e440 | 388 | position = position % 8; |
j3 | 0:6b0161c3e440 | 389 | } |
j3 | 0:6b0161c3e440 | 390 | else |
j3 | 0:6b0161c3e440 | 391 | { |
j3 | 0:6b0161c3e440 | 392 | position = 8; |
j3 | 0:6b0161c3e440 | 393 | } |
j3 | 0:6b0161c3e440 | 394 | p_display->write_digit(3, position, display_buff[idy]); |
j3 | 0:6b0161c3e440 | 395 | } |
j3 | 0:6b0161c3e440 | 396 | else if(position > 8) |
j3 | 0:6b0161c3e440 | 397 | { |
j3 | 0:6b0161c3e440 | 398 | if(position % 8) |
j3 | 0:6b0161c3e440 | 399 | { |
j3 | 0:6b0161c3e440 | 400 | position = position % 8; |
j3 | 0:6b0161c3e440 | 401 | } |
j3 | 0:6b0161c3e440 | 402 | else |
j3 | 0:6b0161c3e440 | 403 | { |
j3 | 0:6b0161c3e440 | 404 | position = 8; |
j3 | 0:6b0161c3e440 | 405 | } |
j3 | 0:6b0161c3e440 | 406 | p_display->write_digit(2, position, display_buff[idy]); |
j3 | 0:6b0161c3e440 | 407 | } |
j3 | 0:6b0161c3e440 | 408 | else |
j3 | 0:6b0161c3e440 | 409 | { |
j3 | 0:6b0161c3e440 | 410 | p_display->write_digit(1, position, display_buff[idy]); |
j3 | 0:6b0161c3e440 | 411 | } |
j3 | 0:6b0161c3e440 | 412 | } |
j3 | 0:6b0161c3e440 | 413 | } |
j3 | 0:6b0161c3e440 | 414 | |
j3 | 0:6b0161c3e440 | 415 | wait_ms(delay); |
j3 | 0:6b0161c3e440 | 416 | } |
j3 | 0:6b0161c3e440 | 417 | } |
j3 | 0:6b0161c3e440 | 418 | |
j3 | 0:6b0161c3e440 | 419 | |
j3 | 0:6b0161c3e440 | 420 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 421 | void quad_all_on(Max7219 *p_display, uint8_t quad) |
j3 | 0:6b0161c3e440 | 422 | { |
j3 | 0:6b0161c3e440 | 423 | if(quad > 1) |
j3 | 0:6b0161c3e440 | 424 | { |
j3 | 0:6b0161c3e440 | 425 | for(uint8_t idx = 0; idx < 8; idx++) |
j3 | 0:6b0161c3e440 | 426 | { |
j3 | 0:6b0161c3e440 | 427 | display_buff[((quad - 1)*8) + DISPLAY_BUFF_START + idx] = 0xFF; |
j3 | 0:6b0161c3e440 | 428 | } |
j3 | 0:6b0161c3e440 | 429 | } |
j3 | 0:6b0161c3e440 | 430 | else |
j3 | 0:6b0161c3e440 | 431 | { |
j3 | 0:6b0161c3e440 | 432 | for(uint8_t idx = 0; idx < 8; idx++) |
j3 | 0:6b0161c3e440 | 433 | { |
j3 | 0:6b0161c3e440 | 434 | display_buff[idx + DISPLAY_BUFF_START] = 0xFF; |
j3 | 0:6b0161c3e440 | 435 | } |
j3 | 0:6b0161c3e440 | 436 | } |
j3 | 0:6b0161c3e440 | 437 | |
j3 | 0:6b0161c3e440 | 438 | p_display->device_all_on(quad); |
j3 | 0:6b0161c3e440 | 439 | } |
j3 | 0:6b0161c3e440 | 440 | |
j3 | 0:6b0161c3e440 | 441 | |
j3 | 0:6b0161c3e440 | 442 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 443 | void quad_all_off(Max7219 *p_display, uint8_t quad) |
j3 | 0:6b0161c3e440 | 444 | { |
j3 | 0:6b0161c3e440 | 445 | if(quad > 1) |
j3 | 0:6b0161c3e440 | 446 | { |
j3 | 0:6b0161c3e440 | 447 | for(uint8_t idx = 0; idx < 8; idx++) |
j3 | 0:6b0161c3e440 | 448 | { |
j3 | 0:6b0161c3e440 | 449 | display_buff[((quad - 1)*8) + DISPLAY_BUFF_START + idx] = 0; |
j3 | 0:6b0161c3e440 | 450 | } |
j3 | 0:6b0161c3e440 | 451 | } |
j3 | 0:6b0161c3e440 | 452 | else |
j3 | 0:6b0161c3e440 | 453 | { |
j3 | 0:6b0161c3e440 | 454 | for(uint8_t idx = 0; idx < 8; idx++) |
j3 | 0:6b0161c3e440 | 455 | { |
j3 | 0:6b0161c3e440 | 456 | display_buff[idx + DISPLAY_BUFF_START] = 0; |
j3 | 0:6b0161c3e440 | 457 | } |
j3 | 0:6b0161c3e440 | 458 | } |
j3 | 0:6b0161c3e440 | 459 | |
j3 | 0:6b0161c3e440 | 460 | p_display->device_all_off(quad); |
j3 | 0:6b0161c3e440 | 461 | } |
j3 | 0:6b0161c3e440 | 462 | |
j3 | 0:6b0161c3e440 | 463 | |
j3 | 0:6b0161c3e440 | 464 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 465 | void all_on(Max7219 *p_display) |
j3 | 0:6b0161c3e440 | 466 | { |
j3 | 0:6b0161c3e440 | 467 | for(uint16_t idx = 0; idx < 256; idx++) |
j3 | 0:6b0161c3e440 | 468 | { |
j3 | 0:6b0161c3e440 | 469 | display_buff[idx] = 0xFF; |
j3 | 0:6b0161c3e440 | 470 | } |
j3 | 0:6b0161c3e440 | 471 | |
j3 | 0:6b0161c3e440 | 472 | p_display->display_all_on(); |
j3 | 0:6b0161c3e440 | 473 | } |
j3 | 0:6b0161c3e440 | 474 | |
j3 | 0:6b0161c3e440 | 475 | |
j3 | 0:6b0161c3e440 | 476 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 477 | void all_off(Max7219 *p_display) |
j3 | 0:6b0161c3e440 | 478 | { |
j3 | 0:6b0161c3e440 | 479 | for(uint16_t idx = 0; idx < 256; idx++) |
j3 | 0:6b0161c3e440 | 480 | { |
j3 | 0:6b0161c3e440 | 481 | display_buff[idx] = 0; |
j3 | 0:6b0161c3e440 | 482 | } |
j3 | 0:6b0161c3e440 | 483 | |
j3 | 0:6b0161c3e440 | 484 | p_display->display_all_off(); |
j3 | 0:6b0161c3e440 | 485 | } |
j3 | 0:6b0161c3e440 | 486 | |
j3 | 0:6b0161c3e440 | 487 | |
j3 | 0:6b0161c3e440 | 488 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 489 | void demo(Max7219 *display, max7219_configuration_t display_config, bool endless_loop) |
j3 | 0:6b0161c3e440 | 490 | { |
j3 | 0:6b0161c3e440 | 491 | uint8_t position = 1; |
j3 | 0:6b0161c3e440 | 492 | uint8_t idx = 0; |
j3 | 0:6b0161c3e440 | 493 | |
j3 | 0:6b0161c3e440 | 494 | do |
j3 | 0:6b0161c3e440 | 495 | { |
j3 | 0:6b0161c3e440 | 496 | all_off(display); |
j3 | 0:6b0161c3e440 | 497 | |
j3 | 0:6b0161c3e440 | 498 | if(position > 25) |
j3 | 0:6b0161c3e440 | 499 | { |
j3 | 0:6b0161c3e440 | 500 | position = 1; |
j3 | 0:6b0161c3e440 | 501 | } |
j3 | 0:6b0161c3e440 | 502 | printf("\nPosition = %d\n", position); |
j3 | 0:6b0161c3e440 | 503 | |
j3 | 0:6b0161c3e440 | 504 | for(idx = 0x20; idx < 0x7F; idx++) |
j3 | 0:6b0161c3e440 | 505 | { |
j3 | 0:6b0161c3e440 | 506 | print_char(display, position, (char) idx); |
j3 | 0:6b0161c3e440 | 507 | wait(0.25); |
j3 | 0:6b0161c3e440 | 508 | } |
j3 | 0:6b0161c3e440 | 509 | position = (position + 8); |
j3 | 0:6b0161c3e440 | 510 | |
j3 | 0:6b0161c3e440 | 511 | all_on(display); |
j3 | 0:6b0161c3e440 | 512 | |
j3 | 0:6b0161c3e440 | 513 | for(idx = 0; idx < 16; idx++) |
j3 | 0:6b0161c3e440 | 514 | { |
j3 | 0:6b0161c3e440 | 515 | display_config.intensity = 15 - idx; |
j3 | 0:6b0161c3e440 | 516 | display->init_display(display_config); |
j3 | 0:6b0161c3e440 | 517 | wait_ms(50); |
j3 | 0:6b0161c3e440 | 518 | } |
j3 | 0:6b0161c3e440 | 519 | |
j3 | 0:6b0161c3e440 | 520 | for(idx = 0; idx < 16; idx++) |
j3 | 0:6b0161c3e440 | 521 | { |
j3 | 0:6b0161c3e440 | 522 | display_config.intensity = idx; |
j3 | 0:6b0161c3e440 | 523 | display->init_display(display_config); |
j3 | 0:6b0161c3e440 | 524 | wait_ms(100); |
j3 | 0:6b0161c3e440 | 525 | } |
j3 | 0:6b0161c3e440 | 526 | |
j3 | 0:6b0161c3e440 | 527 | wait_ms(100); |
j3 | 0:6b0161c3e440 | 528 | all_off(display); |
j3 | 0:6b0161c3e440 | 529 | wait_ms(100); |
j3 | 0:6b0161c3e440 | 530 | |
j3 | 0:6b0161c3e440 | 531 | quad_all_on(display, 1); |
j3 | 0:6b0161c3e440 | 532 | wait_ms(500); |
j3 | 0:6b0161c3e440 | 533 | shift_display_right(display, 32, 100); |
j3 | 0:6b0161c3e440 | 534 | shift_display_left(display, 32, 100); |
j3 | 0:6b0161c3e440 | 535 | all_off(display); |
j3 | 0:6b0161c3e440 | 536 | wait_ms(100); |
j3 | 0:6b0161c3e440 | 537 | |
j3 | 0:6b0161c3e440 | 538 | print_string(display, 1, "Maxim"); |
j3 | 0:6b0161c3e440 | 539 | wait_ms(100); |
j3 | 0:6b0161c3e440 | 540 | shift_display_right(display, 32, 100); |
j3 | 0:6b0161c3e440 | 541 | shift_display_left(display, 32, 100); |
j3 | 0:6b0161c3e440 | 542 | wait_ms(100); |
j3 | 0:6b0161c3e440 | 543 | } |
j3 | 0:6b0161c3e440 | 544 | while(endless_loop); |
j3 | 0:6b0161c3e440 | 545 | } |
j3 | 0:6b0161c3e440 | 546 | |
j3 | 0:6b0161c3e440 | 547 | |
j3 | 0:6b0161c3e440 | 548 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 549 | void endless_scroll_display(Max7219 *display, uint32_t scroll_right) |
j3 | 0:6b0161c3e440 | 550 | { |
j3 | 0:6b0161c3e440 | 551 | if(scroll_right) |
j3 | 0:6b0161c3e440 | 552 | { |
j3 | 0:6b0161c3e440 | 553 | printf("\nScrolling Right\n"); |
j3 | 0:6b0161c3e440 | 554 | while(1) |
j3 | 0:6b0161c3e440 | 555 | { |
j3 | 0:6b0161c3e440 | 556 | shift_display_right(display, 1, 100); |
j3 | 0:6b0161c3e440 | 557 | } |
j3 | 0:6b0161c3e440 | 558 | } |
j3 | 0:6b0161c3e440 | 559 | else |
j3 | 0:6b0161c3e440 | 560 | { |
j3 | 0:6b0161c3e440 | 561 | printf("\nScrolling Left\n"); |
j3 | 0:6b0161c3e440 | 562 | while(1) |
j3 | 0:6b0161c3e440 | 563 | { |
j3 | 0:6b0161c3e440 | 564 | shift_display_left(display, 1, 100); |
j3 | 0:6b0161c3e440 | 565 | } |
j3 | 0:6b0161c3e440 | 566 | } |
j3 | 0:6b0161c3e440 | 567 | } |
j3 | 0:6b0161c3e440 | 568 | |
j3 | 0:6b0161c3e440 | 569 | |
j3 | 0:6b0161c3e440 | 570 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 571 | uint32_t print_menu(void) |
j3 | 0:6b0161c3e440 | 572 | { |
j3 | 0:6b0161c3e440 | 573 | uint32_t user_input; |
j3 | 0:6b0161c3e440 | 574 | |
j3 | 0:6b0161c3e440 | 575 | printf("\nPlease select an option below:\n"); |
j3 | 0:6b0161c3e440 | 576 | printf("%t1. Set Brightness\n"); |
j3 | 0:6b0161c3e440 | 577 | printf("%t2. Print a characther to given position\n"); |
j3 | 0:6b0161c3e440 | 578 | printf("%t3. Print a string to position\n");//TODO fix this case |
j3 | 0:6b0161c3e440 | 579 | printf("%t4. Shift display given number of position and direction\n"); //TODO fix this case |
j3 | 0:6b0161c3e440 | 580 | printf("%t5. Clear display\n"); |
j3 | 0:6b0161c3e440 | 581 | printf("%t6. Run the Demo Rotuine\n"); |
j3 | 0:6b0161c3e440 | 582 | printf("%t7. Run the Demo Rotuine In Endless Loop\n"); |
j3 | 0:6b0161c3e440 | 583 | printf("%t8. Scroll Display In Endless Loop, left or right\n"); |
j3 | 0:6b0161c3e440 | 584 | printf("%t9. Quit\n\n"); |
j3 | 0:6b0161c3e440 | 585 | |
j3 | 0:6b0161c3e440 | 586 | user_input = get_user_input(NULL, 9); |
j3 | 0:6b0161c3e440 | 587 | |
j3 | 0:6b0161c3e440 | 588 | return(user_input); |
j3 | 0:6b0161c3e440 | 589 | } |
j3 | 0:6b0161c3e440 | 590 | |
j3 | 0:6b0161c3e440 | 591 | |
j3 | 0:6b0161c3e440 | 592 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 593 | uint32_t get_user_input(char *msg, uint32_t max_val) |
j3 | 0:6b0161c3e440 | 594 | { |
j3 | 0:6b0161c3e440 | 595 | uint32_t user_input; |
j3 | 0:6b0161c3e440 | 596 | |
j3 | 0:6b0161c3e440 | 597 | do |
j3 | 0:6b0161c3e440 | 598 | { |
j3 | 0:6b0161c3e440 | 599 | printf(msg); |
j3 | 0:6b0161c3e440 | 600 | |
j3 | 0:6b0161c3e440 | 601 | //get user input |
j3 | 0:6b0161c3e440 | 602 | fgets(str, MAX_STRING_LENGTH, stdin); |
j3 | 0:6b0161c3e440 | 603 | |
j3 | 0:6b0161c3e440 | 604 | //Remove trailing newline and CR, if there. |
j3 | 0:6b0161c3e440 | 605 | if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D)) |
j3 | 0:6b0161c3e440 | 606 | { |
j3 | 0:6b0161c3e440 | 607 | str[strlen(str) - 1] = '\0'; |
j3 | 0:6b0161c3e440 | 608 | str[strlen(str) - 1] = '\0'; |
j3 | 0:6b0161c3e440 | 609 | } |
j3 | 0:6b0161c3e440 | 610 | |
j3 | 0:6b0161c3e440 | 611 | user_input = strtoul(str, NULL, 0); |
j3 | 0:6b0161c3e440 | 612 | |
j3 | 0:6b0161c3e440 | 613 | printf("\nYou entered = %d\n", user_input); |
j3 | 0:6b0161c3e440 | 614 | |
j3 | 0:6b0161c3e440 | 615 | if(user_input > max_val) |
j3 | 0:6b0161c3e440 | 616 | { |
j3 | 0:6b0161c3e440 | 617 | printf("\nOut of range\n"); |
j3 | 0:6b0161c3e440 | 618 | } |
j3 | 0:6b0161c3e440 | 619 | |
j3 | 0:6b0161c3e440 | 620 | } |
j3 | 0:6b0161c3e440 | 621 | while(user_input > max_val); |
j3 | 0:6b0161c3e440 | 622 | |
j3 | 0:6b0161c3e440 | 623 | return(user_input); |
j3 | 0:6b0161c3e440 | 624 | } |
j3 | 0:6b0161c3e440 | 625 | |
j3 | 0:6b0161c3e440 | 626 | |
j3 | 0:6b0161c3e440 | 627 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 628 | char get_user_char(char *msg) |
j3 | 0:6b0161c3e440 | 629 | { |
j3 | 0:6b0161c3e440 | 630 | |
j3 | 0:6b0161c3e440 | 631 | do |
j3 | 0:6b0161c3e440 | 632 | { |
j3 | 0:6b0161c3e440 | 633 | printf(msg); |
j3 | 0:6b0161c3e440 | 634 | |
j3 | 0:6b0161c3e440 | 635 | //get user input |
j3 | 0:6b0161c3e440 | 636 | fgets(str, 2, stdin); |
j3 | 0:6b0161c3e440 | 637 | |
j3 | 0:6b0161c3e440 | 638 | //Remove trailing newline and CR, if there. |
j3 | 0:6b0161c3e440 | 639 | if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D)) |
j3 | 0:6b0161c3e440 | 640 | { |
j3 | 0:6b0161c3e440 | 641 | str[strlen(str) - 1] = '\0'; |
j3 | 0:6b0161c3e440 | 642 | str[strlen(str) - 1] = '\0'; |
j3 | 0:6b0161c3e440 | 643 | } |
j3 | 0:6b0161c3e440 | 644 | |
j3 | 0:6b0161c3e440 | 645 | printf("\nYou entered = %c\n", str[0]); |
j3 | 0:6b0161c3e440 | 646 | |
j3 | 0:6b0161c3e440 | 647 | if((str[0] < 0x20) || (str[0] > 0x7E)) |
j3 | 0:6b0161c3e440 | 648 | { |
j3 | 0:6b0161c3e440 | 649 | printf("\nOut of range\n"); |
j3 | 0:6b0161c3e440 | 650 | } |
j3 | 0:6b0161c3e440 | 651 | } |
j3 | 0:6b0161c3e440 | 652 | while((str[0] < 0x20) || (str[0] > 0x7E)); |
j3 | 0:6b0161c3e440 | 653 | |
j3 | 0:6b0161c3e440 | 654 | return(str[0]); |
j3 | 0:6b0161c3e440 | 655 | } |
j3 | 0:6b0161c3e440 | 656 | |
j3 | 0:6b0161c3e440 | 657 | |
j3 | 0:6b0161c3e440 | 658 | //********************************************************************* |
j3 | 0:6b0161c3e440 | 659 | char * get_user_string(char *msg) |
j3 | 0:6b0161c3e440 | 660 | { |
j3 | 0:6b0161c3e440 | 661 | printf(msg); |
j3 | 0:6b0161c3e440 | 662 | //get user input |
j3 | 0:6b0161c3e440 | 663 | fgets(str, MAX_STRING_LENGTH, stdin); |
j3 | 0:6b0161c3e440 | 664 | //Remove trailing newline and CR, if there. |
j3 | 0:6b0161c3e440 | 665 | if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D)) |
j3 | 0:6b0161c3e440 | 666 | { |
j3 | 0:6b0161c3e440 | 667 | str[strlen(str) - 1] = '\0'; |
j3 | 0:6b0161c3e440 | 668 | str[strlen(str) - 1] = '\0'; |
j3 | 0:6b0161c3e440 | 669 | } |
j3 | 0:6b0161c3e440 | 670 | |
j3 | 0:6b0161c3e440 | 671 | return(str); |
j3 | 0:6b0161c3e440 | 672 | } |
j3 | 0:6b0161c3e440 | 673 |