Function/method definition error in Arduion/C++. Or syntax error? - c++

I'm translating a program I wrote in Ruby to the Arduino/C++. On my first attempt to define a function/method I keep getting the following error: "BreadboardTestFunctions:41: error: a function-definition is not allowed here before '{' token
BreadboardTestFunctions:91: error: expected `}' at end of input"
Hard to understand because the function-definition must be followed by a pair of brackets. This could reflect a syntax error as it has persisted as I've corrected a score of errors in the function as I've tried to solve this problem. But it looks OK to me now.
I'm generating an output after matrix multiplication in the section beginning "// routine to multiply: behavior=brain * stimulus'". Although the problematic method, "mody" (at line 40) is only called once now, once I get it to work all the outputs will call it.
The code:
/* BREADBOARD
Implement program on Arduino + breadboard
*/
// constants
int foodPin = 2; // to provide food
int painPin = 3; // to punish
int ucsPin = 4; // the UCS
int csPin = 5; // the CS
int lightPin = 6; // turn the "light" stim on/off
int thresh = 700;
// variables
int buttonState = 0; // variable for reading the pushbutton status
boolean lighton = false;
unsigned short int energy = 10000;
int stimulus[11] = {0,0,0,0,0,0,0,0,0,0,0};
int brain[7][11] = { {0,0,0,0,99,0,0,0,0,1,0},
{0,0,0,0,0,99,0,0,0,1,0},
{0,0,0,0,0,0,99,0,0,1,0},
{90,0,0,0,0,0,0,1,-1,1,-99},
{0,90,0,0,0,0,0,1,-1,1,1},
{0,0,90,0,0,0,0,1,-1,1,1},
{0,0,0,90,0,0,0,1,-1,1,1} };
int behavior[7] = {0,0,0,0,0,0,0};
void setup() {
// initialize the pushbutton pins as an input:
pinMode(foodPin, INPUT);
pinMode(painPin, INPUT);
pinMode(ucsPin, INPUT);
pinMode(csPin, INPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600);
int ix=0;
// define behavioral methods
void mody (int ix, int brain[], int stimulus[])
{ int psp=20;
int j;
for(j=7;j<11;j++)
{if (brain[ix][j] > 0) brain[ix][j]+= stimulus[j] * (99-brain[ix][j])/psp;
if (brain[ix][j] < 0) brain[ix][j]+= -1*(stimulus[j] * abs(99-brain[ix][j])/psp);}
return;}
} // end void setup
void loop(){
// decay stimulus vector. do this and check inputs for ALL stimulii later
int k;
for(k=0;k<11;k++)
{if (stimulus[k] > 1) stimulus[k]-=2; else stimulus[k]=0;}
//check inputs
buttonState = digitalRead(foodPin);
if (buttonState == HIGH) stimulus[4] = 9;
buttonState = digitalRead(painPin);
if (buttonState == HIGH) stimulus[5] = 9;
buttonState = digitalRead(ucsPin);
if (buttonState == HIGH) stimulus[6] = 9;
buttonState = digitalRead(lightPin);
if (buttonState == HIGH) {stimulus[7] = 9; stimulus[8] = 9;lighton = true;}
else {stimulus[7] = 0; stimulus[8] = 0;lighton = false;}
buttonState = digitalRead(ucsPin);
if (buttonState == HIGH) stimulus[6] = 9;
// routine to multiply: behavior=brain * stimulus'
int i, j;
for(i=0;i<7;i++)
{ behavior[i]=0;
for (j=0;j<11;j++)
{behavior[i]= behavior[i]+stimulus[j]*brain[i][j]; }
} // end for i
if (behavior[0] > thresh) {Serial.println("Positive Fixer");}
if (behavior[1] > thresh) {Serial.println("Negative Fixer");}
if (behavior[2] > thresh) {Serial.println("UCR"); mody (2, brain[], stimulus[]);}
if (behavior[3] > thresh) {Serial.println("Operant one");}
if (behavior[4] > thresh) {Serial.println("Operant two");}
if (behavior[5] > thresh) {Serial.println("Operant three");}
if (behavior[6] > thresh) {Serial.println("Operant four");}
// generate random operant
if (random(energy) < 10) stimulus[random(4)]= 9 + random(3);
energy --;
Serial.println(energy);
} // end void loop

You may not define one function inside another function. It is what you are trying to do in the following code snippet
void setup() {
// initialize the pushbutton pins as an input:
pinMode(foodPin, INPUT);
pinMode(painPin, INPUT);
pinMode(ucsPin, INPUT);
pinMode(csPin, INPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600);
int ix=0;
// define behavioral methods
void mody (int ix, int brain[], int stimulus[])
You are trying to define function mody inside function setup.

Related

The attributes of my child class are lazily initialized

I have created a parent class in C++ and one child class which I have two attributes: _trigger and _echo. In order to use my child class I declare it and I assign its address to a pointer of the parent class. Hence, I use the methods of my parent class.
My problem: When I use the method cyclePulse without parameters (int trigger, int echo) and the attributes from my class (_trigger and _echo), the method does not work properly. I guess it is because the attributes _trigger and _echo are lazily initialized, or because I am not using new keyword when I am creating my object.
class ISensor {
...
public:
ISensor();
virtual ~ISensor();
...
virtual int connect() = 0;
virtual char * readRequest() = 0;
virtual int disconnect() = 0;
};
class HCSR04: public ISensor {
private:
int _trigger;
int _echo;
public:
HCSR04();
HCSR04(int trigger, int echo);
...
int connect();
char * readRequest();
uint64_t cyclePulse(int trigger, int echo);
float distanceCentimeters();
};
Here is the implementation of HCSR04.cpp. Edited: I forgot the constructors.
HCSR04::HCSR04() {
_echo = RPI_V2_GPIO_P1_13;
_trigger = RPI_V2_GPIO_P1_15;
}
HCSR04::HCSR04(int trigger, int echo) {
_echo = echo;
_trigger = trigger;
}
char * HCSR04::readRequest() {
float preCent = distanceCentimeters();
char* buf = new char[20];
sprintf(buf, "%.10f", preCent);
return buf;
}
float HCSR04::distanceCentimeters() {
return (float) cyclePulse(_trigger, _echo) / 55.5;
}
uint64_t HCSR04::cyclePulse(int trigger, int echo) {
uint64_t width, begin, start, end;
int max = 80, check;
begin = bcm2835_st_read();
// Emit pulse for 10 microseconds
bcm2835_gpio_write(_trigger, HIGH); // Set trigger state HIGH
bcm2835_delayMicroseconds(10); // Wait 10 microseconds
bcm2835_gpio_write(_trigger, LOW); // Set trigger state LOW
while (bcm2835_gpio_lev(_echo) == LOW && check < max) {
start = bcm2835_st_read();
check = (int) begin - start;
}
while (bcm2835_gpio_lev(_echo) == HIGH) {
bcm2835_delayMicroseconds(1);
}
end = bcm2835_st_read();
width = end - start;
return width;
}
I am using the class like this:
HCSR04 deviceUltrasonic;
ISensor * sensorUltrasonic = &deviceUltrasonic;
char* readRequestArray = sensorUltrasonic->readRequest();
Giving my previous comment as a possible answer here, because I assume that the uninitialized variables are causing your fault here:
uint64_t HCSR04::cyclePulse(int trigger, int echo) {
uint64_t width, begin, start, end;
int max = 80, check = 0; // <<< init check to 0.
// Btw: size_t or any other unsigned type matches
// better the purpose of what you want to achieve.
begin = bcm2835_st_read();
// begin is not used afterwards. Did you mean to initialize "start" here?
start = begin;
// Emit pulse for 10 microseconds
bcm2835_gpio_write(_trigger, HIGH); // Set trigger state HIGH
bcm2835_delayMicroseconds(10); // Wait 10 microseconds
bcm2835_gpio_write(_trigger, LOW); // Set trigger state LOW
while (bcm2835_gpio_lev(_echo) == LOW && check < max) {
start = bcm2835_st_read();
check = (int) begin - start;
}
while (bcm2835_gpio_lev(_echo) == HIGH) {
bcm2835_delayMicroseconds(1);
}
end = bcm2835_st_read();
width = end - start;
return width;
}

arduino if statement giving error

I am working on some arduino code and my program keeps on giving me this error,
ISO C++ forbids comparison between pointer and integer [-fpermissive]
I've tried searching on the internet to solve this issue but, either the solution is incorrect, or irrelevant. here is where the arduino software is saying the problem is,
if((millis - incLastDebounce) > debounceDelay) {
and if you need the rest of the code here it is,
#include <LiquidCrystal.h>
int freq = 0;
int change = 0;
const int incPin = 3;
const int setPin = 2;
int incButtonState;
int setButtonState;
int incPreviousState;
int setPreviousState;
int incLastDebounce;
int setLastDebounce;
const int debounceDelay = 50;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2);
pinMode(setPin, INPUT);
pinMode(incPin, INPUT);
lcd.print("Frequency: " + freq);
}
void loop() {
// put your main code here, to run repeatedly:
int incReading = digitalRead(incPin);
int setReading = digitalRead(setPin);
if(setReading != setPreviousState) {
setLastDebounce = millis();
}
if(incReading != incPreviousState) {
incLastDebounce = millis();
}
if((millis - setLastDebounce) > debounceDelay) {
if(setReading != setButtonState) {
setButtonState = setReading;
}
if(setButtonState == HIGH) {
//Okay so here you will do your set lcd voodoo
}
}
if((millis - incLastDebounce) > debounceDelay) {
if(incReading != buttonState) {
incButtonState = incReading;
}
if(buttonState == HIGH) {
// here you can put the lcd code
change = change + 500;
if(change == 10500){
change = 0;
}
}
}
incPreviousState = incReading;
setPreviousState = setReading;
}
hopefully you can find the problem and help.
Looks like you're missing parentheses after millis, so instead of calling the function, you're trying to do arithmetic with its memory address.
This will probably work better:
if ((millis() - incLastDebounce) > debounceDelay) {

Arduino - Append int value to the end of a string for concatenated variables

I wanted to expand on the Arduino Button and ForLoop tutorials by going through sequential inputs to check their status and illuminate the LED if any of them are pressed. Ultimately, I just want to do a single shot scan of the inputs before everything starts and anything that is closed (or shorted out) will be taken out of the rotation in the main program.
If the pins were sequential, I'd just do buttonIn++ starting at the first pin. Unfortunately, the input pins are not sequential but the names are.
I want to just add the int "1" to the end of char buttonIn = "myButton" and ++ the number in the string. That doesn't seem to be as easy as I had thought.
Now, I can do this with PHP easily
<?php
$myButton1="7";
$myButton2="15";
$myButton3="3";
$myButton4="11";
$myButton5="8";
for ($i=0;$i<=5;$i++) {
$buttonIn="myButton".$i;
echo $buttonIn." = ".$$buttonIn."\n";
}
?>
Which then outputs:
myButton1 = 7
myButton2 = 15
myButton3 = 3
myButton4 = 11
myButton5 = 8
Perfect, I can get both the variable name and its value.
However, this doesn't work with C. The commented out lines are what I've tried so far. Hopefully someone else has a better idea to do this without having to specify every single pin in the pre-run loop thus saving space and time.
const int myButton1 = 7;
const int myButton2 = 15;
const int myButton3 = 3;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(myButton1, INPUT);
pinMode(myButton2, INPUT);
pinMode(myButton3, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
char buttonIn[13];
for (int x=1;x<=5;x++) {
// char buttonIn = "OSD1button",x;
// char buttonIn[13]="OSD1button",x;
// int sprintf(str, "OSD1button%d",x);
// sprintf(buttonIn,"OSD1button%d",x);
// strncat(buttonIn,x,2);
// char nameIn[12]="OSD1button";
//buttonIn=nameIn + x;
// sprintf(buttonIn, "%d", x);
char OSD="OSD1button";
// buttonIn=OSD+itoa(x,OSD,13);
strncpy(buttonIn,OSD,x);
buttonState = digitalRead(buttonIn);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin,LOW);
}
}
}
Here's the current error message:
Arduino: 1.5.6-r2 (Windows 7), Board: "Arduino Due (Programming Port)"
OSD_Test.ino: In function 'void loop()':
OSD_Test:67: error: invalid conversion from 'const char*' to 'char'
OSD_Test:69: error: invalid conversion from 'char' to 'const char*'
OSD_Test:69: error: initializing argument 2 of 'char* strncpy(char*, const char*, size_t)'
OSD_Test:70: error: invalid conversion from 'char*' to 'uint32_t'
OSD_Test:70: error: initializing argument 1 of 'int digitalRead(uint32_t)'
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Thanks in advance!
Since Arduino actually provides a StringAdditonOperator http://arduino.cc/en/Tutorial/StringAdditionOperator you could use:
for(int x = 1; x <=5; x++) {
String desiredString = "Button"+x;
Serial.println(desiredString);
}
Which will output:
Button1
Button2
...
As far as I understand from the comments you want to do something like this:
int buttonArray[3] = {7,15,3}; //Or on whatever pins your buttons are
// Setup code and anything else you need goes here
void loop() {
for(int x = 0; x <= 3; x++) {
int buttonState = digitalRead(buttonArray[x]);
digitalWrite(ledPin,buttonState);
}
}
But be aware that this will change the state of the LED-Pin only to the last button state read.
Got it. This works great. Thanks.
const int myButton1 = 7;
const int myButton2 = 15;
const int myButton3 = 3;
const int myButton4 = 27;
const int myButton5 = 22;
const int myButton6 = 18;
const int myButton7 = 23;
const int myButton8 = 11;
const int myOutput1 = 8;
const int myOutput2 = 16;
const int myOutput3 = 4;
const int myOutput4 = 28;
const int myOutput5 = 24;
const int myOutput6 = 19;
const int myOutput7 = 25;
const int myOutput8 = 12;
Becomes
int myButton[8]={7,15,3,27,22,18,23,11};
int myOutput[8]={8,16,4,28,24,19,25,12};
Then add this to the setup
for (int i=0;i<8;i++) {
pinMode(myButton[i], INPUT);
pinMode(myOutput[i], OUTPUT);
}

c++ Floodfill algorithm final errors

My floodfilling algorithm is nearly finished, but there is a small error somewhere, I've spent about 3 hours debugging, but i can't seem to find it!
note:
When reading in I use numbers from 0 to 15 to define the walls
1 = top
2 = right
4 = bottom
8 = left
(so 13 would mean that the top/bottom/left walls are there)
My Program:
It reads in number of fields to calculate the biggest room from (so everything below here is a cycle that gets repeated for the number of fields).
Then it gets the room's dimensions
Now in the class field, it creates an array of objects (Cell) which store the walls around (left right down up), and a value below 16
Now here is where I think the problem comes, reading in values through std::cin
and then when everything is read in, it scans for empty (0), and then creates a room, and checks for availeble spaces around it (using the wall-check)
and at the end it returns the max value, and we are done.
The input I use:
1
2 2
13 3
15 14
so what happens is is that somewhere, in or the wall-check, or the creation of a object Cell something goes wrong (I think)
Here is my script, and sorry to have to ask something silly like this!
Thanks in advance
// een simpele floodfill
#include <stdlib.h>
#include <iostream>
#include <bitset>
class Cell {
private:
int kamer, value;
bool left, right, up, down;
public:
// constructor
Cell::Cell() {};
// functions
bool CanLeft() { return left ; }
bool CanRight() { return right; }
bool CanDown() { return down ; }
bool CanUp() { return up ; }
int GetRoom() { return kamer; }
void SetRoom(int x) { kamer = x ; }
void SetValue(int x, int room=0) { value = x;
kamer = room;
std::bitset<sizeof(int)> bits(value);
if (bits[3]) left = true;
else left = false;
if (bits[2]) down = true;
else down = false;
if (bits[1]) right = true;
else right = false;
if (bits[0]) up = true;
else up = false;
}
};
class Field {
private:
int Biggest_Chamber;
int Y;
int X;
int temp;
Cell playfield[][1];
public:
// constructor
Field::Field(int SizeY, int SizeX) {
Y = SizeY;
X = SizeX;
Cell playfield[SizeY-1][SizeX-1];
}
// Create a 2d array and fill it
void Get_input() {
for (int Yas = 0; Yas < Y; Yas++){
for (int Xas = 0; Xas < X; Xas++){
std::cin >> temp;
playfield[Yas][Xas].SetValue(temp);
}
}
};
void Start() { Mark(0,0,1); }
void Mark(int y, int x, int nr) {
std::cout << nr;
temp = nr;
playfield[y][x].SetRoom(nr);
if (playfield[y][x].CanLeft()) {
if (playfield[y][x-1].GetRoom() != 0) {
Mark(y, x-1, nr);
std::cout << nr;
system("pause");}}
if (playfield[y][x].CanDown()) {
if (playfield[y+1][x].GetRoom() != 0) {
Mark(y+1, x, nr);
std::cout << nr;
system("pause");}}
if (playfield[y][x].CanRight()) {
if (playfield[y][x+1].GetRoom() != 0) {
Mark(y, x+1, nr);
std::cout << nr;
system("pause");}}
if (playfield[y][x].CanUp()) {
if (playfield[y-1][x].GetRoom() != 0) {
Mark(y-1, x, nr);
std::cout << nr;
system("pause");}}
for (int vertical = 0; vertical < Y; vertical++) {
for (int horizontal = 0; horizontal < X; horizontal++) {
if (playfield[vertical][horizontal].GetRoom() == 0) Mark(vertical, horizontal, nr+1);
}
}
}
int MaxValue() {
int counter[temp];
int max = 0;
for (int y = 0; y < Y; y++) {
for (int x = 0; x < X; x++) {
counter[playfield[y][x].GetRoom()]++;
}
}
for (int i = 0; i < temp; i++)
{
if (counter[i] > max)
max = counter[i];
}
return max;
}
};
int main() {
using namespace std;
int NrKamers;
int sizeY;
int sizeX;
std::cin >> NrKamers;
for (int i = 0; i < NrKamers; i++){
std::cin >> sizeY >> sizeX;
Field floodfield(sizeY, sizeX);
floodfield.Get_input();
floodfield.Start();
std::cout << floodfield.MaxValue() << std::endl;
}
return 0;
}
I have not had much time to deal with the code, but my first impression is that you are not marking (or rather not using the mark) each visited position in the array, so that you move in one direction, and while processing that other position you return back to the original square. Consider that the sequence of tests where: left, right, up, down; and that you start in the top-left corner:
You cannot move left, but you can move right. At that second recursion level you can move left and go back to square one. Then you cannot move left, but you can move right, so you go back to square two, from which you move to square one... infinitedly.
Before you move to the next square you have to mark your square as visited, and also check that the square you intend to move to has not been visited in the current run.
The segmentation fault is the result of infinite recursion, after you exhaust the stack.
1-11-2017: NEW-VERSION; SUCCESFULLY TESTED WITH TWO BITMAPS.
I propose my C version of the Flood-Fill algorithm, which doesn't uses recursive calls, but only a queue of the offsets of the new points, it works on the window: WinnOffs-(WinDimX,WinDimY) of the double-buffer: *VBuffer (copy of the screen or image) and, optionally, it write a mask of the flood-fill's result (*ExtraVBuff).
ExtraVBuff must be filled it with 0 before the call (if you don't need a mask you may set ExtraVBuff= NULL); using it after call you can do gradient floodfill or other painting effects. NewFloodFill works with 32 Bit per Pixel and it is a C function. I've reinvented this algorithm in 1991 (I wrote his in Pascal), but now it works in C with 32 Bit per Pixel; also not uses any functions calls, does only a division after each "pop" from queue, and never overflows the queue, that, if it is sized in the right way (about 1/4 of the pixels of the image), it allows always to fill correctly any area; I show before the c-function (FFILL.C), after the test program (TEST.C):
#define IMAGE_WIDTH 1024
#define IMAGE_HEIGHT 768
#define IMAGE_SIZE IMAGE_WIDTH*IMAGE_HEIGHT
#define QUEUE_MAX IMAGE_SIZE/4
typedef int T_Queue[QUEUE_MAX];
typedef int T_Image[IMAGE_SIZE];
void NewFloodFill(int X,
int Y,
int Color,
int BuffDimX,
int WinOffS,
int WinDimX,
int WinDimY,
T_Image VBuffer,
T_Image ExtraVBuff,
T_Queue MyQueue)
/* Replaces all pixels adjacent to the first pixel and equal to this; */
/* if ExtraVBuff == NULL writes to *VBuffer (eg BUFFER of 786432 Pixel),*/
/* otherwise prepare a mask by writing on *ExtraVBuff (such BUFFER must */
/* always have the same size as *VBuffer (it must be initialized to 0)).*/
/* X,Y: Point coordinates' of origin of the flood-fill. */
/* WinOffS: Writing start offset on *VBuffer and *ExtraVBuff. */
/* BuffDimX: Width, in number of Pixel (int), of each buffer. */
/* WinDimX: Width, in number of Pixel (int), of the window. */
/* Color: New color that replace all_Pixel == origin's_point. */
/* WinDimY: Height, in number of Pixel (int), of the window. */
/* VBuffer: Pointer to the primary buffer. */
/* ExtraVBuff: Pointer to the mask buffer (can be = NULL). */
/* MyQueue: Pointer to the queue, containing the new-points' offsets*/
{
int VBuffCurrOffs=WinOffS+X+Y*BuffDimX;
int PixelIn=VBuffer[VBuffCurrOffs];
int QueuePnt=0;
int *TempAddr=((ExtraVBuff) ? ExtraVBuff : VBuffer);
int TempOffs1;
int TempX1;
int TempX2;
char FLAG;
if (0<=X && X<WinDimX && 0<=Y && Y<WinDimY) do
{
/* Fill to left the current line */
TempX2=X;
while (X>=0 && PixelIn==VBuffer[VBuffCurrOffs])
{
TempAddr[VBuffCurrOffs--]=Color;
--X;
}
TempOffs1=VBuffCurrOffs+1;
TempX1=X+1;
/* Fill to right the current line */
VBuffCurrOffs+=TempX2-X;
X=TempX2;
while (X+1<WinDimX && PixelIn==VBuffer[VBuffCurrOffs+1])
{
++X;
TempAddr[++VBuffCurrOffs]=Color;
}
TempX2=X;
/* Backward scan of the previous line; puts new points offset in Queue[] */
if (Y>0)
{
FLAG=1;
VBuffCurrOffs-=BuffDimX;
while (X-->=TempX1)
{
if (PixelIn!=VBuffer[VBuffCurrOffs] ||
ExtraVBuff && Color==ExtraVBuff[VBuffCurrOffs])
FLAG=1;
else
if (FLAG)
{
FLAG=0;
if (QueuePnt<QUEUE_MAX)
MyQueue[QueuePnt++]=VBuffCurrOffs;
}
--VBuffCurrOffs;
}
}
/* Forward scan of the next line; puts new points offset in Queue[] */
if (Y<WinDimY-1)
{
FLAG=1;
VBuffCurrOffs=TempOffs1+BuffDimX;
X=TempX1;
while (X++<=TempX2)
{
if (PixelIn!=VBuffer[VBuffCurrOffs] ||
ExtraVBuff && Color==ExtraVBuff[VBuffCurrOffs])
FLAG=1;
else
if (FLAG)
{
FLAG=0;
if (QueuePnt<QUEUE_MAX)
MyQueue[QueuePnt++]=VBuffCurrOffs;
}
++VBuffCurrOffs;
}
}
/* Gets a new point offset from Queue[] */
if (--QueuePnt>=0)
{
VBuffCurrOffs=MyQueue[QueuePnt];
TempOffs1=VBuffCurrOffs-WinOffS;
X=TempOffs1%BuffDimX;
Y=TempOffs1/BuffDimX;
}
/* Repeat the main cycle until the Queue[] is not empty */
} while (QueuePnt>=0);
}
Here there is the test program:
#include <stdio.h>
#include <malloc.h>
#include "ffill.c"
#define RED_COL 0xFFFF0000
#define WIN_LEFT 52
#define WIN_TOP 48
#define WIN_WIDTH 920
#define WIN_HEIGHT 672
#define START_LEFT 0
#define START_TOP 671
#define BMP_HEADER_SIZE 54
typedef char T_Image_Header[BMP_HEADER_SIZE];
void main(void)
{
T_Image_Header bmpheader;
T_Image *image;
T_Image *mask;
T_Queue *MyQueue;
FILE *stream;
char *filename1="ffill1.bmp";
char *filename2="ffill2.bmp";
char *filename3="ffill3.bmp";
int bwritten;
int bread;
image=malloc(sizeof(*image));
mask=malloc(sizeof(*mask));
MyQueue=malloc(sizeof(*MyQueue));
stream=fopen(filename1,"rb");
bread=fread(&bmpheader, 1, BMP_HEADER_SIZE, stream);
bread=fread((char *)image, 1, IMAGE_SIZE<<2, stream);
fclose(stream);
memset(mask,0,IMAGE_SIZE<<2);
NewFloodFill(START_LEFT,
START_TOP,
RED_COL,
IMAGE_WIDTH,
IMAGE_WIDTH*WIN_TOP+WIN_LEFT,
WIN_WIDTH,
WIN_HEIGHT,
*image,
NULL,
*MyQueue);
stream=fopen(filename2,"wb+");
bwritten=fwrite(&bmpheader, 1, BMP_HEADER_SIZE, stream);
bwritten=fwrite((char *)image, 1, IMAGE_SIZE<<2, stream);
fclose(stream);
stream=fopen(filename3,"wb+");
bwritten=fwrite(&bmpheader, 1, BMP_HEADER_SIZE, stream);
bwritten=fwrite((char *)mask, 1, IMAGE_SIZE<<2, stream);
fclose(stream);
free(MyQueue);
free(mask);
free(image);
}
I've used, for the input of the test program shown, the follow Windows uncompressed .BMP image (ffill1.bmp):
Filled, by the test program shown, as follows (ffill2.bmp):
Using "mask" instead of NULL, the output bitmap is (ffill3.bmp):

Help in combining two functions in c++

I am just trying something with somebody else's code.
I have two functions:
int Triangle(Render *render, int numParts, Token *nameList, Pointer *valueList)
int i;
for (i=0; i<numParts; i++)
{
switch (nameList[i])
{
case GZ_NULL_TOKEN:
break;
case GZ_POSITION:
return putTrianglePosition(render, (Coord *)valueList[i]);
break;
}
}
return SUCCESS;
}
int putTrianglePosition(Render *render, Coord vertexList[3]) /*vertexList[3][3:xyz]*/
{
Coord *pv[3];
int i,j;
// sort verts by inc. y and inc. x
pv[0] = &vertexList[0];
pv[1] = &vertexList[1];
pv[2] = &vertexList[2];
for (i=0; i<2; i++)
for (j=i+1; j<3; j++)
{
if ((*pv[i])[1]>(*pv[j])[1] ||
(*pv[i])[1]==(*pv[j])[1] && (*pv[i])[0]>(*pv[j])[0]) {
Coord *tmp;
tmp = pv[i];
pv[i] = pv[j];
pv[j] = tmp;
}
}
;
// all y the same?
if ((*pv[0])[1] == (*pv[2])[1]) {
drawHorizonLine(render, *pv[0], *pv[2]);
return SUCCESS;
}
// assign middle point
Coord mid;
mid[1] = (*pv[1])[1]; // y
float ratio = ((*pv[1])[1] - (*pv[0])[1]) / ((*pv[2])[1] - (*pv[0])[1]);
mid[0] = (*pv[0])[0] + ratio * ((*pv[2])[0] - (*pv[0])[0]); // x
mid[2] = (*pv[0])[2] + ratio * ((*pv[2])[2] - (*pv[0])[2]); // z
if (mid[0]<=(*pv[1])[0]) { // compare X
drawTrapzoid(render, *pv[0], mid, *pv[0], *pv[1]); // upper tri
drawTrapzoid(render, mid, *pv[2], *pv[1], *pv[2]); // lower tri
}else{
drawTrapzoid(render, *pv[0], *pv[1], *pv[0], mid); // upper tri
drawTrapzoid(render, *pv[1], *pv[2], mid, *pv[2]); // lower tri
}
return SUCCESS;
}
I don't want two functions here. I want to copy the putTrianglePosition() function into the Triangle() function.
I tried doing that, but I got a lot of errors.
Can somebody else show me how to do this?
You shouldn't put functions together, you should split them apart. Put a new function wherever you can name them -- try to make them as small as you can. If you want a function that does all of that stuff, have a function that calls the other functions.
int foobar() {
int a;
int b;
/* do a whole bunch of stuff with a */
/* do a whole bunch of stuff with b */
return a + b;
}
this is sort of what you're trying to do. Instead, do this:
int foo(){
int a;
/* do a bunch of stuff with a */
return a;
}
int bar() {
int b;
/* do a bunch of stuff with b */
return b;
}
int foobar() {
return foo() + bar();
}
The result will be cleaner, easier to maintain and re-usable.
If you just change the line
return putTrianglePosition(render, (Coord *)valueList[i]);
into:
Coord* vertexList = (Coord*) valueList[i];
followed by the whole body of what's now putTrianglePosition from the opening { to the closing } included, I believe it should just work. If not, please edit your question to add the exact, complete, code as obtained by this edit and the exact, complete error messages you get.
I strongly recommend you to go with Functions because it allows better separation of logic and allows you to reuse the logic. But still in case if you want to use it that way please check the function below :
int Triangle(Render *render, int numParts, Token *nameList, Pointer *valueList)
{
int iOuter;
for (iOuter=0; iOuter<numParts; iOuter++)
{
switch (nameList[iOuter])
{
case GZ_NULL_TOKEN:
break;
case GZ_POSITION:
{
Coord* vertexList = (Coord*) valueList[i];
Coord *pv[3];
int i,j;
// sort verts by inc. y and inc. x
pv[0] = &vertexList[0];
pv[1] = &vertexList[1];
pv[2] = &vertexList[2];
for (i=0; i<2; i++)
for (j=i+1; j<3; j++)
{
if ((*pv[i])[1]>(*pv[j])[1] ||
(*pv[i])[1]==(*pv[j])[1] && (*pv[i])[0]>(*pv[j])[0]) {
Coord *tmp;
tmp = pv[i];
pv[i] = pv[j];
pv[j] = tmp;
}
}
;
// all y the same?
if ((*pv[0])[1] == (*pv[2])[1]) {
drawHorizonLine(render, *pv[0], *pv[2]);
return SUCCESS;
}
// assign middle point
Coord mid;
mid[1] = (*pv[1])[1]; // y
float ratio = ((*pv[1])[1] - (*pv[0])[1]) / ((*pv[2])[1] - (*pv[0])[1]);
mid[0] = (*pv[0])[0] + ratio * ((*pv[2])[0] - (*pv[0])[0]); // x
mid[2] = (*pv[0])[2] + ratio * ((*pv[2])[2] - (*pv[0])[2]); // z
if (mid[0]<=(*pv[1])[0]) { // compare X
drawTrapzoid(render, *pv[0], mid, *pv[0], *pv[1]); // upper tri
drawTrapzoid(render, mid, *pv[2], *pv[1], *pv[2]); // lower tri
}else{
drawTrapzoid(render, *pv[0], *pv[1], *pv[0], mid); // upper tri
drawTrapzoid(render, *pv[1], *pv[2], mid, *pv[2]); // lower tri
}
return SUCCESS;
}
break;
}
}
return SUCCESS;
}
Well, since the tag says C++ (even though the code seems to be pure C), the solution would be to put an inline modifier before the function:
inline int putTrianglePosition(Render *render, Coord vertexList[3])
{
...
}
However, even after thinking about this for ten minutes, I still fail a valid reason for wanting this.