A twist to the computer that landed man on the moon. More information can be found at http://hackaday.io/project/294-Open-Source-DSKY

Dependencies:   DS1302 keypad mbed

Hardware

main.cpp

Committer:
VivaPenguinos
Date:
2014-04-24
Revision:
3:29cff763ae36
Parent:
2:aee9ee1296cd
Child:
4:99535d368a9f

File content as of revision 3:29cff763ae36:

/* Introduction
MBED DSKY
This is a personal project used developed by penguinos to mix old school technology with a modern twist.
Version 0.03: March. 8 2014
    - Planning to add a DS1302 RTC into the platform
Notable Credits:
NASA! - Release of technical documentations regarding the AGC and DSKY
Ron Burkley and Contributers of "Virtual AGC — AGS — LVDC — Gemini" - Providing Technical documentations, Source Code, explanations,
    and refrences on other sites to make this project successful

Warning: This may contain author crude language, random memes, and random thoughts.
 */
 
 /* Pin Usage
Real Time Clock
    SCLK    PTC5
    IO      PTC4
    CE      PTC3
Keypad
    Row0    PTD4
    Row1    PTA12
    Row2    PTD3
    Row3    PTA5
    Col1    PTA13
    Col2    PTD5
    Col3    PTD0
    Col4    PTD2
Shift Registers
    Latch   PTC6
    Clock   PTC10
    Data    PTC11
 */
 
// For RTC 
#define SCLK    PTC5
#define IO      PTC4
#define CE      PTC3

// Comment Line if DS1302 is already running
#define INITIAL_RUN

using namespace std;
#include "mbed.h"
#include "Keypad.h" // Keypad
#include "DS1302.h" // RTC for DS1032

DS1302 clk(SCLK, IO, CE); //DS1302 clk(SCLK, IO, PTC3);

//Declare Variables
int shift = 0; // Shift Data
// In the Arduino Version it uses a String. Will use an Int instead to see if this works on Mbed
int Verb_Number1 = 0;
int Verb_Number2 = 0;
int Noun_Number1 = 0;
int Noun_Number2 = 0;
int Enter_Number1 = 0;
int Enter_Number2 = 0;
int Verb; // DSKY Verb
int Noun; // DSkY Noun
int Enter; // DSKY Enter
// GET USE
int Set_GET_Seconds = 30; // Configure the Set Get in seconds. 1/100th of a second
char Set_get_miliseconds = 10;
char Set_Get_Seconds_Byte = 0;
char Set_Get_Seconds_Byte2 = 0;
// Shift Register
char data = 0;
char data2 = 0;
char data3 = 0;
char nibble[10] = { // Holds variables for nibbles for number 0-9 in hexes. nibble ALL the things!
    0x00, 0x01, 0x02, 0x03, 0x04,
    0x05, 0x06, 0x07, 0x08, 0x09
};
// Keypad
char Keytable[] = { '1', '2', '3', '+', // R0
                    '4', '5', '6', '-', // R1
                    '7', '8', '9', 'E', // R2
                    'V', '0', 'N', 'R', // R3
                  };
//  C0   C1   C2   C3

// RTC Variables
int TIME_VALUE = 1397702161;
int Time_Difference;

// Button State
int32_t Index = -1; // Button
int     State;

// Function Declaration
void Verb_Button();
void Noun_Button();
void Enter_Button();
void compare();
void Button_Sort(int sort); // Function that sorts Verb Values
void blinkAll(int amount, int delay);// Function that Blinks all LED
//void ShiftingOut(char myDataOut);

// Program Names
void Ground_Elapse_Time(); // Ground Elapse Time

// Pin modes for each pins
DigitalOut LatchPin(PTC6); //Pin for ST_CP of 74HC595 Pin # 12
DigitalOut ClockPin(PTC10); //Pin for SH_CP of 74HC595 Pin # 11
DigitalOut DataPin(PTC11);  //Pin for to DS of 74HC595 Pin # 14

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX); // tx, rx Serial Output to PC Enabling this WILL consume resources. Use only for debug

uint32_t cbAfterInput(uint32_t index)
{
    Index = index;
    return 0;
}

int main()
{
    wait(1);
    pc.printf("Shifting");
    blinkAll(4,1);
    pc.printf("Done");
    wait(1);
    State = 1;
    //             r0   r1   r2   r3   c0   c1   c2   c3
    Keypad keypad(PTD4, PTA12, PTD3, PTA5, PTA13, PTD5, PTD0, PTD2, 20);
    keypad.attach(&cbAfterInput);
    keypad.start();  // energize the columns c0-c3 of the keypad
    while (1) {
        __wfi();
        if (Index >-1) {
            pc.printf("Interrupted");
            pc.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]);
            if (Index == 12) { // Index 12 = Verb Button
                Verb_Button(); // Goes to Verb_Button 
            }
            if (Index == 14) { // Index 14 = Noun Button
                Noun_Button(); // Goes to Noun_Button
            }
            if (Index == 11) { // Index 11 = Enter Button
               Enter_Button(); // Goes to Enter_Button
            }
            Index = -1;
        }
        // Debug use only
        //pc.printf("%c", Keytable[Verb_Number1]);
        //pc.printf("%c", Keytable[Verb_Number2]);
        //pc.printf("%c", Keytable[Noun_Number1]);
        //pc.printf("%c", Keytable[Noun_Number2]);
    }
}

/* This function is used when the Verb Button has been pressed. Take the inputs of the button twice and determine if the buttons are integers.
If other buttons are pressed send it to their perspective locations.
*/
void Verb_Button(){
        Verbz: // Don't Ask
        // All Variables for Verb, Noun and Enter are zeroed out
        Verb_Number1 = 0;
        Verb_Number2 = 0;
        Noun_Number1 = 0;
        Noun_Number2 = 0;
        Enter_Number1 = 0;
        Enter_Number2 = 0;
        pc.printf("Please enter a Verb Number");          
        __wfi();
        Verb_Number1 = Index;
        if (Verb_Number1 == 12){ // This is section is used on atleast one of the two Verb_Number variabels to prevent a Nestled If within if within if.
            goto Verbz; // Line 110
        }
        else{
            Button_Sort(Verb_Number1);
        }
        //wait(0.01); // Is needed so that Verb_Number1 can store the variable from the keypad. Not sure why it requires an additional job for it to store the data.
        //pc.printf("%c", Verb_Number1);
        //pc.printf("%c", Keytable[Index]);
        __wfi();
        Verb_Number2 = Index;
        Button_Sort(Verb_Number2);
        //wait(0.01); // Is needed so that Verb_Number2 can store the variable form the keypad. Not sure why it requires an additional job for it to store the data.
        //pc.printf("%c", Verb_Number2);
        //pc.printf("%c", Verb_Number1);
        //pc.printf("%c\n", Verb_Number2);
}

/* This Function is used when the Verb Button has been Pressed. Take the inputs of the button twice and determine if the buttons are integers.
If other buttons are pressed send it to their perspective locations.
*/
void Noun_Button()
{
    Nounz:
    pc.printf("Please enter a Noun Number");
    __wfi();
    Noun_Number1 = Index;
    if (Noun_Number1 == 14) {// This is section is used on atleast one of the two Noun_Number variabels to prevent a Nestled If within if within if.
        goto Nounz; //Line 140
        }
    else{
        Button_Sort(Noun_Number1);
        }
    __wfi();
    Noun_Number2 = Index;
    Button_Sort(Noun_Number2);
}

/* This function is used wen the Enter button has been pressed. This function is a special circumstance since it will be checking both the Verb and Noun variables before entering variable to Enter.
*/
void Enter_Button()
{
    Enterz:
    pc.printf("Checking Whether there is a variable stored in Noun\n");
    if (Noun_Number1 == 0 && Noun_Number2 == 0){
        pc.printf("Please enter a Enter Number");
        __wfi();
        Enter_Number1 = Index;
        if (Enter_Number1 == 11) { // This section is used on atleast one of the two Enter_Number variable to prevent a nestled if within if within if.
            goto Enterz;
            }
        else{
            Button_Sort(Enter_Number1);
            }
        __wfi();
        Enter_Number2 = Index;
        Button_Sort(Enter_Number2);
    }
    if (Noun_Number1 != 0 && Noun_Number2 != 0){ 
        pc.printf("Check completed, Executing");
        compare();
    }
    
}

// This function is used to sort the button press for Verb and Noun.
void Button_Sort(int Sort)
{
        // Determines what button are pressed before moving on to the 2nd button.
    if (Sort == 12){ //comparing the array number NOT what value in the array it is. Actual Array value is V
        Verb_Button();
        }
    else if (Sort == 14) { //comparing the array number NOT what value in the array it is. Actual Array value is N
        Noun_Button();
        }
    else if (Sort == 3) { //comparing the array number NOT what value in the array it is. Actual Array value is +
        pc.printf("+");
        }
    else if (Sort == 7) { //comparing the array number NOT what value in the array it is. Actual Array value is -
        pc.printf("-");
        }
    else if (Sort == 15) { //comparing the array number NOT what value in the array it is. Actual Array value is R
        pc.printf("R");
        }
}
// Compares the button to what program the DSKY should be running. This section will be inefficent until I can improve on my programming skills :]
void compare()
{
    if (Verb_Number1 == 0 && Verb_Number2 == 6 && Noun_Number1 == 2 && Noun_Number2 == 6 && Enter_Number1 == 0 && Enter_Number2 == 0) {// Verb 16, Noun 36, Enter 00
        //Configure GET
        pc.printf("Configuring GET");
        Ground_Elapse_Time();
    } else if (Verb_Number1 == 3 && Verb_Number2 == 5 && Noun_Number1 == 0 && Noun_Number2 == 0) {
        // Test Light
    } else if (Verb == 25 && Noun == 36) {
        // Configure GET
    } else if (Verb == 37 && Enter == 00) {
        // Idle Program
    }
}

void ShiftingOut(int myDataOut)
{
    // This shifts 8 bits out to the MSB first, The the rising edge of the clock, while clock idles low.
    // Internal Fucntions
    DataPin = 0;
    ClockPin = 0;
    int i= 0;
    int pinState;
    // Clears Everything within shift registers
    DataPin = 0;
    ClockPin = 0;

    for (i=7; i>= 0; i--) {
        ClockPin = 0;

        // Value passed to my data out
        if ( myDataOut & (1<<i)) {
            pinState = 1;
            myled = 1;
            pc.printf("1");
        } else {
            pinState = 0;
            myled = 0;
            pc.printf("0");
        }
        DataPin = pinState;
        ClockPin = 1;
        DataPin = 0;
    }
    ClockPin = 0;
    DataPin = 1;
}

//blinks the whole registered based on the number of times you want to blink with a certain delay
void blinkAll(int amount, int delay)
{
    LatchPin = 0;
    ShiftingOut(0);
    ShiftingOut(0);
    LatchPin = 1;
    wait(0.2);
    for (int x = 0; x < amount; x++) {
        LatchPin = 0;
        pc.printf("sending");
        ShiftingOut(255);
        ShiftingOut(255);
        ShiftingOut(255);
        LatchPin = 1;
        wait(delay);
        LatchPin = 0;
        ShiftingOut(0);
        ShiftingOut(0);
        ShiftingOut(0);
        LatchPin = 1;
        wait(delay);
    }
}

// Need to fill this too...
void Ground_Elapse_Time()
{
    #ifdef INITIAL_RUN
    clk.set_time(TIME_VALUE);
    #endif
    char storedByte = clk.recallByte(0);
    printf("\r\nStored byte was %d, now increasing by one\r\n", storedByte);
    clk.storeByte(0, storedByte + 1);
    
    while(1) {
        time_t seconds = clk.time(NULL);
        Time_Difference = seconds - TIME_VALUE;
        printf("%d \n", Time_Difference);
        //printf("Time as a basic string = %s\r", ctime(&seconds));
        wait(1);
    }
}