Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 21 additions & 219 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=EiMOS
version=0.2.0
version=0.2.1
author=Chito Kim <nekroztrap@gmail.com>
maintainer=Chito Kim <nekroztrap@gmail.com>
sentence=Versatile Mahjong Scorer
Expand Down
117 changes: 70 additions & 47 deletions src/EiMOS.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
/*
Special Thanks to Mahjong
Special Thanks to Mahjong

EiMOS Library - EiMOS.cpp
# Formerly known as mahjongAsst
# Renamed June 6, 2024
EiMOS Library - EiMOS.cpp
Formerly known as mahjongAsst
Renamed June 6, 2024

A Library for legacy scoring system of Japanese mahjong tables.
Legacy mahjong scorers implement special score sticks containing electrical elements such as R, L, or C.
This library measures parallel resistances/capacitances of stack-piled score sticks,
and convert the values into actual scores of 4 mahjong players.
A Library for legacy scoring system of Japanese mahjong tables.
Legacy mahjong scorers implement special score sticks containing electrical elements such as R, L, or C.
This library measures parallel resistances/capacitances of stack-piled score sticks,
and convert the values into actual scores of 4 mahjong players.

* Copyright (c) 2023, Chito Kim
* All rights reserved.
*
* 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/>.
Copyright (c) 2023, Chito Kim
All rights reserved.

Codes for capacitance measurement is based on a guide of Circuit Basics,
and library "arduino-capacitor" written by Jonathan Nethercott.
https://www.circuitbasics.com/how-to-make-an-arduino-capacitance-meter/
The core idea of using internal pull-ups and capacitance calculation through
logarithm is first designed by Jonathan Nethercott.
https://github.com/codewrite/arduino-capacitor
Codes for capacitance measurement is based on an example code of Jonathan Nethercott.
https://wordpress.codewrite.co.uk/pic/2014/01/25/capacitance-meter-mk-ii/
*/
#include "EiMOS.h"
#include <Arduino.h>
Expand Down Expand Up @@ -510,7 +493,7 @@ EiMOS::mesRLC(int slot_num)
int dig_val;
float RLC_unit = (pin_p->RLC_per_unit)[slot_num % NSLOT];
float r_ref = (pin_p->R_REF)[slot_num % NSLOT];
float r_par = (pin_p->R_REF)[slot_num % NSLOT];
float r_par = (pin_p->R_PAR)[slot_num % NSLOT];
float RC;
unsigned long t, tf, dt, discharge_t;
switch(mes_type)
Expand All @@ -535,7 +518,7 @@ EiMOS::mesRLC(int slot_num)
} while(!dig_val && dt < 1000000L); // measure time until charged
pinMode(apin, INPUT);
adc = adcRead(apin);
RC = adcToCap(dt, adc, r_ref); // read capacitor voltage and calculate capacitance
RC = adcToCap(dt, adc, r_ref, RLC_unit, r_par); // read capacitor voltage and calculate capacitance
discharge_t = 5L * dt / 1000L;
if(pull_type == INPUT_PULLUP)
{
Expand Down Expand Up @@ -577,10 +560,6 @@ EiMOS::RLCToNum(float RLC, int slot_num)
break;
case CAP:
ratio = RLC / RLC_unit;
if(hasParRes(r_par))
{
ratio = correctCap(ratio, r_par, r_ref);
}
num = (int) ratio;
if(num == 0 && ratio > 0.8f)
{
Expand Down Expand Up @@ -616,19 +595,63 @@ EiMOS::hasParRes(float f)
// return false because by default parres equals to PIN_NONE
}
float
EiMOS::adcToCap(unsigned long t, uint16_t adc, float r_ref)
{
// calculate capacitance using charge time and capacitor voltage
return -(float) t / r_ref / log(1.0f - (float) adc / (float) env_p->ADC_MAX);
}
float
EiMOS::correctCap(float ratio, float r_par, float r_ref)
EiMOS::adcToCap(unsigned long t, uint16_t adc, float r_ref, float c_unit, float r_par = PIN_NONE)
{
// corrects the influence of resistance parallel to a capacitor
// example : CENTURY_GOLD 5k stick has a 1M parallel resistance
// needs experiments
float RHO = r_par / 2.0f / r_ref;
return (sqrt(RHO * RHO + 2.0f * RHO * ratio) - RHO);
// calculate capacitance from time and adc readings
// if r_par exists calculate by iterations
float vRatio = (float) adc / (float) env_p->ADC_MAX;
const float lambda = 0.5f;
const float tolerance = 1e-6;

// Inital Setup
float alpha = 1.0f;
float k_new = 0;
float k_old = 0;

// Calculate the uncorrected k (Ce factor) to be the best possible starting point (k_old)
float log_init_arg = 1.0f - vRatio;
if (log_init_arg <= 0)
{
// Vc is too high, log is impossible
return 0.0f;
}
k_old = - t / (r_ref * c_unit * log(log_init_arg));
k_new = k_old; // Initialize k_new

int i;

// iteration loop with convergence and stability checks
for(i = 0; i < 50; i++)
{
// calculate alpha from last calculated k
float k_used = (k_old <= 0.0) ? 1.0 : k_old;
float alpha_calc = r_par / (k_used * r_ref + r_par);

// damp alpha so that capacitance measurements don't diverge
float alpha_new = lambda * alpha_calc + (1.0f - lambda) * alpha;
float log_arg = 1.0f - vRatio / alpha_new;

if(log_arg <= 0)
{
// divergence occured. return the last stable result
return k_old * c_unit;
}

// calculate new k using the damped alpha
k_new = -t / (alpha_new * r_ref * c_unit * log(log_arg));

// check for convergence
if(fabs(k_new - k_old) < tolerance)
{
break;
}

// update for next iteration
k_old = k_new;
alpha = alpha_new;
}

return k_new * c_unit;
}
void
EiMOS::discharge(int cpin, int apin)
Expand Down
52 changes: 17 additions & 35 deletions src/EiMOS.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
/*
Special Thanks to Mahjong

EiMOS Library - EiMOS.h
# Formerly known as mahjongAsst
# Renamed 6 June, 2024

A Library for legacy scoring system of Japanese mahjong tables.
Legacy mahjong scorers implement special score sticks containing electrical elements such as R, L, or C.
This library measures parallel resistances/capacitances of stack-piled score sticks,
and convert the values into actual scores of 4 mahjong players.

* Copyright (c) 2023, Chito Kim
* All rights reserved.
*
* 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/>.

Codes for capacitance measurement is based on a guide of Circuit Basics,
and library "arduino-capacitor" written by Jonathan Nethercott.
https://www.circuitbasics.com/how-to-make-an-arduino-capacitance-meter/
The core idea of using internal pull-ups and capacitance calculation through
logarithm is first designed by Jonathan Nethercott.
https://github.com/codewrite/arduino-capacitor
Special Thanks to Mahjong

EiMOS Library - EiMOS.h
Formerly known as mahjongAsst
Renamed 6 June, 2024

A Library for legacy scoring system of Japanese mahjong tables.
Legacy mahjong scorers implement special score sticks containing electrical elements such as R, L, or C.
This library measures parallel resistances/capacitances of stack-piled score sticks,
and convert the values into actual scores of 4 mahjong players.

Copyright (c) 2023, Chito Kim
All rights reserved.

Codes for capacitance measurement is based on an example code of Jonathan Nethercott.
https://wordpress.codewrite.co.uk/pic/2014/01/25/capacitance-meter-mk-ii/
*/

#ifndef _MAHJONGASST_H
Expand Down Expand Up @@ -140,8 +123,7 @@ class EiMOS
////
void setParRes(float f[]);
int hasParRes(float f);
float adcToCap(unsigned long t, uint16_t adc, float r);
float correctCap(float f, float r, float ref);
float adcToCap(unsigned long t, uint16_t adc, float r, float c_unit, float r_par);
void discharge(int cpin, int apin);
void charge(int cpin);
//
Expand Down
23 changes: 5 additions & 18 deletions src/MUX.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
/*
MUX.cpp
A library that controlls the switching of an MUX.
MUX.cpp
A library that controlls the switching of an MUX.

* Copyright (c) 2023, Chito Kim
* All rights reserved.
*
* 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/>.
*/
Copyright (c) 2023, Chito Kim
All rights reserved.
*/

#include "MUX.h"
#include <Arduino.h>
Expand Down
23 changes: 5 additions & 18 deletions src/MUX.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
/*
MUX.h
A library that controlls the switching of an MUX.
MUX.h
A library that controlls the switching of an MUX.

* Copyright (c) 2023, Chito Kim
* All rights reserved.
*
* 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/>.
*/
Copyright (c) 2023, Chito Kim
All rights reserved.
*/

#ifndef _MUX_H
#define _MUX_H
Expand Down
25 changes: 6 additions & 19 deletions src/components.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
/*
components.h
A library that handles the parameters of mahjong scoring,
into three different structs : ENV, PIN, VAL
components.h
A library that handles the parameters of mahjong scoring,
into three different structs : ENV, PIN, VAL

* Copyright (c) 2023, Chito Kim
* All rights reserved.
*
* 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/>.
*/
Copyright (c) 2023, Chito Kim
All rights reserved.
*/
#include "ADS1X15.h"
#ifndef _COMPONENTS_H
#define _COMPONENTS_H
Expand Down