Author Topic: C Q&A Thread  (Read 19120 times)

0 Members and 1 Guest are viewing this topic.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
C Q&A Thread
« on: July 03, 2011, 08:29:36 pm »
I'm just going to create a thread for all of the questions I have in regard to nspire C programming, so there isn't like 20 threads with my questions.
First question: Is there a way to make printf display output while a program is running? When I do it, everything gets printed at once after I exit the program. I'd like to see stuff printed while I run the program for debugging purposes.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: C Q&A Thread
« Reply #1 on: July 11, 2011, 05:39:43 pm »
New question? Why is my program giving me this error in the emulator:
data abort exception, lr=1107a4d0
the calc then resets.
Apparently, that means my program tried to write to an illegal memory address, so how do I stop it from doing so?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: C Q&A Thread
« Reply #2 on: July 12, 2011, 01:44:59 pm »
For the first question, try using NU_Local_Control_Interrupts(0x00000000) or NU_Local_Control_Interrupts(0xFFFFFFFF). I don't remember which one, but one of them enables interrupts and the other disables interrupts. You'll want them enabled for console output.

For the second question, it might help if I saw the C code in question.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: C Q&A Thread
« Reply #3 on: July 12, 2011, 04:43:20 pm »
Yes, you should use TCT_Local_Control_Interrupts(0); . See samples/hello.c in the Ndless SDK.

To debug exceptions you don't understand, try a C debugger. Setting it up is not easy, and I haven't yet released a version of Ncubate compatible with the latest Nspire OS, but its still quite useful for nasty bugs.
Ndless.me with the finest TI-Nspire programs

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: C Q&A Thread
« Reply #4 on: July 13, 2011, 02:01:53 pm »
Am I allowed to post only part of the code? I know the contest rules forbid posting the whole code but it doesn't say anything about a section of it (which happens to be most of the code right now).

Also, I tried using arm-eabi-none-gdb, but after typing in "break main", then typing "n" or "s" it tells me "Cannot find bounds of current function" ???

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: C Q&A Thread
« Reply #5 on: July 14, 2011, 04:11:51 am »
Try "c" after "break main", then run your program from the Documents menu.
Ndless.me with the finest TI-Nspire programs

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: C Q&A Thread
« Reply #6 on: August 04, 2011, 11:50:37 am »
Here's another problem I can't seem to figure out:

The compiler gives me the error "math.h:2:13: error: expected identifier or '(' before '{' token" and I have no idea why. Here's the entire contents of the math.h and math.c files:
Code: (math.h) [Select]
extern float sqrt(float x);
extern long abs(long x);
extern float fabs(float x);
extern float sin(float X);
extern float cos(float x);
Code: (math.c) [Select]
#include "math.h"

float sqrt(float x) {
  float r = x / 2;
  int i;
  for (i = 0; i < 10; i++) {
    r = 0.5 * (r + x / r);
  }
  return r;
}

long abs(long x) {
  return (x > 0) ? x : -x;
}

float fabs(float x) {
  if (x > 0) return x;
  if (x < 0) return -x;
  return x;
}

float cos(float x) {
  x = x - TWOPI * ((int) (x / TWOPI));
  float x2 = x * x;
  return 1 + x2 * (-.5 + x2 * (.0417 - x2 * 0.00138));
}

float sin(float x) {
  x = x - TWOPI * ((int) (x / TWOPI));
  float x2 = x * x;
  return x * (1 + x2 * (-0.1667 + .00833 * (x2 - .0002 * x2 )));
}

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C Q&A Thread
« Reply #7 on: August 04, 2011, 12:36:17 pm »
math.h is a default library, so it should be included with <>, #include <math.h>.

Oh wait, are you creating your own math.h? I think you should give it a new name, so it doesn't conflit with the default.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: C Q&A Thread
« Reply #8 on: August 04, 2011, 03:35:19 pm »
Well, ndless doesn't have a math.h so it shouldn't be an issue. I have compiled programs previously that use math.c and math.h and they work fine.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: C Q&A Thread
« Reply #9 on: August 05, 2011, 08:28:34 pm »
I figured out why...ndless already has an absolute value function. Now my code compiles fine :)

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: C Q&A Thread
« Reply #10 on: August 08, 2011, 01:28:41 am »
I have an array of the struct sprite, is there a way for me to take one struct in the array and set all the values at once?
Code: (The struct) [Select]
struct sprite {long type; float x; float y; float velocityX; float velocityY;};
Code: (The array) [Select]
struct sprite sprite[numSprites];

sprite[0] = {1, 6.5, 17.5, 0, 0};
The compiler rejects what I tried to do in that last line, so basically, what would be the syntax for replacing the contents of one struct with another's?

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: C Q&A Thread
« Reply #11 on: August 08, 2011, 04:40:06 pm »
Here's another question: How do I tell if the ON key is pressed? I can't do it with isKeyPressed.
« Last Edit: August 08, 2011, 04:40:15 pm by t0xic_kitt3n »

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: C Q&A Thread
« Reply #12 on: August 08, 2011, 04:46:59 pm »
For your first question this complies with C99:
Code: [Select]
struct sprite sprite = {.type = 1, .x = 6.5, .y = 17.5, .velocityX = 0, .velocityY = 0};But it strangely fails if sprite[0] is used, I don't know why.

For you second question, check 900B0028 bit 4, see http://hackspire.unsads.com/wiki/index.php/Memory-mapped_I/O_ports#900B0000_-_Power_management .
Ndless.me with the finest TI-Nspire programs

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: C Q&A Thread
« Reply #13 on: August 08, 2011, 04:53:36 pm »
So I could do this, right?
Code: [Select]
inline long is_on_key_pressed() {
  return !(*0x900B0028 & 0x00000008);
}
« Last Edit: August 08, 2011, 04:53:54 pm by t0xic_kitt3n »

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: C Q&A Thread
« Reply #14 on: August 08, 2011, 05:01:40 pm »
Rather this (untested):

Code: [Select]
inline int is_on_key_pressed() {
  return !(*(volatile int*)0x900B0028 & 4);
}
Ndless.me with the finest TI-Nspire programs