LEDgadget

Share your advanced PureBasic knowledge/code with the community.
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

LEDgadget

Post by localmotion34 »

here is a little attempt in my largetr scheme to enhance PB functionality and expand the gadget list to many of the advanced and cool gadgets out there. this is a gauge or LED gadget that tracks percent ranges exactly like that of the task manager processor usage window. just compile the code and move the mouse around and you will see. it's in the beginning stages right now, but shouldnt be too difficult to create a fully working PB library.

Code: Select all

Structure gauge
  imagegad.l
  imageid.l
  width.l
  height.l
  imagehwnd.l
  precision.l
  ticks.l
EndStructure 

NewList led.gauge()

ProcedureDLL LEDgadget(number,x,y,width,height,display)
  AddElement(led())
  SelectElement(led(),number)
  led()\width=width
  led()\height=height
  led()\imageid=CreateImage(#PB_Any,led()\width,led()\height)
  StartDrawing(ImageOutput())
  Box(0,0,led()\width,led()\height,#Black)
  ledheight=led()\height-20
  boxwidth=(led()\width-10-1)/2
  secondx= 6+boxwidth
  tickcount=0
  For a=0 To led()\height-20 Step 4
    tickcount=tickcount+1
    Box(5,a,boxwidth,3,$7F00)
    Box(secondx,a,boxwidth,3,$7F00)
  Next  
  led()\ticks=tickcount
  Locate((led()\width/2)-10,led()\height-15)
  BackColor(0,0,0)
  FrontColor(0, $FF, 0)
  DrawText("0%")
  StopDrawing()
  led()\imagegad=ImageGadget(#PB_Any,x,y,width,height,UseImage(led()\imageid),#PB_Image_Border)
  ProcedureReturn led()\imagegad
EndProcedure 

ProcedureDLL setLEDstate(led,percent)
  SelectElement(led(),led)
  tickcount=led()\ticks
  perc.f=100/led()\ticks
  percents.f=(percent/100)
  finalpercent.f=percents*tickcount
  stringpercent.s=StrF(finalpercent)
  Result.f = Round(finalpercent, 1)
  finalresult=led()\ticks-Result
  ledheight=led()\height-20
  boxwidth=(led()\width-10-1)/2
  secondx=6+boxwidth
  UseImage(led()\imageid)
  StartDrawing(ImageOutput())
  tickcount=0
  For a=0 To led()\height-20 Step 4
    tickcount=tickcount+1
    If tickcount>=finalresult
      Box(5,a,boxwidth,3,$FF00)
      Box(secondx,a,boxwidth,3,$FF00)
    Else
      Box(5,a,boxwidth,3,$7F00)
      Box(secondx,a,boxwidth,3,$7F00)
    EndIf 
  Next 
  Box(0,led()\height-15,led()\width,15,#Black)
  Locate((led()\width/2)-10,led()\height-15)
  BackColor(0,0,0)
  FrontColor(0, $FF, 0)
  DrawText(Str(percent)+"%")
  StopDrawing() 
  SetGadgetState(led()\imagegad,UseImage(led()\imageid))
EndProcedure 

#WindowWidth  = 390
#WindowHeight = 350
If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, #PB_Window_MinimizeGadget, "")
  If CreateGadgetList(WindowID(0))
  led=LEDgadget(0,50,50,75,200,0)
  EndIf
  
  Repeat
    setLEDstate(0,Random(100))
    EventID = WaitWindowEvent()
    
    If EventID = #PB_EventGadget
      
      Select EventGadgetID()
        Case led
          
      EndSelect
      
    EndIf
    
  Until EventID = #PB_EventCloseWindow
  
EndIf

End 
tell me what you guys think, or give some suggestions.

NOTE: there will be an optional command to "attatch" the gadget to a process that MUST return a value between 0 and 100 to "monitor" anything really desired.

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

localmotion34, congratz! I love it.
I'd like to see an horizontal mode as well. :)

For the current, it would be nice if there was an option to setup the peaks size and maybe colors for them, as well as resizing the surrounding background for the peaks.

Hmm maybe im asking too much for a gadget :?
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

And why not circles instead of peaks as well? :D
That will give a better use of the LED name indeed heh.
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

Post by Pantcho!! »

Yeah dagcrack is right this is great! expanding this gadget will be great for horiz/vert + circle! :) i say do a LedGadget PB lib.
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post by techjunkie »

Really cool!! I love it... back to the 80's... Hehe...
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

@dagcrack

can you be more specific as to what you mean by setting up peak sizes? i understand that i could change the color of the indicator boxes from green to yellow to red as it approaches 100%, like a real gauge might to warn of approaching maximum.

ill try working on the horizontal LED gadget as well.

this gadget is probably the easiest of all the advanced gadgets i am making because there is no subclassing involved, and all the gadget procedures are done with straight PB code instead of processing messages.

anyone have any other ideas or even image examples of gadgets like this they'd want?

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

Blimey! You're inviting a long Wish List there...

- MapGadget(GID,x,y,w,h,image.s)
that returns X,Y coordinates

- SwitchGadget(GID,x,y,w,h,"option1|option2|option4...",#VERTICAL|#HORIZONTAL)
where 2 or more Radio-type gadgets are grouped, clicking one switches the others off.

- LineGadget(GID,x,y,length,size,#VERTICAL|HORIZONTAL)
to produce a simple line - either horizontal or vertical. (a bit like HTML <HR SIZE=xx>) - currently do this with the Frame3dGadget

- DateGadget(GID,x,y,w,h,"DD.MM.YY|DD.MM.YYY|DDMMYY|MMM.DD.YYY...")
that allows only valid DD/MM/YY or similar formats

- CurrencyGadget(GID,x,y,w,h,min,max,"£0000.00|£^^^0.00|0.00|^^.^^...")
that allows valid values within a given range (though this would be better as a flag for StringGadget)

- AutoCompletGadget(GID,x,y,w,h,"option1|option2|option3...")
An Editable ComboBox that will autocomplete for you (would be nice to be able to load values in the command or use SetGadgetText to load them

- BlockGadget(GID,x,y,w,h,r,g,b)
Just draws a big square block of colour (would be nice visually or even for producing simple graphs)

- WebGadget2(GID,x,y,w,h,URL.s)
exactly the same as WebGadget but captures all HyperLink Clicks and returns them to PB. Also supresses the Right Click

I know many of these already have existing PB code examples to achieve these results (and I use many of them regularly).

But for me I want my source to be simple for maintenance reasons.
By moving such code into PB libraries, I can hide the complexity and pretend that my resultant application is very simple :mrgreen:
Ta - N
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

BTW - love the LED gadget - as soon as its a LIB, I'm gonna use it (I'll find some excuse - lol!)
Ta - N
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Great gadget :)
Tommeh
Enthusiast
Enthusiast
Posts: 149
Joined: Sun Aug 29, 2004 2:25 pm
Location: United Kingdom

Post by Tommeh »

Here i took a load of localmotions shoulders by adding the ability to change the front and back colour of the gadget :-)

It still closely follows how the gadget is done so not to make the code alien to the rest of it.

Code: Select all

Structure gauge
  imagegad.l
  imageid.l
  width.l
  height.l
  imagehwnd.l
  precision.l
  ticks.l
  frontcol.l
  backcol.l
EndStructure

NewList led.gauge()


ProcedureDLL setLEDfcol(led,col)
  SelectElement(led(),led)
  led()\frontcol = col
EndProcedure
  
ProcedureDLL setLEDbcol(led,col)
  SelectElement(led(),led)
  led()\backcol = col
EndProcedure
  
  
ProcedureDLL LEDgadget(number,x,y,width,height,display)
  AddElement(led())
  SelectElement(led(),number)
  led()\width=width
  led()\height=height
  led()\imageid=CreateImage(#PB_Any,led()\width,led()\height)
  StartDrawing(ImageOutput())
  Box(0,0,led()\width,led()\height,#Black)
  ledheight=led()\height-20
  boxwidth=(led()\width-10-1)/2
  secondx= 6+boxwidth
  tickcount=0
  For a=0 To led()\height-20 Step 4
    tickcount=tickcount+1
    Box(5,a,boxwidth,3,led()\backcol)
    Box(secondx,a,boxwidth,3,led()\backcol)
  Next 
  led()\ticks=tickcount
  Locate((led()\width/2)-10,led()\height-15)
  BackColor(0,0,0)
  FrontColor(0, $FF, 0)
  DrawText("0%")
  StopDrawing()
  led()\imagegad=ImageGadget(#PB_Any,x,y,width,height,UseImage(led()\imageid),#PB_Image_Border)
  ProcedureReturn led()\imagegad
EndProcedure

ProcedureDLL setLEDstate(led,percent)
  SelectElement(led(),led)
  tickcount=led()\ticks
  perc.f=100/led()\ticks
  percents.f=(percent/100)
  finalpercent.f=percents*tickcount
  stringpercent.s=StrF(finalpercent)
  Result.f = Round(finalpercent, 1)
  finalresult=led()\ticks-Result
  ledheight=led()\height-20
  boxwidth=(led()\width-10-1)/2
  secondx=6+boxwidth
  UseImage(led()\imageid)
  StartDrawing(ImageOutput())
  tickcount=0
  For a=0 To led()\height-20 Step 4
    tickcount=tickcount+1
    If tickcount>=finalresult
      Box(5,a,boxwidth,3,led()\frontcol)
      Box(secondx,a,boxwidth,3,led()\frontcol)
    Else
      Box(5,a,boxwidth,3,led()\backcol)
      Box(secondx,a,boxwidth,3,led()\backcol)
    EndIf
  Next
  Box(0,led()\height-15,led()\width,15,#Black)
  Locate((led()\width/2)-10,led()\height-15)
  BackColor(0,0,0)
  FrontColor(0, $FF, 0)
  DrawText(Str(percent)+"%")
  StopDrawing()
  SetGadgetState(led()\imagegad,UseImage(led()\imageid))
EndProcedure


#WindowWidth  = 390
#WindowHeight = 350
If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, #PB_Window_MinimizeGadget, "")
  If CreateGadgetList(WindowID(0))
  led=LEDgadget(0,50,50,75,200,0)

  EndIf
 
  Repeat
    setLEDstate(0,Random(100))
    setLEDfcol(0, RGB(Random(255), Random(255), Random(255)))
    setLEDbcol(0, RGB(Random(255), Random(255), Random(255)))
    EventID = WaitWindowEvent()
   
    If EventID = #PB_EventGadget
     
      Select EventGadgetID()
        Case led
         
      EndSelect
     
    EndIf
   
  Until EventID = #PB_EventCloseWindow
 
EndIf

End
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Nice tommeh, but yes localmotion34 captured my vision about the peak colors getting from green (safe) to yellow (caution!) to red (warning!) as well as having simple color setup as yours which is good as well.

Im going to draw some examples of what I mean with peak sizes (each little greenie bar is a peak, aight!, someone might want them smaller, as I do :p).

Image

Also, I think a value to setup how many peaks on the gadget? because if not they go up to the "roof" - You see how at 75% they look nicer (at least to me!) . and yes, a smooth gradient from green to red (passing through yellow) would give a nice look as well.

Ah whats next, we need transistor gadgets :)
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

give me an example of a transistor gadget and i will try and compile an example.

i am working on a radiobutton like LEDgadget that upon click will change from red to green or yellow or whatever. just give me examples of what they look like and how they should operate.

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
Tommeh
Enthusiast
Enthusiast
Posts: 149
Joined: Sun Aug 29, 2004 2:25 pm
Location: United Kingdom

Post by Tommeh »

Ok, this now has the different colours as it goes up, it also has the ability to get the current state of it :-)

Dance to the music! ;)

Code: Select all

Structure gauge
  imagegad.l
  imageid.l
  width.l
  height.l
  imagehwnd.l
  precision.l
  ticks.l
  frontcol.l
  backcol.l
  cpercent.l
EndStructure

NewList led.gauge()


ProcedureDLL setLEDfcol(led,col)
  SelectElement(led(),led)
  led()\frontcol = col
EndProcedure
 
ProcedureDLL setLEDbcol(led,col)
  SelectElement(led(),led)
  led()\backcol = col
EndProcedure
 
 
ProcedureDLL LEDgadget(number,x,y,width,height,display)
  AddElement(led())
  SelectElement(led(),number)
  led()\width=width
  led()\height=height
  led()\imageid=CreateImage(#PB_Any,led()\width,led()\height)
  StartDrawing(ImageOutput())
  Box(0,0,led()\width,led()\height,#Black)
  ledheight=led()\height-20
  boxwidth=(led()\width-10-1)/2
  secondx= 6+boxwidth
  tickcount=0
  For a=0 To led()\height-20 Step 4
    tickcount=tickcount+1
    Box(5,a,boxwidth,3,led()\backcol)
    Box(secondx,a,boxwidth,3,led()\backcol)
  Next
  led()\ticks=tickcount
  Locate((led()\width/2)-10,led()\height-15)
  BackColor(0,0,0)
  FrontColor(0, $FF, 0)
  DrawText("0%")
  StopDrawing()
  led()\imagegad=ImageGadget(#PB_Any,x,y,width,height,UseImage(led()\imageid),#PB_Image_Border)
  ProcedureReturn led()\imagegad
EndProcedure

ProcedureDLL setLEDstate(led,percent)
  SelectElement(led(),led)
  led()\cpercent = percent
  tickcount=led()\ticks
  perc.f=100/led()\ticks
  percents.f=(percent/100)
  finalpercent.f=percents*tickcount
  stringpercent.s=StrF(finalpercent)
  Result.f = Round(finalpercent, 1)
  finalresult=led()\ticks-Result
  ledheight=led()\height-20
  boxwidth=(led()\width-10-1)/2
  secondx=6+boxwidth
  UseImage(led()\imageid)
  StartDrawing(ImageOutput())
  tickcount=0
  For a=0 To led()\height-20 Step 4
    tickcount=tickcount+1
    If tickcount>=finalresult
      Box(5,a,boxwidth,3,led()\frontcol)
      Box(secondx,a,boxwidth,3,led()\frontcol)
    Else
      Box(5,a,boxwidth,3,led()\backcol)
      Box(secondx,a,boxwidth,3,led()\backcol)
    EndIf
  Next
  Box(0,led()\height-15,led()\width,15,#Black)
  Locate((led()\width/2)-10,led()\height-15)
  BackColor(0,0,0)
  FrontColor(0, $FF, 0)
  DrawText(Str(percent)+"%")
  StopDrawing()
  SetGadgetState(led()\imagegad,UseImage(led()\imageid))
EndProcedure

ProcedureDLL getLEDstate(led)
  SelectElement(led(),led)
  ProcedureReturn led()\cpercent
EndProcedure

ProcedureDLL Beep(freq.l, time.l)
n.f = (freq/1000)*100
setLEDfcol(0, RGB(n.f*2.5, 250-(n.f*2.5), 0))
setLEDstate(0, n.f)
Beep_(freq, time)
EndProcedure

ProcedureDLL StartBeep(times)
tempo=180
For t=1 To times
Beep(284,tempo)
Beep(568,tempo)
Beep(426,tempo)
Beep(379,tempo)
Beep(758,tempo)
Beep(426,tempo)
Beep(716,tempo)
Beep(426,tempo)

Beep(284,tempo)
Beep(568,tempo)
Beep(426,tempo)
Beep(379,tempo)
Beep(758,tempo)
Beep(426,tempo)
Beep(716,tempo)
Beep(426,tempo)

Beep(319,tempo)
Beep(568,tempo)
Beep(426,tempo)
Beep(379,tempo)
Beep(758,tempo)
Beep(426,tempo)
Beep(716,tempo)
Beep(426,tempo)

Beep(319,tempo)
Beep(568,tempo)
Beep(426,tempo)
Beep(379,tempo)
Beep(758,tempo)
Beep(426,tempo)
Beep(716,tempo)
Beep(426,tempo)

Beep(379,tempo)
Beep(568,tempo)
Beep(426,tempo)
Next t
EndProcedure

#WindowWidth  = 390
#WindowHeight = 350
If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, #PB_Window_MinimizeGadget, "")
  If CreateGadgetList(WindowID(0))
  led=LEDgadget(0,50,50,75,200,0)
  setLEDbcol(0, RGB(45,60,45))
  StartBeep(2)
  EndIf
 
  Repeat
  
    EventID = WaitWindowEvent()
   
    If EventID = #PB_EventGadget
     
      Select EventGadgetID()
        Case led
         
      EndSelect
     
    EndIf
   
  Until EventID = #PB_EventCloseWindow
 
EndIf

End
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

ok try this gadget library test: it has included a LED switch button sort of like a readio button, but all jazzed up.

http://www.penguinbyte.com/apps/pbwebst ... ample.zip

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

I heard schwarzenegger yelling "IT WERKZ!!"

Although the radio buttons, their quality isnt very nice.. and the background is different ( ? )
Post Reply