//
// h8300-hms-gcc -O -mh -mint32 -r -fno-common -o automate automate.c
//
// 3 jun 04
//
#include <mes.h>
// 10 jun 04
struct st_ad {
unsigned short ADDRA;
unsigned short ADDRB;
unsigned short ADDRC;
unsigned short ADDRD;
union {
unsigned char BYTE;
struct {
unsigned char ADF :1;
unsigned char ADIE:1;
unsigned char ADST:1;
unsigned char SCAN:1;
unsigned char CKS :1;
unsigned char CH :3;
} BIT;
} ADCSR;
union {
unsigned char BYTE;
struct {
unsigned char TRGE:1;
} BIT;
} ADCR;
};
#define AD (*(volatile struct st_ad *)0xFFFFE0)
#define MASTER 24
#define MISTRESS 48
// 12 jun 04
// 時計調整用
// 遅れるとき → 小さく
// 進むとき → 大きく
#define JUSTAMOMENT 493
int second;
int minute;
int hour;
int date;
int month;
int year;
signed int heat[MASTER];
// 5 jun 04 --- 10 jun 04
void getacross(char channel)
{
int flag, fd, size, opt, banner;
char* name;
if (channel == 0){ name = "/ram0/zero"; }
if (channel == 1){ name = "/ram0/first"; }
if (channel == 2){ name = "/ram0/second"; }
if (channel == 8){ name = "/ram0/eighth"; }
if (channel == 9){ name = "/ram0/ninth"; }
signed char keep[MISTRESS];
char* data;
data = &(keep[0]);
opt = READ_FILE;
size = MISTRESS;
fd = open(name, opt);
flag = read(fd, data, size);
banner = close(fd);
// printf("read %d %d %d\n",fd,flag,banner);
int j;
for (j=0; j<MASTER-1; j++){
heat[j+1] = keep[j*2] * 100 + keep[j*2+1];
}
}
// 5 jun 04 --- 10 jun 04
void putdown(char channel)
{
signed char keep[MISTRESS];
int j, flag, fd, size, opt, banner;
char* name;
if (channel == 0){ name = "/ram0/zero"; }
if (channel == 1){ name = "/ram0/first"; }
if (channel == 2){ name = "/ram0/second"; }
if (channel == 8){ name = "/ram0/eighth"; }
if (channel == 9){ name = "/ram0/ninth"; }
for (j=0; j<MASTER; j++){
keep[j*2] = heat[j] / 100;
keep[j*2+1] = heat[j] % 100;
}
char* data;
data = &(keep[0]);
opt = 0;
size = MISTRESS;
fd = open(name, opt);
flag = write(fd, data, size);
banner = close(fd);
// printf("write %d %d %d\n",fd,flag,banner);
}
// 11 jun 04
void dateandtime(char channel)
{
signed int temperature;
if (channel == 8){
temperature = (signed int)(month * 100 + date);
}
if (channel == 9){
temperature = (signed int)(hour * 100 + minute);
}
getacross(channel);
heat[0] = temperature;
putdown(channel);
}
// 5 jun 04 --- 10 jun 04
void thermometer(char channel)
{
signed int temperature;
unsigned short atmospheric;
AD.ADCSR.BIT.ADF = 0;
AD.ADCSR.BIT.ADIE = 0;
AD.ADCSR.BIT.ADST = 0;
AD.ADCSR.BIT.SCAN = 0;
AD.ADCSR.BIT.CKS = 0;
AD.ADCSR.BIT.CH = channel;
AD.ADCSR.BIT.ADST = 1;
while (AD.ADCSR.BIT.ADF == 0);
AD.ADCSR.BIT.ADF = 0;
if(channel == 0 || channel == 4){
atmospheric = AD.ADDRA >> 6;
if (atmospheric < 1){ atmospheric = 1; }
temperature = (signed int)(atmospheric);
}
if(channel == 1 || channel == 5){
atmospheric = AD.ADDRB >> 6;
if (atmospheric < 1){ atmospheric = 1; }
temperature = (signed int)(atmospheric);
}
if(channel == 2 || channel == 6){
atmospheric = AD.ADDRC >> 6;
if (atmospheric < 1){ atmospheric = 1; }
temperature = (signed int)(atmospheric);
}
if(channel == 3 || channel == 7){
atmospheric = AD.ADDRD >> 6;
if (atmospheric < 1){ atmospheric = 1; }
temperature = (signed int)(atmospheric);
}
getacross(channel);
heat[0] = temperature;
putdown(channel);
}
// 5 jun 04
void preserve()
{
char keep[7];
keep[0] = second;
keep[1] = minute;
keep[2] = hour;
keep[3] = date;
keep[4] = month;
keep[5] = year / 100;
keep[6] = year % 100;
int flag, fd, size, opt, banner;
char* name = "/ram0/present";
char* data;
data = &(keep[0]);
opt = 0;
size = 7;
fd = open(name, opt);
flag = write(fd, data, size);
banner = close(fd);
// printf("write %d %d %d\n",fd,flag,banner);
}
// 5 jun 04
void takeout()
{
int flag, fd, size, opt, banner;
char* name = "/ram0/present";
char keep[7];
char* data;
data = &(keep[0]);
opt = READ_FILE;
size = 7;
fd = open(name, opt);
flag = read(fd, data, size);
banner = close(fd);
// printf("read %d %d %d\n",fd,flag,banner);
second = keep[0];
minute = keep[1];
hour = keep[2];
date = keep[3];
month = keep[4];
year = keep[5] * 100 + keep[6];
}
// 4 jun 04
int intercalary()
{
int day;
int judge;
int decide;
if (month == 2){ day = 28; }
judge = year % 4;
if (judge == 0){ day = 29; }
judge = year % 100;
decide = (year / 100) % 4;
if (judge == 0 && decide != 0){ day = 28; }
if (month == 1){ day = 31; }
if (month == 3){ day = 31; }
if (month == 5){ day = 31; }
if (month == 7){ day = 31; }
if (month == 8){ day = 31; }
if (month == 10){ day = 31; }
if (month == 12){ day = 31; }
if (month == 4){ day = 30; }
if (month == 6){ day = 30; }
if (month == 9){ day = 30; }
if (month == 11){ day = 30; }
return day;
}
// 4 jun 04
void interval()
{
// printf("%d/%d/%d %d:%d:%d\n",year,month,date,hour,minute,second);
second++;
if (second >= 60){
takeout();
minute++;
second = 0;
}
if (minute >= 60){
hour++;
minute = 0;
}
if (hour >= 24){
date++;
hour = 0;
}
if (date > intercalary()){
month++;
date = 1;
}
if (month > 12){
year++;
month = 1;
}
if (second == 0){
preserve();
}
if (second == 0 && minute == 0){
thermometer(0);
thermometer(1);
thermometer(2);
dateandtime(8);
dateandtime(9);
}
set_timer(interval, JUSTAMOMENT);
}
// 3 jun 04
int main(int argc, char **argv)
{
second = 0;
minute = 0;
hour = 0;
date = 1;
month = 1;
year = 2004;
int j;
for (j=0; j<MASTER; j++){
heat[j] = 0;
}
preserve();
putdown(0);
putdown(1);
putdown(2);
putdown(8);
putdown(9);
int flag, gwip;
unsigned int ip, mask;
int argcex;
char* name = "ne0";
gwip = IPADDR(192,168,0,1);
flag = ifgateway(gwip);
// printf("ifgateway %d ",flag);
// flag = if_dhcp(name);
// printf("if_dhcp %d ",flag);
ip = IPADDR(192,168,0,70);
mask = IPADDR(255,255,255,0);
flag = ifconfig(name,ip,mask);
// printf("ifconfig %d ",flag);
argcex = 1;
char *argvex = "/rom0/http";
flag = execute(argcex,&argvex);
// printf("execute %d\n",flag);
set_timer(interval, JUSTAMOMENT);
while(1);
}