mirror of
https://github.com/NaitLee/Cat-Printer.git
synced 2025-05-16 23:30:15 -07:00
int_to_bytes length fix for cat printer protocol
This commit is contained in:
parent
58077d4ca5
commit
87e5118229
@ -45,17 +45,16 @@ def reverse_bits(i: int):
|
|||||||
i = ((i & 0b11001100) >> 2) | ((i & 0b00110011) << 2)
|
i = ((i & 0b11001100) >> 2) | ((i & 0b00110011) << 2)
|
||||||
return ((i & 0b11110000) >> 4) | ((i & 0b00001111) << 4)
|
return ((i & 0b11110000) >> 4) | ((i & 0b00001111) << 4)
|
||||||
|
|
||||||
def int_to_bytes(i: int, big_endian=False):
|
def int_to_bytes(i: int, length=1, big_endian=False) -> bytes:
|
||||||
''' Turn `int` into `bytearray`, that have
|
b = bytearray(length)
|
||||||
least bytes possible to represent the int
|
p = 0
|
||||||
'''
|
|
||||||
result = bytearray()
|
|
||||||
while i != 0:
|
while i != 0:
|
||||||
result.append(i & 0xff)
|
b[p] = i & 0xff
|
||||||
i >>= 8
|
i >>= 8
|
||||||
|
p += 1
|
||||||
if big_endian:
|
if big_endian:
|
||||||
result.reverse()
|
b = reversed(b)
|
||||||
return result
|
return bytes(b)
|
||||||
|
|
||||||
class Commander(metaclass=ABCMeta):
|
class Commander(metaclass=ABCMeta):
|
||||||
''' Semi-abstract class, to be inherited by `PrinterDriver`
|
''' Semi-abstract class, to be inherited by `PrinterDriver`
|
||||||
@ -122,11 +121,11 @@ class Commander(metaclass=ABCMeta):
|
|||||||
|
|
||||||
def retract_paper(self, pixels: int):
|
def retract_paper(self, pixels: int):
|
||||||
'Retract the paper for some pixels'
|
'Retract the paper for some pixels'
|
||||||
self.send( self.make_command(0xa0, int_to_bytes(pixels)) )
|
self.send( self.make_command(0xa0, int_to_bytes(pixels, length=2)) )
|
||||||
|
|
||||||
def feed_paper(self, pixels: int):
|
def feed_paper(self, pixels: int):
|
||||||
'Feed the paper for some pixels'
|
'Feed the paper for some pixels'
|
||||||
self.send( self.make_command(0xa1, int_to_bytes(pixels)) )
|
self.send( self.make_command(0xa1, int_to_bytes(pixels, length=2)) )
|
||||||
|
|
||||||
def set_speed(self, value: int):
|
def set_speed(self, value: int):
|
||||||
''' Set how quick to feed/retract paper. **The lower, the quicker.**
|
''' Set how quick to feed/retract paper. **The lower, the quicker.**
|
||||||
@ -140,7 +139,7 @@ class Commander(metaclass=ABCMeta):
|
|||||||
''' Set thermal energy, max to `0xffff`
|
''' Set thermal energy, max to `0xffff`
|
||||||
By default, it's seems around `0x3000` (1 / 5)
|
By default, it's seems around `0x3000` (1 / 5)
|
||||||
'''
|
'''
|
||||||
self.send( self.make_command(0xaf, int_to_bytes(amount)) )
|
self.send( self.make_command(0xaf, int_to_bytes(amount, length=2)) )
|
||||||
|
|
||||||
def draw_bitmap(self, bitmap_data: bytearray):
|
def draw_bitmap(self, bitmap_data: bytearray):
|
||||||
'Print `bitmap_data`. Also does the bit-reversing job.'
|
'Print `bitmap_data`. Also does the bit-reversing job.'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user