PDA

View Full Version : "Stretching" a byte variable into two byte variables?



CuriousOne
- 24th October 2021, 06:13
Hello :)
I don't know the exact term I need, so here's what I'm doing. Since I've created custom 8x8 fonts for ST7920, I also want it to be able to display bigger letters too. Stretching vertically is not a problem - just double the lines and that's all. But when it comes to horizontal stretching, I'm facing some issues. The code should work as follows - initial bitmap data, read from single byte in eeprom, should be "stretched" into two byte variables and written to display. The code below shows how I see it. But it has a lot of if-then lines, and my guess is, that there should be a something simpler to do this task. Any ideas?



'bytes in eeprom at left, what should be at output - at right
'this is a letter "A"
'A B C
'(eeprom) (MSB) (LSB)
'00111100 00001111 11110000
'01000010 00110000 00001100
'01000010 00110000 00001100
'01111110 00111111 11111100
'01000010 00110000 00001100
'01000010 00110000 00001100

IF A.0=0 THEN B.0=0:B.1=0
IF A.0=1 THEN B.0=1:B.1=1
IF A.1=0 THEN B.2=0:B.3=0
IF A.1=1 THEN B.2=1:B.3=1
IF A.2=0 THEN B.4=0:B.5=0
IF A.2=1 THEN B.4=1:B.5=1
IF A.3=0 THEN B.6=0:B.7=0
IF A.3=1 THEN B.6=1:B.7=1


IF A.4=0 THEN C.0=0:C.1=0
IF A.4=1 THEN C.0=1:C.1=1
IF A.5=0 THEN C.2=0:C.3=0
IF A.5=1 THEN C.2=1:C.3=1
IF A.6=0 THEN C.4=0:C.5=0
IF A.6=1 THEN C.4=1:C.5=1
IF A.7=0 THEN C.6=0:C.7=0
IF A.7=1 THEN C.6=1:C.7=1

richard
- 24th October 2021, 06:43
sample the msb of your byte
if its set add three to your word var
shift left the byte var 1 bit the word 2 bits
repeat for all 8 bits

CuriousOne
- 24th October 2021, 09:47
and if it not?

richard
- 24th October 2021, 11:28
don't add 3

tumbleweed
- 24th October 2021, 12:51
If you start off with B=0 and C=0 you can skip half those statements and only do the ones where A.x = 1

CuriousOne
- 24th October 2021, 16:21
I wanted it to be shorter, by using loop and

X.Y=Z
but as I can see, you can't use another variable as bit reference, only static values.

CuriousOne
- 24th October 2021, 17:24
uint16_t enlargeYourByte(uint8_t input)





2
{





3
uint16_t x = input;





4
x = (x ^ (x << 4)) & 0x0f0f;





5
x = (x ^ (x << 2)) & 0x3333;





6
x = (x ^ (x << 1)) & 0x5555;





7
return x | (x << 1));





8
}







This is how they do it in C. Can we port this to PBP ?

mpgmike
- 24th October 2021, 18:30
I think this does it:


Input VAR BYTE
Wide VAR WORD
X VAR WORD
Y VAR WORD

EnlargeYourByte:
X = Input
Y = Input << 4
X = X ^ Y
X = X & $0F0F
Y = X << 2
X = X ^ Y
X = X & $3333
Y = X << 1
X = X ^ Y
X = X & $5555
Wide = X << 1
RETURN

richard
- 24th October 2021, 23:18
any_byte=101011 ;eg input any_byte-> 10101011
for any_cnt=0 to 7
any_word=any_word<<2
if any_byte.7 then any_word=any_word+3
any_byte=any_byte<<1
next
output any_word-> 1100110011001111


mikes



X VAR WORD
Y VAR WORD
;eg input any_byte-> 10101011


X = ani_r
Y = ani_r << 4
X = X ^ Y
X = X & $0F0F
Y = X << 2
X = X ^ Y
X = X & $3333
Y = X << 1
X = X ^ Y
X = X & $5555
ani_h = X << 1


output any_word-> 1000100010001010


not working



corrected


x = ani_r;
x = (x ^ (x << 4)) & $0f0f;
x = (x ^ (x << 2)) & $3333;
x = (x ^ (x << 1)) & $5555;
ani_h = x | (x << 1);


and works , probably quicker the my loop

richard
- 24th October 2021, 23:48
full test code here (http://www.picbasic.co.uk/forum/showthread.php?t=24218&p=147363#post147363)