// Write a string to an address, truncating at a maximum length, with optional termination (NUL) void AT24C32Helper::WriteString(int addr, String val, int maxLen, bool ExcludeTermination) { for (int i = 0; (i < val.length()) && (i < ExcludeTermination ? maxLen : maxLen - 1); ++i) { i2c_eeprom_write_byte(deviceAddr, addr + i, val[i]); delay(2); // Seems like the data can be corrupted if the EEPROM is taking longer than usual for its write cycle } if (!ExcludeTermination) i2c_eeprom_write_byte(deviceAddr, addr + ((val.length() >= maxLen) ? maxLen - 1 : val.length()), 0x00); delay(2); }
/* eeprom_read(_data1, 18, PAGE_1, 0); eeprom_read(_data2, 18, PAGE_2, 0); // eeprom_write(data1, 18, PAGE_1, 0); // eeprom_write(data2, 18, PAGE_2, 0); eeprom_read(_data1, 18, PAGE_2, 0); eeprom_read(_data2, 18, PAGE_1, 0); */ int eeprom_write( void *data, unsigned char len, unsigned short page, unsigned char word) { unsigned char i; for(i=0;i<len;i++){ i2c_eeprom_write_byte(EEPROM_ADDRESS, page+word+i, ((unsigned char *)data+i)); delay_ms(10); } return 1; }
/*********************** PowerDownSensor start ***********************************/ void PowerDownSensor(void) { if(_led_status == _led_off) { uint8_t b0_0x32_off; uint8_t b0_0x33_off; uint8_t b0_0x35_off; i2c_eeprom_write_byte(0x7F,0x00); //bank0 b0_0x32_off = i2c_eeprom_read_byte(0x32); b0_0x33_off = i2c_eeprom_read_byte(0x33); b0_0x35_off = i2c_eeprom_read_byte(0x35); _b1_0x37_off = b0_0x32_off ; _b1_0x36_off = 0x62 & (( b0_0x33_off & 0x3 ) << 5) ; _b0_0x4d_off = 0x9A & (( b0_0x33_off & 0x4 ) << 5) ; _b0_0x48_off = 0x0F & ( b0_0x35_off & 0xF ) ; i2c_eeprom_write_byte(0x06,0x0A); } else { uint8_t b0_0x32_on; uint8_t b0_0x33_on; uint8_t b0_0x35_on; i2c_eeprom_write_byte(0x7F,0x00); //bank0 b0_0x32_on = i2c_eeprom_read_byte(0x32); b0_0x33_on = i2c_eeprom_read_byte(0x33); b0_0x35_on = i2c_eeprom_read_byte(0x35); _b1_0x37_on = b0_0x32_on ; _b1_0x36_on = 0x62 & (( b0_0x33_on & 0x3 ) << 5) ; _b0_0x4d_on = 0x9A & (( b0_0x33_on & 0x4 ) << 5) ; _b0_0x48_on = 0x0F & ( b0_0x35_on & 0xF ) ; if(_touch_flag == 0x80) //if no touch, do not go to power down. i2c_eeprom_write_byte(0x06,0x0A); else { i2c_eeprom_write_byte(0x7f,0x00); //for bank0 i2c_eeprom_write_byte(0x5, 0xB8); i2c_eeprom_write_byte(0x7F, 0x01); i2c_eeprom_write_byte(0x38, 0xFF); } } }
int setMenu(int id) { int i=0; menu_un Menu_temp; int menu_start_addr=0; for(i=0;i<MENU_DATA_LENGTH;i++) { Menu_temp.data[i]=Menu.data[i]; } menu_start_addr=MENU_DATA_START_ADDR+(id*MENU_DATA_LENGTH); for(i=0;i<MENU_DATA_LENGTH;i++) { i2c_eeprom_write_byte(menu_start_addr+i,Menu_temp.data[i]); delay(50); } getMenu(id); for(i=0;i<MENU_DATA_LENGTH;i++) { if(Menu_temp.data[i]!=Menu.data[i]) return 0; } return 1; }
void led_ctrl(uint8_t touch) { if(touch == 0x80) { uint8_t data; uint16_t Frame_Average, EP_L, EP_H, Exposure_Line; i2c_eeprom_write_byte(0x7f,0x00); i2c_eeprom_write_byte(0x05, 0x98); data = i2c_eeprom_read_byte(0x33); EP_H=data&0x03; data = i2c_eeprom_read_byte(0x32); EP_L=data; Exposure_Line=(EP_H<<8)+EP_L; i2c_eeprom_write_byte(0x7f,0x01); if(_sleepflag==1) { i2c_eeprom_write_byte(0x38, (0xE0|DEFAULT_LED_STEP)); _sleepflag = 0 ; } if (_state_count <= STATE_COUNT_TH) { _state_count++; _led_current_change_flag = 0; } else { _state_count = 0; if(_state == 0) { if( (Exposure_Line>=LED_CTRL_EXPO_TIME_HI_BOUND) || (Exposure_Line<=LED_CTRL_EXPO_TIME_LOW_BOUND ) ) { data = i2c_eeprom_read_byte(0x38); _led_step=data&0x1f; if( (Exposure_Line>=LED_CTRL_EXPO_TIME_HI_BOUND) && (_led_step < LED_CURRENT_HI)) { _state = 1 ; _led_step=_led_step+LED_INC_DEC_STEP; if(_led_step>LED_CURRENT_HI) _led_step=LED_CURRENT_HI; i2c_eeprom_write_byte(0x38, (_led_step|0xE0)); _led_current_change_flag = 1; } else if((Exposure_Line<=LED_CTRL_EXPO_TIME_LOW_BOUND) && (_led_step > LED_CURRENT_LOW)) { _state = 2 ; if(_led_step<=(LED_CURRENT_LOW+LED_INC_DEC_STEP)) _led_step=LED_CURRENT_LOW; else _led_step=_led_step-LED_INC_DEC_STEP; i2c_eeprom_write_byte(0x38, (_led_step|0xE0)); _led_current_change_flag = 1; }else { _state = 0 ; _led_current_change_flag = 0; } } else { _led_current_change_flag = 0; } } else if(_state == 1) { if(Exposure_Line > LED_CTRL_EXPO_TIME_HI) { _state = 1 ; _led_step=_led_step+LED_INC_DEC_STEP; if(_led_step>=LED_CURRENT_HI) { _state = 0 ; _led_step=LED_CURRENT_HI; } i2c_eeprom_write_byte(0x38, (_led_step|0xE0)); _led_current_change_flag = 1; } else { _state = 0 ; _led_current_change_flag = 0; } } else { if(Exposure_Line < LED_CTRL_EXPO_TIME_LOW) { _state = 2 ; if(_led_step<=(LED_CURRENT_LOW+LED_INC_DEC_STEP)) { _state = 0 ; _led_step=LED_CURRENT_LOW; } else _led_step=_led_step-LED_INC_DEC_STEP; i2c_eeprom_write_byte(0x38, (_led_step|0xE0)); _led_current_change_flag = 1; } else { _state = 0; _led_current_change_flag = 0; } } } } else { i2c_eeprom_write_byte(0x7f,0x00); i2c_eeprom_write_byte(0x05, 0xB8); i2c_eeprom_write_byte(0x7F, 0x01); _led_step = DEFAULT_LED_STEP; //i2c_eeprom_write_byte(0x38, (0xE0 | DEFAULT_LED_STEP)); //for Asian person only i2c_eeprom_write_byte(0x38, 0xFF); _sleepflag = 1; _led_current_change_flag = 0; } }