File: EEPRom.m

package info (click to toggle)
octave-arduino 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,616 kB
  • sloc: cpp: 3,221; python: 438; makefile: 152; xml: 22; sh: 1
file content (172 lines) | stat: -rw-r--r-- 5,639 bytes parent folder | download
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
## Copyright (C) 2018-2020 John Donoghue <john.donoghue@ieee.org>
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see
## <https://www.gnu.org/licenses/>.

classdef EEPRom < arduinoio.LibraryBase
  ## -*- texinfo -*- 
  ## @deftypefn {} {} arduinoioaddons.EEPRomAddon.EEPRom
  ## EEPROM addon for arduino
  ##
  ## Allows read and write of uint8 data to the onboard arduino EEPROM.
  ##
  ## @subsubheading Example
  ## Assuming eeprom addon has been programmed into the Arduino:
  ## @example
  ## a = arduino ();
  ## e = addon (a, "eepromaddon/eeprom");
  ## write (e, 0, uint8("hello world"));
  ## str = uint8( read(e, 0, 11) )
  ## @end example
  ##
  ## @seealso{addon}
  ## @end deftypefn
  ##
  ## @subsubheading Properties
  ## @var{length} - Size of the EEPROM.
  ##
  ## @subheading Methods
  ## @deftypefn {} {@var{eeprom} =} EEPRom ()
  ## Constructor to create EEPROM device.
  ## @subsubheading Outputs
  ## @var{eeprom} - created EEPROM device.
  ## @end deftypefn
  ##
  ## @deftypefn {} {} erase ()
  ## Erase all values in EEPROM (Effectively setting the 0xFF)
  ## @end deftypefn
  ##
  ## @deftypefn {} {} write (@var{address}, @var{uintdata})
  ## Write data to EEPROM at the provided address.
  ## @subsubheading Inputs
  ## @var{address} - start address to write data to, should be an
  ## integer between 0 and the size of the EEPROM.
  ##
  ## @var{uintdata} a value or array of uint8 data to write to EEPROM.
  ## @end deftypefn
  ##
  ## @deftypefn {} {@var{data} =} read (@var{address})
  ## @deftypefnx {} {@var{data} =} read (@var{address}, @var{count})
  ## Read data from starting address of EEPROM.
  ## @subsubheading Inputs
  ## @var{address} - start address to read data from, should be an
  ## integer between 0 and the size of the EEPROM.
  ##
  ## @var{count} - Number of uint8 values to read from the EEPROM (default is 1)
  ##
  ## @subsubheading Outputs
  ## @var{data} a value or array of uint8 data read from the EEPROM.
  ## @end deftypefn

  properties(Access = private)
    len = 0;
  endproperties

  # commands
  properties(Access = private, Constant = true)
    INIT_COMMAND  = hex2dec('00');
    ERASE_COMMAND = hex2dec('01');
    READ_COMMAND  = hex2dec('02');
    WRITE_COMMAND = hex2dec('03');
  endproperties   

  properties(Access = protected, Constant = true)
    LibraryName = 'EEPRomAddon/EEPRom';
    DependentLibraries = {};
    #ArduinoLibraryHeaderFiles = {};
    CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'EEPRomAddon.h');
    CppClassName = 'EEPRom';
  endproperties

  methods
    # constructor
    function obj = EEPRom(parentObj)
      obj.Parent = parentObj;

      if nargin != 1
        error ("EEPRom: expected arduino parent as only argument");
      endif

      obj.Pins = {};

      data = sendCommand(obj.Parent, obj.LibraryName, obj.INIT_COMMAND, []);

      length = uint16(data(1))*256 + uint16(data(2))
      obj.len = length;

    endfunction

    function len = length (obj)
      len = obj.len;
    endfunction

    function erase(obj)
      cmdID = obj.ERASE_COMMAND;
      sendCommand(obj.Parent, obj.LibraryName, cmdID, []);
    endfunction

    function value = read(obj, address, num)
      cmdID = obj.READ_COMMAND;

      if nargin != 2 && nargin != 3
        error ("EEPRom: expected address and optional number of bytes");
      endif
      if nargin == 2
        num = 1;
      endif
      if ! isnumeric(address)  || address < 0 || address >= obj.len || mod(address, 1) != 0
        error ("EEProm: expected address to be between 0 and %d", obj.len);
      endif
      if ! isnumeric(num)  || num < 0 || num > 128 || mod(num, 1) != 0
        error ("EEProm: num expected to be integer between  0 and %d", 128);
      endif

      if address + num > obj.len
	error ("EEPRom: address + num (%d) is out of EEPRom bounds of %d\n", (address+num), obj.len);
      endif
 
      intval = uint16(address);
      datain = [ bitshift(intval,-8) bitand(intval, 255) num];
      dataout = sendCommand(obj.Parent, obj.LibraryName, cmdID, datain);
      value = dataout;
    endfunction

    function write(obj, address, value)
      cmdID = obj.WRITE_COMMAND;
      if nargin != 3
        error ("EEPRom: expected address and value");
      endif
      if ! isnumeric(address)  || address < 0 || address >= obj.len || mod(address, 1) != 0
        error ("expected address to be between 0 and %d", obj.len);
      endif

      num = numel(value);
      if num < 0 || num > 128 
        error ("EEProm: value size expected to be between  0 and %d", 128);
      endif
      if (address + num > obj.len)
	error ("EEPRom: address + numel(value) (%d) is out of EEPRom bounds of %d\n", (address+num), obj.len);
      endif

      intval = uint16(address);
      datain = [ bitshift(intval,-8) bitand(intval, 255) value];
      sendCommand(obj.Parent, obj.LibraryName, cmdID, datain);
    endfunction

    function disp (obj)
      printf("    %s with properties\n", class(obj));
      printf("        length = %d\n", obj.len);
    endfunction
  endmethods
endclassdef