@@ -427,6 +427,49 @@ unsigned long lwdTimeOutMillis = LWD_TIMEOUT;
427
427
#define BLINK_CLIENT_CONNECT 3
428
428
#define BLINK_RESET_DEVICE 5
429
429
430
+ template <unsigned int max_digits>
431
+ class Counter {
432
+ public:
433
+ Counter () { reset (); }
434
+
435
+ void reset () {
436
+ memset (buffer, ' 0' , max_digits);
437
+ buffer[max_digits] = ' \0 ' ;
438
+ val = 0 ;
439
+ len = 1 ;
440
+ }
441
+
442
+ Counter & operator ++() {
443
+ inc_string (max_digits - 1 );
444
+ ++val;
445
+ return *this ;
446
+ }
447
+
448
+ operator unsigned int () const { return val; }
449
+ const char * c_str () const { return buffer + max_digits - len; }
450
+ size_t strlen () const { return len; }
451
+
452
+ protected:
453
+ inline void inc_string (int pos) {
454
+ if (pos < 0 )
455
+ return ;
456
+
457
+ if (buffer[pos] < ' 9' ) {
458
+ buffer[pos]++;
459
+ } else {
460
+ buffer[pos] = ' 0' ;
461
+ inc_string (pos - 1 );
462
+ }
463
+
464
+ len = max (max_digits - pos, len);
465
+ }
466
+
467
+ protected:
468
+ char buffer[max_digits + 1 ];
469
+ unsigned int val;
470
+ size_t len;
471
+ };
472
+
430
473
void SetupWifi () {
431
474
Serial.println (" Connecting to: " + String (SSID));
432
475
WiFi.mode (WIFI_STA); // Setup ESP in client mode
@@ -518,6 +561,23 @@ void handleSystemEvents(void) {
518
561
yield ();
519
562
}
520
563
564
+ // https://stackoverflow.com/questions/9072320/split-string-into-string-array
565
+ String getValue (String data, char separator, int index)
566
+ {
567
+ int found = 0 ;
568
+ int strIndex[] = {0 , -1 };
569
+ int max_index = data.length () - 1 ;
570
+
571
+ for (int i = 0 ; i <= max_index && found <= index; i++) {
572
+ if (data.charAt (i) == separator || i == max_index) {
573
+ found++;
574
+ strIndex[0 ] = strIndex[1 ] + 1 ;
575
+ strIndex[1 ] = (i == max_index) ? i + 1 : i;
576
+ }
577
+ }
578
+ return found > index ? data.substring (strIndex[0 ], strIndex[1 ]) : " " ;
579
+ }
580
+
521
581
void waitForClientData (void ) {
522
582
client_buffer = " " ;
523
583
@@ -674,8 +734,7 @@ void setup() {
674
734
void loop () {
675
735
br_sha1_context sha1_ctx, sha1_ctx_base;
676
736
uint8_t hashArray[20 ];
677
- String duco_numeric_result_str;
678
-
737
+
679
738
// 1 minute watchdog
680
739
lwdtFeed ();
681
740
@@ -742,22 +801,21 @@ void loop() {
742
801
743
802
String result = " " ;
744
803
if (LED_BLINKING) digitalWrite (LED_BUILTIN, LOW);
745
- for (unsigned int duco_numeric_result = 0 ; duco_numeric_result < job. difficulty ; duco_numeric_result++ ) {
804
+ for (Counter< 8 > counter; counter < difficulty; ++counter ) {
746
805
// Difficulty loop
747
806
sha1_ctx = sha1_ctx_base;
748
- duco_numeric_result_str = String (duco_numeric_result);
749
807
750
- br_sha1_update (&sha1_ctx, duco_numeric_result_str .c_str (), duco_numeric_result_str. length ());
808
+ br_sha1_update (&sha1_ctx, counter .c_str (), counter. strlen ());
751
809
br_sha1_out (&sha1_ctx, hashArray);
752
810
753
811
if (memcmp (job.expected_hash , hashArray, 20 ) == 0 ) {
754
812
// If result is found
755
813
if (LED_BLINKING) digitalWrite (LED_BUILTIN, HIGH);
756
814
unsigned long elapsed_time = micros () - start_time;
757
815
float elapsed_time_s = elapsed_time * .000001f ;
758
- hashrate = duco_numeric_result / elapsed_time_s;
816
+ hashrate = counter / elapsed_time_s;
759
817
share_count++;
760
- client.print (String (duco_numeric_result )
818
+ client.print (String (counter )
761
819
+ " ,"
762
820
+ String (hashrate)
763
821
+ " ,"
@@ -774,7 +832,7 @@ void loop() {
774
832
Serial.println (client_buffer
775
833
+ " share #"
776
834
+ String (share_count)
777
- + " (" + String (duco_numeric_result ) + " )"
835
+ + " (" + String (counter ) + " )"
778
836
+ " hashrate: "
779
837
+ String (hashrate / 1000 , 2 )
780
838
+ " kH/s ("
0 commit comments