Christian Dupaty / MODMAX7456

Fork of MODMAX7456 by Andy K

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX7456.cpp Source File

MAX7456.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #include "mbed.h"
00024 #include "MAX7456.h"
00025 
00026 #ifdef MAX7456_DEBUG
00027 #include "IOMACROS.h"
00028 extern Serial pc;
00029 #endif
00030 
00031 #define CS_ASSERT   _cs->write(0)
00032 #define CS_DEASSERT _cs->write(1)
00033 
00034 // This header should only be included once here in this
00035 // file as it compiles inline directly. Include within
00036 // the library namespace.
00037 #include "MAX7456CHARS.h"
00038 
00039 /* Map ASCII table to the MAX7456 character map.
00040    Note, the MAX7456 in-built character map is no where near the ascii
00041    table mapping and very few characters are available to map. Where 
00042    possible we create new characters for those we need that are missing
00043    from the MAX7456 that we want to use and also we create some special
00044    characters of our own that are not ASCII chars (crosshair for example).
00045    These additional character definitions are listed below the table. 
00046    Character maps we have create can be found in MAX7456CHARS.h */
00047 const unsigned char MAX7456_ascii[256] = {
00048 
00049     /* Regular ASCII table. */
00050     /*         00    01    02    03    04    05    06    07     08    09    0A    0B    0C    0D    0E    0F  */  
00051     /* 00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00052     /* 10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00053     /* 20 */ 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x46,  0x3F, 0x40, 0x00, 0x4d, 0x45, 0x49, 0x41, 0x47, 
00054     /* 30 */ 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,  0x08, 0x09, 0x44, 0x43, 0x4A, 0x00, 0x4B, 0x42,
00055     /* 40 */ 0x4C, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11,  0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 
00056     /* 50 */ 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21,  0x22, 0x23, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 
00057     /* 60 */ 0x46, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B,  0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33,
00058     /* 70 */ 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,  0x3C, 0x3D, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 
00059     
00060     /* Extended ASCII table. */
00061     /*         00    01    02    03    04    05    06    07     08    09    0A    0B    0C    0D    0E    0F  */  
00062     /* 80 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00063     /* 90 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00064     /* A0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00065     /* B0 */ 0xB0, 0x00, 0x00, 0xB3, 0xB4, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBF,   
00066     /* C0 */ 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00067     /* D0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0xD9, 0xDA, 0x00, 0x00, 0x00, 0x00, 0x00, 
00068     /* E0 */ 0xe0, 0xe1, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
00069     /* F0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00     
00070 };
00071 
00072 MAX7456::MAX7456(PinName mosi, PinName miso, PinName sclk, const char *name, PinName cs, PinName rst, PinName vsync)
00073 {
00074     init(mosi, miso, sclk, name, cs, rst, vsync);
00075 }
00076 
00077 MAX7456::MAX7456(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rst, PinName vsync)
00078 {
00079     init(mosi, miso, sclk, NULL, cs, rst, vsync);
00080 }
00081 
00082 MAX7456::~MAX7456()
00083 {
00084     delete(_ssp);
00085     delete(_cs);
00086     delete(_rst);
00087     delete(_vsync);
00088 }
00089 
00090 void
00091 MAX7456::init(PinName mosi, PinName miso, PinName sclk, const char *name, PinName cs, PinName rst, PinName vsync)
00092 {
00093     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00094     // Here code change !!!! "name" is not use in new library
00095     // Christian Dupaty 03/2016
00096     // original is :
00097     //_ssp = new SPI(mosi, miso, sclk, name);
00098     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00099     _ssp = new SPI(mosi, miso, sclk);
00100     _cs  = new DigitalOut(cs);
00101     _rst = new DigitalOut(rst);    
00102     if (vsync != NC) {
00103         _vsync = new InterruptIn(vsync);
00104         _vsync->fall(this, &MAX7456::vsync_isr);   
00105     }
00106     
00107     // Set default output signals.
00108     CS_DEASSERT;
00109     _rst->write(1);
00110    
00111     // Setup the SSP.
00112     _ssp->format(8,0);
00113     _ssp->frequency(25000000);
00114     
00115     // Reset the MAX7456
00116     reset();
00117     
00118     /* Write the custom CM map. */
00119     write(0, read(0) & 0xF7);
00120     for (int index = 0; custom_chars[index].ascii != 0; index++) {
00121         write_char_map(custom_chars[index].ascii, custom_chars[index].map);
00122     }
00123     
00124     wait_us(100000);
00125     
00126     /* Change the vertical offset. */
00127     vos(0x16);
00128     
00129     /* Set the blink rate. */
00130     //vm1((3 << 2) | 3);
00131     
00132     /* Enable display of OSD image. */
00133     vm0(0x48);
00134 }
00135 
00136 void 
00137 MAX7456::write(unsigned char address, unsigned char byte) 
00138 {
00139     CS_ASSERT;
00140  
00141     /* MAX7456 addresses are always less than 0x80 so if the
00142        address is > 0x7F then the caller is requesting an direct
00143        8bit data transfer. */
00144     if (address < 0x80) { _ssp->write(address); }
00145     
00146     _ssp->write(byte);
00147     CS_DEASSERT;
00148 }
00149 
00150 int 
00151 MAX7456::read(unsigned char address) 
00152 {
00153     int data;
00154     address |= 0x80;
00155     CS_ASSERT;
00156     _ssp->write(address);
00157     data = _ssp->write(0xFF);
00158     CS_DEASSERT;
00159     return data;
00160 }
00161 
00162 void
00163 MAX7456::reset(void)
00164 {
00165     _rst->write(0); 
00166     wait_us(100000);
00167     _rst->write(1);  
00168     wait_us(100000);
00169 }
00170 
00171 void
00172 MAX7456::clear_display(void)
00173 {
00174     dmm(1 << 2);
00175     while(dmm() & (1 << 2));
00176 }
00177 
00178 void 
00179 MAX7456::cursor(int x, int y) 
00180 {
00181     uint16_t pos = (y * 30) + x;
00182     dmah((pos >> 8) & 0x1);
00183     dmal(pos & 0xFF);    
00184 }
00185 
00186 void 
00187 MAX7456::convert_string(char *s) 
00188 {
00189     while(*(s)) {
00190         *(s) = MAX7456_ascii[*(s)];
00191         s++;
00192     }
00193 }
00194 
00195 int
00196 MAX7456::string(char *s) 
00197 {
00198     int len = 0;
00199     dmm(1 | (1 << 6)); /* Enable 8bit write */    
00200     while(*(s)) {
00201         write(0x80, MAX7456_ascii[*s++]);        
00202         len++;
00203     }
00204     write(0x80, 0xFF);
00205     return len;
00206 }
00207 
00208 void
00209 MAX7456::attributes_xyl(int x, int y, char *s, int len)
00210 {
00211     uint16_t pos = (y * 30) + x;
00212     dmah(((pos >> 8) & 0x1) | (1 << 1));
00213     dmdi(pos & 0xFF);    
00214     dmm(1 | (1 << 6)); /* Enable 8bit write */    
00215     while (len--) {
00216         write(0x80, *s++);
00217     }
00218     write(0x80, 0xFF);
00219 }
00220 
00221 int
00222 MAX7456::stringxy(int x, int y, char *s) 
00223 {
00224     cursor(x, y);
00225     return string(s);
00226 }
00227 
00228 int
00229 MAX7456::stringxy(int x, int y, char *s, uint8_t a) 
00230 {
00231     char *q = NULL;
00232     int len;
00233     cursor(x, y);
00234     len = string(s);
00235     q = (char *)malloc(len);
00236     if (!q) {
00237         return -1;
00238     }
00239     memset(q, a, len);
00240     attributes_xyl(x, y, q, len);
00241     free(q);
00242     return len;
00243 }
00244 
00245 int
00246 MAX7456::stringxy(int x, int y, char *s, char *a) 
00247 {
00248     int len;
00249     cursor(x, y);
00250     len = string(s);
00251     attributes_xyl(x, y, a, 30);
00252     return len;
00253 }
00254 
00255 void 
00256 MAX7456::stringl(int x, int y, char *s, int len) 
00257 {
00258     cursor(x, y);            
00259     dmm(1); /* Enable 8bit write */    
00260     while(len--) {
00261         if (*s == '\0') break;
00262         write(0x80, MAX7456_ascii[*s++]);
00263     }
00264     write(0x80, 0xFF);
00265 }
00266 
00267 void 
00268 MAX7456::read_char_map(unsigned char address, unsigned char *data54) 
00269 {
00270     cmah((uint8_t)address);
00271     cmm(0x50);
00272     for (uint8_t index = 0; index < 54; index++) {
00273         cmal(index);
00274         wait_us(1000);        
00275         *(data54 + index) = read(0xC0);
00276     }
00277 }
00278 
00279 void 
00280 MAX7456::write_char_map(unsigned char address, const unsigned char *data54) 
00281 {
00282     unsigned char index, c, match = 1;
00283     
00284     write(CMAH, address);
00285     write(CMM, 0x50);
00286     wait_us(20000);
00287     for (index = 0; index < 54; index++) {
00288         write(CMAL, index);
00289         c = read(0xC0);
00290         if (c != data54[index]) {
00291             match = 0;            
00292             break;
00293         }
00294     }
00295     
00296     if (!match) {   
00297         write(CMAH, address);
00298         for (index = 0; index < 0x36; index++) {
00299             write(CMAL, index);
00300             write(CMDI, data54[index]);
00301         }
00302         write(CMM, 0xA0);
00303         wait_us(20000);
00304         while ((read(0xA0) & 0x20) != 0x00);    
00305     }
00306 }
00307 
00308 
00309