Pseudo-Medical Monitor Code ControlPanel STUFF

From LVL1
Jump to navigation Jump to search

#define ControlPanel_INT_PIN 2
#define ControlPanel_I2C_ADDR 0x38

class Dual_Encoder_Class
{
  private:  
  static volatile uint8_t pcf_switch_status;
  static volatile uint8_t pcf_present_value;
  static volatile int pcf_encoder_1;
  static volatile int pcf_encoder_2;
  static volatile bool pcf_bypass_flag;
  
  static PCF pcf_device;
  int i2c_address;    

  static void embedded_check()
    {
      if (pcf_bypass_flag)
        return;
        
      uint8_t encoder_1 = 0, encoder_2 = 0;

      pcf_present_value = pcf_device.read();

      encoder_1 = pcf_present_value & 0b00000011;
      encoder_2 = pcf_present_value & 0b00001100;
      encoder_2 = encoder_2 >> 2;
      encoder_1 = encoder_1 | ((pcf_previous_value & 0b00000011) << 2);
      encoder_2 = encoder_2 | (pcf_previous_value & 0b00001100);

      pcf_encoder_1 = 0;
      switch (encoder_1)
        {
          case 0b0001:  // fall through..
          case 0b0111:
          case 0b1110:
          case 0b1000:
            pcf_encoder_1 = -1;
            break;
          case 0b0010:
          case 0b0100:
          case 0b1101:
          case 0b1011:
            pcf_encoder_1 = 1;
            break;
        }

      pcf_encoder_2 = 0;
      switch (encoder_2)
        {
          case 0b0001:  // fall through..
          case 0b0111:
          case 0b1110:
          case 0b1000:
            pcf_encoder_2 = -1;
            break;
          case 0b0010:
          case 0b0100:
          case 0b1101:
          case 0b1011:
            pcf_encoder_2 = 1;
            break;
        }

      pcf_switch_status = 0;
      pcf_switch_status = pcf_present_value & 0b00110000;
      pcf_switch_status = (pcf_switch_status ^ 0b00110000) >> 4;
      pcf_previous_value = pcf_present_value;      
      pcf_event = true;
    }

  public:
  static volatile bool pcf_event;
  static volatile uint8_t pcf_previous_value;
    
  int encoder_A_Dir(void)    
    {
      int return_value = pcf_encoder_1;
      pcf_encoder_1 = 0;
      return(return_value);
    }
  int encoder_B_Dir(void)    
    {
      int return_value = pcf_encoder_2;
      pcf_encoder_2 = 0;
      return(return_value);
    }
  bool encoder_A_Switch(void)
    {
      bool return_value = pcf_switch_status & 0b01;
      pcf_switch_status = pcf_switch_status & 0b10;
      return(return_value);      
    }
  bool encoder_B_Switch(void)
    {
      bool return_value = (pcf_switch_status & 0b10) >> 1;
      pcf_switch_status = pcf_switch_status & 0b01;
      return(return_value);      
    }
    
  void begin(int i2cAddr)
    {  
      pcf_device.setup(i2cAddr); 
      i2c_address = i2cAddr;
      pinMode(ControlPanel_INT_PIN, INPUT_PULLUP);    
      attachInterrupt(digitalPinToInterrupt(ControlPanel_INT_PIN), embedded_check, FALLING);    
      reset_All();
    }

  void reset_All()
    {
      pcf_device.write(0b11111111); // Turns all port HIGH      
      pcf_encoder_1 = 0;
      pcf_encoder_2 = 0;  
      pcf_switch_status = 0;   
      pcf_event = false;   
      pcf_bypass_flag = false;
      Serial.println("Hit");
    }

  static void LED_Off(int led_number)
    {
      uint8_t data_bin = pcf_previous_value;
      pcf_bypass_flag = true;
      switch(led_number)
        {
          case 1:
            data_bin = data_bin | 0b10000000;
            pcf_previous_value = data_bin;
            data_bin = data_bin | 0b00111111;
            pcf_device.write(data_bin);
            break;
          case 2:
            data_bin = data_bin | 0b01000000;
            pcf_previous_value = data_bin;
            data_bin = data_bin | 0b00111111;
            pcf_device.write(data_bin);            
            break;
        }          
      pcf_bypass_flag = false;  
    }

  static void LED_On(int led_number)
    {
      uint8_t data_bin = pcf_previous_value;
      pcf_bypass_flag = true;      
      switch(led_number)
        {
          case 1:
            data_bin = data_bin & 0b01111111;
            pcf_previous_value = data_bin;
            data_bin = data_bin | 0b00111111;
            pcf_device.write(data_bin);
            break;
          case 2:
            data_bin = data_bin & 0b10111111;
            pcf_previous_value = data_bin;
            data_bin = data_bin | 0b00111111;
            pcf_device.write(data_bin);
            break;
        }          
      pcf_bypass_flag = false;
    }    
};

volatile uint8_t Dual_Encoder_Class::pcf_switch_status;
volatile uint8_t Dual_Encoder_Class::pcf_previous_value;
volatile uint8_t Dual_Encoder_Class::pcf_present_value;
volatile int Dual_Encoder_Class::pcf_encoder_1;
volatile int Dual_Encoder_Class::pcf_encoder_2;
volatile bool Dual_Encoder_Class::pcf_event;
volatile bool Dual_Encoder_Class::pcf_bypass_flag;
PCF Dual_Encoder_Class::pcf_device;


Dual_Encoder_Class Controls;
  


bool setup_ControlPanel()
{
  Controls.begin(ControlPanel_I2C_ADDR); 
  
  return(true);
}
  
 

Pseudo-Medical_Monitor_Code#Custom_Includes