Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Hyperbola, Ellipse, and Circle commands for GLCD

MBB
2014-05-14
2014-05-17
  • MBB
    MBB
    2014-05-14

    I previously posted a program that plotted ellipses and circles on a GLCD. I've now added Hyperbolas to this software. The entire program is below.

        ;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
        #define LCD_IO 2
        #define LCD_CB PORTC.1
        #define LCD_DB PORTC.2
    
    ;Variables
    Dim MajVTemp2, Maj_MinTemp As long; ellipse 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 As long
    Dim Yaxis_Hor_ellipse, Yaxis_Hor_ellipse2, Yaxis, Xaxis, y_1 As long
    Dim TransAxis, ConjAxis, ValueX, ValueY, Xoffset, Yoffset As integer; hyperbola variables
    Dim AxisX, AxisY, AxisXX, AxisYY, AxisX_Hor_hyperbola, ValueX2, ValueY2 As integer
    Dim TransAxis2, Trans_Conj, AxisX_Hor_hyperbola2 As long
    Dim ConjAxis2, AxisY_Hor_hyperbola2, AxisY_Hor_hyperbola As long
    Dim G_1 As long; square root variable
    
    '
    InitGLCD
    
    '
    GLCDCLS
    
    'Sample Left-Right Hyperbola
    Hyperbola 15, 10, 63, 31, 3; Hyperbola in center of display
    wait 3 s
    GLCDCLS
    
    'Sample Top-Bottom Hyperbola
    Hyperbola 5, 5, 63, 31, 4; Hyperbola in center of display
    wait 3 s
    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 Hyperbola (In TransAxis, In ConjAxis, In Xoffset, In Yoffset, In HypType)
        '
        ConjAxis2 = ConjAxis * ConjAxis; Conjugate Axissquared
        TransAxis2 = TransAxis * TransAxis ; TransAxis squared
        Trans_Conj = (100 * TransAxis2) / ConjAxis2; Scale up by 100
    
        '******************************************************
        If HypType = 3 then
            For ValueY = -32 to 32; ?????????????????????????
                ValueY2 = ValueY * ValueY
                On_Off1 = 1; To turn PSET on or off
                On_Off2 = 1
                AxisX_Hor_hyperbola2 = (Trans_Conj * (ConjAxis2 + ValueY2)) / 100; Scale down by 100
                Sq_Root AxisX_Hor_hyperbola2, AxisX_Hor_hyperbola
                AxisY = ValueY + Yoffset
                AxisX = Xoffset - AxisX_Hor_hyperbola
                AxisX_Hor_hyperbola = AxisX_Hor_hyperbola + Xoffset
                AxisYY = AxisY
    
                'Prevent negative values from plotting mirror image on display
                If (AxisX_Hor_hyperbola > 127) OR (AxisY > 63) OR (AxisX_Hor_hyperbola < 0) OR (AxisY < 0) then
                    AxisX_Hor_hyperbola = 0
                    AxisY = 0
                    On_Off2 = 0
                end if
    
                'Prevent negative values from plotting mirror image on display
                If (AxisX > 127) OR (AxisYY > 63) OR (AxisX < 0) OR (AxisYY < 0) then
                    AxisX = 0
                    AxisYY = 0
                    On_Off1 = 0
                end if
    
                '
                pset AxisX, AxisYY, On_Off1; Left half of hyperbola
                pset AxisX_Hor_hyperbola, AxisY, On_Off2; Right half of hyperbola
            Next ValueY
        End If
    
        '
        If HypType = 4 then
            For ValueX = -64 to 64
                ValueX2 = ValueX * ValueX
                On_Off1 = 1
                On_Off2 = 1
                AxisY_Hor_hyperbola2 = (Trans_Conj * (TransAxis2 + ValueX2 )) / 100; Scale down by 100
                Sq_Root AxisY_Hor_hyperbola2, AxisY_Hor_hyperbola
                AxisX = ValueX + Xoffset
                AxisY = AxisY_Hor_hyperbola + Yoffset;
                AxisY_Hor_hyperbola = Yoffset - AxisY_Hor_hyperbola;
                AxisXX = AxisX
    
                'Prevent negative values from plotting mirror image on display
                If (AxisY_Hor_hyperbola > 63) OR (AxisY > 63) OR (AxisY_Hor_hyperbola < 0) OR (AxisY < 0) then
                    AxisY_Hor_hyperbola = 0
                    AxisX = 0
                    On_Off2 = 0
                end if
    
                'Prevent negative values from plotting mirror image on display
                If (AxisXX > 127) OR (AxisY > 63) OR (AxisXX < 0) OR (AxisY < 0) then
                    AxisY = 0
                    AxisXX = 0
                    On_Off1 = 0
                end if
                pset AxisX, AxisY_Hor_hyperbola, On_Off2; Top half of hyperbola
                pset AxisXX, AxisY, On_Off1; Bottom half of hyperbola
            Next ValueX
        End If
    End Sub
    
    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;
    
            '
            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
    

    @edited by Anobium to change formatting only.

     
    Last edit: Anobium 2014-05-17
  • Anobium
    Anobium
    2014-05-17

    A really nice piece of work.

    I just copied the code, changed the start to my ST7920 GLCD and everything works. Smooth and fast.

    Nice job.