draw()

[Main Functions]
void draw();

Description

The draw() function does precisely what its name suggests: it draws on the chip. This function is called repeatedly after power-up the chip, allowing your program to change the appearance of the chip, show the internal state of the chip, draw symbols, or even animate the chip.

Parameters

None.

Returns

Nothing.

Example Code

The code updates the color of a graphic element based on the state of the logical input a1. If a1 is false, the fillTriangle() function will display the color green, and if it is true, the color red will be displayed.

// Define the static variable a1
static bool a1;

void setup() {

    chipName("Inverter");

    // Define the input pin
    pinLabel(1, "1A");
    // Define the power pin
    pinLabel(2, "VCC");
    powerPin(2);
    // Define ground pin
    pinLabel(3, "GND");
    groundPin(3);
    // Define the output pin
    pinLabel(4, "1Y");
    pinMode(4, OUTPUT);

}

// Define the loop function
void loop() {
    // Write the negation of the input pin to the output pin
    a1 = digitalRead(1);
    digitalWrite(4, !a1);
}

// Define the color constants
#define RED 255, 0, 0
#define GREEN 0, 255, 0

// Define the draw function
void draw(){

    // Draw the not gate symbol
    int w = chipWidth();   // gets the width of the chip
    int h = chipHeight(); // gets the height of the chip
    int middle_w = w / 2;
    int middle_h = h / 2;

    drawTriangle(middle_h - 10 ,middle_w - 10,middle_h - 10, middle_w + 10, middle_h + 10, middle_w);
    drawLine(middle_h - 20, middle_w , middle_h -10, middle_w);
    drawLine(middle_h + 18, middle_w, middle_h +24, middle_w);
    drawCircle(middle_h + 14, middle_w, 4);

    // Set color based on a1 value
    if (!a1)
        setColor(GREEN);
    else
        setColor(RED);

    // Draw Fill
    fillTriangle(middle_h - 9 ,middle_w - 9,middle_h - 9, middle_w + 9, middle_h + 8, middle_w);

    // Reset the color
    setDefaultColor();
}