aio: fix styling of case statement
[contrib/mraa.git] / src / aio / aio.c
1 /*
2  * Author: Nandkishor Sonar
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <stdlib.h>
26
27 #include "aio.h"
28
29 static maa_result_t aio_get_valid_fp(maa_aio_context* dev)
30 {
31     char file_path[64]= "";
32
33     //Open file Analog device input channel raw voltage file for reading.
34     snprintf(file_path, 64, "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw",
35         dev->channel );
36
37     if (NULL == (dev->adc_in_fp = fopen(file_path, "r"))) {
38         fprintf(stderr, "Failed to open Analog input raw file %s for "
39             "reading!\n", file_path); return( MAA_ERROR_INVALID_RESOURCE);
40     }
41
42     return MAA_SUCCESS;
43 }
44
45 /** Initialise an Analog input, connected to the specified channel
46  *
47  * @param aio_channel Analog input channel to read
48  *
49  * @returns pointer to maa_aio_context structure  after initialisation of
50  * Analog input pin connected to the device successfully, else returns NULL.
51  */
52 maa_aio_context* maa_aio_init(unsigned int aio_channel)
53 {
54     maa_aio_context* dev;
55
56     int checked_pin = maa_check_aio(aio_channel);
57     if (checked_pin < 0) {
58         switch(checked_pin) {
59             case -1:
60                 fprintf(stderr, "Invalid analog input channel %d specified\n",
61                             aio_channel);
62                 return NULL;
63             case -2:
64                 fprintf(stderr, "Failed to set-up analog input channel %d "
65                         "multiplexer\n", aio_channel);
66                 return NULL;
67             case -3:
68                 fprintf(stderr, "Platform not initialised");
69                 return NULL;
70             default: return NULL;
71         }
72     }
73
74     //Create ADC device connected to specified channel
75     dev = (maa_aio_context*) malloc(sizeof(maa_aio_context));
76     if (dev == NULL) {
77         fprintf(stderr, "Insufficient memory for specified Analog input channel "
78             "%d\n", aio_channel);
79         return NULL;
80     }
81     dev->channel = checked_pin;
82
83     //Open valid  analog input file and get the pointer.
84     if (MAA_SUCCESS != aio_get_valid_fp(dev)) {
85         free(dev);
86         return NULL;
87     }
88
89     return dev;
90 }
91
92 /** Read the input voltage, represented as an unsigned short in the range [0x0,
93  * 0xFFFF]
94  *
95  * @param pointer to maa_aio_context structure  initialised by
96  * maa_aio_init()
97  *
98  * @returns
99  *   16-bit unsigned int representing the current input voltage, normalised to
100  *   a 16-bit value
101  */
102 unsigned int maa_aio_read_u16(maa_aio_context* dev)
103 {
104     char buffer[16] = "";
105     unsigned int raw_value=0;
106     unsigned int analog_value=0;
107     unsigned int shifter_value=0;
108
109     if (NULL == dev->adc_in_fp) {
110         aio_get_valid_fp(dev);
111     }
112
113     fseek(dev->adc_in_fp, SEEK_SET, 0);
114     fread(buffer, sizeof(buffer), 1, dev->adc_in_fp);
115     fseek(dev->adc_in_fp, SEEK_SET, 0);
116
117     raw_value = atoi(buffer);
118
119     /* Adjust the raw analog input reading to supported resolution value*/
120     if (ADC_RAW_RESOLUTION_BITS == ADC_SUPPORTED_RESOLUTION_BITS) {
121         analog_value = raw_value;
122     }
123     else {
124         if (ADC_RAW_RESOLUTION_BITS > ADC_SUPPORTED_RESOLUTION_BITS) {
125             shifter_value = ADC_RAW_RESOLUTION_BITS - ADC_SUPPORTED_RESOLUTION_BITS;
126             analog_value =  raw_value >> shifter_value;
127         } else {
128             shifter_value = ADC_SUPPORTED_RESOLUTION_BITS - ADC_RAW_RESOLUTION_BITS;
129             analog_value = raw_value << shifter_value;
130         }
131     }
132
133     return analog_value;
134 }
135
136 /** Close the analog input and free context memory
137  *
138  * @param dev - the analog input context
139  *
140  * @return maa result type.
141  */
142 maa_result_t maa_aio_close(maa_aio_context* dev)
143 {
144     if (NULL != dev)
145         free(dev);
146
147     return(MAA_SUCCESS);
148 }