ESP32 and the philosopers dinner source code

      

#define MAX_PHILOSOPHERS 6

#define LEFT  (i+MAX_PHILOSOPHERS-1)%MAX_PHILOSOPHERS
#define RIGHT (i+1)%MAX_PHILOSOPHERS

#define THINKING 0
#define HUNGRY   1
#define EATING   2

// GPIO map                      0   1   2   3   4   5
int pinMap[MAX_PHILOSOPHERS] = {32, 22, 19, 33, 23, 25};

int numbering=0;
SemaphoreHandle_t mutex;
int state[MAX_PHILOSOPHERS];
TaskHandle_t taskHandle[MAX_PHILOSOPHERS];


/**
 * A mechanism to give a different number to each task
 */
int giveMeANumber(){
  xSemaphoreTake( mutex, portMAX_DELAY );
    int nextNumber = numbering;
    numbering++;
  xSemaphoreGive( mutex );
  return nextNumber;
} 

/**
 * May the philosopher eat?
 */
int mayEat(int i){
  xSemaphoreTake( mutex, portMAX_DELAY );
  
    int response=0;
    // A philosopher can eat only if their neighbors philosopher are not eating
    if((state[i] == HUNGRY) && (state[LEFT] != EATING) && (state[RIGHT] != EATING)){
      state[i] = EATING;
      Serial.print("\tPhilosopher ");
      Serial.print(i);
      Serial.println(" free to eat ");
      response=1;
    }
    
  xSemaphoreGive( mutex );
  return response;
}

/** 
 * Leave eating and go to think 
 */
void got_to_think(int i){
  xSemaphoreTake( mutex, portMAX_DELAY );
  
    Serial.print("Philosopher ");
    Serial.print(i);  
    Serial.println(" got to thinking");
    // Just change its state
    state[i] = THINKING;
  
  xSemaphoreGive( mutex );
}

/** 
 * Leave thinking then HUNGRY state then wait for forks and go to eat 
 */
void go_to_eat(int i){
  int cont=0;
  state[i] = HUNGRY;
  Serial.print("Philosopher ");
  Serial.print(i);   
  Serial.println(" is HUNGRY");
  // Checking many times for the forks availability: mayEat() ?
  while(!mayEat(i)){
    // I am HUNGRY and must WAIT !!! grrrr
    digitalWrite(pinMap[i], HIGH);
    delay(30);
    digitalWrite(pinMap[i], LOW);
    delay(30);
  }
}

/** 
 * The philospher is thinking  
 */
void thinking(int i){
  Serial.print("Philosopher ");
  Serial.print(i);   
  Serial.println(" is thinking");  

  // Thinking simulation :)
  int amountOfTinkings = random(10,30);
  for(int j=0;j<=amountOfTinkings;j++){
    digitalWrite(pinMap[i], HIGH);
    delay(30);
    digitalWrite(pinMap[i], LOW);
    delay(400);
    digitalWrite(5, HIGH);
  }
}

/** 
 * The philosopher is eating yami yami  
 */
void eating(int i){
  Serial.print("Philosopher ");
  Serial.print(i);   
  Serial.println(" is eating yami yami mmmm");  

  // Eating simulation :)
  int amountOfMeal = random(250,300);
  int duty=0, delta=1;
  ledcSetup(i, 5000, 8);
  ledcAttachPin(pinMap[i], i);  
  for(int j=0;j<=amountOfMeal;j++){
    ledcWrite(i, duty);
    duty+=delta;
    if(duty >= 30) delta=-1;
    if(duty <=  0) delta=+1;
    delay(50);
    digitalWrite(5, LOW);
  }
  ledcDetachPin(pinMap[i]);
}

/** 
 * Philosopher life cicle 
 */
void philosopherLifeCicle( void * parameter ){
  int pn = giveMeANumber();
  Serial.print("Philosopher ");
  Serial.print(pn);   
  Serial.print(" was born on core ");
  Serial.println(xPortGetCoreID());
  delay(100);
  
  // Endless life cicle of a philosopher
  for (;;) {
    thinking    (pn); // Doing what the philosophers know how to do
    go_to_eat   (pn); // Checking many times for the forks availability
    eating      (pn); // Yami yami
    got_to_think(pn); // Back to work :)
  }
}

void setup() {
  char buff[32];
  // Hardware setup
  Serial.begin(115200);
  randomSeed(analogRead(0));
  for (int i=0; i < MAX_PHILOSOPHERS; i++){
    pinMode(pinMap[i], OUTPUT);
  }
  pinMode(5, OUTPUT);

  // Create semaphore
  mutex = xSemaphoreCreateMutex();

  // Create tasks
  Serial.println("Creating philosophers");
  for (int i=0; i < MAX_PHILOSOPHERS-1; i++){
    sprintf(buff, "philosopher_%d", i);
    xTaskCreatePinnedToCore(
        philosopherLifeCicle, // Code to be executed
        buff,                 // Descriptive name
        1000,                 // Stack size
        NULL,                 // Parameters
        1,                    // Prioroty
        &taskHandle[i],       // Handler
        (i%2)                 // Core 0 or 1
    );
    delay(100);
  }
  Serial.println("Philosophers created");
}


void loop() {
  // The last philosopher runs in the main task
  // In this way all the tasks are used and the amount 
  // of processor-time is the same for all them
  philosopherLifeCicle(NULL);
}