N2CMU Arduino
n2cmu.h
Go to the documentation of this file.
1/*
2 * This file is part of the N2CMU Arduino library (https://github.com/nthnn/n2cmu-arduino).
3 * Copyright (c) 2024 Nathanne Isip.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
29#ifndef N2CMU_H
30#define N2CMU_H
31
32#include <SoftwareSerial.h>
33
34#define N2CMU_RX_PIN 6
35#define N2CMU_TX_PIN 5
36#define N2CMU_RESET_TIMEOUT 4558
37
48private:
49 SoftwareSerial *n2serial;
50
60 bool getResultStatus();
61
67 bool sendCommand(uint8_t command);
68
73 float readF32();
74
79 uint8_t readU8();
80
85 uint16_t readU16();
86
91 void writeF32(float data);
92
97 void writeU16(uint16_t data);
98
104 void writeData(const uint8_t *data, uint8_t length);
105
106public:
117 uint8_t rx = N2CMU_RX_PIN,
118 uint8_t tx = N2CMU_TX_PIN
119 ): n2serial(new SoftwareSerial(rx, tx)) { }
120
132 bool begin();
133
144 bool handshake();
145
156 bool cpuReset();
157
171 void createNetwork(
172 uint8_t inputCount,
173 uint8_t hiddenCount,
174 uint8_t outputCount
175 );
176
192 bool train(
193 float* data,
194 float* output,
195 uint16_t len,
196 float learningRate
197 );
198
212 bool infer(float* input, float* output);
213
223 void resetNetwork();
224
237 void setInputCount(uint8_t inputCount);
238
251 void setHiddenCount(uint8_t hiddenCount);
252
265 void setOutputCount(uint8_t outputCount);
266
277 void setEpochCount(uint16_t epoch);
278
287 uint8_t getInputCount();
288
297 uint8_t getHiddenCount();
298
307 uint8_t getOutputCount();
308
317 uint16_t getEpochCount();
318
331 bool setHiddenNeuron(float* hiddenNeuron);
332
344 bool setOutputNeuron(float* outputNeuron);
345
357 bool setHiddenWeights(float* hiddenWeights);
358
370 bool setOutputWeights(float* outputWeights);
371
383 bool setHiddenBias(float* hiddenBias);
384
396 bool setOutputBias(float* outputBias);
397
410 bool setHiddenGradient(float* hiddenGrad);
411
424 bool setOutputGradient(float* outputGrad);
425
437 void getHiddenNeuron(float* hiddenNeuron);
438
450 void getOutputNeuron(float* outputNeuron);
451
464 void getHiddenWeights(float* hiddenWeights);
465
478 void getOutputWeights(float* outputWeights);
479
491 void getHiddenBias(float* hiddenBias);
492
504 void getOutputBias(float* outputBias);
505
517 void getHiddenGradient(float* hiddenGrad);
518
530 void getOutputGradient(float* outputGrad);
531
532 // Not yet implemented.
533 void loadFromFile(const char *modelFilename);
534
535 // Not yet implemented.
536 void saveToFile(const char* modelFilename);
537};
538
539#endif
Class representing the N2CMU device.
Definition: n2cmu.h:47
void setHiddenCount(uint8_t hiddenCount)
Set the number of hidden neurons.
Definition: n2cmu.cpp:178
bool infer(float *input, float *output)
Make inference with the neural network using provided input data.
Definition: n2cmu.cpp:146
void createNetwork(uint8_t inputCount, uint8_t hiddenCount, uint8_t outputCount)
Create a neural network with specified input, hidden, and output neuron counts.
Definition: n2cmu.cpp:104
bool train(float *data, float *output, uint16_t len, float learningRate)
Train the neural network with provided data and output.
Definition: n2cmu.cpp:119
uint8_t getOutputCount()
Get the number of output neurons.
Definition: n2cmu.cpp:201
void getOutputBias(float *outputBias)
Get output neuron biases.
Definition: n2cmu.cpp:320
bool cpuReset()
Reset the CPU of the N2CMU device.
Definition: n2cmu.cpp:97
void getOutputNeuron(float *outputNeuron)
Get output neuron values.
Definition: n2cmu.cpp:244
bool setHiddenBias(float *hiddenBias)
Set hidden neuron biases.
Definition: n2cmu.cpp:292
void getHiddenNeuron(float *hiddenNeuron)
Get hidden neuron values.
Definition: n2cmu.cpp:226
N2Coprocessor(uint8_t rx=N2CMU_RX_PIN, uint8_t tx=N2CMU_TX_PIN)
Constructor for N2Coprocessor class.
Definition: n2cmu.h:116
bool setOutputWeights(float *outputWeights)
Set output neuron weights.
Definition: n2cmu.cpp:272
bool begin()
Initialize the N2CMU device.
Definition: n2cmu.cpp:86
void getOutputWeights(float *outputWeights)
Get output neuron weights.
Definition: n2cmu.cpp:283
bool setOutputGradient(float *outputGrad)
Set output neuron gradients.
Definition: n2cmu.cpp:346
void setInputCount(uint8_t inputCount)
Set the number of input neurons.
Definition: n2cmu.cpp:164
bool setHiddenNeuron(float *hiddenNeuron)
Set hidden neuron values.
Definition: n2cmu.cpp:216
void getHiddenGradient(float *hiddenGrad)
Get hidden neuron gradients.
Definition: n2cmu.cpp:338
bool setHiddenGradient(float *hiddenGrad)
Set hidden neuron gradients.
Definition: n2cmu.cpp:328
void setOutputCount(uint8_t outputCount)
Set the number of output neurons.
Definition: n2cmu.cpp:192
bool handshake()
Perform handshake with the N2CMU device.
Definition: n2cmu.cpp:93
void setEpochCount(uint16_t epoch)
Set the epoch count for training.
Definition: n2cmu.cpp:206
void resetNetwork()
Reset the neural network parameters.
Definition: n2cmu.cpp:160
void getOutputGradient(float *outputGrad)
Get output neuron gradients.
Definition: n2cmu.cpp:356
uint16_t getEpochCount()
Get the epoch count for training.
Definition: n2cmu.cpp:211
void getHiddenBias(float *hiddenBias)
Get hidden neuron biases.
Definition: n2cmu.cpp:302
uint8_t getHiddenCount()
Get the number of hidden neurons.
Definition: n2cmu.cpp:187
bool setOutputNeuron(float *outputNeuron)
Set output neuron values.
Definition: n2cmu.cpp:234
uint8_t getInputCount()
Get the number of input neurons.
Definition: n2cmu.cpp:173
void getHiddenWeights(float *hiddenWeights)
Get hidden neuron weights.
Definition: n2cmu.cpp:263
bool setOutputBias(float *outputBias)
Set output neuron biases.
Definition: n2cmu.cpp:310
bool setHiddenWeights(float *hiddenWeights)
Set hidden neuron weights.
Definition: n2cmu.cpp:252
#define N2CMU_TX_PIN
Pin number for transmitting data to N2CMU.
Definition: n2cmu.h:35
#define N2CMU_RX_PIN
Pin number for receiving data from N2CMU.
Definition: n2cmu.h:34