I need help with my library

hey guys,
i wrote my first little library for arduino. and i am kinda proud of it.
i wanna know if there is any way i can improve the library?

what my library does is take a string, convert it to morse code and broadcast it via light or audio :yum:

this is the library,

Hi

Thanks for sharing

Wow that’s a lot of functions to do the encoding! Most implementation would use a PROGMEM stored array describing the mapping between a letter and its encoding.

You are wasting a lot of SRAM there

void MorseEncoder::encode(String text, int mode) {
  text.toUpperCase();
  char* _text = new char[text.length() + 1];
  strcpy(_text, text.c_str());
  int len = strlen(_text);
 

You pass the string by copy, so the original string is duplicated and then you allocate another buffer again to have a cString.

If you really want to use a String then You could pass the parameter by reference and use the String’s functions to access each character one by one and apply the toupper() then. You will save SRAM this way (two unnecessary copies) and the time it takes to make the copies.

if you want some ideas to add to the features, the class could work like SoftwareSerial and print the message using the selected mode in the background.

omg thank you for your valuable feedback. i really appreciate it, I will start working on that :innocent: :black_heart:

um sorry could you explain it bit more please :innocent: :black_heart:

what you want to do is a kind of print, basically something that know how to get a message out to some device.

The Print class is how this is usually done and by inheriting from the Print class, you would just have to implement the write() virtual function which takes one byte and sends it out.

@ktauchathuranga Here is an possible implementation of this idea.

/* Morse code table in ASCII ordering from ' ' to '_'.
 *
 * The first 1 marks the sequence start, `.` is encoded as 0, `-` as 1.
 *
 * Example: `A` is at position 33, its value (0x06) is 0b110 in binary. The
 * first 1 is discarded, leaving us with 0b10, which is translated to `-.`.
 */
uint8_t const morse[] PROGMEM {
  0x01, 0x75, 0x52, 0x00, 0xc8, 0x00, 0x22, 0x5e, 0x2d, 0x6d,
  0x00, 0x2a, 0x73, 0x61, 0x6a, 0x29, 0x3f, 0x3e, 0x3c, 0x38,
  0x30, 0x20, 0x21, 0x23, 0x27, 0x2f, 0x47, 0x55, 0x00, 0x31,
  0x00, 0x4c, 0x56, 0x06, 0x11, 0x15, 0x09, 0x02, 0x14, 0x0b,
  0x10, 0x04, 0x1e, 0x0d, 0x12, 0x07, 0x05, 0x0f, 0x16, 0x1b,
  0x0a, 0x08, 0x03, 0x0c, 0x18, 0x0e, 0x19, 0x1d, 0x13, 0x00,
  0x00, 0x00, 0x00, 0x6c};


uint8_t sequence(char const letter) {
  if (letter < ' ' or letter > '_') {
    return 0x00;
  }
  return pgm_read_byte(morse + letter - ' ');
}

void encode(void (* const action)(bool const), char const letter) {
  uint8_t const seq {sequence(letter)};

  uint8_t i {0x80};
  for (; i and not (seq & i); i >>= 1);  // Find the first 1.
  while (i >>= 1) {
    action(seq & i);
  }
}

Example:

void printMorse(bool const data) {
  Serial.print(data ? '-' : '.');
}


void setup() {
  Serial.begin(9600);

  for (char const& c: "MORSE") {
    encode(printMorse, c);
    Serial.print(' ');
  }
}

void loop() {}

Output:

-- --- .-. ... .  

Hi @ktauchathuranga ,

Congratulations on your first lib..
Great work, keep it up..

Did an Async version myself awhile ago..
Async Morse

happy coding.. ~q

thank you sooo much for your replies. i really appreciate it :innocent:

guys i did some research.
and to my knowledge, I think its not worth implementing "asynchronous".
because in dots, dashes, and in spaces I used delay to get the right timing.
i mean the pin on-off time. lemme know if um wrong :innocent:

of course that would need to change :slight_smile:

did async so doesn't interfere with loop..
Async example..

spent more time bit packing the morse into bytes..
got it down to 1 byte per morse char..

only using 1 unsigned long, so looks like just 1 millis timer..
fun stuff..

but no, if you're happy, then move on to bigger and better..

have fun.. ~q

now i am having another problem :sneezing_face:
updated library working fine with arduino uno board
but when I upload it to my esp8266 board.
board is just keep serial printing this msg.

ets Jan  8 2013,rst cause:2, boot mode:(3,6)

 ets Jan  8 2013,rst cause:1, boot mode:(3,6)

load 0x4010f000, len 3424, room 16 
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8 
tail 0
chksum 0x2b
csum 0x2b
v00041b20
~ld

what could be the reason?
that main for loop? :sneezing_face:

quick search on this..
says it's a normal message..
meaning it's rebooting..
is it a power problem??
bad connection, try a different usb..
remember uno is 5v and esp 3.3v..
disconnect everything, still do it??

could be triggering a watchdog, not so sure about esp8266 but esp32 has them..
can you upload the basic blink sketch??

~q

hi thank you for the reply :black_heart: :innocent:,
yes, I did upload blink and it works fine. and its very functional board I mean, I never had that error message when I upload codes in to the esp8266

actually, there is nothing to disconnect I just uploaded the code first.

and yes it is a problem with watchdog, I have searched about the problem but I couldn't find any specific reason. some say its cuz low power supply but that is not the case in here I tried many sketches previously it didn't have that issue.
and also they mentioned the loops that can cause this issue but I couldn't find any detailed explanation how to fix that :sneezing_face:

Interesting. You can add it to the Arduino library list/manager

aww thank you for the long and detailed reply.
i will definitely check that out.
thank you soo much :black_heart: :innocent:

hii
yes, i already added that, thank you for letting me know... :innocent:

GYSSSSS
after hours of reading, i fixed the esp8266 reboot problem :star_struck:, with this Can someone tell me if that change is going to affect other Arduino boards?
i changed

pgm_read_word

with

pgm_read_dword

for more details please go to the link that I provided.
:innocent: :black_heart:

weird

pgm_read_word() would read a word from the program space with a 16-bit (near) address

pgm_read_dword() would read a double word from the program space with a 16-bit (near) address

you would use pgm_read_dword_far() to read a double word from the program space with a 32-bit (far) address

heyyyyyyyyyyyyyyyy,
i released a updateeeee
that uses your suggestion :black_heart: :innocent: :heart_hands: