removed the duplicate bit shifting in favor of single operation in function.
This commit is contained in:
		
							parent
							
								
									b64c306250
								
							
						
					
					
						commit
						87b1cb9acd
					
				
					 2 changed files with 13 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -105,7 +105,7 @@ void InitProg(void);
 | 
			
		|||
/**
 | 
			
		||||
 * @brief Reads the ADC pin from the fader.
 | 
			
		||||
 */
 | 
			
		||||
uint16_t ReadFader(void);
 | 
			
		||||
uint8_t ReadFader(void);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										25
									
								
								src/main.c
									
										
									
									
									
								
							
							
						
						
									
										25
									
								
								src/main.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -104,7 +104,7 @@ uint8_t ReadSpeed(void) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// change to ReadFader(void)
 | 
			
		||||
uint16_t ReadFader(void) {
 | 
			
		||||
uint8_t ReadFader(void) {
 | 
			
		||||
    // Initialize ADC
 | 
			
		||||
  ADMUX = (1 << MUX1) | (1 << MUX0);                  // Select ADC3 (PB3)
 | 
			
		||||
  ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8
 | 
			
		||||
| 
						 | 
				
			
			@ -112,7 +112,7 @@ uint16_t ReadFader(void) {
 | 
			
		|||
  ADCSRA |= (1 << ADSC); // Start conversion
 | 
			
		||||
  while (ADCSRA & (1 << ADSC)) {
 | 
			
		||||
  } // Wait for conversion to finish
 | 
			
		||||
  return ADC;
 | 
			
		||||
  return (uint8_t)(ADC >> 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
| 
						 | 
				
			
			@ -129,25 +129,24 @@ static inline uint8_t diff(uint8_t a, uint8_t b) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
void MotorSetSavePos(uint8_t *ADR) {
 | 
			
		||||
  uint8_t pos = (uint8_t)(ReadFader() >> 2);
 | 
			
		||||
  //eeprom_write_byte((uint8_t *)ADR, pos);
 | 
			
		||||
  eeprom_update_byte((uint8_t *)ADR, pos);
 | 
			
		||||
  uint8_t pos = ReadFader();
 | 
			
		||||
  eeprom_write_byte((uint8_t *)ADR, pos);
 | 
			
		||||
  return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t MotorGetSavedPos(uint8_t *ADR) {
 | 
			
		||||
  return (uint8_t)eeprom_read_byte((uint8_t *)POSITION1_ADR);
 | 
			
		||||
  return (uint8_t)eeprom_read_byte((uint8_t *)ADR);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MotorMoveTo(uint8_t target) {
 | 
			
		||||
 | 
			
		||||
  uint8_t on_delay = ReadSpeed();
 | 
			
		||||
  uint8_t pos = (uint8_t)(ReadFader() >> 2);
 | 
			
		||||
  uint8_t pos = ReadFader();
 | 
			
		||||
  uint8_t idx = 0;
 | 
			
		||||
 | 
			
		||||
  while (diff(target, pos) > 8) {
 | 
			
		||||
  while (diff(target, pos) > 4) {
 | 
			
		||||
    on_delay = ReadSpeed();
 | 
			
		||||
    pos = (uint8_t)(ReadFader() >> 2);
 | 
			
		||||
    pos = ReadFader();
 | 
			
		||||
    if (target > pos) {
 | 
			
		||||
      MotorMove(1);
 | 
			
		||||
    } else {
 | 
			
		||||
| 
						 | 
				
			
			@ -270,14 +269,14 @@ static void UpdateButtonOutput(btn_state *b) {
 | 
			
		|||
    /*If the button pres was a short one.*/
 | 
			
		||||
    else if (!b->is_long_pressed) {
 | 
			
		||||
      if (b->is_active) {
 | 
			
		||||
          /*Save the current position into position 2 EEPROM*/
 | 
			
		||||
          MotorSetSavePos((uint8_t *)POSITION2_ADR);
 | 
			
		||||
 | 
			
		||||
            /*If already in saved position then go back to original pos.*/
 | 
			
		||||
          if(diff(MotorGetSavedPos((uint8_t *)POSITION1_ADR), ReadFader()) > 8){
 | 
			
		||||
                MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION1_ADR));
 | 
			
		||||
          if(diff(MotorGetSavedPos((uint8_t *)POSITION1_ADR), ReadFader()) < 4){
 | 
			
		||||
                MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION2_ADR));
 | 
			
		||||
          }
 | 
			
		||||
          else{
 | 
			
		||||
                /*Save the current position into position 2 EEPROM*/
 | 
			
		||||
                MotorSetSavePos((uint8_t *)POSITION2_ADR);
 | 
			
		||||
                MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION1_ADR));
 | 
			
		||||
          }
 | 
			
		||||
      } else {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue