??? 03/28/08 15:52 Read: times |
#152674 - Like this Responding to: ???'s previous message |
Following is some code that I think does what you want. The point of posting this isn't so much to solve the problem for you (I think you've already got it figured out) as it is to illustrate a particular programming technique.
The trick here is the table that associates each of the scaled variables with the limits that apply to it. Without such a table or something similar, you will probably wind up with a bunch of nasty, duplicated code to somehow handle the four scaled variables one at a time. With the table, you can write the code that actually does the dirty work only once and invoke it within a loop for all four scaled variables as I have shown. Besides making your program more compact and easier to maintain, this technique makes it really easy to add more variables or change the limit values. To do that, you only have to change the table. The code remains the same. One other thing to notice is the calculation itself. If you look at it carefully and think about it a little bit, you will see that the term (POT_RANGE / 2) in the dividend takes care of the rounding problem that Russell mentioned. With that term in place, the result of the division is rounded to the nearest integer, rather than being truncated as it would be by a plain old integer divide. -- Russ typedef struct { unsigned char *pValue; // Pointer to scaled value unsigned loLimit; // Low limit unsigned hiLimit; // High limit } CTableEntry; unsigned char L, M, N, O; // The scaled variables CTableEntry table[] = { // Table that correlates each { &L, 10, 35 }, // of the scaled variables { &M, 30, 45 }, // with its particular set of { &N, 60, 80 }, // limits { &O, 25, 40 } }; #define TABLE_SIZE (sizeof(table) / sizeof(CTableEntry)) /* Some abbreviations that will make the code in DoScaling() easier to read */ #define VALUE *table[j].pValue #define RANGE (table[j].hiLimit - table[j].loLimit) #define OFFSET table[j].loLimit #define POT_RANGE 255 void DoScaling ( // Sets the scaled variables to unsigned char potValue // match this pot setting ) { int j; // Table index for (j=0; j<TABLE_SIZE; j++) { // For each table entry VALUE = OFFSET + (((RANGE * potValue) // Calculate the corresponding + (POT_RANGE / 2)) / POT_RANGE); // scaled variable } // End 'for each table entry' } // End DoScaling() /* Test program to make sure everything abovve this point works right */ void Dump(pot) { printf("Pot: %3d, L: %2d, M: %2d, N: %2d, O: %2d\n", pot, L, M, N, O); } void main() { DoScaling( 0); Dump( 0); DoScaling( 64); Dump( 64); DoScaling(128); Dump(128); DoScaling(192); Dump(192); DoScaling(255); Dump(255); } The program above gives the output below: Pot: 0, L: 10, M: 30, N: 60, O: 25 Pot: 64, L: 16, M: 34, N: 65, O: 29 Pot: 128, L: 23, M: 38, N: 70, O: 33 Pot: 192, L: 29, M: 41, N: 75, O: 36 Pot: 255, L: 35, M: 45, N: 80, O: 40 |
Topic | Author | Date |
Hey function lovers....how would you handle this? | 01/01/70 00:00 | |
Scale and offset? | 01/01/70 00:00 | |
kinda.....but dependant | 01/01/70 00:00 | |
I'm not sure of what you want.. | 01/01/70 00:00 | |
Ah....clarity | 01/01/70 00:00 | |
Nitpicking | 01/01/70 00:00 | |
Yes, but whats a .1 among friends? | 01/01/70 00:00 | |
Overlap | 01/01/70 00:00 | |
The overlap is proper | 01/01/70 00:00 | |
RE: not sure why one would do that | 01/01/70 00:00 | |
in the example give | 01/01/70 00:00 | |
Now I understand! | 01/01/70 00:00 | |
Like this | 01/01/70 00:00 | |
Wow Russ....very nice piece of code there![]() | 01/01/70 00:00 |