1 /*
2  * libiio - Library for interfacing industrial I/O (IIO) devices
3  *
4  * Copyright (C) 2015 Analog Devices, Inc.
5  * Author: Paul Cercueil <paul.cercueil@analog.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * */
18 
19 using System;
20 using System.Collections.Generic;
21 using System.Globalization;
22 using System.Linq;
23 using System.Text;
24 using System.Threading.Tasks;
25 
26 namespace iio
27 {
28     /// <summary><see cref="iio.Attr"/> class:
29     /// Contains the representation of a channel or device attribute.</summary>
30     public abstract class Attr
31     {
32         /// <summary>The name of this attribute.</summary>
33         public readonly string name;
34 
35         /// <summary>The filename in sysfs to which this attribute is bound.</summary>
36         public readonly string filename;
37 
Attr(string name, string filename = null)38         internal Attr(string name, string filename = null)
39         {
40             this.filename = filename == null ? name : filename;
41             this.name = name;
42         }
43 
44         /// <summary>Read the value of this attribute as a <c>string</c>.</summary>
45         /// <exception cref="System.Exception">The attribute could not be read.</exception>
read()46         public abstract string read();
47 
48         /// <summary>Set this attribute to the value contained in the <c>string</c> argument.</summary>
49         /// <param name="val">The <c>string</c> value to set the parameter to.</param>
50         /// <exception cref="System.Exception">The attribute could not be written.</exception>
write(string val)51         public abstract void write(string val);
52 
53         /// <summary>Read the value of this attribute as a <c>bool</c>.</summary>
54         /// <exception cref="System.Exception">The attribute could not be read.</exception>
read_bool()55         public bool read_bool()
56         {
57             string val = read();
58             return (val.CompareTo("1") == 0) || (val.CompareTo("Y") == 0);
59         }
60 
61         /// <summary>Read the value of this attribute as a <c>double</c>.</summary>
62         /// <exception cref="System.Exception">The attribute could not be read.</exception>
read_double()63         public double read_double()
64         {
65             return double.Parse(read(), CultureInfo.InvariantCulture);
66         }
67 
68         /// <summary>Read the value of this attribute as a <c>long</c>.</summary>
69         /// <exception cref="System.Exception">The attribute could not be read.</exception>
read_long()70         public long read_long()
71         {
72             return long.Parse(read(), CultureInfo.InvariantCulture);
73         }
74 
75         /// <summary>Set this attribute to the value contained in the <c>bool</c> argument.</summary>
76         /// <param name="val">The <c>bool</c> value to set the parameter to.</param>
77         /// <exception cref="System.Exception">The attribute could not be written.</exception>
write(bool val)78         public void write(bool val)
79         {
80             if (val)
81             {
82                 write("1");
83             }
84             else
85             {
86                 write("0");
87             }
88         }
89 
90         /// <summary>Set this attribute to the value contained in the <c>long</c> argument.</summary>
91         /// <param name="val">The <c>long</c> value to set the parameter to.</param>
92         /// <exception cref="System.Exception">The attribute could not be written.</exception>
write(long val)93         public void write(long val)
94         {
95             write(val.ToString(CultureInfo.InvariantCulture));
96         }
97 
98         /// <summary>Set this attribute to the value contained in the <c>double</c> argument.</summary>
99         /// <param name="val">The <c>double</c> value to set the parameter to.</param>
100         /// <exception cref="System.Exception">The attribute could not be written.</exception>
write(double val)101         public void write(double val)
102         {
103             write(val.ToString(CultureInfo.InvariantCulture));
104         }
105     }
106 }
107