Post Reply 
Resistor colors
03-01-2018, 03:54 PM
Post: #1
Resistor colors
A litte program that translate colors on resistors
Code:
// Initialize procedures used in project (by khapster)
EVENTHANDLER();
PUTMENU();
GETMENU();

DRAW_RES();
DRAW_COLOR();

//Program created by by Lars F. 
//Credits: salvomic, Cyrille de Brébisson, Akmon, Dale (DrD), Eddie W. Shore, Didier Lachiese, kharpster

mSEL;
eTYP;
kP;
m;
m1;
lookup;

// things to do
// remove black from menu on first color selection. Done
// remove grey and white selecting third color. Done
// add support for 4 color resistors?

EXPORT Resistor()
BEGIN
   LOCAL mx,my,meny, active_menu, colors, count, res;
   meny :={ {"Black","Brown","Red","Orange","Yellow","Next"}, {"Prev","Green","Blue","Purple","Grey","White"} };
   lookup := {"Black","Brown","Red","Orange","Yellow","Green","Blue","Purple","Grey","White"};
   colors := {"","",""};
   count := 1;

   active_menu := 1; //start menu
   DRAW_RES(colors); // draw a sketch of a resistor
  // at this time only resistors with 3 colors is supported, skipping tolerance 
  WHILE count <= 3 DO
     PUTMENU(meny[active_menu], active_menu, count); 
     // Flush the mouse buffer
     WHILE MOUSE(1) ≥ 0 DO END;

     // Loop until we have a keyboard or mouse event
     REPEAT
       EVENTHANDLER();
     UNTIL eTYP <> "";

     CASE
       // If the event type was a keyboard action then process it
       IF eTYP == "K" THEN
         // If the ESC key was pressed set the program to end
         IF kP == 4 THEN
           KILL;
         END;        
       END;
       
       // If the event type was mouse action then process it
       IF eTYP == "M" THEN
         // Convert mouse coordinates to decimal
         mx := B→R(m1(1));  
         my := B→R(m1(2));
         // Determine if mouse coordinates are inside of a valid menu item otherwise return a zero
         GETMENU(mx,my,meny[active_menu]);
         IF mSEL > 0 AND mSEL <= 6 THEN
           CASE 
             // If menu item 1 and 6 need some special treatment
             IF mSEL == 1 THEN
               IF active_menu == 1 AND count > 1 THEN  // skip if on first color selection (black)
                 colors(count) := meny(active_menu,mSEL);
                 count := count + 1;
               ELSE  // previous pressed.
                 //RECT_P(); // clear screen
                 active_menu := 1;
                 PUTMENU(meny[active_menu],active_menu,count); 
               END;  
             END;
             
             IF mSEL == 6 THEN
               IF active_menu == 2 THEN
                 colors(count) := meny(active_menu,mSEL);
                 count := count + 1;
             ELSE  // next pressed
                //RECT_P(); // clear screen
                 active_menu := 2;
                 PUTMENU(meny[active_menu],active_menu,count); 
             END;    
           END;
           DEFAULT
              colors(count) := meny(active_menu,mSEL);
              count := count + 1;
           END; //case
           // draw the color if any color pressed
           IF count > 1 THEN 
              DRAW_COLOR(count-1,colors(count-1));
           END;         
         END; //mSel
       END; //eTyp
     END; //case
  END; //while colors

// we got 3 colors, now calculate the value
  res := "";
  FOR I FROM 1 TO SIZE(colors) do
    IF I == 3 THEN
      FOR J FROM 1 TO POS(lookup,colors(I)) - 1 DO
        res := res + "0"
      END 
    ELSE
      res := res + STRING(POS(lookup,colors(I)) - 1);
    END
  END;
  // cleanup
  LOCAL pre;
  LOCAL value;
  value := EXPR(res);
  
  IF value >= 1000 THEN
    IF value >= 1000000 THEN 
      value := value/1000000;
      pre :="MΩ";
    ELSE
      value := value / 1000;
      pre := "kΩ";
    END;
  ELSE
    pre :="Ω";
  END;
  
// print the value
  TEXTOUT_P("Resistance= " + value + " " +pre,50,150); 
  WAIT(2); 
  RETURN EXPR(res);
END;

EVENTHANDLER()
BEGIN
  eTYP := "";
  kP := GETKEY;
  IF kP <> -1 THEN
    eTYP := "K";
  ELSE
    m := MOUSE;
    m1 := m(1);
    IF  SIZE(m1) > 0 THEN
      eTYP := "M";
    END;
  END;
END;

// ----------------------------------------------
// Draw the menu using the list passed in
// ----------------------------------------------
PUTMENU(mTXT, num, c)
BEGIN
  CASE
    IF c == 1 AND num == 1 THEN  // selecting the frst color, no black 
      DRAWMENU("",mTXT(2),mTXT(3),mTXT(4),mTXT(5),mTXT(6));
    END;
    IF c==3 AND num == 2 THEN // about to select third color, removing grey and white
      DRAWMENU(mTXT(1),mTXT(2),mTXT(3),mTXT(4));
    END;
  DEFAULT
    DRAWMENU(mTXT(1),mTXT(2),mTXT(3),mTXT(4),mTXT(5),mTXT(6));
  END;
END;

// ------------------------------------------------------------------------------------------
// Get the number of the menu item selected (1-6) by checking mouse position
// Menu items with empty/blank text will return a zero
// ------------------------------------------------------------------------------------------
GETMENU(mx,my,mTXT)
BEGIN
 mSEL := 0;
 IF my≥220 AND my≤239 THEN
   CASE
     IF mx≥0 AND mx≤51 AND mTXT(1)>"" THEN
       mSEL := 1;
     END;
     IF mx≥53 AND mx≤104 AND mTXT(2)>"" THEN
       mSEL := 2;
     END;
     IF mx≥106 AND mx≤157 AND mTXT(3)>"" THEN
       mSEL := 3;
     END;
     IF mx≥159 AND mx≤210AND mTXT(4)>""  THEN
       mSEL := 4;
     END;
     IF mx≥212 AND mx≤263 AND mTXT(5)>"" THEN
       mSEL := 5;
     END;
     IF mx≥265 AND mx≤319 AND mTXT(6)>"" THEN
       mSEL := 6;
     END;
   END;
 END;
END;

// draw a resistor....
DRAW_RES(color_list)
BEGIN
   LOCAL mots, ring,colors;
   mots := {110,108,120,110,120,110,200,110,200,108,210,108,210,132,200,132,200,130,12​0,130,120,132,110,132,110,108};
   
   RECT_P();
   FILLPOLY_P(mots,#FFE5CCh);
   LINE_P(60,120,110,120,#00h);
   LINE_P(210,120,260,120,#00h);
END;

// draw the color 
DRAW_COLOR(number,color)
BEGIN
   LOCAL ring,colors;
   ring := {{130,110,142,130},{154,110,166,130},{178,110,190,130}};
   //               black          brown       red           orange      yellow       green        blue         purple       grey          white  
   colors := {#000000h,#994A00h,#FF0000h,#FF751Ah,#FFFF00h,#33CC33h,#3366FFh,#9933FFh,#8​08080h,#FFFFFFh};
   
   RECT_P(ring(number,1),ring(number,2),ring(number,3),ring(number,4),colors(P​OS(lookup,color)));
END;

can also us it in calculation like this 1/(1/resistor + 1/resistor)
/Lars
Find all posts by this user
Quote this message in a reply
03-01-2018, 04:58 PM (This post was last modified: 03-01-2018 05:04 PM by salvomic.)
Post: #2
RE: Resistor colors
hi Lars,
thank you for the credit and for all!
Please, check about here
Code:

BEGIN
   LOCAL mots, ring,colors;
   mots := {110,108,120,110,120,110,200,110,200,108,210,108,210,132,200,132,200,130,12​​0,130,120,132,110,132,110,108};

I get a check error, I think in 12 0 instead of 120...
Then I get another error with #8 08080h.
Maybe only space not necessary...

Nice program!
Salvo

EDIT:
this code works
Code:

// Initialize procedures used in project (by khapster)
EVENTHANDLER();
PUTMENU();
GETMENU();

DRAW_RES();
DRAW_COLOR();

//Program created by by Lars F. 
//Credits: salvomic, Cyrille de Brébisson, Akmon, Dale (DrD), Eddie W. Shore, Didier Lachiese, kharpster

mSEL;
eTYP;
kP;
m;
m1;
lookup;

// things to do
// remove black from menu on first color selection. Done
// remove grey and white selecting third color. Done
// add support for 4 color resistors?

EXPORT Resistor()
BEGIN
   LOCAL mx,my,meny, active_menu, colors, count, res;
   meny :={ {"Black","Brown","Red","Orange","Yellow","Next"}, {"Prev","Green","Blue","Purple","Grey","White"} };
   lookup := {"Black","Brown","Red","Orange","Yellow","Green","Blue","Purple","Grey","White"};
   colors := {"","",""};
   count := 1;

   active_menu := 1; //start menu
   DRAW_RES(colors); // draw a sketch of a resistor
  // at this time only resistors with 3 colors is supported, skipping tolerance 
  WHILE count <= 3 DO
     PUTMENU(meny[active_menu], active_menu, count); 
     // Flush the mouse buffer
     WHILE MOUSE(1) ≥ 0 DO END;

     // Loop until we have a keyboard or mouse event
     REPEAT
       EVENTHANDLER();
     UNTIL eTYP <> "";

     CASE
       // If the event type was a keyboard action then process it
       IF eTYP == "K" THEN
         // If the ESC key was pressed set the program to end
         IF kP == 4 THEN
           KILL;
         END;        
       END;
       
       // If the event type was mouse action then process it
       IF eTYP == "M" THEN
         // Convert mouse coordinates to decimal
         mx := B→R(m1(1));  
         my := B→R(m1(2));
         // Determine if mouse coordinates are inside of a valid menu item otherwise return a zero
         GETMENU(mx,my,meny[active_menu]);
         IF mSEL > 0 AND mSEL <= 6 THEN
           CASE 
             // If menu item 1 and 6 need some special treatment
             IF mSEL == 1 THEN
               IF active_menu == 1 AND count > 1 THEN  // skip if on first color selection (black)
                 colors(count) := meny(active_menu,mSEL);
                 count := count + 1;
               ELSE  // previous pressed.
                 //RECT_P(); // clear screen
                 active_menu := 1;
                 PUTMENU(meny[active_menu],active_menu,count); 
               END;  
             END;
             
             IF mSEL == 6 THEN
               IF active_menu == 2 THEN
                 colors(count) := meny(active_menu,mSEL);
                 count := count + 1;
             ELSE  // next pressed
                //RECT_P(); // clear screen
                 active_menu := 2;
                 PUTMENU(meny[active_menu],active_menu,count); 
             END;    
           END;
           DEFAULT
              colors(count) := meny(active_menu,mSEL);
              count := count + 1;
           END; //case
           // draw the color if any color pressed
           IF count > 1 THEN 
              DRAW_COLOR(count-1,colors(count-1));
           END;         
         END; //mSel
       END; //eTyp
     END; //case
  END; //while colors

// we got 3 colors, now calculate the value
  res := "";
  FOR I FROM 1 TO SIZE(colors) do
    IF I == 3 THEN
      FOR J FROM 1 TO POS(lookup,colors(I)) - 1 DO
        res := res + "0"
      END 
    ELSE
      res := res + STRING(POS(lookup,colors(I)) - 1);
    END
  END;
  // cleanup
  LOCAL pre;
  LOCAL value;
  value := EXPR(res);
  
  IF value >= 1000 THEN
    IF value >= 1000000 THEN 
      value := value/1000000;
      pre :="MΩ";
    ELSE
      value := value / 1000;
      pre := "kΩ";
    END;
  ELSE
    pre :="Ω";
  END;
  
// print the value
  TEXTOUT_P("Resistance= " + value + " " +pre,50,150); 
  WAIT(2); 
  RETURN EXPR(res);
END;

EVENTHANDLER()
BEGIN
  eTYP := "";
  kP := GETKEY;
  IF kP <> -1 THEN
    eTYP := "K";
  ELSE
    m := MOUSE;
    m1 := m(1);
    IF  SIZE(m1) > 0 THEN
      eTYP := "M";
    END;
  END;
END;

// ----------------------------------------------
// Draw the menu using the list passed in
// ----------------------------------------------
PUTMENU(mTXT, num, c)
BEGIN
  CASE
    IF c == 1 AND num == 1 THEN  // selecting the frst color, no black 
      DRAWMENU("",mTXT(2),mTXT(3),mTXT(4),mTXT(5),mTXT(6));
    END;
    IF c==3 AND num == 2 THEN // about to select third color, removing grey and white
      DRAWMENU(mTXT(1),mTXT(2),mTXT(3),mTXT(4));
    END;
  DEFAULT
    DRAWMENU(mTXT(1),mTXT(2),mTXT(3),mTXT(4),mTXT(5),mTXT(6));
  END;
END;

// ------------------------------------------------------------------------------------------
// Get the number of the menu item selected (1-6) by checking mouse position
// Menu items with empty/blank text will return a zero
// ------------------------------------------------------------------------------------------
GETMENU(mx,my,mTXT)
BEGIN
 mSEL := 0;
 IF my≥220 AND my≤239 THEN
   CASE
     IF mx≥0 AND mx≤51 AND mTXT(1)>"" THEN
       mSEL := 1;
     END;
     IF mx≥53 AND mx≤104 AND mTXT(2)>"" THEN
       mSEL := 2;
     END;
     IF mx≥106 AND mx≤157 AND mTXT(3)>"" THEN
       mSEL := 3;
     END;
     IF mx≥159 AND mx≤210AND mTXT(4)>""  THEN
       mSEL := 4;
     END;
     IF mx≥212 AND mx≤263 AND mTXT(5)>"" THEN
       mSEL := 5;
     END;
     IF mx≥265 AND mx≤319 AND mTXT(6)>"" THEN
       mSEL := 6;
     END;
   END;
 END;
END;

// draw a resistor....
DRAW_RES(color_list)
BEGIN
   LOCAL mots, ring,colors;
   mots := {110,108,120,110,120,110,200,110,200,108,210,108,210,132,200,132,200,130,12​0,130,120,132,110,132,110,108};
   
   RECT_P();
   FILLPOLY_P(mots,#FFE5CCh);
   LINE_P(60,120,110,120,#00h);
   LINE_P(210,120,260,120,#00h);
END;

// draw the color 
DRAW_COLOR(number,color)
BEGIN
   LOCAL ring,colors;
   ring := {{130,110,142,130},{154,110,166,130},{178,110,190,130}};
   //               black          brown       red           orange      yellow       green        blue         purple       grey          white  
   colors := {#000000h,#994A00h,#FF0000h,#FF751Ah,#FFFF00h,#33CC33h,#3366FFh,#9933FFh,#8​08080h,#FFFFFFh};
   
   RECT_P(ring(number,1),ring(number,2),ring(number,3),ring(number,4),colors(P​OS(lookup,color)));
END;
but if I try Red Orange Yellow I can see the image, then the program quits...
I'm trying with v13441. Perhaps somewhere it need a pause...

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2018, 05:21 PM
Post: #3
RE: Resistor colors
Should not be any spaces, have sneaked in there at copy I guess. The program is supposed to quit after enter 3 colors and a pause on 2 seconds. It's because it should work at calculations.
Find all posts by this user
Quote this message in a reply
03-01-2018, 05:30 PM (This post was last modified: 03-01-2018 05:53 PM by salvomic.)
Post: #4
RE: Resistor colors
(03-01-2018 05:21 PM)LarsF Wrote:  Should not be any spaces, have sneaked in there at copy I guess. The program is supposed to quit after enter 3 colors and a pause on 2 seconds. It's because it should work at calculations.

then it is too quick for me (I'm getting old...) :-)

Salvo

EDIT: at start there is no "Black" color here. It appears only clicking on "Brown". Please, check...
However, it's also ok! It doesn't mean anything to input 0 ... as first value Smile

Nice program!
Sorry for my misunderstood...
Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2018, 06:17 PM
Post: #5
RE: Resistor colors
(03-01-2018 05:30 PM)salvomic Wrote:  
(03-01-2018 05:21 PM)LarsF Wrote:  Should not be any spaces, have sneaked in there at copy I guess. The program is supposed to quit after enter 3 colors and a pause on 2 seconds. It's because it should work at calculations.

then it is too quick for me (I'm getting old...) :-)

Salvo

EDIT: at start there is no "Black" color here. It appears only clicking on "Brown". Please, check...
However, it's also ok! It doesn't mean anything to input 0 ... as first value Smile

Nice program!
Sorry for my misunderstood...
Salvo

Just change the WAIT statement at the end of Resistor().
Should not be any Black at first colors, does not make sense :-)
Also at third color I have removed colors Grey and White, a bit too many zeros.
/Lars
Find all posts by this user
Quote this message in a reply
03-01-2018, 06:18 PM
Post: #6
RE: Resistor colors
(03-01-2018 06:17 PM)LarsF Wrote:  Just change the WAIT statement at the end of Resistor().
Should not be any Black at first colors, does not make sense :-)
Also at third color I have removed colors Grey and White, a bit too many zeros.
/Lars

Well done. Right!

ciao
Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)