├── LICENSE
├── README.md
└── libpyfb.py
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 @raspiduino
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # libpyfb
2 | Python library for controling raw framebuffer. Written in pure Python.
3 |
4 | ## What is this?
5 | libpyfb is a low-level framebuffer control library written in Python. This work on all Linux devices, include x86, x86_64 and ARM.
6 |
7 | ## Why made this?
8 | Currently, there is no low-level Python module for direct writing to framebuffer. Pygame still require SDL and other C libraries. Their is also a gist in C about this and also a Go script here
9 |
So I made this small library for doing that. Have fun!
10 |
11 | ## How this work?
12 | Linux Framebuffer can be easily accessed in /dev/fb0. Like any other file, this can be read and write to (if the user in video group). So this library will open fb0, map it to memory and control the screen from that. Each pixel is 4 bytes (32 bit) or 2 bytes (16 bit). These bytes are B, G, R and one transparency bytes.
13 |
14 | ## Todo
15 | - Add graphical drawing
16 | - Add mouse and keyboard support
17 |
18 | ## License
19 | Under MIT license.
20 |
21 | And one last thing...
22 | Enjoy your 2021!
23 |
--------------------------------------------------------------------------------
/libpyfb.py:
--------------------------------------------------------------------------------
1 | '''
2 | Python raw framebuffer library (for Linux)
3 | Copyright @raspiduino 2020
4 | Date created 8:30 PM 31/12/2020
5 | '''
6 |
7 | import os
8 | import mmap # Memory map library
9 | import getpass
10 |
11 | class Framebuffer():
12 | '''
13 | Framebuffer class
14 | Varriables:
15 | screenx: x screen size
16 | screeny: y screen size
17 | bpp : Bit per pixel
18 | fbpath : Framebuffer path
19 | fbdev : Framebuffer opened as normal file
20 | fb : Framebuffer memory mapped object
21 | Functions:
22 | __init__ : init function
23 | drawpixel: function to draw a single pixel
24 | clear : function to clear the screen
25 |
26 | '''
27 | def __init__(self, fbpath="/dev/fb0"):
28 | '''
29 | __init__ function
30 | Optional: Require frame buffer path. Default is /dev/fb0
31 | Note: Please add your current user to video group. If not,
32 | the library will add this for you, require root.
33 | '''
34 | if os.system('getent group video | grep -q "\b'+ getpass.getuser() +'\b"') == 1:
35 | # User not in video group
36 | print("This command will be run as root, please allow it in order to get framebuffer work!")
37 | os.system("sudo adduser " + getpass.getuser() + " video")
38 | print("Done! Now you can use framebuffer without root!")
39 |
40 | # Now user in video group
41 | # Get screen info
42 |
43 | # Get screen size
44 | _ = open("/sys/class/graphics/fb0/virtual_size", "r")
45 | __ = _.read()
46 | self.screenx,self.screeny = [int(i) for i in __.split(",")]
47 | _.close()
48 |
49 | # Get bit per pixel
50 | _ = open("/sys/class/graphics/fb0/bits_per_pixel", "r")
51 | self.bpp = int(_.read()[:2])
52 | _.close()
53 |
54 | # Open the framebuffer device
55 | self.fbpath = fbpath
56 | self.fbdev = os.open(self.fbpath, os.O_RDWR)
57 |
58 | # Map framebuffer to memory
59 | self.fb = mmap.mmap(self.fbdev, self.screenx*self.screeny*self.bpp//8, mmap.MAP_SHARED, mmap.PROT_WRITE|mmap.PROT_READ, offset=0)
60 |
61 | def drawpixel(self, x, y, r, g, b, t=0):
62 | '''
63 | drawpixel function
64 | Draw a single pixel with color
65 | Require:
66 | x: x coordinate of the pixel
67 | y: y coordinate of the pixel
68 |
69 | RGB color:
70 | r: Red color (0 -> 255)
71 | g: Green color (0 -> 255)
72 | b: Blue color (0 -> 255)
73 |
74 | t: transparency (default set to 0)
75 | '''
76 | self.fb.seek(x*y*self.bpp//8) # Set the pixel location
77 |
78 | if self.bpp == 32:
79 | # 32 bit per pixel
80 | self.fb.write(b.to_bytes(1, byteorder='little')) # Write blue
81 | self.fb.write(g.to_bytes(1, byteorder='little')) # Write green
82 | self.fb.write(r.to_bytes(1, byteorder='little')) # Write red
83 | self.fb.write(t.to_bytes(1, byteorder='little')) # Write transparency
84 |
85 | else:
86 | # 16 bit per pixel
87 | self.fb.write(r.to_bytes(1, byteorder='little') << 11 | g.to_bytes(1, byteorder='little') << 5 | b.to_bytes(1, byteorder='little'))
88 |
89 | def clear(self, r=255, g=255, b=255, t=0):
90 | '''
91 | clear function
92 | Clear the screen with color
93 | Require:
94 | RGB color:
95 | r: Red color (0 -> 255)
96 | g: Green color (0 -> 255)
97 | b: Blue color (0 -> 255)
98 |
99 | t: transparency (default set to 0)
100 | '''
101 | for y in range(self.screeny):
102 | for x in range(self.screenx):
103 | self.drawpixel(x, y, r, g, b, t)
104 |
105 | if __name__ == "__main__":
106 | fb0 = Framebuffer("/dev/fb0")
107 | fb0.clear()
108 | for y in range(fb0.screeny):
109 | for x in range(fb0.screenx):
110 | fb0.drawpixel(x, y, x%256, y%256, (x+y)%256)
111 |
--------------------------------------------------------------------------------