Let's develop something close to embedded with TDD ~ Problem raising ~

Introduction

It is a continuation of the following.

Try to develop something close to embedded with TDD ~ Preparation ~

We are thinking about the content in real time, so if you have any opinions or requests, please feel free to contact us. Code is available on github

Changes and decisions from the last time

Build tool

Changed the build tool from Autotools to CMake. CMake version 2.8 or later is a required environment.

Product code

The product code should be in C language. I don't plan to write very complicated code, so I plan to continue in raw C. If your code gets complicated, consider using glib.

What to make

** Press the "A" button on the keyboard to turn on / off the caps lock LED. ** **

For the time being, we will start from here and expand the specifications with the feeling of the moment, ideas and feedback. Except for the code posted this time, I will make it with TDD. In the end, I'd like to say that I was able to port it properly using hardware other than the keyboard with another hardware such as Raspberry Pi, congratulations! I haven't selected any really specific hardware at this time. You see, that's more like actual development, isn't it?

Problem presentation

Now, when you go back to what you are making and press the "A" button on the keyboard, the caps lock LED turns on and off **, what kind of implementation will you have when you make a program?

If you make it honestly, it will be a pseudo code like the following.

static void mainloop() {
  do {
Get key event
    if (A button was pressed) {
      write(Toggle LED);
    }
  } while (Until some condition is met);
}

int main() {
  mainloop();
  return 0;
}

When I actually made the above pseudo code, it became the following code. You confirmed the operation of libevdev in the previous test for understanding the library.

#define KEYBOARD_DEVICE "/dev/input/event2"
#define LED_DEVICE      "/sys/class/leds/input2::capslock/brightness"

#define KEY_RELEASED 0
#define KEY_PRESSED 1

static void mainloop() {
  struct libevdev *dev = NULL;
  int key_fd = open(KEYBOARD_DEVICE, O_RDONLY|O_NONBLOCK);
  int rc = libevdev_new_from_fd(key_fd, &dev);

  if (rc < 0) {
    fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
    exit(1);
  }

  int led_fd = open(LED_DEVICE, O_WRONLY|O_NONBLOCK);
  if (led_fd < 0) {
    fprintf(stderr, "Failed to init LED device.\n");
    exit(1);
  }

  bool led_on = false;
  do {
    struct input_event ev;
    rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
    if (rc == 0) {
      if (ev.type == EV_KEY && ev.code == KEY_A && ev.value == KEY_PRESSED) {
        led_on = !led_on;
        char buf[2];
        snprintf(buf, 2, "%d", led_on ? 1 : 0);
        write(led_fd, buf, 2);
      }
    }
  } while (rc == 1 || rc == 0 || rc == -EAGAIN);

  libevdev_free(dev);
  close(key_fd);
  close(led_fd);
}

int main() {
  mainloop();
  return 0;
}

If you have a Linux environment, you can actually run it. Please refer to the previous article, check the keyboard device and LED device, and correct the paths of the two macros (* KEYBOARD_DEVICE, LED_DEVICE *). After building and executing ** build / bad_example / bad_example **, the LED turns on and off each time the "A" key is pressed (root privileges are required depending on the environment).

Now, this code looks fine at first glance, but it has a lot of problems.

For these reasons, as the function expansion is repeated, it becomes increasingly difficult to add functions, and it becomes difficult to add functions without causing degreasing.

From the next time, I will actually try what happens when I develop this code with TDD, and add functions in order.

Next: Develop something close to embedded with TDD ~ file open ~

Recommended Posts

Let's develop something close to embedded with TDD ~ Problem raising ~
Let's develop something close to embedded with TDD ~ Preparation ~
Let's develop something close to embedded with TDD ~ Intermediate review ~
Let's develop something close to embedded with TDD ~ Design pattern ~
Let's develop something close to embedded with TDD ~ file open edition ~
Let's develop something close to embedded with TDD ~ Key input detection version ~
Let's develop something close to embedded with TDD ~ libevdev initialization / termination processing ~
Steps to develop Django with VSCode
[Introduction to WordCloud] Let's play with scraping ♬
[Introduction to Python] Let's use foreach with Python
How to read problem data with paiza
Let's develop an investment algorithm with Python 1