Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : scale function
Page 2 of 2 | |||||
Author | Message | ||||
Turbo46 Guru Joined: 24/12/2017 Location: AustraliaPosts: 1593 |
Did you read my post above? Bill Keep safe. Live long and prosper. |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 1643 |
Hmmm. I got to think about that. New to me using mmbasic but worked as 16bit and 32bit vals on 8bit ucntrlr. I will try using integers because some success withprevious code but now testing the captain boing map function with looks just whatI wanted but may use just math for scaling a value. cheers, stan |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 1643 |
Did you read my post above? Bill Naturally. I read all replies. Your link gave error 404. I from 8 bit do not use subs because the call and return uses the stack but no idea about functions speed. I got no problem using in line code not subs but if using arrays then repeating same code would use prog mem.. but it is a pico and has plenty so who cares? Why bother with bits or bytes? use integers and floats for variables Floats for 1 + 2 just seems new. |
||||
Turbo46 Guru Joined: 24/12/2017 Location: AustraliaPosts: 1593 |
Sorry, you should have said. Try this: Sorry but the fact that you asked that question "after reading my post" and reading the manual just suggested to me that you didn't. I tried to demonstrate and explain just that very thing. Bill Keep safe. Live long and prosper. |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 5726 |
True that SUBs and FUNCTIONs are slightly slower than inline code, but if they are of any length they'll save you memory, especially if they are used several times. The performance hit from using them isn't much unless they are being called from inside a loop in which case they may slow things down. If you are bothered about speed then use TIMER to measure how long it takes to call a SUB or FUNCTION and return. It's well worth having a good read about SUBs and FUNCTIONs, as well as about static and local variables. They can make your life a lot easier and enable you to write routines that can be used again in other programs without having to start from scratch. You generally only DIM an array once. After that it doesn't change how much memory it uses as it's already allocated. The memory used comes out of the variables area, not the program area, so you can have some big arrays. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 1643 |
I find the for next final value is strange vin=3.39 vdeg=270.18181822 I do not see why for vin=0 to 3.3 step 0.01 next got to 3.39 which it does just printing vin OPTION EXPLICIT dim vdeg!,vin! cls for vin=0 to 3.3 step 0.01 vdeg=Map(vin,0,3.3,0,270) text 0,0,str$(vin) text 50,0,str$(vdeg) next do loop Function Map(x As Float,rLo As Float,rHi As Float,oLo As Float,oHi As Float) As Float Map=((Min(Max(x,rLo),rHi))-rLo)*((oHi-oLo)/(rHi-rLo))+oLo End Function |
||||
Turbo46 Guru Joined: 24/12/2017 Location: AustraliaPosts: 1593 |
Works for me. I changed the TEXT for PRINT and tested in MMBasic for DOS OPTION EXPLICIT dim vdeg!,vin! cls for vin=0 to 3.3 step 0.01 vdeg=Map(vin,0,3.3,0,270) print str$(vin), str$(vdeg) next do loop Function Map(x As Float,rLo As Float,rHi As Float,oLo As Float,oHi As Float) As Float Map=((Min(Max(x,rLo),rHi))-rLo)*((oHi-oLo)/(rHi-rLo))+oLo End Function These were the last few values: 3.27 267.5454545 3.28 268.3636364 3.29 269.1818182 3.3 270 Bill Keep safe. Live long and prosper. |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5903 |
Stan is print to the LCD so the '9' is left over form the previous value 3.29 He needs to use str$(Vin,2,2) or erase the 9 in othe rways. Similar for 270xxxxx Jim Edited 2022-09-28 08:18 by TassyJim VK7JH MMedit  MMBasic Help |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3015 |
Would 3 divided by 2 times 2 yielding 2 be good enough for you? What's the fear of floats? If you're doing division, floats are your friend. Hmmm. I got to think about that. New to me using mmbasic but worked as 16bit and 32bit vals on 8bit ucntrlr. Wouldn't work on a PICAXE with integer-only math. On what device does it work for you with unsigned 8-bit math? I'm guessing floats have to be available for intermediate values. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
Turbo46 Guru Joined: 24/12/2017 Location: AustraliaPosts: 1593 |
Of course. Thanks Jim So Stan, (just in case... )your code would be: OPTION EXPLICIT dim vdeg!,vin! cls for vin=0 to 3.3 step 0.01 vdeg=Map(vin,0,3.3,0,270) text 0,0,str$(vin,2,2) text 50,0,str$(vdeg,2,2) next do loop Function Map(x As Float,rLo As Float,rHi As Float,oLo As Float,oHi As Float) As Float Map=((Min(Max(x,rLo),rHi))-rLo)*((oHi-oLo)/(rHi-rLo))+oLo End Function And Stan, I recommend that you use: next vin It can get confusing when you are nesting for/next loops Bill Keep safe. Live long and prosper. |
||||
Martin H. Guru Joined: 04/06/2022 Location: GermanyPosts: 896 |
example to Scale a Picture for better readability I left the data uncompressed 'scale a Picture Dim Col%(15) Restore colors:For f%=1 To 15:Read Col%(f%):Next f% MODE 2 Draw_tree 'SCALE it down For y%=0 To 44 For x%=0 To 63 cl%=Pixel(x%,y%) Pixel 64+x%/1.25,10+y%/1.25,cl% :'Scale 0.8 Pixel 116+x%/1.5,16+y%/1.5,cl% :'Scale 0.67 Pixel 160+x%/1.8,21+y%/1.8,cl% :'Scale 0.55 Pixel 196+x%/2,24+y%/2,cl% :'Scale 0.5 Pixel 228+x%/2.5,28+y%/2.5,cl% :'Scale 0.4 Pixel 254+x%/3,31+y%/3,cl% :'Scale 0.33 Pixel 276+x%/4,35+y%/4,cl% :'Scale 0.25 Pixel 292+x%/6,39+y%/6,cl% :'Scale 0.166 Next Next Print @(0,128) Sub draw_tree Local p%,n%,byt$,m$ '--- Read/Create Sprite Restore Tree For p%=0 To 40 Read Byt$:Byt$=expand$(Byt$) For n%=1 To Len(Byt$) m$=Mid$(Byt$,n%,1) cl%=Val("&H"+m$):Pixel n%-1,45-p%,COL%(cl%) Next n% Next p% 'Sprite read #1,0,0,64,45 End Sub Function expand$(pxl$) Local n%,nmb%,tmp$,co$ For n%=1 To Len(pxl$) If Asc(Mid$(pxl$,n%,1))< 71 Then tmp$=tmp$+Mid$(pxl$,n%,1) Else co$=Hex$(Asc(Mid$(pxl$,n%,1))-71) Inc n%:nmb%=Val("&H"+Mid$(pxl$,n%,1)):tmp$=tmp$+String$(nmb%+1,co$) EndIf Next expand$=tmp$ End Function colors: '--Colorscheme accordung to matherp Data RGB(BLUE),RGB(GREEN),RGB(CYAN),RGB(RED) Data RGB(MAGENTA),RGB(YELLOW),RGB(WHITE),RGB(MYRTLE) Data RGB(COBALT) ,RGB(MIDGREEN),RGB(CERULEAN),RGB(RUST) Data RGB(FUCHSIA),RGB(BROWN),RGB(LILAC) Tree: Data "00000000000000808000AAAAAAAA3AAAA3E3A2A2A0AAA2A08080000000000000" Data "0000000000000A22A0A2A2A2A2AAACE4EE4EBE2EAAA2AA080000000000000000" Data "00000000000000000000000000A0A2A3EEEE40E2A2A000000000000000000000" Data "000000000000000000000000000E0E0EE4CCCC0E0E0000000000000000000000" Data "00000000000000000000000000000004CCCE0000000000000000000000000000" Data "00000000000000000000000000000004CC4C0000000000000000000000000000" Data "00000000000000000000000000000004CCCE0000000000000000000000000000" Data "00000000000000000000000000000004CCCC0000000000000000000000000000" Data "0000000000000000000000000000000CCC4E0000000000000000000000000000" Data "0000000000000000000000000000000CCCCE0000000000000000000000000000" Data "00000000000800000000000000000CCCE4EE0000000000000000000000000000" Data "00000000002820002282000000000E4CCCCCC000000000000000000000000000" Data "000000000ECC0220E8E0200000000CCCCCC4E000000000000288200000000000" Data "00000E20E0202020E8E020000000E88EEEEE8E000000288E8228200000002000" Data "023A0222B82A22828282200000000E0CCECCCE0E0000222222AAE8E002202020" Data "02E822A82282822A828288888888C22882CCC8282828282A3A2B8828E2E88220" Data "0232222A22228282828E828282228282C88882A2A820A22AB2ABEA20A20E2820" Data "002AEAAE0EA0AEE0E0E0E0E00EE0000CC000E0AABE0E0EEAEA3AE0BE3E0003E0" Data "000E2EEAA2A3322E2E8E82A882EE888A888880202A2B83ABAA22A32AA2882000" Data "000000EEBEAEAEEAEAEAE0EAEAE02AAE00002AA3E0E0BAABABE0ABAA0B820300" Data "000000E08EE8E2EEAEAEAEEEAEAAEAEAA2EE8BA0EAEAEE0E00E0E0BEAEBE0E00" Data "00000000EEAAA8EE0E8E88EE8EE8E8EAE8E8E3E8B8EA8E8E8EA8EAEA2AB20000" Data "0000000E2A23AE0EAEAE8EAEEE8EA0AEEEA3AAE8E8EA8EAE8A38E3ABEA3E0000" Data "00000000E2EAEAEAEAEA8E3EAAB82AB8EA3E2E8E0E0EEEABABAE3EB2E2000000" Data "0000000000EAE88E8E8BEEABEE8EE8E8AEAE8E8EE8E8ABAE3AEEAA3E00000000" Data "00000000000EAEEE3EEEEAEAEAEE822222AE2E8EEAEEA3AB0EEEAA3E00000000" Data "00000000000EAABAAAA0E0BEA0B0E0EABAAAE8EA0E3A0EEE0EA3EEA2E0000000" Data "00000000000EA3EE0EBAB0EBEAEAEAEAEEABE8E3EEEEABAEAE0EAA0EE0000000" Data "0000000000002AAE30E0EAEE0EEEEE0EA0EAEA8E0AAEEEAA0E0E000000000000" Data "000000000000EA3BEE0EEA30AAE0EA2E2EEE2222EEA3AEB8EE0EE00000000000" Data "00000000000EEAEE0EAEEAAEEEABA3A3AEAEAAE8EEA8C3B8A3AA200000000000" Data "00000000000EA2EEAEAE0EEAEABAAAABABA3BABEAE0ABBE8EE2EE00000000000" Data "0000000000003E3E0EEAE88E8E8E0E0EEAAAE0E0B0EAEAAAEAE0000000000000" Data "00000000000000023A03A8EE0E8EAE8E8E08AA8EAB8A38BB83E0000000000000" Data "000000000000002A3A2A3A3E2EAEAA3EEBEEEEAEEBA8AABB2000000000000000" Data "00000000000000E02E2E32A3E8A3BE8AAE8E88E8AAAEBB2E0000000000000000" Data "00000000000000002E00E3ABEABAABEEAAEEE8EEEBA2ABE00000000000000000" Data "00000000000000000C000CCC2A3E020AAEAAE8E2A3A232E00000000000000000" Data "000000000000000000000000CC0000C33EE3E8EA0EE0E0000000000000000000" Data "0000000000000000000000000000000C2E08AE22E00000000000000000000000" Data "00000000000000000000000000000000C0E22EE0000000000000000000000000" Data "000000000000000000000000000000000C00CAAC000000000000000000000000" Edited 2022-09-28 20:58 by Martin H. 'no comment |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3015 |
Very nice. Thank you. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 1643 |
Thanks for the replies, sorted the for next prob. Jim- I should have used a trailing space or formatted the str$(vin).cheers. Martin- brill demo. I would not have thought of scaling sprites. thanks for sharing. @Lizby- I have used scale function on 8bit basic that has no floats. I can find the code if you want. https://www.youtube.com/watch?v=d51PTF8Boh0 I think captain boing's code is a nice demo with syntax usage and real values demo. Easy to understand... even for me PS.- Bill. Next val advice is sound but if you indent your code then nested for next loops are readable.. but true.. use a next val to be safe and more readable. Thanks. Edited 2022-09-29 01:16 by stanleyella |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3015 |
I can find the code if you want. You have to do tricks to come up with values like 4.35 using only integers, like multiplying your initial value by 100 and then displaying the result divided by 100, then with ".", then with remainder of division by 100. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 1643 |
I can find the code if you want. You have to do tricks to come up with values like 4.35 using only integers, like multiplying your initial value by 100 and then displaying the result divided by 100, then with ".", then with remainder of division by 100. True.. it is called improvisation edit: I can find 5 year old code for gcb that I am now ashamed of like this a-d meter which was a trig learning experience. I thought you would wonder how the 8bit sin,cos worked. It used tables :) Edited 2022-09-29 03:14 by stanleyella |
||||
vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1082 |
Very nice. Thank you. Very nice indeed! Ausgezeichnet! That scaling algorithm is remarkably effective. Thank you! Visit Vegipete's *Mite Library for cool programs. |
||||
Martin H. Guru Joined: 04/06/2022 Location: GermanyPosts: 896 |
That scaling algorithm is remarkably effective. Thank you! It was more for guiding. :-) There is still room for improvement. As the routine paints over some pixels that have already been set. this can be prevented, by changing your view from source to destination. If a row in destination is just 40 Pixel, then Query just the 40. ;-) 'no comment |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 1985 |
late to the party coz of personal stuff, but... http://www.fruitoftheshed.com/MMBasic.Map-Function.ashx?HL=map |
||||
Page 2 of 2 |
Print this page |