importing arduino "sketch" to Microchip Studio - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: October 08, 2021, 08:52:46 am
content: We had a work experience student design a project and write the code in Arduino. I now have the unhappy task of trying to carry this project on.It uses Bluetooth which I understand nothing of as of yetIt uses an RTC that is not completely working yet and again I understand nothing ofI have discovered that there does not seem to be a well defined way to have a program in multiple files in the arduino "IDE". I tried breaking the files up and there are already a few that are all .h files with code in them and this did not work.So I decided that the next best thing would be to import it all into microchip studio where at least I can read the damn font on a 4k screen.So I now find that I have errors galore, mostly related to not being able to find things that seem to be defined in a file called string.h, if defined is the right word as I don't read C++ and we are now into the bowls of the arduino code. I have tried including string.h into the files that have errors but this does not work.Ultimately it would be nice to update this project working in C but how I work around things like Bluetooth and the RTC I do not know. I should almost start from scratch but that will be a big task as I have to reverse engineer it, understand how to rewrite it and then start again.What are my options for working on arduino code? this all compiles in the arduino "IDE"

author: PKTKS
author_rank: Super Contributor
timestamp: October 08, 2021, 10:25:41 am
content: IMHO..."Importing" anything out from Arduino to other proprietary workspace is **THE WORST** possible thing one can do. Reason being as follow:- Arduino is currently the "de facto" IDE of choice among almost everybody- All serious MCUs vendors already committed to that (ST/Espressif/Atmel)- A **HUGE** code base is already available (although using the C/C++ Arduino paradigm)- The new IDE (still alpha) uses Eclipse base ideas - very good upgrade although not for now- Code base on Arduino is vastly available - even my 6y old nephew wants it !!!So.. forget getting out of it .. embrace itinstall the manufacturer support Board and Libraries ..You will save *ALOT* of time and effort.There is no way back from this...Soon everybody will understand how bad is to depend on "proprietary IDEs"Bottom line is that "Arduino" is no longer some sort of board ..or group of folks disputing the ownership of MCU breadboards..Arduino became the standard common base workspace developer suite..An insane library is already available.. and growingTalks like ArduinoIDEPaul

author: Nusa
author_rank: Super Contributor
timestamp: October 08, 2021, 10:41:55 am
content: Well, the simplest workaround would probably be to use whatever editors float your boat to work on the files, but still use the Arduino IDE for compiling and cross loading.But if you do use the Arduino IDE: File->Preferences allows you to change the interface scale and font size

author: PKTKS
author_rank: Super Contributor
timestamp: October 08, 2021, 10:47:30 am
content: more 2 cents..- Search the relevant downloads to install your target MCU tools...- Install board and libraries for the target.- Have a look on the very handy "arduino-cli" on github.. very good tool- just in case STM holds supporthttps://www.stm32duino.com/- same to Espressifhttps://github.com/espressif/arduino-esp32- same to several other goodies impossible to put hereAl stuff is currently based on GCC.. being 32bits older compilers are better..Paul

author: Simon
author_rank: Unknown
timestamp: October 08, 2021, 11:43:35 am
content: I imported the project to studio as an arduino project and told it where my arduino IDE is etc.The main errors are actually in the arduino code so sorry but you can keep you defacto standard somewhere very safe from me. After some keyword searching for one of the defines that had not been made according to the errors reported I found a note in a file stating that this was a known issue and if I came across a board that it did not work with to do a pull request. What is a pull request? sorry, I'm not messing with stuff like this, it saves no time at all. I have spend more time trying to find out what is going on and there seems to be no proper way to use multiple files.

author: Simon
author_rank: Unknown
timestamp: October 08, 2021, 12:20:56 pm
content: From datestrings.cpp// Arduino.h should properly define PROGMEM, PGM_P, strcpy_P, pgm_read_byte, pgm_read_ptr// But not all platforms define these as they should. If you find a platform needing these// defined, or if any this becomes unnecessary as platforms improve, please send a pull req.found from error: Severity Code Description Project File LineError 'strcpy' was not declared in this scope ArduinoCore *:\*****\Electronics Upgrade\Programming\Full Code\P1\P1\ArduinoCore\include\core\api\deprecated-avr-comp\avr\pgmspace.h 63I have a couple of C++ books on order

author: PKTKS
author_rank: Super Contributor
timestamp: October 08, 2021, 12:34:37 pm
content: Critical information is missing..You need ate very first place define your board and proper settings inside IDEThis will "select" pre made configs to instruct GCC (aka Makefiles)It "seems" you are fetching deprecated headers from an improper dated gcc version.FYI the IDE can and usually have multiple compiler versions and different set for different MCUs.This is a much more advanced use of the IDE instead of simple point click vanilla Arduino old times..Today use of the IDE involves setting multiple GCC versions for multiple MCUsPaul

author: DrG
author_rank: Super Contributor
timestamp: October 08, 2021, 01:06:26 pm
content: Quote from: Simon on October 08, 2021, 08:52:46 amYou glossed over this point a bit and it may be your best approach. I once downloaded a demo program for Arduino which contained 11 files in the same directory. These files were .c, .cpp, .h and, of course, a single .ino. It gave me all sorts of aggravation until I changed one or two lines in the .ino, including overcoming an extern definition in one of the files...LCD_ST7735S LCD; // note: added to overcome extern in lcd.h (just looked at it and my note)I think you might have the greatest success in using the ardunio ide and trying to work through some issues causing errors, which may be easier to solve than you think. As far as included libraries (assuming that the ble and rtc are included libraries) always involve going to those libraries to examine the code....my two cents.

author: PKTKS
author_rank: Super Contributor
timestamp: October 08, 2021, 01:37:31 pm
content: Quote from: DrG on October 08, 2021, 01:06:26 pmThis is actuallyhoweverybody is building libs to the IDE... (interfacing)Forget about making a multi file on the root base of the IDE paradigm.It exists to newbies so to be easy and simple..The devels are using the ~/library/MyLIB/<> root to put everything needed inREAL*cpp and *h files the regular way..It already contains a hell of a huge repos.Including ours customized ones.Paul

author: DrG
author_rank: Super Contributor
timestamp: October 08, 2021, 01:55:30 pm
content: Quote from: PKTKS on October 08, 2021, 01:37:31 pmI agree that it may be the best way, but that may not be the critical issue at this point because, as I see it, the OP is not writing the program but trying to get it to run and understand it with the intent of ultimately updating. It is already in multiple files in the root base, unless I am simply not understanding.In my case, that was exactly what was happening - that is my point. Once I could get it to run and observe it, I could deal with using the lcd as I wanted to and without someone else's "library", unless I chose to do it that way. As to why it did not run when I download the sample code, I don't know.But, again, just my two cents to try and get to a solution.

author: rstofer
author_rank: Super Contributor
timestamp: October 08, 2021, 02:58:41 pm
content: You can use Microsoft Visual Studio as the IDE with a plug-inhttps://www.visualmicro.com/It knows where the files are located...You can also use Visual Studio Code as the IDE:https://maker.pro/arduino/tutorial/how-to-use-visual-studio-code-for-arduinoThe Arduino IDE itself is damn near unusable. Who puts orange messages against a black background! They're invisible! Changing the color scheme is a true PITA. OK, to be fair, I don't have one positive thing to say about the factory IDE. But that's just me...

author: dl6lr
author_rank: Frequent Contributor
timestamp: October 08, 2021, 03:08:23 pm
content: Quote from: rstofer on October 08, 2021, 02:58:41 pmVoting up for using Visual Studio Code. Compiling big projects is a lot faster (or sometimes even possible) with the tool chain from platformio instead of using the Arduino IDE.For the OP I would suggest to first getting it compiled with the Arduino IDE as it was developed. If everything works, you can migrate to another IDE, tool chain and project layout.

author: Simon
author_rank: Unknown
timestamp: October 09, 2021, 07:33:13 am
content: Ultimately I want to be albe to switch microcontroller if I have to. I have 1'000 SAMC's on order but the arduino uses SAMD, my guess is they are all out of stock as used by people basing products off the Arduino plus it's a few cents cheaper. My hope is that I can find alternate libraries or work out how to use the Arduino libraries not in Arduino having stripped aut the Arduino references.

author: Nusa
author_rank: Super Contributor
timestamp: October 09, 2021, 09:07:49 am
content: Heh. You'll just fail to communicate if you imply or think your preferred hardware is identified when you say "the arduino". I believe the AtMega328 is still the most common "Arduino" processor, and is the default assumption when not otherwise mentioned.

author: Simon
author_rank: Unknown
timestamp: October 09, 2021, 02:34:06 pm
content: Quote from: Nusa on October 09, 2021, 09:07:49 amI can turn that back at you and say well you just assumed, I believe one of the big potential pitfalls of the Arduino is that it tries to have a uniform usage on many different micro controllers. Why would by Arduino I be implying the ATmega328?

author: PKTKS
author_rank: Super Contributor
timestamp: October 09, 2021, 06:30:17 pm
content: Quote from: Simon on October 09, 2021, 07:33:13 amclaims to have a working SAMC setuphttps://github.com/mattairtechnever tried so can not tell you if works..Things are changing way to fastPaul

author: xrunner
author_rank: Super Contributor
timestamp: October 09, 2021, 06:47:08 pm
content: Quote from: Simon on October 08, 2021, 08:52:46 amHeads up - if the RTC module is the same DS3231 like I've used before, you might want to inspect the battery. The circuit is designed for a rechargeable CR2032 but most of them come from China stuffed with a non-rechargeable CR2032 - not good. You can use a (non-rechargeable) CR2032 if you remove a resistor. Check this out -https://arduino.stackexchange.com/questions/66681/ds3231-rtc-module-and-battery-problem

author: james_s
author_rank: Super Contributor
timestamp: October 09, 2021, 06:56:59 pm
content: Quote from: Simon on October 09, 2021, 02:34:06 pmBecause that's what the standard Arduino is and it's the most common variant by far, almost anybody is going to assume you mean atmega328 unless you specify otherwise. The Arduino versions based on other chips have different names.

author: Simon
author_rank: Unknown
timestamp: October 09, 2021, 08:30:09 pm
content: Quote from: james_s on October 09, 2021, 06:56:59 pmThe specific chip is irrelevant to the discussion. We were talking about how to open the Arduino code in Microchip studio.

author: Nusa
author_rank: Super Contributor
timestamp: October 09, 2021, 11:01:24 pm
content: Quote from: Simon on October 09, 2021, 08:30:09 pmI'd agree if it wasn't the OP who added specific chips to the conversation. You're allowed to derail yourself.

author: DrG
author_rank: Super Contributor
timestamp: October 10, 2021, 12:23:07 am
content: Quote from: Simon on October 08, 2021, 08:52:46 amSo, I read your original post again because the thread is becoming confusing, at least to me.Your last line....this all compiles in the arduino "IDE"If that is so, then exactly what do you think that you will gain using VS? I assume that you figured out how to get a reasonable font on your 4K within the arduino ide - so, beyond that, it is not clear what you are complaining about or trying to fix - other than the obvious...some student wrote something and now you have to deal with it. I guess what I am saying is; the best option for working on arduino code that has already been written using the arduino IDE, including who knows what board and libraries, and is working (at least to some extent), is the arduino IDE.If you are trying to translate it to C or C++, in order to "carry on" you should still be working in the ide until you can, at the least, see and understand all of the code - the student's .ino and as well as libraries that have been used. So much in there can be standard only to the evolution of arduino and I don't know anybody who thinks that 'it' follows C or C++ standards to a helpful extent.For example, if he used an rtc library, such as rtclib, you need to go into the libraries directory, locate and go through the rtclib.h and rtclib.cpp files - to see what is not working and why it is not working. Certainly, you could rewrite that library in standard C, if you wanted to put forth the effort.Bluetooth is more complicated and my guess is that the student used the esp32 library and so on and so forth. Even there I am guessing, but he either wrote his own stack (unlikely) or he used an existing library and that library is going to be dependent on code for the board (also available through the ide) - the point is that the source code is available to you using the arduino IDE in either case. To rewrite all that is, well, not much fun, I would imagine.I have discovered that there does not seem to be a well defined way to have a program in multiple files in the arduino "IDE". I tried breaking the files up and there are already a few that are all .h files with code in them and this did not work.I think you are probably right, but it can be done. Why, however, did you do this and how is VS going to solve that for you?I'm not trying to give you a hard time, it is just that as I think more about your OP, I just don't get exactly what you think VS is going to do for you?

author: xrunner
author_rank: Super Contributor
timestamp: October 10, 2021, 12:58:19 am
content: Quote from: DrG on October 10, 2021, 12:23:07 amDrG and Simon,You can definitely split the code up into multiple parts using the Beta 12 Arduino IDE which is what I've been using for a project recently. Just make another tab or as many as you want. As you can see in the simplistic test I did, the compute() function is in part_2 and the main is in Part_1. It compiles them together just fine.

author: Simon
author_rank: Unknown
timestamp: October 10, 2021, 07:30:24 am
content: The student is not a coding ace. He's good for a 17 year old that has been at electronics for 2 years and prefers the hardware design to software. Even in the Arduino IDE it's a rickety mess that just holds itself together. I can't straighten it out in the Arduino IDE either, I tried and it broke all over.What I am trying to do is start again using what he wrote as a base so that I can change one thing at a time. If I can reuse any Arduino library I will. I am not sure what in an arduino library I would need to change to make it work. As far as I am aware the only main difference will be pin references. Of course every library will build on another so I will end up in a maze of chasing down libraries and sorting out their dependencies.So for example I found the SD card library on mbed.com:https://developer.mbed.org/teams/mbed-official/code/FATFileSystem/#e2ab678eb692Now it's all of 2 files, Surely SD card controlling is more complicated than that? well for starters there is no hardware level code there for the SPI bus because it is a generic library, but I suspect there is an SPI library somewhere (maybe on mbed.com) or I write my own lower level code and make my function names the same as those in the library like "_spi.write"I don'n know C++ yet so have some books on the way as clearly as soon as I try to touch anything written by anyone I will bump into C++

author: Nusa
author_rank: Super Contributor
timestamp: October 10, 2021, 09:54:15 am
content: Quote from: Simon on October 10, 2021, 07:30:24 amThat's the FAT file system, which is a dependency of the SD File System, which does talk to hardware. Click on the dependencies tree up at the top and find one of the newer versions if you use that resource. It's all implemented in C++ of course. Virtual functions in particular will be new to you, as they don't exist in C. They can be sort of simulated with pointers in C, but it's a lot more work with many more opportunities for error.

author: Simon
author_rank: Unknown
timestamp: October 10, 2021, 03:22:23 pm
content: well right there there is a problem, the dependencies are apparently a whole load of SD libraries by different people and apparently a USB host so that list is not worth much! it looks more like a list of related stuff.

author: Simon
author_rank: Unknown
timestamp: October 10, 2021, 07:35:48 pm
content: OK, I got it, that is a list of the libraries that depend on this one, not really useful, I need to know what other libraries this one depends on. If i look at the api documentation page I get a list of files, do I need these? I mean it's not like someone is intentionally not very well documenting this stuff are they. It seems everywhere I go to involving programming assumes I know something but no one explains the stuff that apparently I should know already from some secret source!

author: westfw
author_rank: Super Contributor
timestamp: October 10, 2021, 08:19:42 pm
content: QuoteMy experience (limited) with the Studio "Import Arduino Sketch" is that it works pretty well for Official AVR-based Arduino boards, but is pretty questionable on the SAMD boards (even the Arduino official SAMD boards.) And it may be pretty hopeless on 3rd party boards/packages :-(AFAIK, this feature was added to Studio back during the infancy of the current Board and Library management system, and has not been very actively maintained :-(QuoteUnfortunately, most C++ books are likely to send you off in unproductive directions, if your goal is to understand Arduino/embedded C++.(Not unlike C books, if you want to write embedded C.)QuoteSilly me. I thought platform independence was a good thing, most of the time.QuotePROGMEM, strcpy_P, etc, are the special functions for AVR's Harvard architecture to access data in flash memory. They're completely unnecessary on ARM CPUs, but it "would be nice" for them to be aliased for backward compatibility. Many Arduino non-AVR platforms do that.Quote"Pull Request" is a github thing. This is approximately "if you fix this, let us know what you did."QuoteThere is, although I don't know whether the Microchip Studio continues to follow it.Multiple .ino files are concatenated into a single file, which then goes through arduino "pre-processing" to generate prototypes and derive library paths.Assembler, C and C++ (.S, .c, .cpp) files are compiled individually and "normally"; that means they need their own prototypes to allow forward references, plus "extern" declarations (even in the .ino files), and probably at least "#include <Arduino.h>" to pull in the Arduino definitions.So you can split a .ino file into multiple .ino files relatively easily, but extracting some of the code into .cpp files will require more effort.

author: Simon
author_rank: Unknown
timestamp: October 10, 2021, 08:35:14 pm
content: Quote from: westfw on October 10, 2021, 08:19:42 pmYes providing it does not introduce bugs and make a mess of things. I have come across a a few funnies in the little amount I have used arduino and prefer to be truly platform independant not locked to some system. Right now I spend more time trying to work out what is what than just looking at programming the thing.I tried making everything an ino file but it fell over. So is the ino file like a C file, so I create an accompanying header file for each for function prototypes? if so why all the bat shit crazy stuff on arduino forums about how to do it? that is why I do not like it, everyone is suddenly an expert and the ones that do most the talking are the ones doing it wrong!

author: Nusa
author_rank: Super Contributor
timestamp: October 10, 2021, 10:12:44 pm
content: You might find this useful:https://arduino.github.io/arduino-cli/0.19/sketch-build-process/From a user viewpoint, ino files are cpp files with the usual stuff like function prototypes added in preprocessing and arduino.h included at the top before it's handed to the usual compiler. Makes it very easy for newbies to get off the ground with no h file required.The arduino-style setup() loop() structure is implemented by the default main() that looks very much like (it's in main.cpp in the core, if you want to find it):Code:int main(void){init(); // init hardware timers, etcsetup(); // user setup functionfor(;;){loop(); // user run function}return 0;}If you provide your own main(), it'll override the default one and you can do what you want. Remember to still call init() or otherwise initialize stuff.

author: Simon
author_rank: Unknown
timestamp: October 11, 2021, 12:19:23 pm
content: So I have created a new project in studio and added the files from SDfilesystem and FATfilesystem on the mbed website. The empty project compiles file using over 5kB of RAM so I assume that these files were compiled in and all is fine. Now I just need to work out what to do with them.....

author: DrG
author_rank: Super Contributor
timestamp: October 13, 2021, 12:34:22 pm
content: So now the fun starts....locating every usage of the the Arduino SD lib in the original Arduino code and then writing functions that mimic those calls using "your" library. Actually, this may not be too much of a pita since the original is, hopefully just doing the usual; open/create file, write value, read values, close file; error reporting.Wait until you get to the BLE part

author: Simon
author_rank: Unknown
timestamp: October 14, 2021, 07:26:48 pm
content: Well I will reuse all that I can and just write the hardware stuff.