New project

Dependencies:   mbed TextLCD

main.cpp

Committer:
jasminealice
Date:
2018-06-11
Revision:
20:32ba0a5f2d02
Parent:
18:f5824ba95892
Child:
21:31647d80614f

File content as of revision 20:32ba0a5f2d02:

#include "mbed.h"
#include "TextLCD.h"
#include "MCP23017.h"
#include "Train.h"
#include "Switch.h"
#include "Track.h"

#include <ctime>

//Board 1

/*----------------------------------------------------------------------------
Pin definitions
*----------------------------------------------------------------------------*/
DigitalOut Track(p20);      // train track

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
DigitalOut externalLed1(p15);
DigitalOut externalLed2(p16);
DigitalOut externalLed3(p17);
DigitalOut externalLed4(p18);

TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd

//AnalogIn Ain(p20);          // pot. met.
/* Train detectors d2, d21, d22 (trainstation) */
// detect_21(p17);
//DigitalIn detect_22(p16);
//DigitalIn detect_2(p15);

DigitalIn sw1(p29);
DigitalIn sw2(p30);
DigitalIn sw3(p11);
DigitalIn sw4(p12);
//InterruptIn sw1(p5);
InterruptIn inter0(p13);
InterruptIn inter1(p14);
MCP23017 *mcp;

/*----------------------------------------------------------------------------
Addresses
*----------------------------------------------------------------------------*/
const unsigned int DCCaddress_darkRed = 0x01;
const unsigned int DCCaddress_lightRed = 0x03;
const unsigned int DCCaddress_switch = 0x06;

/*----------------------------------------------------------------------------
Train movement
*----------------------------------------------------------------------------*/
//move backwards/reverse
//const unsigned int DCCinst_reverse = 0x48; //reverse speed

//speed dial forward
//const unsigned int DCCinst_step2 = 0x72; //step 2
//const unsigned int DCCinst_step4 = 0x73; //step 4
//const unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed
const unsigned int DCCinst_step13 = 0x78; //step 13 1/2 speed
//const unsigned int DCCinst_step20 = 0x75; //step 20 3/4 speed
//const unsigned int DCCinst_step28 = 0x7F; //step 28 Full speed
const unsigned int DCCinst_switch1 = 0x81; //Activate switch1
const unsigned int DCCinst_switch2 = 0x82; //Activate switch2
const unsigned int DCCinst_switch3 = 0x84; //Activate switch3
const unsigned int DCCinst_switch4 = 0x88; //Activate switch4
const unsigned int DCCinst_deactive_switch = 0x80; //Deactivate switches
//stop 
const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000

//detectors addresses
const unsigned int detector_0 = 0xFFFE;
const unsigned int detector_0_1 = 0xFFFC;
const unsigned int detector_1 = 0xFFFD;

/*----------------------------------------------------------------------------
Function definitions
*----------------------------------------------------------------------------*/
void readVoltage();
bool readDetector(DigitalIn detector);
bool readSwitch(DigitalIn theSwitch);
//void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command
void initialize_mcp();


/*----------------------------------------------------------------------------
Main
*----------------------------------------------------------------------------*/
int main() {
    /*sw1.rise(&testInterupt2);
    sw1.fall(&testInterupt);
    inter0.rise(&riseFunction);
    inter1.rise(&riseFunction);*/
    Train lightRed(DCCaddress_darkRed, DCCinst_step13);
    //Train darkRed(DCCaddress_darkRed);
    Switch switch1(DCCaddress_switch,DCCinst_switch1);
    //Switch switch2(DCCaddress_switch,DCCinst_switch2);
    Switch switch3(DCCaddress_switch,DCCinst_switch3);
    Switch switch4(DCCaddress_switch,DCCinst_switch4);

    while(1){
        if(readSwitch(sw1)){
        //DCC_send_command(DCCaddress_switch,DCCinst_switch3,10); //Make sure to deactivate 2 right after activating it
        switch1.switchOn();    
        //lcd.printf("%d",switch1.switchOn());
        lcd.printf("Switch 1 on");
        //switch2.switchOn();  // Need to turn it off immediately!!!
        switch3.switchOn();
        switch4.switchOn();
    
        }
        else{
        //DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
        switch1.switchOff();
        //switch2.switchOff();
        switch3.switchOff();
        switch4.switchOff();
        lcd.printf("Switch 1 off");    
            
        }
            
        if(readSwitch(sw2)){
            lightRed.goForward(DCCinst_step13);
        }
        else 
        {
            lightRed.Stop(DCCinst_stop);
        }
    }
    
    
    /*lcd.printf("Start the journey");
    time_t tstart, tend; 
    initialize_mcp();
    int data = mcp->readRegister(0x12);
    lcd.printf("%d", data);
    wait(2);
    
    while(1){
        
        wait(0.2);
        if(readSwitch(sw1)){
            //lcd.cls();
            //lcd.printf("Forward");
            tstart = time(0);
            DCC_send_command(DCCaddress_darkRed,DCCinst_step13,10); // forward half speed train address 3
            tend = time(0); 
            lcd.cls();
            lcd.printf("Time to send command:");
            lcd.printf("%f", difftime(tend, tstart));

        }else{
            //lcd.cls();
            //lcd.printf("Stop");
            tstart = time(0);
            DCC_send_command(DCCaddress_darkRed,DCCinst_stop,10); // forward half speed train address 3
            tend = time(0); 
            lcd.cls();
            lcd.printf("Time to send command:");
            lcd.printf("%f", difftime(tend, tstart));
        }
        
        if(readSwitch(sw2)){
             
             DCC_send_command(DCCaddress_switch,DCCinst_switch1,10);
             myled1 = 1;
             externalLed1 = 1;
             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
             myled1 = 0;
             externalLed1 = 0;
             
             DCC_send_command(DCCaddress_switch,DCCinst_switch2,10); //Make sure to deactivate 2 right after activating it
             myled2 = 1;
             externalLed2 = 1;
             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
             myled2 = 0;
             externalLed2 = 0;
             
             DCC_send_command(DCCaddress_switch,DCCinst_switch3,10);
             myled3 = 1;
             externalLed3 = 1;
             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
             myled3 = 0;
             externalLed3 = 0;
             
             DCC_send_command(DCCaddress_switch,DCCinst_switch4,10);
             myled4 = 1;
             externalLed4 = 1;
             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
             myled4 = 0;
             externalLed4 = 0;
             
        }
    }*/
}

/*----------------------------------------------------------------------------
Functions
*----------------------------------------------------------------------------*/

/*
void testInterupt(){
    lcd.cls();
    myled = 1;
    lcd.printf("In interupt function");
    lcd.printf("%d", sw1.read());
    myled4 = 0;
}

void testInterupt2(){
    lcd.cls();
    myled = 0;
    lcd.printf("In interupt2 function");
    lcd.printf("%d", sw1.read());
    myled4 = 1;
}

void riseFunction(){
    lcd.printf("In rise function");
    lcd.printf("%d", inter1.read());
}

void fallFunction(){
    lcd.printf("In fall function");
    lcd.printf("%d", inter1.read());    
}*/

bool readSwitch(DigitalIn theSwitch){
    int val = theSwitch.read();
    lcd.printf("%d", val);
    if(val == 1)
    return true;
    else
    return false;
}

bool readDetector(DigitalIn detector){
    /*int val = detect.read();
    if(val == 1)
    {
        lcd.cls();
        lcd.printf("Detect: ");
        lcd.printf("%d", val);
        return true;
    }*/
    return false;
}

void readVoltage(){
        /*float f = Ain.read(); //Read voltage value
        float Vin = f *3.3;
        lcd.printf("%.2f", Vin);
        wait(0.1);
        lcd.printf("\n");*/
}
void initialize_mcp(){
    mcp = new MCP23017(p28, p27, 0x40);
    mcp->reset();     
    mcp->writeRegister(0x00, (unsigned char )0xff);     
    mcp->writeRegister(0x01, (unsigned char )0xff);     
    mcp->writeRegister(0x02, (unsigned char )0x00);     
    mcp->writeRegister(0x03, (unsigned char )0x00);
    mcp->writeRegister(0x04, (unsigned char )0xff);     
    mcp->writeRegister(0x05, (unsigned char )0xff);     
    mcp->writeRegister(0x06, (unsigned char )0xff);     
    mcp->writeRegister(0x07, (unsigned char )0xff);     
    mcp->writeRegister(0x08, (unsigned char )0xff);     
    mcp->writeRegister(0x09, (unsigned char )0xff);     
    mcp->writeRegister(0x0a, (unsigned char )0x42);     
    mcp->writeRegister(0x0b, (unsigned char )0x42);     
    mcp->writeRegister(0x0c, (unsigned char )0x00);     
    mcp->writeRegister(0x0d, (unsigned char )0x00);
}

/*
void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
{
    unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
    unsigned __int64 temp_command = 0x0000000000000000;
    unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
    unsigned int error = 0x00; //error byte
    //calculate error detection byte with xor
    error = address ^ inst;
    //combine packet bits in basic DCC format
    command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
    //printf("\n\r %llx \n\r",command);
    int i=0;
//repeat DCC command lots of times
    while(i < repeat_count) {
        temp_command = command;
//loops through packet bits encoding and sending out digital pulses for a DCC command
        for (int j=0; j<64; j++) {
            if((temp_command&0x8000000000000000)==0) { //test packet bit
                //send data for a "0" bit
                Track=0;
                wait_us(100);
                Track=1;
                wait_us(100);
            } else {
                //send data for a "1"bit
                Track=0;
                wait_us(58);
                Track=1;
                wait_us(58);
            }
            // next bit in packet
            temp_command = temp_command<<1;
        }
        i++;
    }
}
*/