This is a sample program to use an IS color switch from NKK. It reads a file from SD card and send to the switch. Supported file format is the one that is used by a NKK\'s editor for IS series switches. Part of this program is derived from a sample program for PIC micro cotnroller written by NKK.
Dependencies: mbed SDFileSystem
main.cpp@0:290c094a8d94, 2011-01-30 (annotated)
- Committer:
- non
- Date:
- Sun Jan 30 05:44:21 2011 +0000
- Revision:
- 0:290c094a8d94
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
non | 0:290c094a8d94 | 1 | /* |
non | 0:290c094a8d94 | 2 | Sample program to use IS color switch from NKK. |
non | 0:290c094a8d94 | 3 | Part of this program is derived from a sample program for PIC |
non | 0:290c094a8d94 | 4 | micro cotnroller written by NKK. |
non | 0:290c094a8d94 | 5 | */ |
non | 0:290c094a8d94 | 6 | #include "mbed.h" |
non | 0:290c094a8d94 | 7 | #include "SDFileSystem.h" |
non | 0:290c094a8d94 | 8 | |
non | 0:290c094a8d94 | 9 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
non | 0:290c094a8d94 | 10 | SPI spi(p11, p12, p13); // mosi(SDO), miso(SDI), sclk(SCLK) |
non | 0:290c094a8d94 | 11 | DigitalOut is_cs(p21); |
non | 0:290c094a8d94 | 12 | DigitalOut is_reset(p22); |
non | 0:290c094a8d94 | 13 | DigitalOut is_dc(p23); |
non | 0:290c094a8d94 | 14 | DigitalOut is_vcc(p24); |
non | 0:290c094a8d94 | 15 | DigitalOut led1(LED1); |
non | 0:290c094a8d94 | 16 | |
non | 0:290c094a8d94 | 17 | void OledAdrInit( int DevType ); |
non | 0:290c094a8d94 | 18 | void OledDspMode( int DspMode ); |
non | 0:290c094a8d94 | 19 | void OledInit( void ); |
non | 0:290c094a8d94 | 20 | void OledSendCommand( const unsigned char *com ); |
non | 0:290c094a8d94 | 21 | void OledSendData( const unsigned char *dat, int len ); |
non | 0:290c094a8d94 | 22 | |
non | 0:290c094a8d94 | 23 | const int DAT = 1; |
non | 0:290c094a8d94 | 24 | const int CMD = 0; |
non | 0:290c094a8d94 | 25 | int DevType = 1; // 0:ISC01, 1:ISC15 |
non | 0:290c094a8d94 | 26 | int DspMode = 1; // 0:256, 1:64k colors |
non | 0:290c094a8d94 | 27 | |
non | 0:290c094a8d94 | 28 | unsigned char dat[64*48*2]; |
non | 0:290c094a8d94 | 29 | const unsigned char cls[64*48*2] = {0}; |
non | 0:290c094a8d94 | 30 | |
non | 0:290c094a8d94 | 31 | int main() { |
non | 0:290c094a8d94 | 32 | // Setup the spi for 8 bit data, high steady state clock, |
non | 0:290c094a8d94 | 33 | // second edge capture, with a 4MHz clock rate |
non | 0:290c094a8d94 | 34 | spi.format(8, 3); |
non | 0:290c094a8d94 | 35 | spi.frequency(4000000); |
non | 0:290c094a8d94 | 36 | |
non | 0:290c094a8d94 | 37 | OledInit(); |
non | 0:290c094a8d94 | 38 | |
non | 0:290c094a8d94 | 39 | #if 1 |
non | 0:290c094a8d94 | 40 | for (;;) { |
non | 0:290c094a8d94 | 41 | DIR *d; |
non | 0:290c094a8d94 | 42 | struct dirent *p; |
non | 0:290c094a8d94 | 43 | d = opendir("/sd"); |
non | 0:290c094a8d94 | 44 | if (d != NULL) { |
non | 0:290c094a8d94 | 45 | while ((p = readdir(d)) != NULL) { |
non | 0:290c094a8d94 | 46 | char fname[20]; |
non | 0:290c094a8d94 | 47 | sprintf(fname, "/sd/%s", p->d_name); |
non | 0:290c094a8d94 | 48 | FILE *fp = fopen(fname, "rb"); |
non | 0:290c094a8d94 | 49 | if (fp == NULL) { |
non | 0:290c094a8d94 | 50 | // error("Could not open file for read\n"); |
non | 0:290c094a8d94 | 51 | } else { |
non | 0:290c094a8d94 | 52 | fread(dat, 1, 8, fp); // skip header |
non | 0:290c094a8d94 | 53 | fread(dat, 1, sizeof(dat), fp); |
non | 0:290c094a8d94 | 54 | fclose(fp); |
non | 0:290c094a8d94 | 55 | OledAdrInit(DevType); |
non | 0:290c094a8d94 | 56 | OledDspMode(DspMode); |
non | 0:290c094a8d94 | 57 | OledSendData(dat, 64*48*2); |
non | 0:290c094a8d94 | 58 | } |
non | 0:290c094a8d94 | 59 | } |
non | 0:290c094a8d94 | 60 | closedir(d); |
non | 0:290c094a8d94 | 61 | } |
non | 0:290c094a8d94 | 62 | } |
non | 0:290c094a8d94 | 63 | #else |
non | 0:290c094a8d94 | 64 | for (int i=0; i<64*48; i ++) { |
non | 0:290c094a8d94 | 65 | dat[i*2] = (i & 0xff00) >> 8; |
non | 0:290c094a8d94 | 66 | dat[i*2 + 1] = i & 0xff; |
non | 0:290c094a8d94 | 67 | } |
non | 0:290c094a8d94 | 68 | OledAdrInit(DevType); |
non | 0:290c094a8d94 | 69 | OledDspMode(DspMode); |
non | 0:290c094a8d94 | 70 | OledSendData(dat, 64*48*2); |
non | 0:290c094a8d94 | 71 | |
non | 0:290c094a8d94 | 72 | for (;;) { |
non | 0:290c094a8d94 | 73 | OledAdrInit(DevType); |
non | 0:290c094a8d94 | 74 | OledDspMode(DspMode); |
non | 0:290c094a8d94 | 75 | OledSendData(dat, 64*48*2); |
non | 0:290c094a8d94 | 76 | led1 = 0; |
non | 0:290c094a8d94 | 77 | wait (0.5); |
non | 0:290c094a8d94 | 78 | OledAdrInit(DevType); |
non | 0:290c094a8d94 | 79 | OledDspMode(DspMode); |
non | 0:290c094a8d94 | 80 | OledSendData(cls, 64*48*2); |
non | 0:290c094a8d94 | 81 | led1 = 1; |
non | 0:290c094a8d94 | 82 | wait (0.5); |
non | 0:290c094a8d94 | 83 | } |
non | 0:290c094a8d94 | 84 | #endif |
non | 0:290c094a8d94 | 85 | } |
non | 0:290c094a8d94 | 86 | |
non | 0:290c094a8d94 | 87 | // OLED's initial setup commands (IS15) |
non | 0:290c094a8d94 | 88 | const unsigned char SetupColumnAddress[] = { 0x03, 0x15, 0x10, 0x4f }; |
non | 0:290c094a8d94 | 89 | const unsigned char SetupRowaddress[] = { 0x03, 0x75, 0x00, 0x2f }; |
non | 0:290c094a8d94 | 90 | const unsigned char SetContrastColorA[] = { 0x02, 0x81, 0x19 }; |
non | 0:290c094a8d94 | 91 | const unsigned char SetContrastColorB[] = { 0x02, 0x82, 0x14 }; |
non | 0:290c094a8d94 | 92 | const unsigned char SetContrastColorC[] = { 0x02, 0x83, 0x24 }; |
non | 0:290c094a8d94 | 93 | const unsigned char MasterCurrentControl[] = { 0x02, 0x87, 0x0f }; |
non | 0:290c094a8d94 | 94 | const unsigned char RemapColorDepth[] = { 0x02, 0xa0, 0x70 }; |
non | 0:290c094a8d94 | 95 | const unsigned char SetDisplayStartLine[] = { 0x02, 0xa1, 0x00 }; |
non | 0:290c094a8d94 | 96 | const unsigned char SetDisplayOffset[] = { 0x02, 0xa2, 0x10 }; |
non | 0:290c094a8d94 | 97 | const unsigned char SetDisplayMode[] = { 0x01, 0xa4 }; |
non | 0:290c094a8d94 | 98 | const unsigned char SetMultiplexRatio[] = { 0x02, 0xa8, 0x2f }; |
non | 0:290c094a8d94 | 99 | const unsigned char DimModeSetting[] = { 0x05, 0xab, 0x12, 0x0c, 0x14, 0x12 }; |
non | 0:290c094a8d94 | 100 | const unsigned char SetDisplayDim[] = { 0x01, 0xac }; |
non | 0:290c094a8d94 | 101 | const unsigned char SetDisplayOff[] = { 0x01, 0xae }; |
non | 0:290c094a8d94 | 102 | const unsigned char SetDisplayNormal[] = { 0x01, 0xaf }; |
non | 0:290c094a8d94 | 103 | const unsigned char SetMasterConfiguration[] = { 0x02, 0xad, 0x8e }; |
non | 0:290c094a8d94 | 104 | const unsigned char PhasePeriodAdjustment[] = { 0x02, 0xb1, 0x44 }; |
non | 0:290c094a8d94 | 105 | const unsigned char DisplayClockDivider[] = { 0x02, 0xb3, 0xa0 }; |
non | 0:290c094a8d94 | 106 | const unsigned char EnableLinearGrayScale[] = { 0x01, 0xb9 }; |
non | 0:290c094a8d94 | 107 | const unsigned char SetPrechargeLevel[] = { 0x02, 0xbb, 0x12 }; |
non | 0:290c094a8d94 | 108 | const unsigned char SetVcomh[] = { 0x03, 0xbe, 0x28 }; |
non | 0:290c094a8d94 | 109 | |
non | 0:290c094a8d94 | 110 | const unsigned char ColorDepth64k[] = { 0x02, 0xa0, 0x70 }; |
non | 0:290c094a8d94 | 111 | const unsigned char ColorDepth256[] = { 0x02, 0xa0, 0x30 }; |
non | 0:290c094a8d94 | 112 | |
non | 0:290c094a8d94 | 113 | // OLED's initial setup commands (for display only type (IS01) ) |
non | 0:290c094a8d94 | 114 | const unsigned char SetupColumnAddress_d[] = { 0x03, 0x15, 0x16, 0x49 }; //display |
non | 0:290c094a8d94 | 115 | const unsigned char SetupRowaddress_d[] = { 0x03, 0x75, 0x00, 0x23 }; //display |
non | 0:290c094a8d94 | 116 | const unsigned char SetContrastColorA_d[] = { 0x02, 0x81, 0x0f }; //display |
non | 0:290c094a8d94 | 117 | const unsigned char SetContrastColorB_d[] = { 0x02, 0x82, 0x0e }; //display |
non | 0:290c094a8d94 | 118 | const unsigned char SetContrastColorC_d[] = { 0x02, 0x83, 0x1b }; //display |
non | 0:290c094a8d94 | 119 | const unsigned char SetDisplayOffset_d[] = { 0x02, 0xa2, 0x1c }; //display |
non | 0:290c094a8d94 | 120 | const unsigned char SetMultiplexRatio_d[] = { 0x02, 0xa8, 0x23 }; //display |
non | 0:290c094a8d94 | 121 | const unsigned char DimModeSetting_d[] = { 0x05, 0xab, 0x0b, 0x08, 0x0f, 0x12 }; //display |
non | 0:290c094a8d94 | 122 | const unsigned char DisplayClockDivider_d[] = { 0x02, 0xb3, 0x30 }; //display |
non | 0:290c094a8d94 | 123 | const unsigned char SetVcomh_d[] = { 0x03, 0xbe, 0x21 }; //display |
non | 0:290c094a8d94 | 124 | // |
non | 0:290c094a8d94 | 125 | // Change OLED's address setting |
non | 0:290c094a8d94 | 126 | // |
non | 0:290c094a8d94 | 127 | void OledAdrInit( int DevType ) { |
non | 0:290c094a8d94 | 128 | if (DevType) { |
non | 0:290c094a8d94 | 129 | OledSendCommand( SetupColumnAddress ); |
non | 0:290c094a8d94 | 130 | OledSendCommand( SetupRowaddress ); |
non | 0:290c094a8d94 | 131 | } else { |
non | 0:290c094a8d94 | 132 | OledSendCommand( SetupColumnAddress_d ); |
non | 0:290c094a8d94 | 133 | OledSendCommand( SetupRowaddress_d ); |
non | 0:290c094a8d94 | 134 | } |
non | 0:290c094a8d94 | 135 | } |
non | 0:290c094a8d94 | 136 | |
non | 0:290c094a8d94 | 137 | // |
non | 0:290c094a8d94 | 138 | // Change OLED's color depth setting (64K or 256 colors) |
non | 0:290c094a8d94 | 139 | // |
non | 0:290c094a8d94 | 140 | void OledDspMode( int DspMode ) { |
non | 0:290c094a8d94 | 141 | if ( DspMode ) { |
non | 0:290c094a8d94 | 142 | OledSendCommand( ColorDepth64k ); |
non | 0:290c094a8d94 | 143 | } else { |
non | 0:290c094a8d94 | 144 | OledSendCommand( ColorDepth256 ); |
non | 0:290c094a8d94 | 145 | } |
non | 0:290c094a8d94 | 146 | } |
non | 0:290c094a8d94 | 147 | |
non | 0:290c094a8d94 | 148 | // |
non | 0:290c094a8d94 | 149 | // initial settings of OLED |
non | 0:290c094a8d94 | 150 | // |
non | 0:290c094a8d94 | 151 | void OledInit( void ) { |
non | 0:290c094a8d94 | 152 | volatile unsigned char delay = 0x20; |
non | 0:290c094a8d94 | 153 | |
non | 0:290c094a8d94 | 154 | // reset OLED |
non | 0:290c094a8d94 | 155 | is_vcc = 0; |
non | 0:290c094a8d94 | 156 | is_reset = 0; |
non | 0:290c094a8d94 | 157 | wait_us(3); |
non | 0:290c094a8d94 | 158 | is_reset = 1; |
non | 0:290c094a8d94 | 159 | is_vcc = 1; |
non | 0:290c094a8d94 | 160 | |
non | 0:290c094a8d94 | 161 | // initial settings |
non | 0:290c094a8d94 | 162 | OledAdrInit(DevType); |
non | 0:290c094a8d94 | 163 | |
non | 0:290c094a8d94 | 164 | if (DevType) { |
non | 0:290c094a8d94 | 165 | OledSendCommand( SetContrastColorA ); |
non | 0:290c094a8d94 | 166 | OledSendCommand( SetContrastColorB ); |
non | 0:290c094a8d94 | 167 | OledSendCommand( SetContrastColorC ); |
non | 0:290c094a8d94 | 168 | OledSendCommand( MasterCurrentControl ); |
non | 0:290c094a8d94 | 169 | OledSendCommand( SetDisplayStartLine ); |
non | 0:290c094a8d94 | 170 | OledSendCommand( SetDisplayOffset ); |
non | 0:290c094a8d94 | 171 | OledSendCommand( SetDisplayMode ); |
non | 0:290c094a8d94 | 172 | OledSendCommand( SetMultiplexRatio ); |
non | 0:290c094a8d94 | 173 | OledSendCommand( DimModeSetting ); |
non | 0:290c094a8d94 | 174 | OledSendCommand( SetMasterConfiguration ); |
non | 0:290c094a8d94 | 175 | OledSendCommand( PhasePeriodAdjustment ); |
non | 0:290c094a8d94 | 176 | OledSendCommand( DisplayClockDivider ); |
non | 0:290c094a8d94 | 177 | OledSendCommand( EnableLinearGrayScale ); |
non | 0:290c094a8d94 | 178 | OledSendCommand( SetPrechargeLevel ); |
non | 0:290c094a8d94 | 179 | OledSendCommand( SetVcomh ); |
non | 0:290c094a8d94 | 180 | } else { |
non | 0:290c094a8d94 | 181 | OledSendCommand( SetContrastColorA_d ); |
non | 0:290c094a8d94 | 182 | OledSendCommand( SetContrastColorB_d ); |
non | 0:290c094a8d94 | 183 | OledSendCommand( SetContrastColorC_d ); |
non | 0:290c094a8d94 | 184 | OledSendCommand( MasterCurrentControl ); |
non | 0:290c094a8d94 | 185 | OledSendCommand( SetDisplayStartLine ); |
non | 0:290c094a8d94 | 186 | OledSendCommand( SetDisplayOffset_d ); |
non | 0:290c094a8d94 | 187 | OledSendCommand( SetDisplayMode ); |
non | 0:290c094a8d94 | 188 | OledSendCommand( SetMultiplexRatio_d ); |
non | 0:290c094a8d94 | 189 | OledSendCommand( DimModeSetting_d ); |
non | 0:290c094a8d94 | 190 | OledSendCommand( SetMasterConfiguration ); |
non | 0:290c094a8d94 | 191 | OledSendCommand( PhasePeriodAdjustment ); |
non | 0:290c094a8d94 | 192 | OledSendCommand( DisplayClockDivider_d ); |
non | 0:290c094a8d94 | 193 | OledSendCommand( EnableLinearGrayScale ); |
non | 0:290c094a8d94 | 194 | OledSendCommand( SetPrechargeLevel ); |
non | 0:290c094a8d94 | 195 | OledSendCommand( SetVcomh_d ); |
non | 0:290c094a8d94 | 196 | } |
non | 0:290c094a8d94 | 197 | |
non | 0:290c094a8d94 | 198 | // display mode settings |
non | 0:290c094a8d94 | 199 | OledDspMode(DspMode); |
non | 0:290c094a8d94 | 200 | |
non | 0:290c094a8d94 | 201 | OledSendCommand( SetDisplayNormal ); |
non | 0:290c094a8d94 | 202 | } |
non | 0:290c094a8d94 | 203 | |
non | 0:290c094a8d94 | 204 | // |
non | 0:290c094a8d94 | 205 | // Send a command / data to OLED via SPI |
non | 0:290c094a8d94 | 206 | // |
non | 0:290c094a8d94 | 207 | void OledSend( const unsigned char *p, int len ) { |
non | 0:290c094a8d94 | 208 | for (; len>0; len --, p ++) { |
non | 0:290c094a8d94 | 209 | is_cs = 0; |
non | 0:290c094a8d94 | 210 | spi.write(*p); |
non | 0:290c094a8d94 | 211 | is_cs = 1; |
non | 0:290c094a8d94 | 212 | } |
non | 0:290c094a8d94 | 213 | } |
non | 0:290c094a8d94 | 214 | |
non | 0:290c094a8d94 | 215 | // |
non | 0:290c094a8d94 | 216 | // Send a command to OLED |
non | 0:290c094a8d94 | 217 | // |
non | 0:290c094a8d94 | 218 | void OledSendCommand( const unsigned char *com ) { |
non | 0:290c094a8d94 | 219 | is_dc = CMD; |
non | 0:290c094a8d94 | 220 | OledSend(com+1, *com); |
non | 0:290c094a8d94 | 221 | } |
non | 0:290c094a8d94 | 222 | |
non | 0:290c094a8d94 | 223 | // |
non | 0:290c094a8d94 | 224 | // Send data to OLED |
non | 0:290c094a8d94 | 225 | // |
non | 0:290c094a8d94 | 226 | void OledSendData( const unsigned char *dat, int len ) { |
non | 0:290c094a8d94 | 227 | is_dc = DAT; |
non | 0:290c094a8d94 | 228 | OledSend(dat, len); |
non | 0:290c094a8d94 | 229 | } |