#BB10 Namespaces & Macros for Arduino (and other) microcontrollers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome to my series of short videos in which we discuss how the arduino interacts with various electronic components yes in less than 15 minutes we'll go over the basics how they can be used hints tips tricks and traps yes indeed now this is part two of keeping your code organized and we're going to be talking about namespaces now namespaces are optional obviously but they do help structure your code so let's think about what we mean by namespaces now namespaces allow us as developers to implement a mechanism to express some sort of logical grouping right so we're going to group things together logically and say right we'll name those in a particular way and it really does help let me give you a real world example of where we use namespaces that's got nothing to do with coding whatsoever i want to give a big shout out to pcb way pcb prototype the easy way now we're all familiar with their special pcbs five dollars for 10 pieces but you can also have flex pcbs advanced pcbs and of course you can order custom parts let's have a look at the cnc and 3d printing options they have so it's 3d printing first you upload your cad files as you would do normally for a pcb but then you select your materials and submit a quote request cnc machining is pretty similar in the way you submit your files upload your cad files select one of these many many different types of material you can use and of course there are also 27 different options for the surface finishing just look at a few of those there from anodized brushed bead blast spray painting and there's more under that list as well and finally there's sheet metal laser cutting and bending so let's have a look at that once again you upload your cad files select the products you want to make it from and specify a few of the parameters here about whether you want threads for example and you can always submit your request for a quote and it's about seven to nine business days to get it done okay that's pcb way excellent service good quality why don't you check them out now now let's assume you've been invited to an away day to discuss c plus plus 24. yes i know it hasn't been invented yet but let's assume we've all been invited to make it happen now adafruit's been invited lucky for them a and pimeroni they've been invited pimeron is a uk company by the way but anyway adafruit's there pemeroni is there maybe a few other companies as well and some independents so me i'm there well i'm down there you see as an independent ralph uh jim's there i don't know where he comes from though and brush me she's there as well so we're sort of independent and we're not really belonging to any one particular company so what do the organizers do well obviously they have tables for adafruit yeah that's fine and they have tables for pimeroni but for us they just put us onto independent contractor tables yeah so they cluster us together and why does this help well imagine if you're wondering about this this conference hall now whatever talking to people are what should happen c plus plus 24 you meet somebody and you think who are you so you've wandered around the conference hall and you spot me well you don't know it's me till you get a bit closer and then you read my name badge you go ah it's ralph where on earth does he fit in and underneath there's a logical grouping and it says brackets independent you go ah now i know where you fit into all this you're not part of adafruit you're not part of pimaroni nor anybody else you're an independent great i'll have a little chat in fact ralph you're so good i like your youtube videos so much i'm going to buy you a beer yeah i know i wish anyway you can see how logical grouping in real life works and helps and in c plus it does pretty much the same and the bigger the project the better it is but even for this tiny little project on the screen here it works very well let's talk about this and see the code that does it right now this is a naughty project you can probably barely read that let me just um reduce the lighting on there see just so you can see the screen there we are look this is an lcd screen 16 by two we went through all this in the previous video that that one there right so if you haven't watched that yet about how to split your code up into multiple files i'd highly recommend you watch that first this this video will then make a lot more sense but up to you so lcd screen 16 by two this little thing here that's a temperature sensor a ds18 b20 very easy to use any all three wires and a push button down here and when we push the push button if you watch the lcd screen you'll see that the temperature display there i look 24 degrees and it and it really is yes it's quite warm in here now in real life this screen here looks beautiful and gorgeous and it's nice deep ocean blue with white writing on my video it looks a bit rubbish but never mind that's not what we're here to discuss now we went through the code that runs this in that previous video and we split the code from the main file into at least two separates we're the third one that said global so there wasn't a lot in there so let's have a look at that and also now have a look how namespaces fit into that and why they're useful and why they make your code better and this was the final result where we had three files at the top you see lcd helper sensor helper and globals with not a lot in it really because we didn't really have very many globals although we did discuss whether that button should be put in there and of course yes the answer was this of course it should be put in there however the thing that we did not discuss was in the main file now this main.cpp that's the actual ino file that you would use in an arduino ide which this isn't you'll see things like digital read sensor colon colon pin button so we said what's this colon colon bit yeah now of course as you've already guessed and as we alluded to in the last video it's got something to do with names spaces but what's it all about so let's have a look at this one first yes even though this particular implementation of the namespace we thought um i don't think the button should go into the sensor namespace but we'll move on from that if we have a look at the sensor file at the top then where all our sensor related work is we'll see that yes we've declared all these things down here and we've got a couple of functions down here as well but the bit we didn't discuss last time was this here namespace sensor now what we're saying is we're going to logically group everything between this curly brace and the one right at the bottom where it ends all the way down here blade space sensor there look and we're going to say everything within that belongs to one of those logical tables that we saw you know pimeroni adafruit independent contractors so that we can identify where the things that we are calling come from so within this everything that we ever refer to in here if you're not actually in the file itself has got to start with sensor colon colon fine okay and that sounds pretty simple doesn't it so while we're writing our code in here we don't need to use anything with sensor colon column because it's all within that namespace already yes they know each other it's like all the people are made of fruit table they know who they are and they all belong to the same table they don't have to read their name badges to find out oh you belong to adafruit as well no they know who they are but as soon as we move out of this comfort zone of sensor helper and go back to main or the main sketch ino to get hold of that pin button we have to say sensor colon colon pin button if we took that away the compiler is going to say pin button hmm where does that come from oh look we've got red squiggly and it will say it's not defined correct because pin button without any kind of namespace now would belong in the global namespace i.e the undefined global namespace as part of every c plus program and there isn't one it's only defined in the sensor namespace now when you read your code in future of course you'll see that you have to put the sensor colon colon in front of everything that's related to the sensor namespace including the get temperature function which we defined in that namespace so we have to put sensor colon colon get temperature and your program suddenly becomes much better documenting self-documenting really because although you're calling this routine and if you didn't have a name space in front of it how would you know where it belongs to does does it come from the sensor or the lcd helper or globals or indeed any number of other files or is it buried in this main.cpp file by putting a namespace in front we've identified where it's come from and by logically grouping these things that all belong to the sensor we get a we get a much better feel of how our program is structured and therefore makes it easier to maintain now we said last time that this button this push button that's on the breadboard there and every time we push it it updates the temperature sensor we said well we're not really happy that should be in the sensor file really it just doesn't belong there you come back to this program in a year's time and the last place you'll probably look is in the sensor helper to find some kind of definitional code belonging to that button it really should have been part of globals do we really want this to be a global a real global or not well let's move it and see what we can do so i've moved it out of the sensor helper file at the top there moved it out of there and put it into globals now this is a global yeah global verb i'm so yeah look he's getting angry again we're putting stuff into globals and we shouldn't do we're going to fix that because just putting stuff willy nilly into some global namespace which is there by default is just not a good idea we haven't related this particular pin button to anything is it global or is it does it really belong to something else now you could put it into a made up namespace like buttons so if you had four or five of these buttons in your program you could have a namespace called buttons at least you'd know where to look wouldn't you i guess i know the name of this button is pin button so we understand what it's doing but should we put this into a namespace well let's let's just let's just do it shall we so we've added in the buttons namespace for for good or bad and if you had more than one button i think it would make a lot more sense to have a namespace like this now if we go back to the main file of course you'll discover that the compiler is throwing a hissy fit going uh there isn't a pin button in sensor anymore so let's zap all this and see if it can find it so already it says ah buttons that's namespace button colon colon and it says all pin button that's what you mean because we've only got one thing defined in that namespace it's very easy isn't it so we go yes please i'll have that and it's happy again so now when you're reading this code you're in digital read buttons ah this is my button's name space pin button great you could have called that namespace anything you liked yeah fish ralph buttons whatever the idea though is that you as the developer have to think how will i expect this to be grouped logically in the future and if buttons works for you that's great if you think interface or even the word global would work yes whatever it is if you use a namespace even for global variables a they become not global anymore because they're constrained within the scope of whatever the name space it is you've defined and it just makes your code so much better to maintain and read now namespaces are what's known as open which means you can add stuff to them and continue to do that in separate files if you want so you could for example place all your variables for a particular namespace in one namespace in a file and then in a different file continue that namespace and put all your code so for example in your sensor's helper in here um you've got your code here oh look it's picked up the fact that pin button doesn't doesn't work anymore so you've got your code here as part of sensor but the actual variables these here could have been put into a different file that you keep all your variables in with the namespace around them as well so basically you would just define namespace sensor like that and then let's assume that namespan sensor now ended there so everything within those curly braces is part sensor but and then you do some other stuff for you in a different file you just type it in again namespace and that's it you've done it okay namespace sense and you've continued this even if you have other stuff in here other stuff goes in here compiler doesn't care it sees this name space again and goes ah this is a continuation of a previously named namespace if you named it previously okay oh yes we better fix this then so pin button is no longer part of this local sensor name space let's give it the buttons name space as we're supposed to there we are quite happy with that now it says yep that's the one good and eventually that squiggle will disappear there we are gone everybody's happy and more to the point when you're reading this sensor code in the future and you see this statement here about setting the the pin button you can go ah this is part of the button's namespace lovely nice and structured right one little thing that we mentioned last time in the video about splitting up your code that we never actually got around to we said that this line here digital read buttons colon colon pin button equals low it's all a little bit techy bit a little bit low level isn't it we're actually talking about real code here rather than just get on and do it type code to make it easy to understand what would you say if we replace that with something a bit more english a bit more high level for example what happens if we replaced it with push button pressed now that sounds nice doesn't it i mean that's very meaningful if push button pressed go and do this get the temperature print the temperature sounds good yeah and stops you writing all that funny c plus plus code that you know digital read this that and the other hmm so how does this work well if i hover over it you'll get an idea push button pressed it says oh look it's a define push button pressed and here i've got that code that we used to have in there now this is used quite a lot in fact i use it a couple of times in my smart workshop heater controller because i've got some well worse than this tests you know if it's this time of day and not that time of day and the temperatures higher than this and all that means this big long if statement it basically means in my code if it's daytime and it's warm enough but i don't want to type that in over and over again one it looks ugly and b you're likely to get it wrong so by defining it like this and then using an english statement instead that looks better whereby define this do you think push button pressed where would i define it why i define it in lcd helper in sensor helper or globals well yes it is a global isn't it and defines are not subject to namespace scope because the define happens at preprocessor level before the c plus compiler ever gets it looking so defines even if you were to put them into this namespace of buttons which i haven't it's outside but even if i had it done that would still work exactly the same way because to define remember is a simple textual substitution what happens is when you compile this it whizzes through the code looking for push button pressed and substitutes everything here oh yes incidentally you notice i've got brackets around that code which is always a very good idea to do with defines otherwise you can get the syntax a bit skewed up um with semicolons and more brackets so always put your defines in brackets so that's what i've done here so push button pressed space there's no equals in the divine remember it's a simple textual substitution whenever you see push button pressed put in brackets digital read buttons colon colon pin button close brackets equals load close brackets and all that there is not checked or anything it doesn't it's not not checked here where things are all it says is can i understand this and put it here and that's what it expands to great and that now makes your code even more understandable and manageable okay all this code is up in the github you can play around with namespaces there's there's a little bit more to name spaces but frankly at the hobbyist level i think is more than enough i think what we should do as hobbyists as part of all our helper files put a namespace at the front so that everything within that file has got a namespace in front of it okay i think we might be done so there we have it namespace is in a nutshell a very very useful tool that is very underutilized i think quite frankly great okay now if you found this namespace tutorial helpful in any way or interesting or entertaining please do give it a thumbs up i'd be most grateful and if you've got any questions or queries or just comments about i wouldn't do it like that if you want yeah put them down below and uh don't forget subscribe and ring the bell because otherwise you won't hear from me again we should be a very sad state of affairs as i've said before okay and i'll see you in the next video i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching
Info
Channel: Ralph S Bacon
Views: 14,134
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: P0PYqu83ScY
Channel Id: undefined
Length: 19min 5sec (1145 seconds)
Published: Fri Jun 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.