Menu

Ellipse and Circle Command for GLCD

MBB
2014-05-09
2014-05-10
  • MBB

    MBB - 2014-05-09

    I wrote some software that will create Horizontal and Vertical Ellipses on a GLCD screen. It also does Circles. Here is the code:

    ''''
    ;Chip Settings
    #chip 18F452,20
    #config OSC=HS

    ;Include files (Libraries)
    #include <GLCD.h>

    ;Defines (Constants)
    #define GLCD_DB0 PORTB.3 'D0 to pin 4 on LCD
    #define GLCD_DB1 PORTB.2 'D1 to pin 5 on LCD
    #define GLCD_DB2 PORTB.1 'D2 to pin 6 on LCD
    #define GLCD_DB3 PORTB.0 'D3 to pin 7 on LCD
    #define GLCD_DB4 PORTD.7 'D4 to pin 8 on LCD
    #define GLCD_DB5 PORTD.6 'D5 to pin 9 on LCD
    #define GLCD_DB6 PORTD.5 'D6 to pin 10 on LCD
    #define GLCD_DB7 PORTD.4 'D7 to pin 11 on LCD
    #define GLCD_CS2 PORTC.7 'D8 to pin 12 on LCD - CS1, CS2 are backwards
    #define GLCD_CS1 PORTC.6 'D9 to pin 13 on LCD - CS1, CS2 are backwards
    #define GLCD_RESET PORTC.5 'D10 to pin 14 of LCD
    #define GLCD_RW PORTC.4 'D11 to pin 15 of LCD
    #define GLCD_RS PORTD.3 'D12 to pin 16 D/I pin on LCD
    #define GLCD_ENABLE PORTD.2 'D13 to Pin 17 on LCD

    ;Variables
    Dim MajV, MajV3, MinV, MinV3, Xvalue, Yvalue, OffsetX, OffsetY, MajVTemp, MajVTemp3 As integer
    Dim MinV2, MajV2, Maj_Min, Min_Maj, Xaxis_Hor_ellipse, Xaxis_Hor_ellipse2, Yaxis_Hor_ellipse, Yaxis_Hor_ellipse2, Yaxis, Xaxis, Xaxis, Yaxis, y_1, G_1, MajVTemp2, Maj_MinTemp As long

    InitGLCD
    '
    GLCDCLS

    ' Some sample HORIZONTAL ellipses - MajV > MinV
    Ellipse 20, 12, 5, 5, 0; Horizontal Ellipse on top left of display
    Ellipse 20, 12, 85, 5, 0; Horizontal Ellipse on top right of display
    Ellipse 20, 12, 5, 34, 0; Horizontal Ellipse on bottom left of display
    Ellipse 20, 12, 85, 34, 0; Horizontal Ellipse on bottom right of display
    Ellipse 58, 20, 6, 12, 0; Large centered Horizontal ellipse
    wait 3 s

    '
    GLCDCLS

    ' Some sample VERTICAL ellipses - MajV > MinV
    Ellipse 15, 10, 30, 0, 1; Small Vertical Ellipse on top left of display
    Ellipse 15, 10, 80, 0, 1; SmallVertical Ellipse on top right of display
    Ellipse 15, 10, 30, 32, 1; Small Vertical Ellipse on bottom left of display
    Ellipse 15, 10, 80, 32, 1; Small Vertical Ellipse on bottom right of display
    Ellipse 30, 10, 5, 0, 1; Large Vertical ellipse on left side
    Ellipse 32, 10, 55, 0, 1; Large Vertical ellipse in middle
    Ellipse 32, 10, 105, 0, 1; Large Vertical ellipse on right side

    '
    wait 3 s
    GLCDCLS

    ' Some sample CIRCLES - MajV = MinV, HVtype = don't care, Circle Radius = MajV = MinV
    Ellipse 15, 15, 10, 0; Circle on top left of display
    Ellipse 15, 15, 85, 0; Circle on top right of display
    Ellipse 15, 15, 10, 32; Circle on bottom left of display
    Ellipse 15, 15, 85, 32; Circle on bottom right of display
    Ellipse 20, 20, 43, 10; Circle in center of display

    'SUB ROUTINES ******
    '
    '*******
    ***

    Sub Ellipse (In MajV, In MinV, In OffsetX, In OffsetY, In HVtype)
    '*********
    'MajV is distance from center to a major vertex
    'MinV is distance from center to a minor vertex
    'OffsetX is the distance from the y-axis to the left most edge of the ellipse
    'OffsetY is the distance from the x-axis to the top most edge of the ellipse
    'HVtype is 0 for HORIZONTAL or 1 for VERTICAL Ellipse and not needed for CIRCLE
    ' SYNTAX is Ellipse MajV, MinV, OffsetX, OffsetY, HVtype
    '
    *********
    If HVtype = 1 then

        'Swap values to use same equations for Horizontal and Vertical ellipses
        MajVTemp = MajV
        MajV = MinV
        MinV = MajVTemp
    
        '
    End If
    
    '******************************************************
    'For a 128x64 GLCD -
    'HORIZONTAL ELLIPSE
    ' If 2*(Major Axis) plus OffsetX > 127, ellipse will move off RIGHT edge of screen
    ' If 2*(Minor Axis) plus OffsetY > 63, ellipse will move off BOTTOM edge of screen
    'VERTICAL ELLIPSE
    ' If 2*(Major Axis) plus OffsetY > 63, ellipse will move off BOTTOM edge of screen
    ' If 2*(Minor Axis) plus OffsetY > 127, ellipse will move off RIGHT edge of screen
    ' The below four IF statements address this situation by reducing Ellipse size and OffsetX and/or OffsetY
    '
    If MinV > 31 then
        MinV = 31; keep ellipse on screen
    end if
    
    '
    If MajV > 63 then
        MajV = 63; keep ellipse on screen
    end if
    
    '
    If ((2 * MajV) + OffsetX) > 127 then
        OffsetX = 127 - (2 * MajV)
    end If
    
    '
    If ((2 * MinV) + OffsetY) > 63 then
        OffsetY = 63 - (2 * MinV)
    end If
    
    '******************************************************
    '
    MajV3 = MajV * (-1); Negative of MajV
    MinV3 = MinV * (-1); Negative of MinV
    MinV2 = MinV * MinV; MinV squared
    MajV2 = MajV * MajV; MajV squared
    
    '
    Maj_Min = (100 * MajV2) / MinV2; Scale up by 100
    
    '
    Min_Maj = (100 * MinV2) / MajV2; Scale up by 100
    
    '******************************************************
    'Solve for Horizontal Ellipse X-value and then Y value
    'Solve for Vertical Ellipse Y-value and then X value
    For Yvalue = MinV3 to MinV
        Xaxis_Hor_ellipse2 = (Maj_Min * (MinV2 - (Yvalue * Yvalue))) / 100; Scale down by 100 and Solve for X
        Sq_Root Xaxis_Hor_ellipse2, Xaxis_Hor_ellipse
    
        '
        Yaxis = Yvalue + MinV + OffsetY
        Xaxis = abs(Xaxis_Hor_ellipse - MajV) + OffsetX
        Xaxis_Hor_ellipse = Xaxis_Hor_ellipse + MajV + OffsetX
        pset Xaxis, Yaxis, on; Left half of ellipse
        pset Xaxis_Hor_ellipse, Yaxis, on; Right half of ellipse
    
        '
    Next Yvalue
    
    '******************************************************
    ' Fill in openings
    For Xvalue = MajV3 to MajV
        Yaxis_Hor_ellipse2 = (Min_Maj * (MajV2 - (Xvalue * Xvalue))) / 100; Scale down by 100 and Solve for Y
        Sq_Root Yaxis_Hor_ellipse2, Yaxis_Hor_ellipse
    
        '
        Xaxis = Xvalue + MajV + OffsetX
        Yaxis = Yaxis_Hor_ellipse + MinV + OffsetY; Top half of ellipse
        Yaxis_Hor_ellipse = abs(Yaxis_Hor_ellipse - MinV) + OffsetY; Top half of ellipse
        pset Xaxis, Yaxis_Hor_ellipse, on; Top half of ellipse
        pset Xaxis, Yaxis, on; Bottom half of ellipse
    Next Xvalue
    

    End Sub

    Sub Sq_Root (In y_1, G_1)
    'SQUARE ROOT Approximation
    G_1 = 600; First Guess for 6 Digit y_1
    If y_1 < 100000 then
    G_1 = 200; First Guess for 5 Digit y_1
    End If
    If y_1 < 10000 then
    G_1 = 60; First Guess for 4 Digit y_1
    End If
    If y_1 < 1000 then
    G_1 = 20; First Guess for 3 Digit y_1
    End If
    If y_1 < 100 then
    G_1 = 6; First Guess for 2 Digit y_1
    End If
    If y_1 < 10 then
    G_1 = 2; First Guess for 1 Digit y_1
    End If

    '
    G_1 = (G_1 + (y_1 / G_1))/2
    G_1 = (G_1 + (y_1 / G_1))/2
    G_1 = (G_1 + (y_1 / G_1))/2
    G_1 = (G_1 + (y_1 / G_1))/2
    G_1 = (G_1 + (y_1 / G_1))/2; Square Root of y_1
    

    End Sub

    ''''

     
  • Anonymous

    Anonymous - 2014-05-09

    This is great! Do you plan on adding the remaining two conics?

    And it just occurred to me that rounded rectangles might be useful for creating menu boxes.

    Anyway, between you, Hugh and Anobium, we're getting quite a good collection of GLCD stuff now. Thanks to all!

     
  • MBB

    MBB - 2014-05-10

    Yes, I plan on working on the hyperbola and parabola.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.