Blame view

RIOT/sys/shell/commands/sc_saul_reg.c 3.33 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  /*
   * Copyright (C) 2015 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   *
   */
  
  /**
   * @ingroup     sys_shell_commands
   * @{
   *
   * @file
   * @brief       SAUL registry shell commands
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
  
  #include "saul_reg.h"
  
  /* this function does not check, if the given device is valid */
  static void probe(int num, saul_reg_t *dev)
  {
      int dim;
      phydat_t res;
  
      dim = saul_reg_read(dev, &res);
      if (dim <= 0) {
          printf("error: failed to read from device #%i\n", num);
          return;
      }
      /* print results */
      printf("Reading from #%i (%s|%s)\n", num, dev->name,
             saul_class_to_str(dev->driver->type));
      phydat_dump(&res, dim);
  }
  
  static void probe_all(void)
  {
      saul_reg_t *dev = saul_reg;
      int i = 0;
  
      while (dev) {
          probe(i++, dev);
          puts("");
          dev = dev->next;
      }
  }
  
  static void list(void)
  {
      saul_reg_t *dev = saul_reg;
      int i = 0;
  
      if (dev) {
          puts("ID\tClass\t\tName");
      }
      else {
          puts("No devices found");
      }
      while (dev) {
          printf("#%i\t%s\t%s\n",
                 i++, saul_class_to_str(dev->driver->type), dev->name);
          dev = dev->next;
      }
  }
  
  static void read(int argc, char **argv)
  {
      int num;
      saul_reg_t *dev;
  
      if (argc < 3) {
          printf("usage: %s %s <device id>|all\n", argv[0], argv[1]);
          return;
      }
      if (strcmp(argv[2], "all") == 0) {
          probe_all();
          return;
      }
      /* get device id */
      num = atoi(argv[2]);
      dev = saul_reg_find_nth(num);
      if (dev == NULL) {
          puts("error: undefined device id given");
          return;
      }
      probe(num, dev);
  }
  
  static void write(int argc, char **argv)
  {
      int num, dim;
      saul_reg_t *dev;
      phydat_t data;
  
      if (argc < 4) {
          printf("usage: %s %s <device id> <value 0> [<value 1> [<value 2]]\n",
                 argv[0], argv[1]);
          return;
      }
      num = atoi(argv[2]);
      dev = saul_reg_find_nth(num);
      if (dev == NULL) {
          puts("error: undefined device given");
          return;
      }
      /* parse value(s) */
      memset(&data, 0, sizeof(data));
      dim = ((argc - 3) > (int)PHYDAT_DIM) ? (int)PHYDAT_DIM : (argc - 3);
      for (int i = 0; i < dim; i++) {
          data.val[i] = atoi(argv[i + 3]);
      }
      /* print values before writing */
      printf("Writing to device #%i - %s\n", num, dev->name);
      phydat_dump(&data, dim);
      /* write values to device */
      dim = saul_reg_write(dev, &data);
      if (dim <= 0) {
          if (dim == -ENOTSUP) {
              printf("error: device #%i is not writable\n", num);
          }
          else {
              printf("error: failure to write to device #%i\n", num);
          }
          return;
      }
      printf("data successfully written to device #%i\n", num);
  }
  
  int _saul(int argc, char **argv)
  {
      if (argc < 2) {
          list();
      }
      else {
          if (strcmp(argv[1], "read") == 0) {
              read(argc, argv);
          }
          else if (strcmp(argv[1], "write") == 0) {
              write(argc, argv);
          }
          else {
              printf("usage: %s read|write\n", argv[0]);
          }
      }
      return 0;
  }