Subversion Repositories HomeAutomation

Rev

Rev 2044 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
928 linlun 1
 
2
#include "act_ks0108.h"
3
 
2222 linlun 4
#if GRAPHICS_DRIVER!=DOTMATRIX
928 linlun 5
static unsigned char Splash_left[] PROGMEM = {
1409 linlun 6
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,//Line 1 
7
0xff,0xff,0xff,0x3,0x7f,0x7f,0xbf,0xbf,0xdf,0x7,0xff,0xff,0xff,0x3f,0xbf,0xdf,0xdf,0xdf,0x3f,0xff,0xff,0xff,0x3f,0x3f,0xdf,0x9f,0x7f,0x7f,0xbf,0x9f,0x3f,0xff,0xff,0x7f,0xbf,0xdf,0xdf,0x9f,0x3f,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0xc7,0xf3,0xf,0xff,0xff,0xff,0x1f,0xff,0xff,0xff,0xff,0x3f,0xdf,0xff,0xef,0xef,0xef,0x40,0x5,0xf7,0xff,0xff,0x3f,0xbf,0xdf,0xdf,0xdf,0x3f,0xff,0xff,0xff,0x1f,0x7f,0xdf,0x9f,0x7f,0x7f,0x9f,0xdf,0x3f,0xff,0xff,0xbf,0xbf,0xdf,0xdf,0xdf,0x3f,0xff,0xff,0xef,0xef,0xef,0x40,0x5,0xf7,0xff,0x1d,0xff,0xff,0xff,0xff,0x3f,0xbf,0xdf,0xdf,0xdf,0x3f,0xff,0xff,0xff,0x1f,0xff,0x7f,0x9f,0xdf,0x3f,0xff,0xff,0xff,0xff,0xff,//Line 2 
8
0xff,0xff,0xff,0xd0,0xff,0xff,0xff,0xff,0xff,0xf0,0xff,0xff,0xe0,0xef,0xdf,0xdf,0xcf,0xe3,0xf8,0xff,0xff,0xff,0xc0,0xfe,0xff,0xff,0xe0,0xfa,0xff,0xff,0xe0,0xff,0xff,0xf8,0xe6,0xee,0xee,0xf6,0xf7,0xfb,0xff,0xff,0xef,0xf3,0xfc,0xfe,0xfe,0xfe,0xff,0xf0,0xff,0xff,0xf4,0xcf,0xdf,0xe7,0xfb,0xf0,0xe7,0xff,0xff,0xff,0xff,0xff,0xe0,0xff,0xff,0xe0,0xef,0xdf,0xdf,0xcf,0xe3,0xf8,0xff,0xff,0xff,0xc0,0xfe,0xff,0xff,0xe0,0xfa,0xff,0xff,0xe0,0xff,0xff,0xf3,0xeb,0xed,0xed,0xf5,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xe0,0xff,0xff,0xe0,0xff,0xff,0xff,0xf0,0xcf,0xdf,0xdf,0xcf,0xe3,0xf8,0xff,0xff,0xff,0xf4,0xf9,0xfe,0xff,0xff,0xe0,0xff,0xff,0xff,0xff,0xff,//Line 3 
9
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xff,0xff,0xff,0xff,0xff,//Line 4 
10
0xff,0xff,0xff,0xff,0xff,0xdf,0xef,0xef,0xef,0x1f,0xff,0xff,0xf,0xff,0xff,0xff,0xff,0x3f,0xcf,0xff,0xff,0x1f,0x9f,0xef,0xcf,0x3f,0x3f,0xdf,0xcf,0x1f,0xff,0xff,0x40,0x2f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0x5f,0xef,0xef,0xef,0xff,0xff,0x3f,0x1f,0x6f,0x6f,0xcf,0x9f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0xcf,0xf3,0xfc,0xff,0xff,0xff,0x47,0xb7,0xf7,0xf7,0xf7,0x77,0x8f,0xff,0xff,0xf,0x7f,0x3f,0xdf,0xdf,0xff,0xff,0x7f,0x9f,0xdf,0xef,0xef,0xef,0x1f,0xff,0xff,0xff,0xff,0xdd,0x3f,0xff,0xff,0xff,0x3f,0x1f,0x6f,0x6f,0xcf,0x9f,0xff,0xff,0xfd,0x3,0x7f,0xbf,0xdf,0xef,0xff,0xff,0xf7,0xf7,0xf7,0xa0,0x2,0xfb,0xff,0xff,0xff,//Line 5 
11
0xff,0xff,0xff,0xff,0xf9,0xf6,0xf6,0xfa,0xfa,0xf8,0xff,0xff,0xf8,0xe7,0xef,0xf3,0xfd,0xfc,0xf1,0xff,0xff,0xe0,0xff,0xff,0xff,0xf0,0xfd,0xff,0xff,0xf0,0xff,0xff,0xff,0xf8,0xff,0xff,0xff,0xcf,0xc3,0xd3,0xe3,0xff,0xff,0xff,0xf7,0xe7,0xee,0xf6,0xf1,0xff,0xff,0xfc,0xf3,0xf7,0xf7,0xfb,0xfb,0xfd,0xff,0xff,0xff,0xcf,0xf3,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x80,0x5b,0xf9,0xfd,0xfe,0xff,0xff,0xff,0xf4,0xf8,0xff,0xff,0xff,0xff,0xff,0xf8,0xe7,0xef,0xef,0xe7,0xf1,0xfc,0xff,0xff,0xcf,0xbf,0xbf,0xc0,0xff,0xff,0xff,0xfc,0xfb,0xf7,0xf7,0xfb,0xfb,0xfd,0xff,0xff,0xf8,0xfd,0xfd,0xfb,0xfb,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,//Line 6 
12
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,//Line 7 
13
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0//Line 7 
928 linlun 14
};
2222 linlun 15
#endif
1409 linlun 16
 
17
 
18
 
1019 linlun 19
#define SOLID   0
20
#define TRANSPARENT 1
21
uint8_t color=GLCD_COLOR_BLACK;
22
uint8_t Transparent=SOLID;
928 linlun 23
 
1409 linlun 24
#if act_ks0108_USE_EXTERNAL_EEPROM==1
25
 
26
    void storeImage(uint8_t id, uint8_t size_x , uint8_t size_y);
27
 
28
    void saveImageFromScreen(uint8_t id, uint8_t size_x , uint8_t size_y);
29
 
30
    #define MEMORY_I2C_ADDR  0x57   // 64K EEPROM - allowed addresses are 0x50 to 0x57
31
    #define MEMORY_ADDR_MSB  0      // start at base of memory. Next one is 0x01.
32
    #define MEMORY_ADDR_LSB  0      // start on a page boundary. Next one is 0x80.
33
    unsigned char messageBuf[MESSAGEBUF_SIZE];
34
#endif
35
 
36
 
928 linlun 37
void numtoascii( int16_t num, char **str );
38
void signedtoascii(int16_t num, uint8_t decimalplace, char *string, uint8_t numberofdecimals);
39
void unsignedtoascii(uint16_t num, uint8_t decimalplace, char *string, uint8_t numberofdecimals);
40
 
1409 linlun 41
#if act_ks0108_USE_EXTERNAL_EEPROM==1
42
//valid id is 1 to 255;
43
void printImage(uint8_t id, uint8_t x_start, uint8_t y_start) {
44
    if (id == 0 )
45
        return;     //id=0 not allowed
46
    uint16_t address = 0;
47
    uint16_t buf_index = 0;
48
    uint8_t index;
49
    uint8_t size_x = 0;
50
    uint8_t size_y = 0;
51
    uint16_t max_picture_size;
52
    index = (id>>5)&0x07;
53
    //find starting adress of the requested data
54
    switch (index) {
55
        case 0:
56
            address = (index)*1024;
57
            max_picture_size = 1024;
58
            break;
59
        case 1:
60
            address = 32768+(index)*512;
61
            max_picture_size = 512;
62
            break;
63
        case 2:
64
            address = 32768+32*512+(index)*256;
65
            max_picture_size = 256;
66
            break;
67
        case 3:
68
            address = 32768+32*512+32*256+(index)*128;
69
            max_picture_size = 128;
70
            break;
71
        case 4:
72
            address = 32768+32*512+32*256+32*128+(index)*64;
73
            max_picture_size = 64;
74
            break;
75
        case 5:
76
            address = 32768+32*512+32*256+32*128+32*64+(index)*32;
77
            max_picture_size = 32;
78
            break;
79
        case 6:
80
            address = 32768+32*512+32*256+32*128+32*64+32*32+(index)*16;
81
            max_picture_size = 16;
82
            break;
83
        case 7:
84
            address = 32768+32*512+32*256+32*128+32*64+32*32+32*16+(index)*16;
85
            max_picture_size = 16;
86
            break;
87
    }
88
    //Get size of picture (every picture has an 4 byte info-tag)
89
    messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
90
    messageBuf[1] = (uint8_t)(0);       // Starting address in memory
91
    messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
92
    TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
93
    // Could do other actions in here, then get data.
94
    TWI_Read_Data_From_Buffer( messageBuf, 129 );
95
    size_x = messageBuf[((id&0x1f)<<2)];
96
    size_y = messageBuf[((id&0x1f)<<2)+1];
97
    //more_data = messageBuf[((id&0x1f)<<2)+2];
98
    //last_data = messageBuf[((id&0x1f)<<2)+3];
99
 
100
    // get data from eeprom
101
    messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
102
    messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
103
    messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
104
    TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
105
    // Could do other actions in here, then get data.
106
    TWI_Read_Data_From_Buffer( messageBuf, 129 );
107
    uint8_t jy = 0;
108
    uint8_t jx = 0;
109
    buf_index = 0;
110
    for (jy = 0; jy< size_y; jy+=8){
111
        glcdSetXY(x_start,y_start+jy);
112
        for (jx = 0; jx< size_x ; jx++){
113
            if (buf_index>=128) {   //get new data if buffer empty
114
                address +=128;
115
                messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
116
                messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
117
                messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
118
                TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
119
                // Could do other actions in here, then get data.
120
                TWI_Read_Data_From_Buffer( messageBuf, 129 );
121
                buf_index=0;
122
            }
123
            glcdWriteData(messageBuf[buf_index+1], GLCD_COLOR_CLEAR);
124
            buf_index++;
125
        }
126
    }
127
}
128
volatile uint8_t state;
129
volatile uint8_t lastId = 255;
130
#define IDLE    0
131
#define RECEIVING_DATA 1
132
void newImageData(StdCan_Msg_t *rxMsg) {
133
    //State machine for storing new data
134
    switch (state) {
135
        case IDLE:
136
            if (rxMsg->Header.Command == CAN_MODULE_CMD_KS0108_LCD_NEW_IMAGE)
137
            {
138
                storeImage(rxMsg->Data[0],rxMsg->Data[1],rxMsg->Data[2]);
139
                state = RECEIVING_DATA;
140
                lastId = 255;
141
                //response message
142
                rxMsg->Length = 1;
143
                rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_ACK;
144
                while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
145
            }else {
146
                //error message
147
                rxMsg->Length = 0;
148
                rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_ERROR;
149
                while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
150
            }
151
            break;
152
        case RECEIVING_DATA:
153
            if (rxMsg->Header.Command == CAN_MODULE_CMD_KS0108_LCD_IMAGE_DATA)
154
            {
155
                if (rxMsg->Data[0] == (lastId+1)) {
156
                    storeImage(rxMsg->Data[0],rxMsg->Data[1],rxMsg->Data[2]);
157
                    state = RECEIVING_DATA;
158
                    lastId++;
159
 
160
                    //FIXME: call function that handles the data
161
 
162
                    //response message
163
                    rxMsg->Length = 1;
164
                    rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_ACK;
165
                    while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
166
                } else {
167
                    //wrong id
168
                    rxMsg->Length = 1;
169
                    rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_DATA_ID_ERROR;
170
                    rxMsg->Data[0] = (lastId+1);
171
                    while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
172
                }
173
            } else if (rxMsg->Header.Command == CAN_MODULE_CMD_KS0108_LCD_DATA_DONE) {
174
                //Call function to flush eeprom cache
175
                //FIXME
176
                state=IDLE;
177
            } else {
178
                //error message
179
                rxMsg->Length = 0;
180
                rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_ERROR;
181
                while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
182
            }
183
            break;
184
    }
185
 
186
}
187
 
188
 
189
//valid id is 1 to 255;
190
void storeImage(uint8_t id, uint8_t size_x , uint8_t size_y) {
191
    if (id == 0 )
192
        return;     //id=0 not allowed
193
    //uint16_t address = 0;
194
    //uint16_t buf_index = 0;
195
    uint8_t index;
196
    //uint16_t max_picture_size;
197
    index = (id>>5)&0x07;
198
    //find starting adress of the requested data
199
    //Get size of picture (every picture has an 4 byte info-tag)
200
    messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
201
    messageBuf[1] = (uint8_t)(0);       // Starting address in memory
202
    messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
203
    TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
204
    // Could do other actions in here, then get data.
205
    TWI_Read_Data_From_Buffer( messageBuf, 129 );
206
    messageBuf[((id&0x1f)<<2)+1] = size_x;
207
    messageBuf[((id&0x1f)<<2)+2] = size_y;
208
    //more_data = messageBuf[((id&0x1f)<<2)+2];
209
    //last_data = messageBuf[((id&0x1f)<<2)+3];
210
 
211
    //move data in the buffer
212
    for (index = 128; index > 0; index--) {
213
        messageBuf[index+2] = messageBuf[index];
214
    }
215
    messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
216
    messageBuf[1] = (uint8_t)(0);       // Starting address in memory
217
    messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
218
    TWI_Start_Read_Write( messageBuf, 131 );
219
    // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
220
    while ( TWI_Transceiver_Busy() );    // Check for an error.
221
 
222
    //Now the info-tag is written, only to add data left
223
 
224
    //FIXME........ FIX!!!
225
 
226
}
227
 
228
 
229
//valid id is 1 to 255;
230
void saveImageFromScreen(uint8_t id, uint8_t size_x , uint8_t size_y) {
231
    if (id == 0 )
232
        return;     //id=0 not allowed
233
    uint16_t max_picture_size;
234
    uint8_t index;
235
    uint8_t bufferIndex;
236
    uint16_t address = 0;
237
    index = (id>>5)&0x07;
238
    //find starting adress of the requested data
239
    switch (index) {
240
        case 0:
241
            address = (index)*1024;
242
            max_picture_size = 1024;
243
            break;
244
        case 1:
245
            address = 32768+(index)*512;
246
            max_picture_size = 512;
247
            break;
248
        case 2:
249
            address = 32768+32*512+(index)*256;
250
            max_picture_size = 256;
251
            break;
252
        case 3:
253
            address = 32768+32*512+32*256+(index)*128;
254
            max_picture_size = 128;
255
            break;
256
        case 4:
257
            address = 32768+32*512+32*256+32*128+(index)*64;
258
            max_picture_size = 64;
259
            break;
260
        case 5:
261
            address = 32768+32*512+32*256+32*128+32*64+(index)*32;
262
            max_picture_size = 32;
263
            break;
264
        case 6:
265
            address = 32768+32*512+32*256+32*128+32*64+32*32+(index)*16;
266
            max_picture_size = 16;
267
            break;
268
        case 7:
269
            address = 32768+32*512+32*256+32*128+32*64+32*32+32*16+(index)*16;
270
            max_picture_size = 16;
271
            break;
272
    }
273
    //Get size of picture (every picture has an 4 byte info-tag)
274
    messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
275
    messageBuf[1] = (uint8_t)(0);       // Starting address in memory
276
    messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
277
    TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
278
    // Could do other actions in here, then get data.
279
    TWI_Read_Data_From_Buffer( messageBuf, 129 );
280
    messageBuf[((id&0x1f)<<2)+1] = size_x;
281
    messageBuf[((id&0x1f)<<2)+2] = size_y;
282
 
283
    //move data in the buffer
284
    for (index = 128; index > 0; index--) {
285
        messageBuf[index+2] = messageBuf[index];
286
    }
287
    messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
288
    messageBuf[1] = (uint8_t)(0);       // Starting address in memory
289
    messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
290
    TWI_Start_Read_Write( messageBuf, 131 );
291
    // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
292
    while ( TWI_Transceiver_Busy() );    // Check for an error.
293
 
294
    //Now the info-tag is written, only to add data left
295
    uint8_t  h, i ;
296
 
297
 
298
    messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
299
    messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
300
    messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
301
    bufferIndex = 3;
302
 
303
    h = 0;
304
    while(h+8 <= size_y) {
305
        h += 8;
306
        glcdSetXY(0, h);
307
 
308
        for(i=0; i<=size_x; i++) {
309
            messageBuf[bufferIndex] = glcdReadData();
310
            bufferIndex++;
311
            if (bufferIndex >= 131) {
312
                TWI_Start_Read_Write( messageBuf, 131 );
313
                // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
314
                while ( TWI_Transceiver_Busy() );    // Check for an error.
315
                bufferIndex = 3;
316
                address+=128;
317
                messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
318
                messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
319
                messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
320
            }
321
        }
322
    }
323
}
324
#endif
325
 
2024 arune 326
#if GRAPHICS_DRIVER==DOTMATRIX
327
/* Refresh display */
328
void act_ks0108_Refresh(uint8_t timer)
329
{
330
    glcdRefresh();
331
}
332
#endif
333
 
928 linlun 334
void act_ks0108_Init(void)
335
{
1809 arune 336
    /* Set up PWM controlling brightness */
337
#if GRAPHICS_DRIVER==KS0108
1187 linlun 338
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00);
339
    TCCR0B |= (1<<CS00);
1188 linlun 340
    OCR0A = act_ks0108_INITIAL_BACKLIGHT;
1187 linlun 341
    DDRD |= (1<<PD6);
1809 arune 342
#endif
343
#if GRAPHICS_DRIVER==DOTMATRIX
344
    TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
345
    TCCR0B |= (1<<CS00);
346
    OCR0B = 0xff-act_ks0108_INITIAL_BACKLIGHT;
347
    DDRD |= (1<<PD5);
2016 arune 348
 
349
    /* Start timer for row management */
2024 arune 350
    // TODO polled from process() or callback??
351
    Timer_SetTimeout(act_ks0108_DOTMATRIX_TIMER, 1, TimerTypeFreeRunning, &act_ks0108_Refresh);
1809 arune 352
#endif
928 linlun 353
 
1409 linlun 354
 
355
#if act_ks0108_USE_EXTERNAL_EEPROM==1
356
    TWI_Master_Initialise();
357
    uint8_t temp = 0;
358
    /// Do initial write to EEPROM. Write cycle takes 5 msecs, so flashing the led will delay enough.
359
    //
360
 
361
    messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
362
    messageBuf[1] = (((1024+128)>>8)&0xff);             // Starting address MSB
363
    messageBuf[2] = ((1024+128)&0xff);              // Starting address LSB
364
 
365
    for (temp=0; temp<128; temp++)
366
    {
367
        messageBuf[temp+3] = 128-temp; 
368
    }
369
    TWI_Start_Read_Write( messageBuf, 131 );
370
    // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
371
    while ( TWI_Transceiver_Busy() );    // Check for an error.
372
 
373
    //delay 10ms
374
    uint32_t temp2;
375
    temp2 =Timer_GetTicks();
376
    while (Timer_GetTicks() < temp2 + 10);
377
 
378
    #define TEMPID 0
379
    uint16_t address = (TEMPID+1)*1024; //only addresses on page border is allowed
380
    uint16_t ixa = 0;
381
    uint8_t jxa = 0;
382
    for (jxa = 0; jxa< 8; jxa++){
383
        for (ixa = 0; ixa < 128; ixa++){
384
            messageBuf[ixa+3] = (((uint8_t)pgm_read_byte((uint16_t)&Splash_left+ixa+jxa*128)) ^ 0xff);
385
        }
386
        messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
387
        messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
388
        messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
389
        TWI_Start_Read_Write( messageBuf, 131 );
390
        // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
391
        while ( TWI_Transceiver_Busy() );    // Check for an error.
392
        //delay 10ms
393
        temp2 =Timer_GetTicks();
394
        while (Timer_GetTicks() < temp2 + 10);
395
        address += 128;
396
    }
397
 
398
    address = (TEMPID+2)*1024;  //only addresses on page border is allowed
399
    ixa = 0;
400
    jxa = 0;
401
    for (jxa = 0; jxa< 8; jxa++){
402
        for (ixa = 0; ixa < 128; ixa++){
403
            messageBuf[ixa+3] = (uint8_t)pgm_read_byte((uint16_t)&Splash_left+ixa+jxa*128);
404
        }
405
        messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
406
        messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
407
        messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
408
        TWI_Start_Read_Write( messageBuf, 131 );
409
        // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
410
        while ( TWI_Transceiver_Busy() );    // Check for an error.
411
        //delay 10ms
412
        temp2 =Timer_GetTicks();
413
        while (Timer_GetTicks() < temp2 + 10);
414
        address += 128;
415
    }
416
    for (temp=0; temp<131; temp++)
417
    {
418
        messageBuf[temp] = 0;  
419
    }
420
 
421
#endif
422
 
1789 arune 423
    glcdInit();
928 linlun 424
 
1409 linlun 425
#if act_ks0108_USE_EXTERNAL_EEPROM==0
2222 linlun 426
#if GRAPHICS_DRIVER!=DOTMATRIX
1006 linlun 427
    uint16_t ixa = 0;
428
    uint8_t jxa = 0;
928 linlun 429
    for (jxa = 0; jxa< 8; jxa++){
1017 linlun 430
        glcdSetXY(0,jxa*8);
928 linlun 431
        for (ixa = 0; ixa < 128; ixa++){
1017 linlun 432
            glcdWriteData((uint8_t)pgm_read_byte((uint16_t)&Splash_left+ixa+jxa*128), GLCD_COLOR_CLEAR);
928 linlun 433
        }
434
    }
1409 linlun 435
#endif
2222 linlun 436
#endif
1409 linlun 437
 
438
#if act_ks0108_USE_EXTERNAL_EEPROM==1
439
    printImage(1,0,0);
440
    //delay 10ms
441
    temp2 =Timer_GetTicks();
442
    while (Timer_GetTicks() < temp2 + 5000);
443
    printImage(0,0,0);
444
#endif
1200 linlun 445
    //glcdSetXY(0,0);
446
    //glcdPutStrTransparent("=_-[]_-=",GLCD_COLOR_SET);
447
    //glcdSetXY(0,8);
448
    //glcdPutStrTransparent("--------",GLCD_COLOR_SET);
449
    //glcdSetXY(0,16);
450
    //glcdPutStr("-_=[]=-_",GLCD_COLOR_SET);
451
    //glcdDrawRect(32, 32, 40, 16, GLCD_COLOR_SET);
452
    //glcdDrawLine(10, 10, 100, 60, GLCD_COLOR_SET);
453
    //glcdFillRect(90, 10, 20, 20, GLCD_COLOR_SET);
454
    //glcdInvertRect(28, 10, 20, 20);
455
    //glcdInvert();
456
    //glcdDrawRoundRect(85, 5, 30, 30, 5, GLCD_COLOR_SET);
457
    //glcdDrawCircle(64, 32, 10, 1);
2222 linlun 458
#if GRAPHICS_DRIVER==DOTMATRIX
459
    glcdSetXY(5,0);
460
    glcdPutStrTransparent("auml",GLCD_COLOR_SET);
461
    glcdSetXY(1,8);
462
    glcdPutStrTransparent(" .se",GLCD_COLOR_SET);
463
#else
1200 linlun 464
    glcdSetXY(87,8);
465
    glcdPutStrTransparent("Home-",GLCD_COLOR_SET);
466
    glcdSetXY(80,16);
467
    glcdPutStrTransparent("Automa-",GLCD_COLOR_SET);
468
    glcdSetXY(91,24);
469
    glcdPutStrTransparent("ion",GLCD_COLOR_SET);
470
    //glcdDrawLine(10, 62, 50, 5, GLCD_COLOR_CLEAR);
2222 linlun 471
#endif
928 linlun 472
}
473
 
474
void act_ks0108_Process(void)
475
{
476
    ///TODO: Stuff that needs doing is done here
477
}
478
 
479
void act_ks0108_HandleMessage(StdCan_Msg_t *rxMsg)
480
{
1409 linlun 481
    //StdCan_Msg_t txMsg;
1006 linlun 482
    uint8_t n = 0;
483
 
484
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
485
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
486
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_KS0108 &&
487
        rxMsg->Header.ModuleId == act_ks0108_ID)
488
    {
489
    switch (rxMsg->Header.Command)
490
    {
491
        case CAN_MODULE_CMD_KS0108_LCD_CLEAR:
1019 linlun 492
          if (rxMsg->Length == 1) {
1038 linlun 493
            glcdSetColor((0x80&rxMsg->Data[0])>>7);
1019 linlun 494
          }
1006 linlun 495
          glcdClear();
1019 linlun 496
          rxMsg->Data[0] = glcdGetColor()<<7;
497
          StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
498
          rxMsg->Length = 1;
1409 linlun 499
          #if act_ks0108_USE_EXTERNAL_EEPROM==1
500
              uint8_t temp = 0;
501
              //    Read the data from Memory. Value of read/write bit doesn't matter.
502
                messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
503
                messageBuf[1] = MEMORY_ADDR_MSB;       // Starting address in memory
504
                messageBuf[2] = MEMORY_ADDR_LSB;       // Starting address in memory
505
                TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
506
                // Could do other actions in here, then get data.
507
                temp = TWI_Read_Data_From_Buffer( messageBuf, 129 );
508
              rxMsg->Length = 6;
509
              rxMsg->Data[1] = temp;
510
              rxMsg->Data[2] = messageBuf[0+3];
511
              rxMsg->Data[3] = messageBuf[0+4];
512
              rxMsg->Data[4] = messageBuf[0+5];
513
              rxMsg->Data[5] = messageBuf[0+80];
514
          #endif
1019 linlun 515
          while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
1006 linlun 516
          break;
1019 linlun 517
        case CAN_MODULE_CMD_KS0108_LCD_INVERT:
518
          if (rxMsg->Length == 1) {
1038 linlun 519
            if ((0x80&rxMsg->Data[0])>>7 != glcdGetColor())
1019 linlun 520
                glcdInvert();
521
          } else
522
            glcdInvert();
523
          rxMsg->Data[0] = glcdGetColor()<<7;
524
          StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
525
          rxMsg->Length = 1;
526
          while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
527
          break;
528
        case CAN_MODULE_CMD_KS0108_LCD_CURSOR:
529
          if (rxMsg->Length == 2) {
1031 linlun 530
            glcdSetXY((0x3f&rxMsg->Data[0])*6, rxMsg->Data[1]*8);
1038 linlun 531
            color = ((0x80&rxMsg->Data[2])>>7);
532
            Transparent = ((0x40&rxMsg->Data[2])>>6);
1019 linlun 533
          }
534
          rxMsg->Data[0] = glcdGetColor()<<7;
535
          rxMsg->Data[0] = Transparent<<6;
536
          rxMsg->Data[0] |= (0x3f & (glcdGetX()/6));
537
          rxMsg->Data[1] |= ((glcdGetY()/8));
538
          StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
539
          rxMsg->Length = 2;
540
          while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
1006 linlun 541
 
542
          break;
543
 
544
        case CAN_MODULE_CMD_KS0108_LCD_TEXTAT:
1017 linlun 545
          glcdSetXY((0x3f&rxMsg->Data[0])*6, rxMsg->Data[1]*8);
1006 linlun 546
          for (n = 2; n < rxMsg->Length; n++)
547
          {
1038 linlun 548
              if (((0x40&rxMsg->Data[0])>>6)== TRANSPARENT)
549
                glcdWriteCharTransparent((char)rxMsg->Data[n], (0x80&rxMsg->Data[0])>>7);
1019 linlun 550
              else
1038 linlun 551
                glcdWriteChar((char)rxMsg->Data[n], (0x80&rxMsg->Data[0])>>7);
1006 linlun 552
          }
553
        break;
554
 
555
        case CAN_MODULE_CMD_KS0108_LCD_TEXT:
556
        for (n = 0; n < rxMsg->Length; n++)
557
        {
1019 linlun 558
            if (Transparent == TRANSPARENT)
559
                glcdWriteCharTransparent((char)rxMsg->Data[n], color);
560
            else
561
                glcdWriteChar((char)rxMsg->Data[n], color);
1006 linlun 562
        }
563
        break;
564
 
565
        case CAN_MODULE_CMD_KS0108_LCD_BACKLIGHT:
1187 linlun 566
            if (rxMsg->Length > 0) {
1409 linlun 567
                if ( rxMsg->Data[0] == 0) {
2043 linlun 568
                    #if GRAPHICS_DRIVER==KS0108
1409 linlun 569
                    TCCR0A &= ~(1<<COM0A0);
570
                    TCCR0A &= ~(1<<COM0A1);
571
                    PORTD &= ~(1<<PD6);
572
                    OCR0A = rxMsg->Data[0];
2043 linlun 573
                    #endif
574
                        #if GRAPHICS_DRIVER==DOTMATRIX
2044 linlun 575
                    TCCR0A &= ~(1<<COM0B0);
576
                    TCCR0A &= ~(1<<COM0B1);
2043 linlun 577
                    PORTD |= (1<<PD5);
578
                    OCR0B = 0xff-rxMsg->Data[0];
579
                    #endif
1409 linlun 580
                } else {
2043 linlun 581
                    #if GRAPHICS_DRIVER==KS0108
1409 linlun 582
                    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00);
583
                    OCR0A = rxMsg->Data[0];
2043 linlun 584
                    #endif
585
                        #if GRAPHICS_DRIVER==DOTMATRIX
2044 linlun 586
                    TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
2043 linlun 587
                    OCR0B = 0xff-rxMsg->Data[0];
588
                    #endif
1409 linlun 589
                }
590
 
1187 linlun 591
            }
592
            StdCan_Msg_t txMsg;
593
            StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
594
            StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
595
            txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_KS0108;
596
            txMsg.Header.ModuleId = act_ks0108_ID;
597
            txMsg.Header.Command = CAN_MODULE_CMD_KS0108_LCD_BACKLIGHT;
598
            txMsg.Length = 1;
599
            txMsg.Data[0] = OCR0A;
600
            while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
1006 linlun 601
        break;
1031 linlun 602
 
603
        case CAN_MODULE_CMD_KS0108_LCD_DRAWRECT:
1038 linlun 604
          if ((0x40&rxMsg->Data[0])>>6)
605
            glcdFillRect(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], rxMsg->Data[4], (0x80&rxMsg->Data[0])>>7);
1031 linlun 606
          else
607
            if (rxMsg->Data[5] == 0)
1038 linlun 608
                glcdDrawRect(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], rxMsg->Data[4], (0x80&rxMsg->Data[0])>>7);
1031 linlun 609
            else
1038 linlun 610
                glcdDrawRoundRect(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], rxMsg->Data[4], rxMsg->Data[5],(0x80&rxMsg->Data[0])>>7);
1031 linlun 611
          break;
612
 
613
        case CAN_MODULE_CMD_KS0108_LCD_DRAWLINE:
1038 linlun 614
          glcdDrawLine(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], rxMsg->Data[4], (0x80&rxMsg->Data[0])>>7);
1031 linlun 615
          break;
616
 
617
        case CAN_MODULE_CMD_KS0108_LCD_DRAWCIRCLE:
1038 linlun 618
          glcdDrawCircle(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], (0x80&rxMsg->Data[0])>>7);
1031 linlun 619
          break;
620
 
621
        case CAN_MODULE_CMD_KS0108_LCD_INVERTRECT:
622
          glcdInvertRect(rxMsg->Data[0], rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3]);
623
          break;
624
 
1006 linlun 625
        }
626
    }
627
/*
928 linlun 628
    static uint8_t saer=0;
1005 linlun 629
    static uint8_t saer2=0;
928 linlun 630
    saer++;
1005 linlun 631
    if (saer>10) {
632
        saer2++;
1006 linlun 633
        uint8_t ixa = 0;
634
        uint8_t jxa = 0;
1005 linlun 635
        for (jxa = 0; jxa< 8; jxa++){
636
            glcdSetXY(0,jxa);
637
            for (ixa = 0; ixa < 128; ixa++){
638
                glcdWriteData((uint8_t)pgm_read_byte((uint16_t)&Splash_left+ixa+jxa*128));
639
            }
928 linlun 640
        }
1005 linlun 641
        saer=0;
642
        glcdSetXY(83,1);
643
        glcdPutStr("Tycker ");
644
        glcdSetXY(83,2);
645
        glcdPutStr(" detta ");
646
        glcdSetXY(83,3);
647
        glcdPutStr("funkar!");
648
        glcdSetXY(83,4);
649
        char buffer[20];   
650
        //numtoascii((int16_t)saer2 ,*buffer);
651
        unsignedtoascii((int16_t)saer2,0,buffer,1);
652
        glcdPutStr(buffer);
928 linlun 653
    }
1006 linlun 654
*/
928 linlun 655
}
656
 
657
void act_ks0108_List(uint8_t ModuleSequenceNumber)
658
{
659
    StdCan_Msg_t txMsg;
660
 
953 runge 661
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
662
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
663
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_KS0108;
928 linlun 664
    txMsg.Header.ModuleId = act_ks0108_ID;
953 runge 665
    txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
928 linlun 666
    txMsg.Length = 6;
667
 
1910 arune 668
    uint32_t HwId=BIOS_GetHwId();
669
    txMsg.Data[0] = HwId&0xff;
670
    txMsg.Data[1] = (HwId>>8)&0xff;
671
    txMsg.Data[2] = (HwId>>16)&0xff;
672
    txMsg.Data[3] = (HwId>>24)&0xff;
928 linlun 673
 
674
    txMsg.Data[4] = NUMBER_OF_MODULES;
675
    txMsg.Data[5] = ModuleSequenceNumber;
676
 
967 runge 677
    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
928 linlun 678
}
679
 
680
//� la pengi. Skall libbifieras.
681
    void numtoascii( int16_t num, char **str ) {
682
        if( num==0 ) return;
683
        numtoascii( num/10, str );
684
        **str = '0' + (num%10);
685
        (*str)++;
686
    }
687
 
688
    void signedtoascii(int16_t num, uint8_t decimalplace, char *string, uint8_t numberofdecimals){ //decimalplace is number of decimals
689
        uint8_t i;
690
 
691
        if( num<0 ) {
692
            *(string++) = '-';
693
            num = -num;
694
        }
695
        numtoascii(num>>decimalplace, &string );
696
        if(numberofdecimals!=0) *(string++) = '.';
697
        for(i=0;i<numberofdecimals;i++) {
698
            num %= 1<<decimalplace;
699
            num *= 10;
700
            *(string++) = '0' + (num>>decimalplace);
701
        }
702
        *(string++) = 0;
703
    }
704
 
705
    void unsignedtoascii(uint16_t num, uint8_t decimalplace, char *string, uint8_t numberofdecimals){ //decimalplace is number of decimals
706
        uint8_t i;
707
        numtoascii(num>>decimalplace, &string );
708
        if(numberofdecimals!=0) *(string++) = '.';
709
        for(i=0;i<numberofdecimals;i++) {
710
            num %= 1<<decimalplace;
711
            num *= 10;
712
            *(string++) = '0' + (num>>decimalplace);
713
        }
714
        *(string++) = 0;
715
    }