Wednesday, July 6, 2011

How to make bitmaps for graphic LCD (glcd)?

Making bitmaps for graphic LCD is pretty easy. There is a lot of free tools available over Internet, but I think there is no need to download them if you have already installed an image editing software in your PC. Also it is interesting to know what's actually they do. In this quick tutorial,  an example using GIMP - a free image manipulation program is described. This is a general tutorial and can be applicable for almost all type of graphical LCDs.

Step1: Know the pixel size of your graphic LCD. For KS0108 (a general purpose glcd), it is 128x64.
Step2: Choose an image(any image) to be embedded on LCD.
Step3: Scale the image to 128x64. In GIMP, (menu)Image -> Scale Image. Change height to 64 and width to 128.
If your image is not an exact multiple of 128x64 and if you want to keep the aspect ratio, you need to do any of the following:
a) Crop the image to 128x64.From toolBox(dock in the left hand side most probably), choose Rectangle Select Tool and spcify the size to 128x64 in the bottom of toolbox and select a portion you want and crop it. (menu)Image -> Crop to Selection.
b) Resize the image to less than or equal to 128x64. Then start a new image by (menu)File -> New. Then from the dialog box choose size as 128x64. Copy the previous image to the new image.
c) If you want to just check and image is not matter, image search on Google specifying the size 128x64 ;-)
Step4: Before going to this step, I assume you have an image of the same size of your LCD. In this example, 128x64. Next is the conversion of your color image to monochrome image. For that, choose, (menu)Colors -> Threshold. Using the dialog box, you can change the threshold in realtime.
Step5: Change mode.(menu)Image -> Mode -> Indexed.From the dialog box, choose 'Use Black and White (1bit) palette'. Click 'Convert'
Step6: Save the image.(menu)File -> Save as. Choose extension .bmp (bitmap).

Now your image is ready for embedding to your LCD. For some programs like MikroC for PIC, there is a built in tool to convert the bitmap into array. If you don't have such advanced tools, don't worry, let's do it using open source tools.
Using Octave/Matlab for KS0108
Now you have a monochrome bitmap of only zeros and ones. Now it should be grouped and formatted to a format supported by glcd. I did the conversion using octave. The script shown below accepts the bitmap name as argument and convert it to the glcd compatible form. make appropriate changes to run the script in MATLAB. Also change yourself the program if your lcd size is different.
#This code input a monochrome bitmap of size 128x64 and convert it to a form compatible to ks0108 graphic lcd(glcd)
arg_list=argv();
x=imread(arg_list{1}); #accept argument
num=0;
printf("Controller 1 (left)\n\n");
page=0;
for offset = [1:8:64]
    printf("Page %d:\n",page);
    page+=1;
    for j=1:64
        for i=[offset:offset+7]
            num=num+x(i,j)*(2^(i-offset));
        endfor
        printf("%d ",255-num);
        num=0;
    endfor
    printf("\n\n");
endfor
printf("Controller 2 (right)\n\n");
page=0;
for offset = [1:8:64]
    printf("Page %d:\n",page);
    page+=1;
    for j=65:128
        for i=[offset:offset+7]
            num=num+x(i,j)*(2^(i-offset));
        endfor
        printf("%d ",255-num);
        num=0;
    endfor
    printf("\n\n");
endfor

The output of above script when mickey2.bmp as the argument is given below:
Controller 1 (left)

Page 0:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 128 192 224 240 240 248 248 252 252 252 252 252 252 252 252 252 248 248 240 224 224 128 0 0 0 0 0 0 0 0 0 0 0 0

Page 1:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 240 252 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 240 0 128 128 128 192 192 192 192 192 192

Page 2:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 63 127 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 127 31 15 199 35 33 49 1 3 7 15 31 127

Page 3:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 3 7 7 15 15 15 15 15 15 15 15 143 255 255 255 255 255 15 0 0 0 0 1 0 128 112 8 8 8 16 224 0

Page 4:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 240 192 0 0 0 0 0 127 128 240 248 248 248 241 24

Page 5:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 195 255 255 15 207 103 119 247 151 4 0 0 0 0 9 119 255 251 255 239 231

Page 6:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 15 31 56 112 96 192 193 131 134 140 140 24 48 240 240 240 240 225 225 225

Page 7:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 3 3 7 6 14 29 59 54 108 108 109

Controller 2 (right)

Page 0:
0 0 0 0 0 0 0 0 0 0 0 128 224 240 248 252 252 254 254 254 254 254 254 254 254 254 254 252 252 248 248 240 224 192 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 1:
192 192 192 192 192 192 192 128 128 128 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 252 96 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 2:
127 15 7 7 3 1 49 49 99 195 7 15 63 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 127 31 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 3:
0 128 224 48 8 8 8 112 224 0 0 0 0 0 15 255 255 255 255 255 255 199 7 7 7 7 7 7 3 3 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 4:
0 63 255 248 248 248 240 96 63 0 0 0 0 128 240 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 5:
239 231 239 255 255 255 127 4 8 0 0 0 2 219 251 115 115 99 7 159 255 255 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 6:
225 225 225 225 224 240 240 240 240 56 140 142 195 129 192 224 112 120 28 31 7 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Page 7:
105 105 108 108 127 55 27 29 14 7 7 3 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Now your image is ready to send to your glcd. Almost all microcontroller and embedded tools have an inbuilt function to write the image when the form above is given. Since it is a general tutorial, I just don't go to any specific one.If your software tools have changes, do appropriate changes in the format also. This tutorial aims to make your hands dirty even though full fledged software tools available(sometimes for free). But I recommend to work out this at least once to understand the internals of the image conversion.

No comments: