The Hammer
- Sep 17, 2013
- 164
- Joined
- Sep 17, 2013
- Messages
- 164
Not much time to respond more right now, but each column should update at every 55.55 microseconds. That is my intent.
I think that when dealing with seconds you need to divide instead of multiply, but with Hz it's multiplying.
Why would I still see it though?? I don't understand.
/*
* GccApplication6.cpp
*
* Created: 5/23/2014 2:23:59 PM
* Author: The Hammer
*/
#include <avr/io.h>
struct BPCStruct {
uint8_t Value;
uint8_t FadeTime;
};
struct BCStruct {
uint8_t Value;
};
struct KeyStruct {
uint8_t pressed;
uint8_t fadestep;
BCStruct BC[3];
BPCStruct BPC[3]; // B, G, CC, R
};
KeyStruct key[108];
uint8_t brightstep = 0;
uint8_t ccathodes = 0;
uint8_t KEYn = 0;
uint8_t LEDc = 0;
void LEDanode ();
int main(void)
{
DDRA = 0b11111111;
PORTA = 0b11110000;
DDRB = 0b00000000;
PORTB = 0b11111111;
TCCR0B |= (1 << CS01);
brightstep = 0;
ccathodes = 0;
KEYn = 0;
LEDc = 0;
for (uint8_t ksv = 0; ksv < 108; ++ksv)
{
key[ksv].pressed = 0;
key[ksv].fadestep = 0;
key[ksv].BC[0].Value = 0;
key[ksv].BC[1].Value = 0;
key[ksv].BC[2].Value = 10;
key[ksv].BPC[0].FadeTime = 0;
key[ksv].BPC[1].FadeTime = 0;
key[ksv].BPC[2].FadeTime = 0;
key[ksv].BPC[0].Value = 10;
key[ksv].BPC[1].Value = 0;
key[ksv].BPC[2].Value = 0;
}
while(1)
{
if (1==1)
{
key[(TCNT0 - 30)].pressed = 1;
TCNT0 = 0; //reset time counter
PORTA |= (1 << PINA4); //(set high) copy data out -latch- this outputs changes that are stored in both sets of shift registers
PORTA &= ~(1 << PINA4); //(set low)
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 2 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 3 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 4 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 5 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 6 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
if (KEYn == 107) //--------------key 1 in next column
KEYn = 0;
else
++KEYn;
//--end anodes----------------------begin cathodes--
if (!ccathodes) //adjust which common cathode set is active
{
ccathodes = 17;
PORTA |= (1 << PA2); //cathodes' data (high)
PORTA |= (1 << PA3); //cathodes' sh_cp (high)
PORTA &= ~(1 << PA3); //cathodes' sh_cp (low)
PORTA &= ~(1 << PA2); //cathodes' data (low)
if (brightstep == 19) //adjust current brightness step
brightstep = 0;
else
++brightstep;
}
else
{
--ccathodes;
PORTA |= (1 << PA3); //cathodes' sh_cp (high)
PORTA &= ~(1 << PA3); //cathodes' sh_cp (low)
}
}
/*else
{
for (uint8_t cks = 6; cks > 3; cks--)
{
if (cks == 6)
{
PORTA &= ~(1 << 6);
for (uint8_t ckp = 0; ckp < 8; ckp++)
{
if (!(PINB & (1 << ckp)))
key[ckp].pressed = 1;
else if (key[ckp].pressed)
key[ckp].pressed = 0;
}
PORTA |= (1 << 6);
}
else if (cks == 5)
{
PORTA &= ~(1 << 5);
for (uint8_t ckp = 0; ckp < 8; ckp++)
{
if (!(PINB & (1 << ckp)))
key[(ckp + 8)].pressed = 1;
else if (key[(ckp + 8)].pressed)
key[(ckp + 8)].pressed = 0;
}
PORTA |= (1 << 5);
}
else if (cks == 4)
{
PORTA &= ~(1 << 4);
for (uint8_t ckp = 0; ckp < 8; ckp++)
{
if (!(PINB & (1 << ckp)))
key[(ckp + 16)].pressed = 1;
else if (key[(ckp + 16)].pressed)
key[(ckp + 16)].pressed = 0;
}
PORTA |= (1 << 4);
}
}
}*/
}
}
void LEDanode ()
{
if (key[KEYn].fadestep)
{
if (brightstep < key[KEYn].BPC[LEDc].FadeTime) //set the anode on the shift registers
PORTA |= (1 << PA0);
else
PORTA &= ~(1 << PA0);
if (brightstep == 19)
{
--key[KEYn].fadestep;
if (key[KEYn].pressed) //if this key is pressed
{
key[KEYn].fadestep = 50;
key[KEYn].BPC[LEDc].FadeTime = key[KEYn].BPC[LEDc].Value;
}
else if (key[KEYn].BPC[LEDc].Value > key[KEYn].BC[LEDc].Value)
key[KEYn].BPC[LEDc].FadeTime = key[KEYn].BC[LEDc].Value - (((key[KEYn].BC[LEDc].Value - key[KEYn].BPC[LEDc].Value) / 50) * key[KEYn].fadestep);
else if (key[KEYn].BPC[LEDc].Value < key[KEYn].BC[LEDc].Value)
key[KEYn].BPC[LEDc].FadeTime = ((key[KEYn].BC[LEDc].Value - key[KEYn].BPC[LEDc].Value) / 50) * key[KEYn].fadestep;
else
key[KEYn].fadestep = 0;
}
}
else
{
if (brightstep < key[KEYn].BC[LEDc].Value) //set the anode on the shift registers
PORTA |= (1 << PA0);
else
PORTA &= ~(1 << PA0);
if (key[KEYn].pressed && brightstep == 19)
{
key[KEYn].fadestep = 50;
key[KEYn].BPC[LEDc].FadeTime = key[KEYn].BPC[LEDc].Value;
}
}
PORTA |= (1 << PINA1); //(set high) shift data in-clock
PORTA &= ~(1 << PINA1); //(set low)
}
typedef struct buffer_struct
{
int led;
char red;
char grn;
char blu;
} Buffer;
struct Point
{
int x;
int y;
}
class Driver
{
public:
Buffer [][] buf;
bool flushBufferAndScan(void);
void fillForPoint(Point p);
void clearBuffer(void);
// ...etc.
}
/*
* GccApplication6.cpp
*
* Created: 5/23/2014 2:23:59 PM
* Author: The Hammer
*/
#include <avr/io.h>
struct BPCStruct {
uint8_t Value;
uint8_t FadeTime;
};
struct BCStruct {
uint8_t Value;
};
struct KeyStruct {
uint8_t pressed;
uint8_t fadestep;
BCStruct BC[3];
BPCStruct BPC[3]; // B, G, CC, R
};
KeyStruct key[108];
uint8_t tcntct = 0;
uint8_t brightstep = 0;
uint8_t KEYn = 101;
uint8_t LEDc = 0;
void PerformRow ();
void LEDanode ();
int main(void)
{
DDRA = 0b11111111;
PORTA = 0b00000000;
DDRB = 0b00000000;
PORTB = 0b11111111;
TCCR0B |= (1 << CS01);
brightstep = 0;
KEYn = 0;
LEDc = 0;
for (uint8_t ksv = 0; ksv < 108; ++ksv)
{
key[ksv].pressed = 0;
key[ksv].fadestep = 0;
key[ksv].BC[0].Value = 0;
key[ksv].BC[1].Value = 0;
key[ksv].BC[2].Value = 10;
key[ksv].BPC[0].FadeTime = 0;
key[ksv].BPC[1].FadeTime = 0;
key[ksv].BPC[2].FadeTime = 0;
key[ksv].BPC[0].Value = 15;
key[ksv].BPC[1].Value = 20;
key[ksv].BPC[2].Value = 10;
}
PORTA |= (1 << PA2); //cathodes' data (high)
PORTA |= (1 << PA3); //cathodes' sh_cp (high)
PORTA &= ~(1 << PA3); //cathodes' sh_cp (low)
PORTA &= ~(1 << PA2); //cathodes' data (low)
//key[0].pressed = 1;
key[0].pressed = 1;
while(1)
{
if (124 <= TCNT0)
{
TCNT0 = 0; //reset time counter
PORTA |= (1 << PINA4); //(set high) copy data out -latch- this outputs changes that are stored in both sets of shift registers
PORTA &= ~(1 << PINA4); //(set low)
PerformRow ();
if (brightstep == 19) //adjust current brightness step
{
brightstep = 0;
switch (KEYn)
{
case 102:
PORTA |= (1 << PA2); //cathodes' data (high)
PORTA |= (1 << PA3); //cathodes' sh_cp (high)
PORTA &= ~(1 << PA3); //cathodes' sh_cp (low)
PORTA &= ~(1 << PA2); //cathodes' data (low)
KEYn = 0;
break;
default:
KEYn += 6;
PORTA |= (1 << PA3); //cathodes' sh_cp (high)
PORTA &= ~(1 << PA3); //cathodes' sh_cp (low)
break;
}
}
else
++brightstep;
}
/*else
{
for (uint8_t cks = 6; cks > 3; cks--)
{
if (cks == 6)
{
PORTA &= ~(1 << 6);
for (uint8_t ckp = 0; ckp < 8; ckp++)
{
if (!(PINB & (1 << ckp)))
key[ckp].pressed = 1;
else if (key[ckp].pressed)
key[ckp].pressed = 0;
}
PORTA |= (1 << 6);
}
else if (cks == 5)
{
PORTA &= ~(1 << 5);
for (uint8_t ckp = 0; ckp < 8; ckp++)
{
if (!(PINB & (1 << ckp)))
key[(ckp + 8)].pressed = 1;
else if (key[(ckp + 8)].pressed)
key[(ckp + 8)].pressed = 0;
}
PORTA |= (1 << 5);
}
else if (cks == 4)
{
PORTA &= ~(1 << 4);
for (uint8_t ckp = 0; ckp < 8; ckp++)
{
if (!(PINB & (1 << ckp)))
key[(ckp + 16)].pressed = 1;
else if (key[(ckp + 16)].pressed)
key[(ckp + 16)].pressed = 0;
}
PORTA |= (1 << 4);
}
}
}*/
}
}
void PerformRow ()
{
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 2 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 3 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 4 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 5 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
++KEYn; //--------------key 6 in this column
LEDc = 0; //LED color 0
LEDanode ();
LEDc = 1; //LED color 1
LEDanode ();
LEDc = 2; //LED color 2
LEDanode ();
KEYn -= 5;
}
void LEDanode ()
{
if (key[KEYn].fadestep)
{
if (brightstep < key[KEYn].BPC[LEDc].FadeTime) //set the anode on the shift registers
PORTA |= (1 << PA0);
else
PORTA &= ~(1 << PA0);
if (brightstep == 19)
{
--key[KEYn].fadestep;
if (key[KEYn].pressed) //if this key is pressed
{
key[KEYn].fadestep = 50;
key[KEYn].BPC[LEDc].FadeTime = key[KEYn].BPC[LEDc].Value;
}
else if (key[KEYn].BPC[LEDc].Value > key[KEYn].BC[LEDc].Value)
key[KEYn].BPC[LEDc].FadeTime = key[KEYn].BC[LEDc].Value - (((key[KEYn].BC[LEDc].Value - key[KEYn].BPC[LEDc].Value) / 50) * key[KEYn].fadestep);
else if (key[KEYn].BPC[LEDc].Value < key[KEYn].BC[LEDc].Value)
key[KEYn].BPC[LEDc].FadeTime = ((key[KEYn].BC[LEDc].Value - key[KEYn].BPC[LEDc].Value) / 50) * key[KEYn].fadestep;
else
key[KEYn].fadestep = 0;
}
}
else
{
if (brightstep < key[KEYn].BC[LEDc].Value) //set the anode on the shift registers
PORTA |= (1 << PA0);
else
PORTA &= ~(1 << PA0);
if (key[KEYn].pressed && brightstep == 19)
{
key[KEYn].fadestep = 50;
key[KEYn].BPC[LEDc].FadeTime = key[KEYn].BPC[LEDc].Value;
}
}
PORTA |= (1 << PINA1); //(set high) shift data in-clock
PORTA &= ~(1 << PINA1); //(set low)
}
I already have. I posted the one where you suggested I do one column at a time for all 20 brightness levels, and I posted the one I originally made with where it does the entire array for one brightness level.Can you post just the code which controls the timing of the scanning of your array.
It does add some, and the faster less desirable way it was designed was still not as fast as I needed it to be.It's not going to add that much processing time, and the modularization of the code is only going to make your life easier when it comes to debugging, i.e. here.
I'm not worried about the extra memory usage as it's there to be used, but questioning weather this is going to honestly solve the fact that it's just running too slow.Not only does it allow you to add functions with ease because you already have a programmatic layer to abstract on, but it also means you don't have to scrutinize your whole code body when an error in one part occurs. I don't see how it is adding processing time. Calling everything in a single shot may make the code run "faster" because your brain can intuitively process it as a single block, but modularizing it into a bunch of sub-functions ideally shouldn't make it any slower aside from branching subcalls (which are usually single-cycle) provided the code is of the same level of optimization.
If a timing error occurs, adjust the layer that deals with the timing (the driving layer).
If a color/pattern error occurs, adjust the higher-level layer (the patterning layer).
If a key event error occurs, adjust the key handler.
Modular code improves the debug capability. If it's a one-off sort of project (I write these sorts of things to write dictionary files for my apps), throwing everything in a medley in a int main "pot" works. For code you think you'll be wanting to add to later or that you anticipate any sort of error in, you'll be shooting yourself in the foot without subclassing out functionality to individual delegates.
RAM is not an issue. You're creating 108 instances of KeyStruct which are 11 bytes each. That's over a kB. If you really want to micromanage, you can use pointers to avoid having to explicitly declare an array of bools or uint8_t provided the memory ranging is contiguous.
Get me the board-specific pinouts and I'll see what I can whip up.
I already have. I posted the one where you suggested I do one column at a time for all 20 brightness levels, and I posted the one I originally made with where it does the entire array for one brightness level.
Thanks!I'll check it out. Maybe I didn't quite get what you are doing.