how to do OUTPUT in arduino cpp file - arduino-c++

I am trying to figure out how to modularize my program. I probably should not be, since I am only beginner trying to write code for Arduino. BUT, putting it all in one file is driving me batty.
I have an .INO file. I can make it compile with pinMode and digitalWrite commands in it. If I move that code to a .CPP file, I get errors that OUTPUT, HIGH, LOW, INPUT_PULLUP are not declared in this scope. I have tried including files I thought might contain the definitions but nothing is working.
How do I make this work?

I ADDED THIS TO THE FILES AND IT APPEARS TO WORK
#include <arduino.h>

Related

Make long header files compile once?

Alright so I'm using the "Catch2" framework for C++ Unit Testing and in my "testMain.cpp" (Doesn't matter) I include the single header version of it #include "Catch.hpp".
The problem is every time I write just a small test, I have to compile the program in order to see the outputs again. But the single header is something like 70000 lines and it takes FOREVER.
I understand that with source files you can simply compile them into object files and after that link them. So if you are using the same source file for just linking there is no really a need to recompile it over again.
So the point of this question is, Is it possible to somehow compile the Catch.hpp file and just use it as a link after words? Or in order to slow it down do I have to get the multi-header version of the framework?
Thanks in Advance!
Have you read https://github.com/catchorg/Catch2/blob/devel/docs/slow-compiles.md ?
It is recommended to move the tests main function to a separate file, so that the compile intensive parts are only compiled once.

Setting IAR Workbench variables in code

I made at .bat file that modifies a .h file containing a #define BUILD_VER so that it gets incremented every time I build.
There is also a #define MAJOR_VER in the same .h file that defines the version that the customers will see.
I'm trying to figure out a way to get the MAJOR_VER and BUILD_VER attached the to the binary output file (I'm using MSP430's so the output is a .txt files.)
So with something like this
#define MAJOR_VER 22
#define BUILD_VER 4321
...and building "Project_A"
"Project_A.txt" <== current default behaviour
"Project_A_V22_4321.txt" <==What I'm looking for
It would be nice if I could set an IDE variable using these #defines somehow.
This posts answers how to create custom IDE variables, which I didn't see the last time I started looking into this IAR project variable like $PROJ_DIR$
I know I could just write another .bat script to modify the file name, but this seems cleaner to me. And a little more obvious if I were new to the project.
Any thoughts?

How to add .c and .h files to Atmel Studio 6?

I know there are a lot of questions on this topic, and I've looked through a fair number of them. However I am still having problems.
I started writing a test program for a prototype PCB, and now that it's grown to nearly 1000 lines I'm trying to break it up into libraries that I can use for particular functions.
I thought this would be very simple. Make .c and .h files for each library that I need. I.e. I would have OLED.h and OLED.c for functions that control an OLED display. Copy the appropriate functions/definitions into each file. Then copy these files into the solution in Atmel Studio. I put them into the src folder under the project name.
However, this doesn't work! I get an exceedingly long list of errors. All of the things that are defined in the .h file are apparently undefined as far as the compiler is concerned. I also get many error messages of the type "unknown type name int16_t/uint16_t/uint8_t/etc..." That part is really baffling to me. Why should it matter that functions are in an external library, now the compiler doesn't understand what those data types mean?
So, this is probably a stupid problem to have. I don't want Atmel Studio to control my libraries by wrapping them up in some "library project" or somethig, I want to put them in a folder of my choosing and add them when I need them. I've searched for answers to this problem and I find long tutorials about changing the compiler settings for the project, the linker settings, etc... I tried this tutorial and still no dice: http://www.engblaze.com/tutorial-using-avr-studio-5-with-arduino-projects/#setup
I also can't find a way to add something by right clicking the project and clicking "Add." It wants me to find .a files. The "Add Library" dialog box in Atmel Studio is awful, it seems.
Surely it can't be that convoluted to just add a library to an existing project and have it function normally?! I've used PICs in the past and coming to Atmel I've found horrible documentation and a weird super-slick super-fly whizz bang interface that can't leave well enough alone and obfuscates simple function. What can I do to add these libraries?
UPDATE: Seemed to answer my own question. Turns out I needed to include all of the libraries to recognize data types and whatnot into the .c file. I somehow assumed this only had to be done in the main file but obviously I was mistaken. Adding asf.h seems to work well as it includes all of the MCU specific port definitions/names and all of that. All good for now!
Adding library files to a solution should be simple. Go to the Solution Explorer, right-click on your solution, and go to "Add->Existing Item". If you want to add a pre-existing library and keep it in a separate folder from your solution, click the arrow next to "Add" and choose "Add as link". That saves many headaches due to having a duplicate copy of your library in your solution folder, and files not staying up-to-date.
You are right in saying that you need to include the necessary header files in the .c files where they are used.
The compiler compiles each C file separately, and then links them together at the end, so you got the error unknown typename int_* because the compiler had not seen the relevant header in the context of compiling that C file.
You also seem to be in some confusion as to the difference between definition and declaration.
A function is:
Declared in the header file. This means there is a function prototype, e.g. int some_func(char some_var); which tells the compiler that the function exists, but does not tell it what it is. This is necessary because the compiler only looks at one C file at a time, so needs to be told that other functions exist.
Defined in the C file.This is the actual function body, i.e. int some_func(char some_var) { do_stuff(some_var); }. After compilation of each individual C file in isolation, the linker is called to put all the pieces together and give you your final binary, which you flash to the device.
A function can be (and must be) defined only once, but may be declared many times - even in the same file, so long as the declarations are not conflicting.

Running Fortran on Xcode

I am trying to run sample Fortran code on Xcode 4.3 using a 64-bit compiler and it will not build correctly. The main problem is that despite my best efforts, I cannot get the separate .f90 files to interact with each other, thus code like
USE ElementModule, ONLY : ElementType
will not work. Does anybody have any answers regarding how to get the separate .f90 files to read each other. I'm aware you have to include specific modules, but my search hasn't given me any straight answers regarding what those specific modules are.
Normally when F90 code compiles, it generates 2 files: an object file and a mod file. When compiling subsequent modules, the mod files are used for the USE statements.
If you have circular dependency, then you have to build two or more times. Best to avoid circular dependency if you can avoid it.
The mod files are normally picked up by the same directive that tells the compiler where the include files are.

Adding GLM to project in Xcode 4

I am trying to add GLM to a project in Xcode 4, but I cannot get it to compile. I have added the glm files to my project through the add files dialog.
I get a lexical/preprocessor issue and Xcode cannot find the file <cmath>.
I am not sure what I need to tweak to get this to build.
I have seen How do I add OpenGL Mathematics (GLM) to Xcode 4? already.
All you need to do is add the files to the project and #import "glm.hpp" (for Objective-C++; for simple C++ it should just be #include "glm.hpp").
A couple things to be careful of:
The OpenGL Mathematics library, when you download it, comes with a bunch of stuff you don't need (test code, extraneous utilities). Adding these to your project will result in compile errors which I could not get rid of. The only directory you need is the glm/ directory; you can delete the test/, util/, doc/, and bench/ directories. If you were trying to follow "How do I add OpenGL Mathematics (GLM) to Xcode 4?" and were still having problems, this may be the thing which was tripping you up.
The OpenGL Mathematics documentation tells you to include the or files. In Xcode 4, you should include them like "glm.hpp" or "*.hpp". Xcode will find the files no matter where in the project they are. Supposedly you can add a user-defined build setting "USE_HEADERMAP" and set it to "NO" to disable this, but I didn't have any luck with that.
And, just in case, note that your code files using the OpenGL Mathematics library must be Objective C++ files (ending in ".mm"), not the default/plain Objective C files (ending in ".m"). It is very much a C++ library after all... :-)
I hope that help. I was just working through this myself, and I haven't had the chance to really push this (e.g. I've basically just added a mat4 object or two and made sure things still compiled), but it seems to be working.
I ran into the same problem and I solved it remaning my ViewController.m to ViewController.mm. Change the extension to .mm tells XCode that the file may contain C++ code inside. The article Write Object-C Code explains this in the Classes and Objects section.