direction commands updated to Up down RPM value

Dependencies:   MAX7219pot MCP23S17 mbed

Fork of POT_V_1_0 by Marine Electricals

Revision:
5:f7b7ee0702cc
Parent:
4:710464cb44fc
--- a/main.cpp	Sat Apr 28 10:22:22 2018 +0000
+++ b/main.cpp	Wed Jul 25 19:57:54 2018 +0000
@@ -23,7 +23,7 @@
 Ticker flash_Ticker;
 Ticker R_A_Ticker;
 Ticker CMD_Ticker;
-Ticker DIR_Ticker;
+//Ticker DIR_Ticker;      //function changed to Up-Down Rpm counts
 Ticker HB_fail;
 Ticker RTC_ticker;
 Ticker led_refresh;
@@ -65,7 +65,7 @@
 uint8_t spi_error,no_pot_connected,num_bytes,ticker_count,ticker_count_fast,norm_count,set_bit;
 
 
-int dir, last_dir;
+//int dir, last_dir;
 int checksum_send_byte,checksum_rcvd_byte;
 
 char pot_Addr,addr_ID;
@@ -97,7 +97,7 @@
 
 DigitalOut LED_CMD_ACK(_CMD_ACK);                              //ack command in slave devices only
 DigitalOut LED_CTRL[2] = {_REQ,_ACK};                             //control transfer
-DigitalOut LED_DIR[2] = {_AHEAD,_ASTERN};                         //dir command
+//DigitalOut LED_DIR[2] = {_AHEAD,_ASTERN};                         //dir command
 DigitalOut LED_ACH[8] =  {_WH,_BRDG,_MCR,_ER,_WP,_WS,_OPS,_ASP};  //active channels
 
 //DigitalOut myled(PB_13);  //for internal test LED1
@@ -132,7 +132,7 @@
 };          
 LED_STATUS led_cmd_ack;   //only in slave devices
 LED_STATUS led_ctrl[2];
-LED_STATUS led_dir[2];
+//LED_STATUS led_dir[2];
 LED_STATUS led_ach[8];
 
 enum KEY
@@ -160,6 +160,15 @@
 };
 enum CTRL ctrl_KEY; 
 
+enum UP_DOWN
+{
+    UP_key=1,
+    DOWN_key,
+    FALSE2 = 3
+};
+enum UP_DOWN up_down_KEY;
+
+/*
 enum direction_KEY
 {
     AHEAD = 1,
@@ -167,6 +176,7 @@
     FALSE2 = 3
 };
 enum direction_KEY dir_KEY;
+*/
 
 enum display_KEY
 {
@@ -252,10 +262,14 @@
         {
                 LED_CTRL[i] = ON;
         }
-        for(uint8_t i=0;i<=1;i++)          //control
+        
+        /*
+        for(uint8_t i=0;i<=1;i++)          // dir control
         {
                 LED_DIR[i] = ON;                                  /////////////////working here
         }
+        */
+        
         for(uint8_t i=0;i<=7;i++)          //active channel
         {
                 LED_ACH[i] = ON;                  //duty cycle 10%
@@ -316,7 +330,7 @@
             else
                 __nop();
         }
-        
+        /*
         for(uint8_t i=0;i<=1;i++)          //status update LED_CTRL 
         {
             if(led_dir[i] == ON)
@@ -339,6 +353,7 @@
             else
                 __nop();
         }
+        */
         
         for(uint8_t i=0;i<=7;i++)          //status update LED_ACH
         {
@@ -658,6 +673,8 @@
             __nop();
 }
 
+//Direction function changed to RPM Up-Down Counter so Dir_state_machine function not required
+/*                
 void DIR_state_machine(void)
 {
     static int dir_state_cnt = 0;
@@ -682,6 +699,8 @@
             __nop();
     } 
 }
+*/
+
 void HB_fail_state_machine()
 {
     static uint8_t fail_count = 0;
@@ -693,7 +712,7 @@
     else if(++fail_count >=3)             // if max 07 number of connected eots 7x3=21, it takes 150x21=3.150 sec to alarm
     {
         fail_count = 0;
-        led_dir[dir-1] = OFF;               //current command if any turned OFF
+        //led_dir[dir-1] = OFF;               //current command if any turned OFF
         led_cmd_ack = OFF;
         display_flash = false;
         device_ERR = true;
@@ -711,7 +730,7 @@
     }
 }
 
-void NUMERIC_handler(uint8_t key_ID)                             //function sacnning all the 0-9 number keys
+void NUMERIC_handler(uint8_t key_ID)                             //function sacnning all the 0-9 and up-down number keys
 {  
     uint8_t num;
     num = key_ID;
@@ -786,6 +805,89 @@
         __nop(); //then do nothing
 }
 
+void UP_DOWN_handler(uint8_t key_ID)
+{
+    if(key_ID != FALSE2)
+        {                                                                                  
+        if(((ach_M == pot_Master) && (addr_ID == pot_Master)) || ((ach_M == pot_Submaster) && (addr_ID == pot_Submaster)))                               //for Master
+            {    
+                if(key_ID == UP_key)
+                {
+                    rpm_data[3]++;
+                    if(rpm_data[3] == 10)
+                        {
+                            rpm_data[3] = 0;
+                            rpm_data[2]++;
+                            if(rpm_data[2] == 10)
+                            {
+                                rpm_data[2] = 0;
+                                rpm_data[1]++;
+                                if(rpm_data[1] == 10)
+                                {
+                                    rpm_data[1] = 0;
+                                    rpm_data[0]++;
+                                    if(rpm_data[0] == 10)
+                                        rpm_data[0] = 9;
+                                    else
+                                        __nop();
+                                }
+                                else
+                                    __nop();
+                            }
+                            else
+                                __nop();
+                        }
+                    else
+                        __nop();
+                }
+                else if(key_ID == DOWN_key)
+                    {
+                        rpm_data[3]--;
+                    if(rpm_data[3] == 0)
+                        {
+                            rpm_data[3] = 0;
+                            rpm_data[2]--;
+                            if(rpm_data[2] == 0)
+                            {
+                                rpm_data[2] = 0;
+                                rpm_data[1]--;
+                                if(rpm_data[1] == 0)
+                                {
+                                    rpm_data[1] = 0;
+                                    rpm_data[0]--;
+                                    if(rpm_data[0] == 0)
+                                        rpm_data[0] = 0;
+                                    else
+                                        __nop();
+                                }
+                                else
+                                    __nop();
+                            }
+                            else
+                                __nop();
+                        }
+                    else
+                        __nop();
+                        
+                    }
+                else
+                    __nop();
+                                
+                for (uint8_t j = 1; j <= 4; j++)
+                {
+                    max7219.write_digit(2,j,rpm_data[j-1]);    //update data to display
+                }                                     
+            }
+        else
+            __nop();
+        }
+    else
+        __nop();
+    
+}
+
+//functon not required as DiR keys change to UP-down RPM
+/*
 void DIR_handler(uint8_t DIR_ID)                        //handle  control transfer 
 {
     if(DIR_ID != FALSE2)
@@ -822,6 +924,7 @@
     else 
         __nop();                                //then do nothing
 }
+*/
 
 void DISP_handler(uint8_t DISP_ID)                        //handle  control transfer 
 {
@@ -882,15 +985,31 @@
                     ENTER_ACK_key = true;           //flashing display1 turned steady                              //RPM command acknowledged by active slave
                 } 
                 else  if (addr_ID == ach_M)                                 //in case of active masters
-                {                                              //disp1 start flashing, and data sent over UART for all digits 1-4
-                    display_flash = true;             //display starts flashing
-                    mcp23s17_1.gpintena(0x00);      //ACK key interrupts disabled
-                    wait_us(10);
-                    mcp23s17_1.gpintenb(0x00);      //ACK key interrupts disabled
-                    wait_us(10);
-                    current_status =  2;              //flash
-                    buzzer = ON;
-                    RPM_cmd = true;                   //command need to be send
+                {
+                    int RPM_set = 0;                                              //disp1 start flashing, and data sent over UART for all digits 1-4
+                    RPM_set = (rpm_data[0]*1000) + (rpm_data[1]*100) + (rpm_data[2]*10) + rpm_data[3];
+                    if(RPM_set>Max_RPM)
+                    {
+                        for (uint8_t j = 0; j <=3; j++)
+                        {
+                            rpm_data[j] = 0;   //at every key press array shift to left
+                        }
+                        for (uint8_t j = 1; j <= 4; j++)      // all digits turns to zero
+                        {
+                            max7219.write_digit(2,j,rpm_data[j-1]);
+                        }  
+                    }
+                    else
+                    {
+                        display_flash = true;             //display starts flashing
+                        mcp23s17_1.gpintena(0x00);      //ACK key interrupts disabled
+                        wait_us(10);
+                        mcp23s17_1.gpintenb(0x00);      //ACK key interrupts disabled
+                        wait_us(10);
+                        current_status =  2;              //flash
+                        buzzer = ON;
+                        RPM_cmd = true;                   //command need to be send
+                    }
                 }
                 else
                 {
@@ -1031,7 +1150,7 @@
 void read_KEY_A()                      //function check status of PORTA of MCP23S17 (1) chip
 {
     char key_status = 0;
-    uint8_t num_KEY = 0;
+    num_KEY = FALSE0;
     key_status = read_KEY_SPI(ch_A);    //to read portA of mcp23s17(1)
     if(key_status != 0xff)                    //means some key must have been pressed
     {
@@ -1072,10 +1191,12 @@
 void read_KEY_B()
 {
     char key_status = 0;
-    uint8_t num_KEY = 0;
-    uint8_t dir_KEY = 0;
-    uint8_t disp_KEY = 0;
-    uint8_t ctrl_KEY = 0;
+    num_KEY = FALSE0;
+    ctrl_KEY = FALSE1;
+    //dir_KEY = FALSE2;  /not required
+    up_down_KEY = FALSE2;
+    disp_KEY = FALSE3;
+    
     key_status = read_KEY_SPI(ch_B);   //to read portBof mcp23s17(1)
     if(key_status != 0xff)
     {   
@@ -1089,13 +1210,13 @@
                 num_KEY = NINE;
                 NUMERIC_handler(num_KEY);
             break;
-            case 0xfb:               //0xfb  GPA2
-                dir_KEY = AHEAD;
-                DIR_handler(dir_KEY);
+            case 0xfb:                      //0xfb  GPA2
+                up_down_KEY = UP_key;             //dir_KEY = AHEAD; /not required now
+                UP_DOWN_handler(up_down_KEY);   //DIR_handler(dir_KEY); /not required now                                            
             break;
-            case 0xf7:               //0xf7  GPA3
-                dir_KEY = ASTERN;
-                DIR_handler(dir_KEY);
+            case 0xf7:                          //0xf7  GPA3
+                up_down_KEY = DOWN_key;             //dir_KEY = ASTERN;  /not required now
+                UP_DOWN_handler(up_down_KEY);     //DIR_handler(dir_KEY); /not required now
             break;
             case 0xef:                     //0xef  GPA4
                 ctrl_KEY = ACK_;
@@ -1116,7 +1237,8 @@
             default:
                 num_KEY = FALSE0;
                 ctrl_KEY = FALSE1;
-                dir_KEY = FALSE2;
+                //dir_KEY = FALSE2;
+                up_down_KEY = FALSE2;
                 disp_KEY = FALSE3;
             break;
         }
@@ -1125,8 +1247,8 @@
 void read_KEY_C()
 {
     char key_status = 0;
-    uint8_t ctrl_KEY = 0;
-    uint8_t set_KEY = 0;
+    ctrl_KEY = FALSE1;
+    set_KEY = FALSE4;
     key_status = read_KEY_SPI(ch_C);   //to read portA of mcp23s17(2)
     if(key_status != 0xff)
     {
@@ -1208,9 +1330,9 @@
 
 void send_ack_CMD(uint8_t cmd_t)
 {
-    sprintf(tx_buf,"$PPOTX,%u,%u,%c*", addr_ID, cmd_t,'A');
+    sprintf(tx_buf,"$PPOTX,%u,%c*", addr_ID,'A');
     checksum_send_byte = get_checksum(tx_buf);                
-    sprintf(tx_buf,"$PPOTX,%u,%u,%c*%u",addr_ID,cmd_t,'A',checksum_send_byte);
+    sprintf(tx_buf,"$PPOTX,%u,%c*%u",addr_ID,'A',checksum_send_byte);
     Drive_En = 1;              
     UART.printf("%s\r\n",tx_buf);
     wait_ms(2);
@@ -1228,6 +1350,7 @@
     Drive_En = 0;
 }
 
+/*
 void send_DIR(uint8_t _adr)             //addr 1-8 of leds,error=1 or 0(no error)
 {
     sprintf(tx_buf,"$PPOTD,%u,%u,%c*",_adr,dir,'A');             // dir is 1 for ahead, 2 for astern
@@ -1238,6 +1361,7 @@
     wait_ms(2);
     Drive_En = 0;
 }
+*/
 
 void send_HB(uint8_t _adr)                   //master will send the heartbeat to all presentees at periodic intervals
 {      
@@ -1264,9 +1388,9 @@
  /** Function will send ac-M, ac-S, rpm-data(04digits), cmd_state)**/
 void send_ACH()                 
 {
-    sprintf(tx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c*", ach_M, ach_S, rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],dir,current_status,current_status_dir,'A');
+    sprintf(tx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c*", ach_M, ach_S, rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],current_status,'A');
     checksum_send_byte = get_checksum(tx_buf);                
-    sprintf(tx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c,*%u", ach_M, ach_S, rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],dir,current_status,current_status_dir,'A',checksum_send_byte);
+    sprintf(tx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c,*%u", ach_M, ach_S, rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],current_status,'A',checksum_send_byte);
     Drive_En = 1;              
     UART.printf("%s\r\n",tx_buf);
     wait_ms(2);
@@ -1305,12 +1429,14 @@
                 RPM_cmd_ack = true;  
                 ENTER_ACK_key = false;
             }
+            /*
             else if(DIR_key)                //dir ack key detected at active slave
             {
                 DIR_key = false;
                 DIR_cmd_ack_send = true;
                 send_ack_CMD(2);
             }
+            */
             else if(REQ_key)
             {
                 REQ_key = false;
@@ -1386,7 +1512,7 @@
         led_cmd_ack = OFF;
         max7219.enable_device(2);
     }
-
+    /*
     if(current_status_dir == 2)
     {
         led_dir[dir-1] = _FLASH;
@@ -1399,7 +1525,7 @@
     {
         led_dir[dir-1] = OFF;
     }
-    
+    */
     for(uint8_t i = 1; i <= 4 ; i++)    //current cmd information
     {
         max7219.write_digit(2,i,rpm_data[i-1]);     //device 2 digits 1-4                  //data 0 means left most digit will be blank bcoz no decode mode is used
@@ -1472,7 +1598,7 @@
     }
     CMD_Ticker.attach(&CMD_State_Machine,0.25);                                 //state machine timer started in all connected devices except  master
 }
-
+/*
 void rcvd_dir_CMD()         //all devices check for received command
 {
     DIR_cmd =  true;
@@ -1488,6 +1614,7 @@
     }
     DIR_Ticker.attach(&DIR_state_machine,0.25);
 }
+*/
 
 void rcvd_BROADCAST()
 {
@@ -1535,7 +1662,7 @@
 {  
     char rcvd_char;
     char rcvd_valid;
-    int cmd_t;
+    //int cmd_t;
     uint8_t checksum_calculated;
     timeout.attach(&at_timeout,0.100);    //100ms timeout
     data_fail = false;
@@ -1618,7 +1745,8 @@
                 else
                     valid = 'V';
             }
-            else if(strcmp(rx_buf,"$PPOTD") == 0)                      //* present submaster,Slaves or lsitners receive HB *// 
+            /*
+            else if(strcmp(rx_buf,"$PPOTD") == 0)                      //present submaster,Slaves or lsitners receive HB 
             {
                 sscanf(strtok (NULL,","), "%u", &rcvd_ADDR);        //address of connected device
                 sscanf(strtok (NULL,","), "%u",&dir);        // 1 for ahead and 2 for astern
@@ -1635,34 +1763,19 @@
                 else
                     valid = 'V';                                   //set flag when Heartbeat received
             }
+            */
             else if(strcmp(rx_buf,"$PPOTX") == 0)                //* Master,submaster,Slave or listners receive CMD acknowledge *//
             {
                 sscanf(strtok (NULL,","), "%u", &rcvd_ADDR);       //address extracted
-                sscanf(strtok (NULL,","), "%u",&cmd_t);
+                //sscanf(strtok (NULL,","), "%u",&cmd_t);
                 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid);        //sentence validity char
                 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
-                sprintf(rx_buf,"$PPOTX,%u,%u,%c*",rcvd_ADDR,cmd_t,rcvd_valid);
+                sprintf(rx_buf,"$PPOTX,%u,%c*",rcvd_ADDR,rcvd_valid);
                 checksum_calculated = get_checksum(rx_buf);
                 if(checksum_calculated == checksum_rcvd_byte)
                     {
                         valid = 'A';
-                        switch(cmd_t)
-                        {
-                        case 1:
-                            RPM_cmd_ack = true;
-                        break;
-                    
-                        case 2:
-                            DIR_cmd_ack = true;
-                            led_dir[last_dir-1] = OFF;
-                            last_dir = dir;
-                            led_dir[dir-1] = ON;
-                        break;
-                    
-                        default:
-                            __nop();
-                        break;       
-                        }  
+                        RPM_cmd_ack = true;       
                     }                      
                 else
                      valid = 'V';               // not valid sentence received;       
@@ -1679,12 +1792,12 @@
                 sscanf(strtok (NULL,","), "%u", &last_entry[1]);
                 sscanf(strtok (NULL,","), "%u", &last_entry[2]);
                 sscanf(strtok (NULL,","), "%u", &last_entry[3]);
-                sscanf(strtok (NULL,","), "%u", &dir);
+                //sscanf(strtok (NULL,","), "%u", &dir);
                 sscanf(strtok (NULL,","), "%u", &current_status);
-                sscanf(strtok (NULL,","),"%u", &current_status_dir);
+                //sscanf(strtok (NULL,","),"%u", &current_status_dir);
                 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid);        //sentence validity char
                 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
-                sprintf(rx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c*",ach_M,ach_S,rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],dir,current_status,current_status_dir,rcvd_valid);
+                sprintf(rx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c*",ach_M,ach_S,rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],current_status,rcvd_valid);
                 checksum_calculated = get_checksum(rx_buf);
                 if(checksum_calculated == checksum_rcvd_byte)
                     {
@@ -1851,14 +1964,16 @@
             //HB_Ticker.attach(&HB_State_Machine,0.010);
             CMD_Ticker.attach(&CMD_State_Machine,0.25);                   //CMD_state_machine ticker started              
         }
+        /*
         else if(DIR_cmd)
         {
             send_DIR(send_ADDR);
             DIR_cmd =  false;                                                      //cmd to be send with current running address and cmd value
             wait_ms(2);
             //HB_Ticker.attach(&HB_State_Machine,0.010);
-            DIR_Ticker.attach(&DIR_state_machine,0.25);                   //CMD_state_machine ticker started              
+            //DIR_Ticker.attach(&DIR_state_machine,0.25);                   //CMD_state_machine ticker started              
         }
+        */
         else  if(BROADCAST_send)
         {
             send_BROADCAST();         //broadcast needed err_ID nd err_Status
@@ -2055,17 +2170,19 @@
     
     LED_CMD_ACK = ON;                  
     wait_ms(1);
-        
+       
     for(uint8_t i=0;i<=1;i++)             //ctrl led intialisation
     {
         LED_CTRL[i] = ON;
         wait_ms(1);
     }
+    /*
     for(uint8_t i=0;i<=1;i++)             //ctrl led intialisation
     {
         LED_DIR[i] = ON;
         wait_ms(1);
     }
+    */
     for(uint8_t i=0; i<=7;i++)            //ach led initialisation
     {
         LED_ACH[i] = ON ; 
@@ -2084,12 +2201,14 @@
         led_ctrl[i] = OFF;
         wait_ms(1);
     }
+    /*
     for(uint8_t i=0;i<=1;i++)             //ctrl led intialisation
     {
         LED_DIR[i] = OFF;
         led_dir[i] = OFF;
         wait_ms(1);
     }
+    */
     for(uint8_t i=0; i<=7;i++)         //ach led turned off
     {
         LED_ACH[i] = OFF; 
@@ -2330,7 +2449,7 @@
     spi_error = set_bit = err_ALL = no_pot_connected = norm_count = Hrs_1 = Hrs_2 = Mins_1 = Mins_2 = 0;
     checksum_send_byte = checksum_rcvd_byte = 0;
     
-    dir = last_dir = 0;
+    //dir = last_dir = 0;
     HB_timer =0;
     sync_time = 0;
     num_bytes = 0;