57 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #define TAILLE_MEMOIRE_I2C 256
 | |
| #define TAILLE_MESSAGE_ENVOI_MAX 32
 | |
| byte memoire_I2C[TAILLE_MEMOIRE_I2C];
 | |
| byte memoire_I2C_index=0;
 | |
| 
 | |
| bool nouveau_message=false;
 | |
| 
 | |
| void onRequest(){
 | |
|   uint32_t taille_envoi;
 | |
|   taille_envoi = min (TAILLE_MEMOIRE_I2C-memoire_I2C_index, TAILLE_MESSAGE_ENVOI_MAX);
 | |
|   
 | |
|   Wire.write(&memoire_I2C[memoire_I2C_index], taille_envoi);
 | |
|   memoire_I2C_index++;
 | |
|   if(memoire_I2C_index>=TAILLE_MEMOIRE_I2C){
 | |
|     Serial.printf("memoire_I2C_index>=TAILLE_MEMOIRE_I2C\n");
 | |
|   }
 | |
| }
 | |
| 
 | |
| void onReceive(int len){
 | |
|   memoire_I2C_index = Wire.read();
 | |
|   while(Wire.available()){
 | |
|     nouveau_message=true;
 | |
|     memoire_I2C[memoire_I2C_index] = Wire.read();
 | |
|     memoire_I2C_index++;
 | |
|   }
 | |
| }
 | |
| 
 | |
| void I2C_Slave_init(int addr){
 | |
|   Wire.onReceive(onReceive);
 | |
|   Wire.onRequest(onRequest);
 | |
|   Wire.begin(addr);
 | |
| }
 | |
| 
 | |
| bool I2C_Slave_nouveau_message(){
 | |
|   if(nouveau_message){
 | |
|     nouveau_message=false;
 | |
|     return true;
 | |
|   }
 | |
|   return false;
 | |
| }
 | |
| 
 | |
| void I2C_envoi_8bits(byte value, char adresse){
 | |
|   memoire_I2C[adresse] = value;
 | |
| }
 | |
| 
 | |
| void I2C_envoi_16bits(int16_t value, char adresse){
 | |
|   memoire_I2C[adresse] = value;
 | |
| }
 | |
| 
 | |
| void I2C_envoi_32bits(int32_t value, char adresse){
 | |
|   memoire_I2C[adresse] = value >> 24;
 | |
|   memoire_I2C[adresse+1] = (value >> 16) & 0xFF;
 | |
|   memoire_I2C[adresse+2] = (value >> 8) & 0xFF;
 | |
|   memoire_I2C[adresse+3] = value & 0xFF;
 | |
| }
 | |
| 
 |