Skip to content

Commit

Permalink
Stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
microfarad-de committed Jan 2, 2019
1 parent 7706d8e commit 43cfa6a
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 157 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
.DS_Store
*.tmp
27 changes: 24 additions & 3 deletions adc.cpp
@@ -1,10 +1,31 @@
/*
/*
* Abstraction layer for the ATmega328p ADC
* Non-blocking read the ADC output as an alternative
* to the blocking analogRead() method.
*
* Karim Hraibi - 2018
* This source file is part of the Lithium-Ion Battery Charger Arduino firmware
* found under http://www.github.com/microfarad-de/li-charger
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* 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 <http://www.gnu.org/licenses/>.
*/


#include "adc.h"
#include <assert.h>

Expand Down
24 changes: 22 additions & 2 deletions adc.h
@@ -1,9 +1,29 @@
/*
/*
* Abstraction layer for the ATmega328p ADC
* Non-blocking read the ADC output as an alternative
* to the blocking analogRead() method.
*
* Karim Hraibi - 2018
* This source file is part of the Lithium-Ion Battery Charger Arduino firmware
* found under http://www.github.com/microfarad-de/li-charger
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* 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 <http://www.gnu.org/licenses/>.
*/

#ifndef __ADC_H
Expand Down
74 changes: 46 additions & 28 deletions cli.cpp
@@ -1,9 +1,28 @@
/*
/*
* Command-line interpreter
*
* Karim Hraibi - 2018
* This source file is part of the Lithium-Ion Battery Charger Arduino firmware
* found under http://www.github.com/microfarad-de/li-charger
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* 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
*/

#include <Arduino.h>
//#include <stdio.h>
//#include <stdlib.h>
Expand All @@ -15,7 +34,7 @@
#define SERIAL_BAUD 115200 // Serial baud rate
#define PRINTF_BUF_SIZE 64 // printf buffer size
#define TEXT_LINE_SIZE 70 // Sets the maximum text line size
#define HELP_INDENT 15 // Help text block indentation
#define INDENT 15 // Help text block indentation



Expand Down Expand Up @@ -144,24 +163,23 @@ int CliClass::getCmd(void)
void CliClass::showHelp(void)
{
int i, j;
int duplicateFlag = 0;
Cli_Cmd_s *cmd[CLI_NUM_CMD];
bool duplicate = false;
CliCmd_s *cmd[CLI_NUM_CMD];
int numCmds = 0;
int len = 0;

if (!initialized) return EXIT_FAILURE;

// Assign valid commands to on array of temporary pointers
for (i = 0; i < this->numCmds && (i < CLI_NUM_CMD); i++) {

// Search for duplicate commands
duplicateFlag = 0;
duplicate = false;
for (j = 0; j < i; j++) {
if (this->cmd[i].fct == this->cmd[j].fct)
duplicateFlag = 1;
duplicate = true;
}
// Do not show the same command twice
if (!duplicateFlag) {
if (!duplicate) {
cmd[numCmds] = &this->cmd[i];
numCmds++;
}
Expand All @@ -179,10 +197,10 @@ void CliClass::showHelp(void)
len = strlen(cmd[i]->str) + 2;

// Search for alternative commands and display them between parantheses
duplicateFlag = 0;
duplicate = false;
for (j = 0; (j < this->numCmds) && (j < CLI_NUM_CMD); j++) {
if ((this->cmd[j].fct == cmd[i]->fct) && (strcmp(this->cmd[j].str, cmd[i]->str) != 0)) {
if (duplicateFlag == 0) {
if (!duplicate) {
xprintf(" (");
len += 2;
}
Expand All @@ -192,40 +210,40 @@ void CliClass::showHelp(void)
}
xprintf(this->cmd[j].str);
len += strlen(this->cmd[j].str);
duplicateFlag = 1;
duplicate = true;
}
}

if (duplicateFlag == 1) {
if (duplicate) {
xprintf(")");
len += 1;
}

textPadding(' ', HELP_INDENT - len - 2);
textPadding(' ', INDENT - len - 2);
xprintf(": ");
textPrintBlock(cmd[i]->doc, TEXT_LINE_SIZE, HELP_INDENT);
textPrintBlock(cmd[i]->doc, TEXT_LINE_SIZE, INDENT);
}

xprintf(" help (h) : ");
textPrintBlock("Show this help screen", TEXT_LINE_SIZE, HELP_INDENT);
textPrintBlock("this help screen", TEXT_LINE_SIZE, INDENT);
xputs("");
}


void CliClass::sortCmds(int numCmds, Cli_Cmd_s **cmd)
void CliClass::sortCmds(int numCmds, CliCmd_s **cmd)
{
int sortedFlag = 0;
bool sorted = false;
int i;
Cli_Cmd_s *tempCmd;
CliCmd_s *temp;

while (!sortedFlag) {
sortedFlag = 1;
while (!sorted) {
sorted = true;
for (i = 0; i < numCmds - 1; i++) {
if (strcmp(cmd[i]->str, cmd[i + 1]->str) > 0) {
sortedFlag = 0;
tempCmd = cmd[i];
sorted = false;
temp = cmd[i];
cmd[i] = cmd[i + 1];
cmd[i + 1] = tempCmd;
cmd[i + 1] = temp;
}
}
}
Expand All @@ -235,18 +253,18 @@ void CliClass::sortCmds(int numCmds, Cli_Cmd_s **cmd)
void CliClass::textPrintBlock(const char *text, int lineSize, int offset)
{
const char *c = text;
int cnt, i;
int count, i;

while (*c != 0) {
// remove leading spaces
while (*c == ' ' || *c == '\t') c++;

cnt = 0;
count = 0;
// write a line
while (cnt < (lineSize - offset) && *c != '\r' && *c != '\n' && *c != 0) {
while (count < (lineSize - offset) && *c != '\r' && *c != '\n' && *c != 0) {
xputchar(*c);
c++;
cnt++;
count++;
}

// finish the last word of a line
Expand Down
31 changes: 25 additions & 6 deletions cli.h
@@ -1,8 +1,27 @@
/*
/*
* Command-line interpreter
*
* Karim Hraibi - 2018
* This source file is part of the Lithium-Ion Battery Charger Arduino firmware
* found under http://www.github.com/microfarad-de/li-charger
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* 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 <http://www.gnu.org/licenses/>.
*/

#ifndef __Cli_H_
Expand All @@ -11,7 +30,7 @@

#define CLI_NUM_CMD 10 // Number of cli commands
#define CLI_NUM_ARG 5 // Number of cli arguments
#define CLI_ARG_LEN 8 // Maximum argument length
#define CLI_ARG_LEN 10 // Maximum argument length



Expand All @@ -23,7 +42,7 @@ typedef struct
const char *str; // Command name
const char *doc; // Command description
int (*fct)(int, char **); // Pointer to command function
} Cli_Cmd_s;
} CliCmd_s;


/*
Expand Down Expand Up @@ -76,11 +95,11 @@ class CliClass

void textPadding (char c, int size); // Insert repeated sequence of characters
void textPrintBlock (const char *text, int lineSize, int offset); // Print a formatted block of text
void sortCmds (int numCmds, Cli_Cmd_s **cmd); // Sort commands in alphabetical order
void sortCmds (int numCmds, CliCmd_s **cmd); // Sort commands in alphabetical order

char argBuf[CLI_NUM_ARG][CLI_ARG_LEN]; // Array of charactars for commands and arguments
char *argv[CLI_NUM_ARG]; // Array of pointers to argument strings
Cli_Cmd_s cmd[CLI_NUM_CMD]; // Array of commands
CliCmd_s cmd[CLI_NUM_CMD]; // Array of commands
int numCmds; // Total number of commands
bool initialized = false; // Initialization status

Expand Down
29 changes: 24 additions & 5 deletions helper.cpp
@@ -1,10 +1,29 @@
/*
/*
* Helper functions
*
* Karim Hraibi - 2018
* This source file is part of the Lithium-Ion Battery Charger Arduino firmware
* found under http://www.github.com/microfarad-de/li-charger
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* 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 <http://www.gnu.org/licenses/>.
*/


#include "helper.h"
#include <EEPROM.h>

Expand Down Expand Up @@ -63,7 +82,7 @@ void LedClass::toggle (void) {
}

void LedClass::blink (int32_t count, uint32_t tOn, uint32_t tOff) {
if (!initialized || blinking || count == 0) return;
if (!initialized || count == 0) return;
this->blinking = true;
this->count = 2 * count;
this->tOn = tOn;
Expand All @@ -74,7 +93,7 @@ void LedClass::blink (int32_t count, uint32_t tOn, uint32_t tOff) {
}

void LedClass::blinkStop (void) {
if (!initialized || blinking ) return;
if (!initialized) return;
blinking = false;
digitalWrite (ledPin, powerOn);
}
Expand Down
24 changes: 22 additions & 2 deletions helper.h
@@ -1,7 +1,27 @@
/*
/*
* Helper functions
*
* Karim Hraibi - 2018
* This source file is part of the Lithium-Ion Battery Charger Arduino firmware
* found under http://www.github.com/microfarad-de/li-charger
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* 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 <http://www.gnu.org/licenses/>.
*/

#ifndef __HELPER_H
Expand Down

0 comments on commit 43cfa6a

Please sign in to comment.