Ejemplo n.º 1
0
        public static unsafe LCDBitmap Load(Bitmap bmp)
        {
            LCDBitmap lcdbmp = new LCDBitmap(bmp.Width, bmp.Height);

            BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
            int bpp = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
            byte* fpix = (byte*)data.Scan0;

            Parallel.For(0, bmp.Height, py =>
            {
                byte* line = fpix + (py * data.Stride);
                for (int px = 0; px < bmp.Width; px++)
                {
                    byte b = 255, g = 255, r = 255, a = 0;

                    if (bmp.Palette.Entries.Length > 0)
                    {
                        Color col = bmp.Palette.Entries[line[px * bpp]];
                        b = col.B;
                        g = col.G;
                        r = col.R;
                        a = col.A;
                    }
                    else
                    {
                        if (bpp == 1)
                        {
                            b = g = r = line[px * bpp];
                        }
                        else if (bpp == 3 || bpp == 4)
                        {
                            b = line[px * bpp];
                            g = line[(px + 1) * bpp];
                            r = line[(px + 2) * bpp];
                            if (bpp == 4)
                            {
                                a = line[(px + 3) * bpp];
                            }
                        }
                    }

                    if (a == 0)
                    {
                         lcdbmp.Buffer[px, py] = -1;
                    }
                    else if (r == 0 && g == 0 && b == 0)
                    {
                        lcdbmp.Buffer[px, py] = 1;
                    }
                    else
                    {
                        lcdbmp.Buffer[px, py] = 0;
                    }
                }
            });

            bmp.UnlockBits(data);

            return lcdbmp;
        }
Ejemplo n.º 2
0
        public void Draw(LCDBitmap bmp)
        {
            int[,] oldbuffer = Buffer.GetBuffer();
            int[,] buffer = bmp.GetBuffer();

            // Compute changes, draw whats changed
            for (int memy = 0; memy < 32; memy++)
            {
                int[][] data = new int[16][];
                int firstchanged = -1;
                int lastchanged = -1;
                for (int memx = 0; memx < 16; memx++)
                {
                    int posx = memx * 16;
                    int posy = memy;
                    if (posx >= 128)
                    {
                        posy += 32;
                        posx -= 128;
                    }

                    int dold1 = GetBufferData(oldbuffer, posx, posy);
                    int dold2 = GetBufferData(oldbuffer, posx + 8, posy);
                    int dnew1 = GetBufferData(buffer, posx, posy);
                    int dnew2 = GetBufferData(buffer, posx + 8, posy);

                    data[memx] = new int[] { dnew1, dnew2 };
                    if (dnew1 != dold1 || dnew2 != dold2)
                    {
                        if (firstchanged == -1)
                        {
                            firstchanged = memx;
                        }
                        lastchanged = memx;
                    }
                }

                // Draw
                if (firstchanged != -1)
                {
                    WriteInstruction(128 + memy);
                    WriteInstruction(128 + firstchanged);
                    for (int memx = firstchanged; memx <= lastchanged; memx++)
                    {
                        WriteData(data[memx][0]);
                        WriteData(data[memx][1]);
                    }
                }
            }

            Buffer.DrawLCDBitmap(bmp, 0, 0, 0, 0, 128, 64);
        }
Ejemplo n.º 3
0
        public LCD12864(DigitalPin en, DigitalPin rs, DigitalPin rw, DigitalPin rst, DigitalPin[] d)
        {
            EN = en;
            RS = rs;
            RW = rw;
            RST = rst;
            DB = d;

            EN.SetIOMode(PinMode.Output);
            EN.DigitalWrite(0);
            RS.SetIOMode(PinMode.Output);
            RW.SetIOMode(PinMode.Output);

            for (int i = 0; i < 8; i++)
            {
                DB[i].SetIOMode(PinMode.Output);
            }

            RST.SetIOMode(PinMode.Output);
            RST.DigitalWrite(0);
            Wrapper.delayMicroseconds(10);
            RST.DigitalWrite(1);
            Wrapper.delay(100);

            WriteInstruction(Convert.ToInt32("00110000", 2)); // Function set
            WriteInstruction(Convert.ToInt32("00110000", 2)); // Function set
            WriteInstruction(Convert.ToInt32("00001100", 2)); // Display on/off
            WriteInstruction(Convert.ToInt32("00000001", 2)); // Display clear
            WriteInstruction(Convert.ToInt32("00000110", 2)); // Entry mode set
            WriteInstruction(Convert.ToInt32("00110100", 2)); // Extended Mode
            WriteInstruction(Convert.ToInt32("00110110", 2)); // Graphics mode

            // Screen clear
            Buffer = new LCDBitmap(128, 64, 1);
            Draw(new LCDBitmap(128, 64, 0));
        }
Ejemplo n.º 4
0
 public void DrawLCDBitmap(LCDBitmap src, int srcposx, int srcposy, int destposx, int destposy, int sx, int sy)
 {
     for (int x = 0; x < sx; x++)
     {
         for (int y = 0; y < sy; y++)
         {
             int val = src.GetPixel(srcposx + x, srcposy + y);
             if (val != -1)
             {
                 SetPixel(destposx + x, destposy + y, val);
             }
         }
     }
 }
Ejemplo n.º 5
0
        public LCDBitmapFont(string name, string file, string[][] charmap)
        {
            Name = name;
            if (Fonts.ContainsKey(name))
            {
                Characters = Fonts[name].Characters;
            }
            else
            {
                Bitmap bmp = new Bitmap(file);

                int ofsy = 0;
                for (int chary = 0; chary < charmap.Length; chary++)
                {
                    int lineheight = 0;
                    for (int liney = ofsy; liney < bmp.Height; liney++)
                    {
                        Color col = bmp.GetPixel(0, liney);
                        if (col.R == 0 && col.G == 255 && col.B == 0)
                        {
                            break;
                        }
                        else
                        {
                            lineheight++;
                        }
                    }

                    int ofsx = 0;
                    for (int charx = 0; charx < charmap[chary].Length; charx++)
                    {
                        int charwidth = 0;
                        for (int posx = ofsx; posx < bmp.Width; posx++)
                        {
                            Color col = bmp.GetPixel(posx, ofsy);
                            if (col.R == 0 && col.G == 255 && col.B == 0)
                            {
                                break;
                            }
                            else
                            {
                                charwidth++;
                            }
                        }

                        LCDBitmap charbmp = new LCDBitmap(charwidth, lineheight);
                        for (int posy = 0; posy < lineheight; posy++)
                        {
                            for (int posx = 0; posx < charwidth; posx++)
                            {
                                Color col = bmp.GetPixel(ofsx + posx, ofsy + posy);
                                charbmp.SetPixel(posx, posy, (col.R == 0 && col.G == 0 && col.B == 0) ? 1 : 0);
                            }
                        }
                        Characters.Add(charmap[chary][charx], charbmp);

                        ofsx += charwidth + 1;
                    }

                    ofsy += lineheight + 1;
                }

                Fonts[name] = this;
            }
        }