epaper.js icon indicating copy to clipboard operation
epaper.js copied to clipboard

Add support for Waveshare 12.48in B e-paper

Open clintandrewhall opened this issue 3 years ago • 3 comments

Howdy! I'd love to add support for the 12.48" e-paper.

https://www.waveshare.com/wiki/12.48inch_e-Paper_Module https://github.com/waveshare/12.48inch-e-paper

I'm excited to use this guy, but I'd rather write in Node... hence me coming here! I'm happy to write the binding, but I've never written one from scratch, (and I'm rusty as hell at C). If you could give me a quickstart, I could write and test and keep you up to speed via this issue.

Thanks in advance!

clintandrewhall avatar Aug 29 '21 03:08 clintandrewhall

Hi @clintandrewhall thanks for your interest! It looks like the 12.48" screen uses a different controller than some already supported screens, so it may be a little more work than just a copy / paste, but still pretty doable!

  • The basic idea is that for each screen there is a c++ file that takes the provided Waveshare c drivers and exposes them as a node module using the node.js n-api. As you might be aware, this is how node lets you drop to c / c++ for higher performance / access to system APIs. For the 4.2" display, this file is EPD_4in2_node.cc.
  • There's a build tool called gyp that compiles the c++ file, along with any dependencies. This is configured with the binding.gyp file and referenced as a build step (that occurs automatically when you install epaper.js) inside package.json.
  • The devices.js file imports the c++ driver from the steps above and binds it to a common interface. So each device has an init and displayPNG method. These devices are also the objects the user specifies, when they specify which screen they'd like to use.
  • You'll notice inside the device.displayPNG method references to other methods like convertPNGTo4Grey(). The HTML / JS contents that you display on the screen are rendered as a color PNG, which displayPNG consumes and converts into a format the display can understand. Usually this logic is ported from the example Waveshare c or python code to JavaScript. For example, the 4.2" display came with some Python sample code (seen below), that I ported into the images.js/convertPNGto1BitBW method used to display images in black and white mode.
    def getbuffer(self, image):
        # logging.debug("bufsiz = ",int(self.width/8) * self.height)
        buf = [0xFF] * (int(self.width/8) * self.height)
        image_monocolor = image.convert('1')
        imwidth, imheight = image_monocolor.size
        pixels = image_monocolor.load()
        # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
        if(imwidth == self.width and imheight == self.height):
            logging.debug("Horizontal")
            for y in range(imheight):
                for x in range(imwidth):
                    # Set the bits for the column of pixels at the current position.
                    if pixels[x, y] == 0:
                        buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
        elif(imwidth == self.height and imheight == self.width):
            logging.debug("Vertical")
            for y in range(imheight):
                for x in range(imwidth):
                    newx = y
                    newy = self.height - x - 1
                    if pixels[x, y] == 0:
                        buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
        return buf

That's the basic overview, hopefully it's enough to get you started. If you give it a shot and end up stuck, I'd be happy to connect with you somehow and lend a hand.

samsonmking avatar Sep 09 '21 00:09 samsonmking

@clintandrewhall if you have any questions while working on this, you can also reach out on the discord server.

samsonmking avatar Sep 19 '21 15:09 samsonmking

@samsonmking I was able to get something working, but I wasn't able to join your discord server... perhaps it's moved?

At any rate, your feedback is welcome, especially regarding how https://github.com/samsonmking/epaper.js/pull/70 depends on a C library to work.

I can always extract out my package or something, I'm not sure. Thanks in advance!

clintandrewhall avatar Mar 01 '22 17:03 clintandrewhall