Header file for ATMEGA328PB

A project log for Remote control and image retrieval from Nikon DSLR

This post explains how to control a Nikon D3100 DSLR, and then get the images off of it.

sciencedude1990sciencedude1990 12/05/2020 at 01:454 Comments

Here is the header file (main.h) for the ATMEGA328PB (xplained board).

#ifndef MAIN_H_
#define MAIN_H_

// Serial port stuff
#include <string.h>
#include <stdio.h>

// Printing a string to the serial port
void print_serial(const char * const print_string);

// Given an input array, process the command
void go_process_command();

#endif // MAIN_H_


sciencedude1990 wrote 12/05/2020 at 15:51 point

Yes, there is no intention to modify the contents of the string.  Just "print" it to the USART.  Thanks for reviewing the code - I just posted this less than 24 hours ago!  :)

  Are you sure? yes | no

sciencedude1990 wrote 12/05/2020 at 20:59 point

In a past life, I worked at a place that was fussy about the functions.  If you weren't going to make changes to the contents, then the requirement was that everything was constant.  I guess old habits die hard.

  Are you sure? yes | no

Ken Yap wrote 12/05/2020 at 23:03 point

For a pointer type declaration there are two things that can be affected by the modifier const, depending on the position of the word: the pointer, or the thing pointed to. This generalises to higher order pointers, which can lead to madness on reading. C syntax is tricky that way.

  Are you sure? yes | no

Ken Yap wrote 12/05/2020 at 04:38 point

Did you really intend for print_serial to have a prototype parameter of const char *const? The first const promises that your function will not alter the contents of the string pointed to which is fine, but the second const promises that your function will not alter the pointer parameter. That prevents the function from using the parameter as a local variable, making it necessary to make a copy if modification is done, e.g. advancing the pointer to find the NUL byte. Given that the function receives a copy of the pointer anyway, no purpose is served.

Here's a short program which illustrates what happens.

void print(const char *const s)

int main(void)

and the compilation message:

c.c: In function ‘print’:
c.c:3:3: error: increment of read-only parameter ‘s’

  Are you sure? yes | no