Close

Listening to client updates while displaying animations

A project log for Gem Lamp

Origami enclosed smart lamp

ariAri 06/20/2021 at 23:160 Comments

When displaying animations on micro controller each frame is an instruction. Light these leds, now these ones, and so on.

So I was having the problem of how to make the animation loop keep running but still listen to new commands from the client.
After several attempts and reading the example programs more in depth I started to understand how the code works,

The flow for receiving commands is:

  1. Client Connects
  2. Code reads what is at the ending of the client connection url
  3. Runs the code inside the respective url end
  4. Client Disconnects

server.avaible() will listen if there is a new call to the web server running on the ESP32 board. If there is one, will leave the loop of the animation and start again the general loop where it processes the requests.
In case the animation is too long we can make a checkpoint during the animation to abort the while loop if there's a new client

void swirl(){
  uint8_t sweep[] = {1, 2, 3, 4, 6, 8, 10, 15, 20, 30, 40, 60, 60, 40, 30, 20, 15, 10, 8, 6, 4, 3, 2, 1};
  while(!server.available()){ //run while there is no new calls
  for (uint8_t incr = 0; incr < 24; incr++)
    for (uint8_t x = 0; x < 16; x++)
      for (uint8_t y = 0; y < 9; y++)
        matrix.drawPixel(x, y, sweep[(x + y + incr) % 24]); //display leds
  delay(20);
  }
}

Discussions