Contains all code needed for analog digital and oscope

Dependencies:   mbed

main.cpp

Committer:
EricGlunn
Date:
2020-04-20
Revision:
0:f4b1c6a95f25
Child:
1:14e49afb1d7c

File content as of revision 0:f4b1c6a95f25:

#include "mbed.h"
#define reset       0b00000000          //Define masks 
#define digital     0b00100000
#define analog      0b01000000
#define I2C         0b01100000
#define stateMask   0b11100000
#define pinMask     0b00011111
#define pin1        0b00010000
#define pin2        0b00001000
#define pin3        0b00000100
#define pin4        0b00000010
#define pin5        0b00000001


DigitalOut myled(LED1);
DigitalOut DigitalStatus(LED2);         //Led for when Digital read is running
DigitalOut AnalogStatus(LED3);          //LED for when Analog read is running

DigitalIn  d1(p21);                     //Always set these pins for digital in
DigitalIn  d2(p22);
DigitalIn  d3(p23);
DigitalIn  d4(p24);
DigitalIn  d5(p25);

AnalogIn   a1(p16);                     //Always set these pins for analog in 
AnalogIn   a2(p17);
AnalogIn   a3(p18);
AnalogIn   a4(p19);
AnalogIn   a5(p20);


Serial PC(USBTX,USBRX);                                      
Ticker tickerboi;                   //Initialize the ticker for Sampling

volatile unsigned char DigitalSample;
volatile unsigned char AnalogSample[5];

volatile bool DigitalRunning;
volatile bool AnalogRunning;

volatile unsigned char command;
volatile unsigned char state;

volatile bool newDigital=false;
volatile bool newAnalog=false;

 
 
void digital_sample_func(void){         //sampling function, samples each pin specified and Ors it
    DigitalSample = digital;
    
    if( (command & pin1) == pin1){
        DigitalSample=DigitalSample + d1*pin1;
    }
    if( (command & pin2) == pin2){
        DigitalSample=DigitalSample + d2*pin2;
    }
    if( (command & pin3) == pin3){
        DigitalSample=DigitalSample + d3*pin3;
    }
    if( (command & pin4) == pin4){
        DigitalSample=DigitalSample + d4*pin4;
    }
    if( (command & pin5) == pin5){
        DigitalSample=DigitalSample + d5*pin5;
    }
    newDigital=true;
}


void analog_sample_func(void){
    for(int i=0; i<5; i++){
        AnalogSample[i]=0;   
    }
    if( (command & pin1) == pin1){
        AnalogSample[0]=a1*255;
    }
    if( (command & pin2) == pin2){
        AnalogSample[1]=a2*255;
    }
    if( (command & pin3) == pin3){
        AnalogSample[2]=a3*255;
    }
    if( (command & pin4) == pin4){
        AnalogSample[3]=a4*255;
    }
    if( (command & pin5) == pin5){
        AnalogSample[4]=a5*255;
    }
    newAnalog=true;    
    
}


void SerialInterrupt(void){
       command=PC.getc();
       state= command & stateMask;
       switch(state){
           case reset:
            tickerboi.detach();                 //detach Ticker
            DigitalRunning=false;                //turn off status LED
            AnalogRunning=false;
            break;
                
           case digital:
            tickerboi.attach(&digital_sample_func, .1);  //Ticker will call digital read every 100 ms  
            DigitalRunning=true;               // turn on status LED
            break;        
            
           case analog:
            tickerboi.attach(&analog_sample_func, .01);        //Ticker will call analog read every 10ms
            AnalogRunning=true;    
        }  
}

int main() {
     PC.attach(&SerialInterrupt, Serial::RxIrq);
    
    
    while(1) {
        
        DigitalStatus=DigitalRunning;               //Indicate DigitalRead is running
        AnalogStatus=AnalogRunning;                 //Indicate analogRead is running
        
        if(newDigital){                            //if digital sampling was run
            PC.putc(DigitalSample);
            newDigital=false;
        }
        
        if(newAnalog){                         //if analog sampling was run             
            PC.putc(analog);                   //First bit is 0b01000000
            for(int i=0; i<5; i++){            //Send all of the analog bits
                PC.putc(AnalogSample[i]);   
            }
            PC.putc(command);                     //Last bit is command from PC
            newAnalog=false;
        }
        
        myled = 1;                             //indicate main loop is running
        wait(0.2);
        myled = 0;
        wait(0.2);  
    }
}