Skip to content

Commit b95510b

Browse files
author
Ricky Cheung
committed
avr: Conserve memory with smallest data types
Signed-off-by: Ricky Cheung <rcheung844@gmail.com>
1 parent db49ea6 commit b95510b

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

Arduino_Code/Arduino_Code.ino

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ for default settings use -O0. -O may be a good tradeoff between both */
3131
#define SEP_TOKEN ","
3232
#define END_TOKEN "\n"
3333

34-
/* For 8-bit microcontrollers we should use 16 bit variables since the
35-
difficulty is low, for all the other cases should be 32 bits. */
36-
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
37-
typedef uint32_t uintDiff;
38-
#else
39-
typedef uint32_t uintDiff;
40-
#endif
4134
// Arduino identifier library - https://github.com/ricaun
4235
#include "uniqueID.h"
4336

@@ -48,7 +41,7 @@ typedef uint32_t uintDiff;
4841

4942
bool digitDrawn = false;
5043
bool matrixEnabled = true;
51-
int minedDigitCount = 0;
44+
byte minedDigitCount = 0;
5245

5346
Adafruit_Microbit_Matrix microbit;
5447
#endif
@@ -102,7 +95,7 @@ void lowercase_hex_to_bytes(char const * hexDigest, uint8_t * rawDigest) {
10295
}
10396

10497
// DUCO-S1A hasher
105-
uintDiff ducos1a(char const * prevBlockHash, char const * targetBlockHash, uintDiff difficulty) {
98+
uint32_t ducos1a(char const * prevBlockHash, char const * targetBlockHash, uint32_t difficulty) {
10699
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
107100
// If the difficulty is too high for AVR architecture then return 0
108101
if (difficulty > 655) return 0;
@@ -111,16 +104,16 @@ uintDiff ducos1a(char const * prevBlockHash, char const * targetBlockHash, uintD
111104
uint8_t target[SHA1_HASH_LEN];
112105
lowercase_hex_to_bytes(targetBlockHash, target);
113106

114-
uintDiff const maxNonce = difficulty * 100 + 1;
107+
uint32_t const maxNonce = difficulty * 100 + 1;
115108
return ducos1a_mine(prevBlockHash, target, maxNonce);
116109
}
117110

118-
uintDiff ducos1a_mine(char const * prevBlockHash, uint8_t const * target, uintDiff maxNonce) {
111+
uint32_t ducos1a_mine(char const * prevBlockHash, uint8_t const * target, uint32_t maxNonce) {
119112
static duco_hash_state_t hash;
120113
duco_hash_init(&hash, prevBlockHash);
121114

122115
char nonceStr[10 + 1];
123-
for (uintDiff nonce = 0; nonce < maxNonce; nonce++) {
116+
for (uint32_t nonce = 0; nonce < maxNonce; nonce++) {
124117
ultoa(nonce, nonceStr, 10);
125118

126119
uint8_t const * hash_bytes = duco_hash_try_nonce(&hash, nonceStr);
@@ -174,7 +167,7 @@ void loop() {
174167
newBlockHash[40] = 0;
175168

176169
// Read difficulty
177-
uintDiff difficulty = strtoul(Serial.readStringUntil(',').c_str(), NULL, 10);
170+
uint32_t difficulty = strtoul(Serial.readStringUntil(',').c_str(), NULL, 10);
178171
// Clearing the receive buffer reading one job.
179172
while (Serial.available()) Serial.read();
180173
// Turn off the built-in led
@@ -188,7 +181,7 @@ void loop() {
188181
uint32_t startTime = micros();
189182

190183
// Call DUCO-S1A hasher
191-
uintDiff ducos1result = ducos1a(lastBlockHash, newBlockHash, difficulty);
184+
uint32_t ducos1result = ducos1a(lastBlockHash, newBlockHash, difficulty);
192185

193186
// Calculate elapsed time
194187
uint32_t elapsedTime = micros() - startTime;

0 commit comments

Comments
 (0)