wemos에서 논리 시뮬레이터

개요
wemos에서 논리 시뮬레이터를 만들어 보았다.
xor, and, or를 설치합니다.
사진.

실행

샘플 코드

#define B_T                 1
#define B_D                 10
const int s = 2;
const int c = 5;
const int d = 4;
char buf[20];
char w[8];
char io[8];
char p0[8];
char p1[8];
char p2[8];
char p3[8];
int Ptr = 0;
int OP = 0;
char Val;
char u = 0;
char RD(int ado)
{
    char v = buf[ado];
    return v;
}
void WR(int ado, char data)
{
    buf[ado] = data;
}
void putchr(char c)
{
    Serial.print(c);
}
boolean num()
{
    if (48 <= RD(Ptr) && RD(Ptr) <= 57) return true;
    else return false;
}
boolean getnm2()
{
    char ch;
    if (!num()) return false;
    int n = 0;
    do
    {
        n *= 10;
        ch = RD(Ptr++);
        n += ch - 48;
    } while (num());
    Val = n;
    return true;
}
uint8_t shiftInMod(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t C_T, uint16_t C_D)
{
    uint8_t value = 0;
    uint8_t i;
    for (i = 0; i < 8; ++i)
    {
        digitalWrite(clockPin, (C_T ? LOW : HIGH));
        delayMicroseconds(C_D);
        if (bitOrder == LSBFIRST)
        { 
            value |= digitalRead(dataPin) << i;
        }
        else
        {
            value |= digitalRead(dataPin) << (7 - i);
        }
        digitalWrite(clockPin, (C_T ? HIGH : LOW));
    }
    return value;
}
void shiftOutMod(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t C_T, uint16_t C_D, uint8_t val)
{
    uint8_t i;
    for (i = 0; i < 8; i++)
    {
        if (bitOrder == LSBFIRST)
        {
            digitalWrite(dataPin, !!(val & (1 << i)));
        }
        else
        {
            digitalWrite(dataPin, !!(val & (1 << (7 - i))));
        }
        digitalWrite(clockPin, (C_T ? LOW : HIGH));
        delayMicroseconds(C_D);
        digitalWrite(clockPin, (C_T ? HIGH : LOW));
    }
}
void sendCommand(uint8_t value)
{
    digitalWrite(s, LOW);
    shiftOutMod(d, c, LSBFIRST, B_T, B_D, value);
    digitalWrite(s, HIGH);
}
uint8_t readButtons(void)
{
    uint8_t buttons = 0;
    digitalWrite(s, LOW);
    shiftOutMod(d, c, LSBFIRST, B_T, B_D, 0x42);
    pinMode(d, INPUT);
    for (uint8_t i = 0; i < 4; i++)
    {
        uint8_t v = shiftInMod(d, c, LSBFIRST, B_T, B_D) << i;
        buttons |= v;
    }
    pinMode(d, OUTPUT);
    digitalWrite(s, HIGH);
    return buttons;
}
void reset()
{
    sendCommand(0x40);
    digitalWrite(s, LOW);
    shiftOutMod(d, c, LSBFIRST, B_T, B_D, 0xc0);
    for (uint8_t i = 0; i < 16; i++)
    {
        shiftOutMod(d, c, LSBFIRST, B_T, B_D, 0x00);
    }
    digitalWrite(s, HIGH);
}
void l_on(uint8_t v)
{
    if (v == 0)
    {
        u = u | 0x08;
    }
    if (v == 1)
    {
        u = u | 0x04;
    }
    if (v == 2)
    {
        u = u | 0x02;
    }
    if (v == 3)
    {
        u = u | 0x01;
    }
    if (v == 4)
    {
        u = u | 0x80;
    }
    if (v == 5)
    {
        u = u | 0x40;
    }
    if (v == 6)
    {
        u = u | 0x20;
    }
    if (v == 7)
    {
        u = u | 0x10;
    }
}
void l_off(uint8_t v)
{
    if (v == 0)
    {
        u = u & (~0x08);
    }
    if (v == 1)
    {
        u = u & (~0x04);
    }
    if (v == 2)
    {
        u = u & (~0x02);
    }
    if (v == 3)
    {
        u = u & (~0x01);
    }
    if (v == 4)
    {
        u = u & (~0x80);
    }
    if (v == 5)
    {
        u = u & (~0x40);
    }
    if (v == 6)
    {
        u = u & (~0x20);
    }
    if (v == 7)
    {
        u = u & (~0x10);
    }
}
void setup()
{
    Serial.begin(115200);
    while (!Serial)
    {
        ;
    }
    Serial.println("ronri");
    pinMode(s, OUTPUT);
    pinMode(c, OUTPUT);
    pinMode(d, OUTPUT);
    sendCommand(0x8f);
    reset();
}
void loop()
{
    int i;
    while (Serial.available() > 0)
    {
        char ch = Serial.read();
        putchr(ch);
        if (ch == '\r')
        {
            WR(Ptr++, '\0');
            if (strncmp(buf, "list", 4) == 0)
            {
                Serial.println(" ok");
                for (i = 0; i < OP; i++)
                {
                    Serial.print(p0[i], DEC);
                    Serial.print(" ");
                    Serial.print(p1[i], DEC);
                    Serial.print(" ");
                    Serial.print(p2[i], DEC);
                    Serial.print(" ");
                    Serial.println(p3[i], DEC);
                }
            }
            else if (strncmp(buf, "new", 3) == 0)
            {
                Serial.println(" ok");
                OP = 0;
            }
            else if (strncmp(buf, "run", 3) == 0)
            {
                Serial.println(" ok");
                while(1)
                {
                    uint8_t buttons = readButtons();
                    if (buttons == 0x04)
                    {
                        w[0] = !w[0];
                    }
                    else if (buttons == 0x40)
                    {
                        w[1] = !w[1];
                    }
                    else if (buttons == 0x08)
                    {
                        w[2] = !w[2];
                    }
                    else if (buttons == 0x80)
                    {
                        w[3] = !w[3];
                    }
                    else
                    {
                    }
                    for (i = 0; i < OP; i++)
                    {
                        if (p0[i] == 0) w[p3[i]] = w[p1[i]] ^ w[p2[i]];
                        if (p0[i] == 1) w[p3[i]] = w[p1[i]] & w[p2[i]];
                        if (p0[i] == 2) w[p3[i]] = w[p1[i]] | w[p2[i]];
                    }
                    for (i = 0; i < 8; i++)
                    {
                        if (io[i] == 1)
                        {
                            if (w[i] == 0)
                            {
                                l_off(i);
                            }
                            else
                            {
                                l_on(i);
                            }
                        }
                        if (io[i] == 2)
                        {
                            if (w[i] == 0)
                            {
                                l_off(i);
                            }
                            else
                            {
                                l_on(i);
                            }
                        }
                    }
                    sendCommand(0x44);
                    digitalWrite(s, LOW);
                    shiftOutMod(d, c, LSBFIRST, B_T, B_D, 0xce);
                    shiftOutMod(d, c, LSBFIRST, B_T, B_D, u);
                    digitalWrite(s, HIGH);
                    delay(300);
                }
            }
            else if (strncmp(buf, "in ", 3) == 0)
            {
                Serial.println(" ok");
                Ptr = 3;
                if (getnm2())
                {
                    io[Val] = 1;
                }
            }
            else if (strncmp(buf, "wire ", 5) == 0)
            {
                Serial.println(" ok");
                Ptr = 5;
                if (getnm2())
                {
                    io[Val] = 0;
                }
            }
            else if (strncmp(buf, "out ", 4) == 0)
            {
                Serial.println(" ok");
                Ptr = 4;
                if (getnm2())
                {
                    io[Val] = 2;
                }
            }
            else if (strncmp(buf, "xor ", 4) == 0)
            {
                Serial.println(" ok");
                Ptr = 4;
                p0[OP] = 0;
                if (getnm2())
                {
                    p1[OP] = Val;
                    Ptr++;
                    if (getnm2())
                    {
                        p2[OP] = Val;
                        Ptr++;
                        if (getnm2())
                        {
                            p3[OP] = Val;
                        }
                    }
                }
                OP++;
            }
            else if (strncmp(buf, "and ", 4) == 0)
            {
                Serial.println(" ok");
                Ptr = 4;
                p0[OP] = 1;
                if (getnm2())
                {
                    p1[OP] = Val;
                    Ptr++;
                    if (getnm2())
                    {
                        p2[OP] = Val;
                        Ptr++;
                        if (getnm2())
                        {
                            p3[OP] = Val;
                        }
                    }
                }
                OP++;
            }
            else if (strncmp(buf, "or ", 3) == 0)
            {
                Serial.println(" ok");
                Ptr = 3;
                p0[OP] = 2;
                if (getnm2())
                {
                    p1[OP] = Val;
                    Ptr++;
                    if (getnm2())
                    {
                        p2[OP] = Val;
                        Ptr++;
                        if (getnm2())
                        {
                            p3[OP] = Val;
                        }
                    }
                }
                OP++;
            }
            else
            {
                Serial.println(" err");
            }
            Ptr = 0;
        }
        else
        {
            WR(Ptr++, ch);
        }
    }
}





이상.

좋은 웹페이지 즐겨찾기