New project

Dependencies:   mbed TextLCD

main.cpp

Committer:
jasminealice
Date:
2018-06-13
Revision:
22:c024b20a0e2d
Parent:
21:31647d80614f
Child:
23:bb57966cb776

File content as of revision 22:c024b20a0e2d:

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

//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 int0(p13);
InterruptIn int1(p14);
I2C i2c(p28, p27);
MCP23017 *mcp;

/*----------------------------------------------------------------------------
Addresses
*----------------------------------------------------------------------------*/
const unsigned int DCCaddress_silver = 0x01;
const unsigned int DCCaddress_red = 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
*----------------------------------------------------------------------------*/
bool readDetector(DigitalIn detector);
bool readSwitch(DigitalIn theSwitch);
//void initialize_mcp();
void on_int0_change();
void on_int1_change();
void init_mcp();
void init();



//Trains
Train redTrain(DCCaddress_red, DCCinst_step13, 2);
Train silverTrain(DCCaddress_silver,DCCinst_step13, 12);
//Switches
Switch switch1(DCCaddress_switch,DCCinst_switch1);
//Switch switch2(DCCaddress_switch,DCCinst_switch2);
Switch switch3(DCCaddress_switch,DCCinst_switch3);
Switch switch4(DCCaddress_switch,DCCinst_switch4);
//Detectors
Detector dect;

/*----------------------------------------------------------------------------
Main
*----------------------------------------------------------------------------*/
int main() {
    /*sw1.rise(&testInterupt2);
    sw1.fall(&testInterupt);
    inter0.rise(&riseFunction);
    inter1.rise(&riseFunction);*/
    lcd.printf("Start journey");
    init_mcp();
    init();
    /*int res[2] = {2,4};
    dect.reserveDetector(res); //Run trains counterclockwise and reserve the 
    int res2[2] = {12,13};
    dect.reserveDetector(res2);//detectors in front 
    */
    while(1){ 
        
        if(readSwitch(sw1)){ //Control switches
        /*switch1.switchOn();    
        lcd.printf("Switch 1 on");
        //switch2.switchOn();  // Need to turn it off immediately!!!
        switch3.switchOn();
        switch4.switchOn();*/
        
            // Clear current interrupts
            mcp->_read(GPIOA);
            mcp->_read(GPIOB);
            // Register callbacks
            int0.fall(&on_int0_change);
            int1.fall(&on_int1_change);
    
        }
        /*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)){ //run train
            redTrain.goForward();
            silverTrain.goForward();
        }
        else 
        {
            redTrain.Stop();
            silverTrain.Stop();
        }
        if(!readSwitch(sw3)){ //Change speed
            if(silverTrain.getSpeed() == DCCinst_step2)
                silverTrain.changeSpeed(DCCinst_step13);
            else
                silverTrain.changeSpeed(DCCinst_step2);
        
        }
    } */
    
    /*lcd.printf("Start the journey");
    time_t tstart, tend; 
    initialize_mcp();
    int data = mcp->readRegister(0x12);
    lcd.printf("%d", data);
    wait(2);
    */
    }
}

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


bool testInterupt(){
    int pos = 0;// mcp->readRegister(0x12); 
    lcd.cls();
    //Check which train got the interupt
    Train *temptrain;
    if(redTrain.checkInterupt(pos)){
        redTrain.setPosition(pos);
        temptrain = &redTrain;
    }
    else if(silverTrain.checkInterupt(pos)){
        silverTrain.setPosition(pos);
        temptrain = &silverTrain;    
    }
    else{
        //WE HAVE A PROBLEM
        lcd.printf("WRONG DETECTOR DETECTED");
        return false; 
    }
    
    switch(pos){
    case 0:
    case 1:
        //Slow down
    case 2:
        //IF Clock 2->1
            //Disable Switch2
            //Free 3,4,9 
            //Move forward
        //IF CC
            //Check if 4 or 3 is reserved
                //IF NOT randomize which way to go
                    //0.3 chance to enable the switch2, reserve 3,9 and go
                    //0.7 chance to disable switch2, reserve 4 and go
                //IF 4 is reserved
                    //Enable switch2, reserve 3,9 and go
                //IF 3 reserved 
                    //Disable switch2, reserve 4 and go
    case 3:
        //Clock: Goes from 3 to 2
            //Enable switch2
            //Reserve 0,1,2, 12, 13
            //Go forward
        //Other Clock: Goes from 3->9
            //disable switch2
            //free 0,1,2,12,13
            //Go forward
    case 4:
        //IF CC : goes towards 6
            //free nr 0,1,2,12, 13
            //Turn of switch3
            //Check if 6 is reserved
            //IF yes STOP
            //else reserve 6,7,8 and go forward
        //IF Clock goes towards 2
            //free nr 5,6, 7, 8
            //Turn of switch2
            //Check if 2 is reserved
            //IF yes STOP
            //else reserve 12,13,0,1,2 go forward
        
    case 5:
        //Clock: Goes from 5->6
            //Enable switch3
            //Reserve 6,7,8
            //Go forward
        //Other Clock: Goes from 5->11
            //disable switch3
            //free 6,7,8
            //Go forward
    case 6:
        //IF CC
            //Disable switch3
            //Free 4,5,11
            //Move forward
        //IF Clock
            //Check if 4 or 5 is reserved
                //IF NOT randomize which way to go
                    //0.3 chance to enable the switch3, reserve 5,11 and go
                    //0.7 chance to disable switch3, reserve 4 and go
                //IF 4 is reserved
                    //Enable switch3, reserve 5,11 and go
                //IF 5 reserved 
                    //Disable switch3, reserve 4 and go
                    
    
    case 7:
    case 8:
        //IF Clock 8->7
            //Disable switch4
            //Free 3,9,10
            //Move forward
        //IF CC
            //Check if 9 or 10 is reserved
                //IF NOT randomize which way to go
                    //0.3 chance to enable the switch4, reserve 3,9 and go
                    //0.7 chance to disable switch4, reserve 10 and go
                //IF 10 is reserved
                    //Enable switch4, reserve 3,9 and go
                //IF 9 reserved 
                    //Disable switch4, reserve 10 and go
    case 9:
        //Clock: Goes from 9 to 8
            //Enable switch4
            //Reserve 6,7,8
            //Go forward
        //Other Clock: Goes from 9->3
            //disable switch4
            //free 6,7,8
            //Go forward
    case 10:
        //IF CC
            //free nr 6,7,8,9
            //Turn off switch1
            //Check if 12 is reserved
            //IF yes STOP
            //else reserve 12,13,0,1,2 and go forward
        //IF Clock
            //Free nr 0,1,2,11, 12, 13
            //Turn off switch4
            //Check if 8 is reserved
            //IF yes STOP
            //else reserve 6,7,8 go forward
    case 11:
        //Clock: Goes from 11 to 12
            //Enable switch1
            //Reserve 0,1,2, 12, 13
            //Go forward
        //Other Clock: Goes from 11->5
            //disable switch1
            //free 0,1,2,12,13
            //Go forward
    case 12: 
        //IF CC: 12->13
            //Disable switch1
            //Free 5,10,11
            //Move forward
        //IF Clock
            //Check if 11 or 10 is reserved
                //IF NOT randomize which way to go
                    //0.3 chance to enable switch1, reserve 5,11 and go
                    //0.7 chance to disable switch1, reserve 10 and go
                //IF 10 is reserved
                    //Enable switch1, reserve 5,11 and go
                //IF 11 reserved 
                    //Disable switch1,reserve 10 and go
    case 13: 
        
        lcd.printf("In case ");
        lcd.printf("%d", pos);
        break;
    default:   
        lcd.printf("Not in a valid case");
        break; 
    }
    
    return true;
}

/*
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 init_mcp() {
    // Initialisation of MCP registers,documentation on registers is availableatNiels/Jacco/Natalia
    mcp = new MCP23017(i2c, 0x40);
    mcp->_write(IODIRA, (unsigned char )0xff);
    mcp->_write(IODIRB, (unsigned char )0xff);
    mcp->_write(IPOLA, (unsigned char )0x00);
    mcp->_write(IPOLB, (unsigned char )0x00);
    mcp->_write(DEFVALA, (unsigned char )0xff);
    mcp->_write(DEFVALB, (unsigned char )0xff);
    mcp->_write(INTCONA, (unsigned char )0xff);
    mcp->_write(INTCONB, (unsigned char )0xff);
    mcp->_write(IOCONA, (unsigned char )0x2);
    mcp->_write(IOCONB, (unsigned char )0x2);
    mcp->_write(GPPUA, (unsigned char )0xff);
    mcp->_write(GPPUB, (unsigned char )0xff);
}

void init() {
    // Clear current interrupts
    mcp->_read(GPIOA);
    mcp->_read(GPIOB);
    // Register callbacks
    int0.fall(&on_int0_change);
    int1.fall(&on_int1_change);
    // Enable interrupts on MCP
    mcp->_write(GPINTENA, (unsigned char )0xff);
    mcp->_write(GPINTENB, (unsigned char )0xff);// Ready to go!
}

void on_int0_change() {
    lcd.printf("In int0)");
    // In your code, you might want to move this logic out of the interrupt routine
    // (for instance, by setting a flag and checking this flagin your main-loop)
    wait_us(200000);
    int sensor_data = mcp->_read(INTCAPA);
    // The only thing we do with the interrupt signal is printing it
    lcd.cls();
    lcd.printf("GOT INT0 (0x%x)\n", sensor_data);
}

void on_int1_change() {
    lcd.printf("In int1)");
    // In your code, you might want to move this logic out of the interrupt routine
    // (for instance, by setting a flag and checking this flagin your main-loop)
    wait_us(200000);
    int sensor_data = mcp->_read(INTCAPB);
    // The only thing we do with the interrupt signal is printing it
    lcd.cls();
    lcd.printf("GOT INT0 (1) (0x%x)\n", sensor_data);
}