|
Forum Index : Microcontroller and PC projects : PicoMite BASIC Structure UNION?
| Page 1 of 2 |
|||||
| Author | Message | ||||
| Mark Regular Member Joined: 26/11/2022 Location: United StatesPosts: 99 |
Is it possible to have two (or more) variables with different STRUCT definitions refer to the same area in memory? I'd like to use one layout for disk storage and another for in memory processing. The issue is that they are not the same size. I need to have a few extra bytes at the end for temporary processing that are not needed on disk. I know I could just save the extra bytes to disk as there is plenty of room on the SD card, but I thought I'd ask. Thanks, Mark |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11215 |
No: If you want to program in C then it is avilable but this is Basic and structures are already way beyond what is normally available |
||||
| bfwolf Senior Member Joined: 03/01/2025 Location: GermanyPosts: 235 |
If I understand your problem correctly, the following might be a solution for your use case: A "container struct" in RAM that has the on-file struct as a member, followed by the members you need for management in RAM. |
||||
| Mark Regular Member Joined: 26/11/2022 Location: United StatesPosts: 99 |
matherp, That makes sense. I am so used to STRUCT's in other languages, I take them for granted, not realizing how limited BASIC was. bfwolf, I thought about that, but it appears that STRUCT SAVE only works with entire STRUCT, not fields. Mark |
||||
| bfwolf Senior Member Joined: 03/01/2025 Location: GermanyPosts: 235 |
Hello @Peter, @Mark Once again, I experimented: Type TPerson FirstName As string * 15 LastName As string * 15 End Type Type TPersonContainer Person As TPerson NodeId As integer End Type Dim PersonList(10) As TPersonContainer Dim P1 As TPerson PersonList(1).Person.FirstName = "Peter" PersonList(1).Person.LastName = "Marther" PersonList(1).NodeId = 1 'The following 2 statements give errors (if enabled): 'Struct Print PersonList(1).Person 'Err: Cannot print a structure member... 'printPerson(PersonList(1).Person) 'Err: Structure type mismatch... 'Works, my be used as workaround: P1 = PersonList(1).Person printPerson(P1) 'Works: Print PersonList(1).NodeId Sub printPerson(p As TPerson) Struct Print p End Sub > run TPERSON: .FIRSTNAME = "Peter" .LASTNAME = "Marther" 1 > I think instead of "Struct Print" it could just as easily be "Struct Save" or "Struct Load"? @Peter: So the two statements reported as errors could be bugs? Perhaps one could also live with the fact that it's not allowed to pass a struct, that is part of another struct, as a "member" parameter to subs and functions. Then one would have to take the "detour" via an intermediate variable, which is of course less efficient, but works as a "workaround". If only we had pointer variables (or reference variables) in MMBasic... But yes, I know, there's practically no BASIC that supports pointer variables (or reference variables). Not even VB.NET offers that. OK - BASIC is not C..Regards, bfwolf Not to forget: > option list PicoMite MMBasic RP2350B V6.02.01 OPTION FLASH SIZE 16777216 OPTION COLOURCODE ON OPTION CONTINUATION LINES ON OPTION PICO OFF OPTION CPUSPEED (KHz) 200000 OPTION PSRAM PIN GP47 > |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11215 |
Not a bug but a limitation. This is why it gives an error as the code can't handle that level of complexity |
||||
| bfwolf Senior Member Joined: 03/01/2025 Location: GermanyPosts: 235 |
That's what I suspected. And one "can live with it" since it's possible via a "detour" (as shown above). |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 1849 |
What is the argument for structures? Does a micro CONTROLLER need this? Does it mean greater efficiency? Asking for a friend |
||||
| EDNEDN Senior Member Joined: 18/02/2023 Location: United StatesPosts: 288 |
The ability to write cleaner, more easily understood code. |
||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3742 |
Well, you can write a shell sort, or some variety of sort or use the MMBasic SORT command if it suits your needs. Or you can have all your data (maybe a large amount) in an array of structures, and say STRUCT SORT bigarray().city Then you can do: index = Struct(FIND bigarray().city, "St. Louis") If you don't have lots of data, it may not be all that useful. If you do, structures are quite valuable. But could you do it on a PC rather than a microcontroller? Probably. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on FOTS |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3156 |
My limited experience is large amounts of data may be generated by a controller that logs its inputs and outputs but analysis is not done onboard. The data will typically be in CSV files or similar that you periodically download then put the controller back to work. Minimum downtime. If the data is not just being archived for quality control tracking, analysis is done in a PC spreadsheet or database program. |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 8769 |
STRUCT was designed for writing adventure games, I think. Much less boring than ordinary database stuff. ;) As such, having it on a baby computer-controller makes some sense. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 1849 |
I am still keeping var-names to < five characters, believing that this is the most efficient way for Basic to identify them. Am I out of date? |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11215 |
Yes: variable names are hashed so the length is pretty much irrelevant |
||||
| bfwolf Senior Member Joined: 03/01/2025 Location: GermanyPosts: 235 |
There are basically two types of arguments for this: 1. It allows you to implement the rule "keep data together that belong together!". Imagine a table like in a spreadsheet: There are many rows of data records with different types of values (strings, numbers) in the columns. With a two-dimensional array, all fields in the table must have the same type. This would mean that even columns with numbers in the fields would have to be stored as strings! You would then have to convert them repeatedly for calculations and then back into strings. That wouldn't be very efficient! and 2. It can also be more performant, since instead of multiple parameters, you only need to pass a reference to a struct to a sub/function. Regards, bfwolf. |
||||
| NPHighview Senior Member Joined: 02/09/2020 Location: United StatesPosts: 218 |
As a developer of embedded software for medical devices, I can definitively say that STRUCTs are extraordinarily useful. I used STRUCTs in C to develop the task scheduler for a cardiac ultrasound imager, avoiding the overhead of more general OOP constructs. An essential feature was the ability to embed pointers to the functions that handled the event triggered by a bitmapped mask to a bit array that indicated a change in the inputs collected from all of the UI/UX elements. There were twenty or thirty such events that could trigger actions, so the array of STRUCTs contained twenty or thirty elements. The imager used a commercial RTOS on a Motorola 68000, probably at the upper end of what you might consider a microcontroller :-) Live in the Future. It's Just Starting Now! |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 1849 |
Thanks guys, you just made me a huge fan of structures, especially now that I don't need to worry about var-name length |
||||
| bfwolf Senior Member Joined: 03/01/2025 Location: GermanyPosts: 235 |
Calling the M68000 a "microcontroller" is an understatement! It was designed for workstations and servers in its days. Unfortunately, in addition to ROM and RAM, it also required several external support and peripheral chips. Only the later M68332 was truly a "microcontroller," and even later, of course, the ColdFire microcontrollers/processors, which omitted the slow CISC 68k instructions (which require multiple clock cycles and couldn't be executed using a hardware multiplier, for example). However, these omitted instructions could be emulated via a library using an illegal instruction trap, in case older 680xx software needed to run. The ColdFire compilers simply didn't generate the missing instructions. The M68000 was certainly the father of the ARM architecture and, in my opinion, is still one of the most beautiful CPUs from a programmer's perspective. However, ARM microcontrollers like the Pi Pico are now orders of magnitude faster. Like the ARM, the M68000 supports structs particularly well through its indirect addressing modes such as offset16(An). |
||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3742 |
I don't remember multi-clock instructions on the 68000. It was around 45 years ago, but I wire-wrapped a 68000 single-stepper which, as I recall, depended on the one-instruction-per-cycle characteristic. Or maybe it was mostly one cycle per instruction, with some requiring multiple button-pushes. As I say, a long time ago. A great chip, though, with a flat 1MB memory architecture. It was a disappointment to me that the IBM PC didn't use the 68000. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on FOTS |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 8769 |
IMHO the 68000 was a microprocessor or, at a pinch, almost a microcomputer. :) Nothing is a microcontroller unless it has IO pins that can be attached directly to external devices. Having to drive external IO chips over an external address/data bus doesn't count. A microcontroller *includes* a microprocessor and IO peripherals on-chip. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
| Page 1 of 2 |
|||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |